Implement NetworkService request interception/handling (see issue #2622).

Implementation notes:
- Chromium change: CookieMonster::SetCookieableSchemes needs to be called
  immediately after the CookieMonster is created in NetworkContext::
  ApplyContextParamsToBuilder. Add a Profile::GetCookieableSchemes method and
  NetworkContextParams.cookieable_schemes member (set from
  ProfileNetworkContextService::CreateNetworkContextParams) to support that.
- Chromium change: Add a ContentBrowserClient::HandleExternalProtocol variant
  that exposes additional NetworkService request information.
- GetResourceResponseFilter is not yet implemented.

API changes:
- Resource-related callbacks have been moved from CefRequestHandler to a new
  CefResourceRequestHandler interface which is returned via the
  GetResourceRequestHandler method. If the CefRequestHandler declines to handle
  a resource it can optionally be handled by the CefRequestContextHandler, if
  any, associated with the loading context.
- The OnProtocolExecution callback has been moved from CefRequestHandler to
  CefResourceRequestHandler and will be called if a custom scheme request is
  unhandled.
- Cookie send/save permission callbacks have been moved from CefRequestHandler
  and CefResourceHandler to CefResourceRequestHandler.
- New methods added to CefResourceHandler that better match NetworkService
  execution sequence expectations. The old methods are now deprecated.
- New methods added to CefRequest and CefResponse.

Known behavior changes with the NetworkService implementation:
- Modifying the |new_url| parameter in OnResourceRedirect will no longer result
  in the method being called an additional time (likely a bug in the old
  implementation).
- Modifying the request URL in OnResourceResponse would previously cause a
  redirect. This behavior is now deprecated because the NetworkService does not
  support this functionality when using default network loaders. Temporary
  support has been added in combination with CefResourceHandler usage only.
- Other changes to the request object in OnResourceResponse will now cause the
  request to be restarted. This means that OnBeforeResourceLoad, etc, will be
  called an additional time with the new request information.
- CefResponse::GetMimeType will now be empty for non-200 responses.
- Requests using custom schemes can now be handled via CefResourceRequestHandler
  with the same callback behavior as builtin schemes.
- Redirects of custom scheme requests will now be followed as expected.
- Default handling of builtin schemes can now be disabled by setting
  |disable_default_handling| to true in GetResourceRequestHandler.
- Unhandled requests (custom scheme or builtin scheme with default handling
  disabled) will fail with an CefResponse::GetError value of
  ERR_UNKNOWN_URL_SCHEME.
- The CefSchemeHandlerFactory::Create callback will now include cookie headers.

To test:
- Run `cefclient --enable-network-service`. All resources should load
  successfully (this tests the transparent proxy capability).
- All tests pass with NetworkService disabled.
- The following tests pass with NetworkService enabled:
  - CookieTest.*
  - FrameTest.* (excluding .*Nav)
  - NavigationTest.* (excluding .Redirect*)
  - RequestHandlerTest.*
  - RequestContextTest.Basic*
  - RequestContextTest.Popup*
  - RequestTest.*
  - ResourceManagerTest.*
  - ResourceRequestHandlerTest.* (excluding .Filter*)
  - SchemeHandlerTest.*
  - StreamResourceHandlerTest.*
This commit is contained in:
Marshall Greenblatt
2019-04-24 02:50:25 +00:00
parent 019611c764
commit 8f240861e3
141 changed files with 12733 additions and 3927 deletions

View File

@@ -17,12 +17,17 @@
#include "third_party/blink/public/platform/web_http_body.h"
#include "url/gurl.h"
namespace blink {
class WebURLRequest;
} // namespace blink
namespace navigation_interception {
class NavigationParams;
}
namespace net {
class HttpRequestHeaders;
struct RedirectInfo;
class UploadData;
class UploadDataStream;
class UploadElement;
@@ -31,9 +36,11 @@ class URLFetcher;
class URLRequest;
} // namespace net
namespace blink {
class WebURLRequest;
} // namespace blink
namespace network {
class DataElement;
struct ResourceRequest;
class ResourceRequestBody;
} // namespace network
struct CefMsg_LoadRequest_Params;
struct CefNavigateParams;
@@ -67,6 +74,10 @@ class CefRequestImpl : public CefRequest {
void SetPostData(CefRefPtr<CefPostData> postData) override;
void GetHeaderMap(HeaderMap& headerMap) override;
void SetHeaderMap(const HeaderMap& headerMap) override;
CefString GetHeaderByName(const CefString& name) override;
void SetHeaderByName(const CefString& name,
const CefString& value,
bool overwrite) override;
void Set(const CefString& url,
const CefString& method,
CefRefPtr<CefPostData> postData,
@@ -79,6 +90,19 @@ class CefRequestImpl : public CefRequest {
TransitionType GetTransitionType() override;
uint64 GetIdentifier() override;
// Populate this object from the ResourceRequest object.
void Set(const network::ResourceRequest* request, uint64 identifier);
// Populate the URLRequest object from this object.
// If |changed_only| is true then only the changed fields will be updated.
void Get(network::ResourceRequest* request, bool changed_only) const;
// Populate this object from the RedirectInfo object.
void Set(const net::RedirectInfo& redirect_info);
// Populate this object from teh HttpRequestHeaders object.
void Set(const net::HttpRequestHeaders& headers);
// Populate this object from the URLRequest object.
void Set(const net::URLRequest* request);
@@ -112,7 +136,14 @@ class CefRequestImpl : public CefRequest {
void SetReadOnly(bool read_only);
void SetTrackChanges(bool track_changes);
// Enable or disable tracking of changes. If |track_changes| is true the
// status of changes will be tracked, and retrievable via GetChanges(). If
// |backup_on_change| is true the original value will be backed up before the
// first change. The original values can later be restored by calling
// RevertChanges() before calling SetTrackChanges(false).
void SetTrackChanges(bool track_changes, bool backup_on_change = false);
void RevertChanges();
void DiscardChanges();
uint8_t GetChanges() const;
static network::mojom::ReferrerPolicy NetReferrerPolicyToBlinkReferrerPolicy(
@@ -121,7 +152,13 @@ class CefRequestImpl : public CefRequest {
network::mojom::ReferrerPolicy blink_policy);
private:
// Mark values as changed. Must be called before the new values are assigned.
void Changed(uint8_t changes);
// Used with the Set() methods that export data to other object types. Returns
// true if the values should be set on the export object. If |changed_only| is
// true then only return true if the value has been changed in combination
// with track changes.
bool ShouldSet(uint8_t changes, bool changed_only) const;
void Reset();
@@ -138,16 +175,35 @@ class CefRequestImpl : public CefRequest {
// The below members are used by CefURLRequest.
int flags_;
GURL site_for_cookies_;
GURL first_party_for_cookies_;
// Stores backup of values for use with track changes.
struct Backup {
// Bitmask of values that have been backed up.
uint8_t backups_ = kChangedNone;
GURL url_;
std::string method_;
GURL referrer_url_;
ReferrerPolicy referrer_policy_;
CefRefPtr<CefPostData> postdata_;
std::unique_ptr<HeaderMap> headermap_;
int flags_;
GURL first_party_for_cookies_;
};
std::unique_ptr<Backup> backup_;
// True if this object is read-only.
bool read_only_;
bool read_only_ = false;
// True if this object should track changes.
bool track_changes_;
bool track_changes_ = false;
// True if original values should be backed up when |track_changes_| is true.
bool backup_on_change_ = false;
// Bitmask of |Changes| values which indicate which fields have changed.
uint8_t changes_;
uint8_t changes_ = kChangedNone;
mutable base::Lock lock_;
@@ -167,6 +223,8 @@ class CefPostDataImpl : public CefPostData {
bool AddElement(CefRefPtr<CefPostDataElement> element) override;
void RemoveElements() override;
void Set(const network::ResourceRequestBody& body);
scoped_refptr<network::ResourceRequestBody> GetBody() const;
void Set(const net::UploadData& data);
void Set(const net::UploadDataStream& data_stream);
void Get(net::UploadData& data) const;
@@ -218,6 +276,8 @@ class CefPostDataElementImpl : public CefPostDataElement {
void* GetBytes() { return data_.bytes.bytes; }
void Set(const network::DataElement& element);
void Get(network::ResourceRequestBody& body) const;
void Set(const net::UploadElement& element);
void Set(const net::UploadElementReader& element_reader);
void Get(net::UploadElement& element) const;