feat: add getUploadProgress API to the net API (#14446)

This commit is contained in:
trop[bot] 2018-09-04 12:26:50 -05:00 committed by Charles Kerr
parent ce592a5705
commit 89a6f1efbb
6 changed files with 45 additions and 0 deletions

View file

@ -125,6 +125,18 @@ bool URLRequest::ResponseState::Failed() const {
return IsFlagSet(ResponseStateFlags::kFailed); return IsFlagSet(ResponseStateFlags::kFailed);
} }
mate::Dictionary URLRequest::GetUploadProgress(v8::Isolate* isolate) {
mate::Dictionary progress = mate::Dictionary::CreateEmpty(isolate);
if (atom_request_) {
progress.Set("active", true);
atom_request_->GetUploadProgress(&progress);
} else {
progress.Set("active", false);
}
return progress;
}
URLRequest::URLRequest(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) { URLRequest::URLRequest(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) {
InitWith(isolate, wrapper); InitWith(isolate, wrapper);
} }
@ -183,6 +195,7 @@ void URLRequest::BuildPrototype(v8::Isolate* isolate,
.SetMethod("setChunkedUpload", &URLRequest::SetChunkedUpload) .SetMethod("setChunkedUpload", &URLRequest::SetChunkedUpload)
.SetMethod("followRedirect", &URLRequest::FollowRedirect) .SetMethod("followRedirect", &URLRequest::FollowRedirect)
.SetMethod("_setLoadFlags", &URLRequest::SetLoadFlags) .SetMethod("_setLoadFlags", &URLRequest::SetLoadFlags)
.SetMethod("getUploadProgress", &URLRequest::GetUploadProgress)
.SetProperty("notStarted", &URLRequest::NotStarted) .SetProperty("notStarted", &URLRequest::NotStarted)
.SetProperty("finished", &URLRequest::Finished) .SetProperty("finished", &URLRequest::Finished)
// Response APi // Response APi

View file

@ -112,6 +112,7 @@ class URLRequest : public mate::EventEmitter<URLRequest> {
void OnResponseData(scoped_refptr<const net::IOBufferWithSize> data); void OnResponseData(scoped_refptr<const net::IOBufferWithSize> data);
void OnResponseCompleted(); void OnResponseCompleted();
void OnError(const std::string& error, bool isRequestError); void OnError(const std::string& error, bool isRequestError);
mate::Dictionary GetUploadProgress(v8::Isolate* isolate);
protected: protected:
explicit URLRequest(v8::Isolate* isolate, v8::Local<v8::Object> wrapper); explicit URLRequest(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);

View file

@ -501,4 +501,16 @@ void AtomURLRequest::InformDelegateErrorOccured(const std::string& error,
delegate_->OnError(error, isRequestError); delegate_->OnError(error, isRequestError);
} }
void AtomURLRequest::GetUploadProgress(mate::Dictionary* progress) const {
net::UploadProgress upload_progress;
if (request_) {
progress->Set("started", true);
upload_progress = request_->GetUploadProgress();
} else {
progress->Set("started", false);
}
progress->Set("current", upload_progress.position());
progress->Set("total", upload_progress.size());
}
} // namespace atom } // namespace atom

View file

@ -43,6 +43,7 @@ class AtomURLRequest : public base::RefCountedThreadSafe<AtomURLRequest>,
void PassLoginInformation(const base::string16& username, void PassLoginInformation(const base::string16& username,
const base::string16& password) const; const base::string16& password) const;
void SetLoadFlags(int flags) const; void SetLoadFlags(int flags) const;
void GetUploadProgress(mate::Dictionary* progress) const;
protected: protected:
// Overrides of net::URLRequest::Delegate // Overrides of net::URLRequest::Delegate

View file

@ -215,3 +215,17 @@ response object,it will emit the `aborted` event.
#### `request.followRedirect()` #### `request.followRedirect()`
Continues any deferred redirection request when the redirection mode is `manual`. 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.

View file

@ -352,6 +352,10 @@ class ClientRequest extends EventEmitter {
abort () { abort () {
this.urlRequest.cancel() this.urlRequest.cancel()
} }
getUploadProgress () {
return this.urlRequest.getUploadProgress()
}
} }
function writeAfterEndNT (self, error, callback) { function writeAfterEndNT (self, error, callback) {