From 2d5e8d2fa5347baee3b46eee76e7d1910314108d Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Tue, 24 Jul 2018 19:15:47 +1000 Subject: [PATCH] feat: add getUploadProgress API to the net API --- atom/browser/api/atom_api_url_request.cc | 15 +++++++++++++++ atom/browser/api/atom_api_url_request.h | 1 + atom/browser/net/atom_url_request.cc | 13 +++++++++++++ atom/browser/net/atom_url_request.h | 1 + docs/api/client-request.md | 14 ++++++++++++++ lib/browser/api/net.js | 4 ++++ 6 files changed, 48 insertions(+) diff --git a/atom/browser/api/atom_api_url_request.cc b/atom/browser/api/atom_api_url_request.cc index 51c7969e31f44..239db2e152fad 100644 --- a/atom/browser/api/atom_api_url_request.cc +++ b/atom/browser/api/atom_api_url_request.cc @@ -125,6 +125,20 @@ bool URLRequest::ResponseState::Failed() const { return IsFlagSet(ResponseStateFlags::kFailed); } +mate::Dictionary URLRequest::GetUploadProgress(v8::Isolate* isolate) { + mate::Dictionary progress = mate::Dictionary::CreateEmpty(isolate); + + if (!atom_request_) { + progress.Set("active", false); + return progress; + } else { + progress.Set("active", true); + } + + atom_request_->GetUploadProgress(progress); + return progress; +} + URLRequest::URLRequest(v8::Isolate* isolate, v8::Local wrapper) { InitWith(isolate, wrapper); } @@ -183,6 +197,7 @@ void URLRequest::BuildPrototype(v8::Isolate* isolate, .SetMethod("setChunkedUpload", &URLRequest::SetChunkedUpload) .SetMethod("followRedirect", &URLRequest::FollowRedirect) .SetMethod("_setLoadFlags", &URLRequest::SetLoadFlags) + .SetMethod("getUploadProgress", &URLRequest::GetUploadProgress) .SetProperty("notStarted", &URLRequest::NotStarted) .SetProperty("finished", &URLRequest::Finished) // Response APi diff --git a/atom/browser/api/atom_api_url_request.h b/atom/browser/api/atom_api_url_request.h index 3b5e667d7590b..4a5a2b65160bf 100644 --- a/atom/browser/api/atom_api_url_request.h +++ b/atom/browser/api/atom_api_url_request.h @@ -112,6 +112,7 @@ class URLRequest : public mate::EventEmitter { void OnResponseData(scoped_refptr data); void OnResponseCompleted(); void OnError(const std::string& error, bool isRequestError); + mate::Dictionary GetUploadProgress(v8::Isolate* isolate); protected: explicit URLRequest(v8::Isolate* isolate, v8::Local wrapper); diff --git a/atom/browser/net/atom_url_request.cc b/atom/browser/net/atom_url_request.cc index 1aac780d88213..2483e9434bc43 100644 --- a/atom/browser/net/atom_url_request.cc +++ b/atom/browser/net/atom_url_request.cc @@ -504,4 +504,17 @@ void AtomURLRequest::InformDelegateErrorOccured(const std::string& error, delegate_->OnError(error, isRequestError); } +void AtomURLRequest::GetUploadProgress(mate::Dictionary& progress) { + if (!request_) { + progress.Set("started", false); + progress.Set("current", 0); + progress.Set("total", 0); + } else { + progress.Set("started", true); + auto upload_progress = request_->GetUploadProgress(); + progress.Set("current", upload_progress.position()); + progress.Set("total", upload_progress.size()); + } +} + } // namespace atom diff --git a/atom/browser/net/atom_url_request.h b/atom/browser/net/atom_url_request.h index 2b03ad7bc37ce..e27b064fa2866 100644 --- a/atom/browser/net/atom_url_request.h +++ b/atom/browser/net/atom_url_request.h @@ -43,6 +43,7 @@ class AtomURLRequest : public base::RefCountedThreadSafe, void PassLoginInformation(const base::string16& username, const base::string16& password) const; void SetLoadFlags(int flags) const; + void GetUploadProgress(mate::Dictionary& progress); protected: // Overrides of net::URLRequest::Delegate diff --git a/docs/api/client-request.md b/docs/api/client-request.md index 5cd9562b654a2..1ad0752ad1e14 100644 --- a/docs/api/client-request.md +++ b/docs/api/client-request.md @@ -215,3 +215,17 @@ response object,it will emit the `aborted` event. #### `request.followRedirect()` Continues any deferred redirection request when the redirection mode is `manual`. + +#### `request.getUploadProgress()` + +Returns `Object`: + +* `active` Boolean - Whether the request is currently active, if this is false +no other properties will be set +* `started` Boolean - Whether the upload has started, if this is false both +`current` and `total` will be set to 0. +* `current` Integer - The number of bytes that have been uploaded so far +* `total` Integer - The number of bytes that will be uploaded this request + +You can use this method in conjunction with `POST` requests to get the progress +of a file upload or other data transfer. diff --git a/lib/browser/api/net.js b/lib/browser/api/net.js index 54ef99d407ff3..3f2d470a27037 100644 --- a/lib/browser/api/net.js +++ b/lib/browser/api/net.js @@ -352,6 +352,10 @@ class ClientRequest extends EventEmitter { abort () { this.urlRequest.cancel() } + + getUploadProgress () { + return this.urlRequest.getUploadProgress() + } } function writeAfterEndNT (self, error, callback) {