2016-10-25 10:41:01 +00:00
|
|
|
// Copyright (c) 2016 GitHub, Inc.
|
2016-09-15 13:59:40 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:56:58 +00:00
|
|
|
#ifndef SHELL_BROWSER_API_ATOM_API_URL_REQUEST_H_
|
|
|
|
#define SHELL_BROWSER_API_ATOM_API_URL_REQUEST_H_
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
#include <array>
|
2016-09-29 09:31:08 +00:00
|
|
|
#include <string>
|
2018-05-07 05:52:25 +00:00
|
|
|
|
2016-09-30 12:17:29 +00:00
|
|
|
#include "base/memory/weak_ptr.h"
|
2016-10-06 15:28:21 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2016-09-15 13:59:40 +00:00
|
|
|
#include "native_mate/handle.h"
|
2016-09-30 12:17:29 +00:00
|
|
|
#include "native_mate/wrappable_base.h"
|
|
|
|
#include "net/base/auth.h"
|
|
|
|
#include "net/base/io_buffer.h"
|
2016-09-21 07:23:00 +00:00
|
|
|
#include "net/http/http_response_headers.h"
|
2016-09-29 09:31:08 +00:00
|
|
|
#include "net/url_request/url_request_context.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/api/event_emitter.h"
|
|
|
|
#include "shell/browser/api/trackable_object.h"
|
2016-09-21 07:23:00 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2016-09-19 09:21:09 +00:00
|
|
|
class AtomURLRequest;
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2016-09-19 09:21:09 +00:00
|
|
|
namespace api {
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2016-09-30 12:17:29 +00:00
|
|
|
// The URLRequest class implements the V8 binding between the JavaScript API
|
2016-10-04 15:12:17 +00:00
|
|
|
// and Chromium native net library. It is responsible for handling HTTP/HTTPS
|
2016-09-30 12:17:29 +00:00
|
|
|
// requests.
|
|
|
|
//
|
2016-10-04 15:12:17 +00:00
|
|
|
// The current class provides only the binding layer. Two other JavaScript
|
|
|
|
// classes (ClientRequest and IncomingMessage) in the net module provide the
|
2016-09-30 12:17:29 +00:00
|
|
|
// final API, including some state management and arguments validation.
|
|
|
|
//
|
2016-10-04 15:12:17 +00:00
|
|
|
// URLRequest's methods fall into two main categories: command and event
|
|
|
|
// methods. They are always executed on the Browser's UI thread.
|
2016-09-30 12:17:29 +00:00
|
|
|
// Command methods are called directly from JavaScript code via the API defined
|
2016-10-04 15:12:17 +00:00
|
|
|
// in BuildPrototype. A command method is generally implemented by forwarding
|
|
|
|
// the call to a corresponding method on AtomURLRequest which does the
|
|
|
|
// synchronization on the Browser IO thread. The latter then calls into Chromium
|
|
|
|
// net library. On the other hand, net library events originate on the IO
|
2016-09-30 12:17:29 +00:00
|
|
|
// thread in AtomURLRequest and are synchronized back on the UI thread, then
|
2016-10-04 15:12:17 +00:00
|
|
|
// forwarded to a corresponding event method in URLRequest and then to
|
2016-09-30 12:17:29 +00:00
|
|
|
// JavaScript via the EmitRequestEvent/EmitResponseEvent helpers.
|
|
|
|
//
|
2016-10-04 15:12:17 +00:00
|
|
|
// URLRequest lifetime management: we followed the Wrapper/Wrappable pattern
|
2016-09-30 12:17:29 +00:00
|
|
|
// defined in native_mate. However, we augment that pattern with a pin/unpin
|
|
|
|
// mechanism. The main reason is that we want the JS API to provide a similar
|
2016-10-04 15:12:17 +00:00
|
|
|
// lifetime guarantees as the XMLHttpRequest.
|
2016-09-30 12:17:29 +00:00
|
|
|
// https://xhr.spec.whatwg.org/#garbage-collection
|
|
|
|
//
|
|
|
|
// The primary motivation is to not garbage collect a URLInstance as long as the
|
|
|
|
// object is emitting network events. For instance, in the following JS code
|
|
|
|
//
|
|
|
|
// (function() {
|
|
|
|
// let request = new URLRequest(...);
|
|
|
|
// request.on('response', (response)=>{
|
|
|
|
// response.on('data', (data) = > {
|
|
|
|
// console.log(data.toString());
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// })();
|
|
|
|
//
|
2016-10-04 15:12:17 +00:00
|
|
|
// we still want data to be logged even if the response/request objects are n
|
2016-09-30 12:17:29 +00:00
|
|
|
// more referenced in JavaScript.
|
|
|
|
//
|
|
|
|
// Binding by simply following the native_mate Wrapper/Wrappable pattern will
|
|
|
|
// delete the URLRequest object when the corresponding JS object is collected.
|
2016-10-04 15:12:17 +00:00
|
|
|
// The v8 handle is a private member in WrappableBase and it is always weak,
|
2016-09-30 12:17:29 +00:00
|
|
|
// there is no way to make it strong without changing native_mate.
|
2016-10-04 15:12:17 +00:00
|
|
|
// The solution we implement consists of maintaining some kind of state that
|
2016-09-30 12:17:29 +00:00
|
|
|
// prevents collection of JS wrappers as long as the request is emitting network
|
2016-10-04 15:12:17 +00:00
|
|
|
// events. At initialization, the object is unpinned. When the request starts,
|
|
|
|
// it is pinned. When no more events would be emitted, the object is unpinned
|
|
|
|
// and lifetime is again managed by the standard native mate Wrapper/Wrappable
|
2016-09-30 12:17:29 +00:00
|
|
|
// pattern.
|
|
|
|
//
|
2016-10-04 15:12:17 +00:00
|
|
|
// pin/unpin: are implemented by constructing/reseting a V8 strong persistent
|
2016-09-30 12:17:29 +00:00
|
|
|
// handle.
|
|
|
|
//
|
2016-10-04 15:12:17 +00:00
|
|
|
// The URLRequest/AtmURLRequest interaction could have been implemented in a
|
2016-09-30 12:17:29 +00:00
|
|
|
// single class. However, it implies that the resulting class lifetime will be
|
2016-10-04 15:12:17 +00:00
|
|
|
// managed by two conflicting mechanisms: JavaScript garbage collection and
|
|
|
|
// Chromium reference counting. Reasoning about lifetime issues become much
|
2016-09-30 12:17:29 +00:00
|
|
|
// more complex.
|
|
|
|
//
|
2016-10-04 15:12:17 +00:00
|
|
|
// We chose to split the implementation into two classes linked via a
|
2016-10-25 14:19:26 +00:00
|
|
|
// reference counted/raw pointers. A URLRequest instance is deleted if it is
|
|
|
|
// unpinned and the corresponding JS wrapper object is garbage collected. On the
|
|
|
|
// other hand, an AtmURLRequest instance lifetime is totally governed by
|
|
|
|
// reference counting.
|
2016-09-30 12:17:29 +00:00
|
|
|
//
|
2016-09-19 09:21:09 +00:00
|
|
|
class URLRequest : public mate::EventEmitter<URLRequest> {
|
2016-09-15 13:59:40 +00:00
|
|
|
public:
|
|
|
|
static mate::WrappableBase* New(mate::Arguments* args);
|
|
|
|
|
2016-10-14 09:51:45 +00:00
|
|
|
static void BuildPrototype(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::FunctionTemplate> prototype);
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2016-09-30 12:17:29 +00:00
|
|
|
// Methods for reporting events into JavaScript.
|
2017-03-23 19:37:54 +00:00
|
|
|
void OnReceivedRedirect(
|
|
|
|
int status_code,
|
|
|
|
const std::string& method,
|
|
|
|
const GURL& url,
|
|
|
|
scoped_refptr<net::HttpResponseHeaders> response_headers);
|
2019-04-20 17:20:37 +00:00
|
|
|
void OnAuthenticationRequired(const net::AuthChallengeInfo& auth_info);
|
2016-10-04 15:33:34 +00:00
|
|
|
void OnResponseStarted(
|
2016-10-14 08:58:16 +00:00
|
|
|
scoped_refptr<net::HttpResponseHeaders> response_headers);
|
2016-09-30 12:17:29 +00:00
|
|
|
void OnResponseData(scoped_refptr<const net::IOBufferWithSize> data);
|
|
|
|
void OnResponseCompleted();
|
2016-10-14 15:37:39 +00:00
|
|
|
void OnError(const std::string& error, bool isRequestError);
|
2018-07-27 14:15:48 +00:00
|
|
|
mate::Dictionary GetUploadProgress(v8::Isolate* isolate);
|
2016-09-30 12:17:29 +00:00
|
|
|
|
2016-09-15 13:59:40 +00:00
|
|
|
protected:
|
2019-07-04 01:56:19 +00:00
|
|
|
URLRequest(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
|
2016-09-15 13:59:40 +00:00
|
|
|
~URLRequest() override;
|
|
|
|
|
2016-09-29 09:31:08 +00:00
|
|
|
private:
|
2016-10-04 15:12:17 +00:00
|
|
|
template <typename Flags>
|
|
|
|
class StateBase {
|
|
|
|
public:
|
|
|
|
void SetFlag(Flags flag);
|
2016-10-14 09:51:45 +00:00
|
|
|
|
2016-10-04 15:12:17 +00:00
|
|
|
protected:
|
|
|
|
explicit StateBase(Flags initialState);
|
|
|
|
bool operator==(Flags flag) const;
|
|
|
|
bool IsFlagSet(Flags flag) const;
|
2016-10-14 09:51:45 +00:00
|
|
|
|
2016-10-04 15:12:17 +00:00
|
|
|
private:
|
2016-10-14 09:51:45 +00:00
|
|
|
Flags state_;
|
2016-10-04 15:12:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum class RequestStateFlags {
|
2016-10-14 09:51:45 +00:00
|
|
|
kNotStarted = 0x0,
|
|
|
|
kStarted = 0x1,
|
|
|
|
kFinished = 0x2,
|
|
|
|
kCanceled = 0x4,
|
|
|
|
kFailed = 0x8,
|
|
|
|
kClosed = 0x10
|
2016-10-04 15:12:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class RequestState : public StateBase<RequestStateFlags> {
|
|
|
|
public:
|
|
|
|
RequestState();
|
|
|
|
bool NotStarted() const;
|
|
|
|
bool Started() const;
|
|
|
|
bool Finished() const;
|
|
|
|
bool Canceled() const;
|
|
|
|
bool Failed() const;
|
|
|
|
bool Closed() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class ResponseStateFlags {
|
|
|
|
kNotStarted = 0x0,
|
|
|
|
kStarted = 0x1,
|
|
|
|
kEnded = 0x2,
|
2016-10-04 15:54:34 +00:00
|
|
|
kFailed = 0x4
|
2016-10-04 15:12:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ResponseState : public StateBase<ResponseStateFlags> {
|
|
|
|
public:
|
|
|
|
ResponseState();
|
|
|
|
bool NotStarted() const;
|
|
|
|
bool Started() const;
|
|
|
|
bool Ended() const;
|
|
|
|
bool Canceled() const;
|
|
|
|
bool Failed() const;
|
|
|
|
bool Closed() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool NotStarted() const;
|
|
|
|
bool Finished() const;
|
|
|
|
bool Canceled() const;
|
|
|
|
bool Failed() const;
|
2016-10-14 09:51:45 +00:00
|
|
|
bool Write(scoped_refptr<const net::IOBufferWithSize> buffer, bool is_last);
|
2016-10-04 15:12:17 +00:00
|
|
|
void Cancel();
|
2017-03-23 19:37:54 +00:00
|
|
|
void FollowRedirect();
|
2016-09-27 08:21:11 +00:00
|
|
|
bool SetExtraHeader(const std::string& name, const std::string& value);
|
|
|
|
void RemoveExtraHeader(const std::string& name);
|
|
|
|
void SetChunkedUpload(bool is_chunked_upload);
|
2016-11-30 20:01:32 +00:00
|
|
|
void SetLoadFlags(int flags);
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
int StatusCode() const;
|
|
|
|
std::string StatusMessage() const;
|
2016-10-25 10:41:01 +00:00
|
|
|
net::HttpResponseHeaders* RawResponseHeaders() const;
|
2016-09-21 15:35:03 +00:00
|
|
|
uint32_t ResponseHttpVersionMajor() const;
|
|
|
|
uint32_t ResponseHttpVersionMinor() const;
|
2016-09-21 07:23:00 +00:00
|
|
|
|
2016-10-04 15:12:17 +00:00
|
|
|
void Close();
|
2016-10-25 10:41:01 +00:00
|
|
|
void Pin();
|
|
|
|
void Unpin();
|
2016-10-26 09:10:15 +00:00
|
|
|
template <typename... Args>
|
|
|
|
void EmitRequestEvent(Args... args);
|
|
|
|
template <typename... Args>
|
|
|
|
void EmitResponseEvent(Args... args);
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2016-09-26 12:03:49 +00:00
|
|
|
scoped_refptr<AtomURLRequest> atom_request_;
|
2016-10-04 15:12:17 +00:00
|
|
|
RequestState request_state_;
|
|
|
|
ResponseState response_state_;
|
2016-09-30 12:17:29 +00:00
|
|
|
|
|
|
|
// Used to implement pin/unpin.
|
2016-09-19 09:21:09 +00:00
|
|
|
v8::Global<v8::Object> wrapper_;
|
2016-10-13 15:51:19 +00:00
|
|
|
scoped_refptr<net::HttpResponseHeaders> response_headers_;
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2016-09-15 13:59:40 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(URLRequest);
|
|
|
|
};
|
|
|
|
|
2016-09-29 09:31:08 +00:00
|
|
|
} // namespace api
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2019-06-19 20:56:58 +00:00
|
|
|
#endif // SHELL_BROWSER_API_ATOM_API_URL_REQUEST_H_
|