1 // Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights 2 // reserved. Use of this source code is governed by a BSD-style license that 3 // can be found in the LICENSE file. 4 5 #ifndef CEF_LIBCEF_COMMON_REQUEST_IMPL_H_ 6 #define CEF_LIBCEF_COMMON_REQUEST_IMPL_H_ 7 #pragma once 8 9 #include <stdint.h> 10 11 #include <memory> 12 13 #include "include/cef_request.h" 14 15 #include "base/synchronization/lock.h" 16 #include "cef/libcef/common/mojom/cef.mojom.h" 17 #include "net/cookies/site_for_cookies.h" 18 #include "services/network/public/mojom/referrer_policy.mojom-shared.h" 19 #include "url/gurl.h" 20 21 namespace blink { 22 class WebURLRequest; 23 } // namespace blink 24 25 namespace navigation_interception { 26 class NavigationParams; 27 } 28 29 namespace net { 30 class HttpRequestHeaders; 31 struct RedirectInfo; 32 } // namespace net 33 34 namespace network { 35 class DataElement; 36 struct ResourceRequest; 37 class ResourceRequestBody; 38 } // namespace network 39 40 // Implementation of CefRequest 41 class CefRequestImpl : public CefRequest { 42 public: 43 enum Changes { 44 kChangedNone = 0, 45 kChangedUrl = 1 << 0, 46 kChangedMethod = 1 << 1, 47 kChangedReferrer = 1 << 2, 48 kChangedPostData = 1 << 3, 49 kChangedHeaderMap = 1 << 4, 50 kChangedFlags = 1 << 5, 51 kChangedSiteForCookies = 1 << 6, 52 }; 53 54 CefRequestImpl(); 55 56 bool IsReadOnly() override; 57 CefString GetURL() override; 58 void SetURL(const CefString& url) override; 59 CefString GetMethod() override; 60 void SetMethod(const CefString& method) override; 61 void SetReferrer(const CefString& referrer_url, 62 ReferrerPolicy policy) override; 63 CefString GetReferrerURL() override; 64 ReferrerPolicy GetReferrerPolicy() override; 65 CefRefPtr<CefPostData> GetPostData() override; 66 void SetPostData(CefRefPtr<CefPostData> postData) override; 67 void GetHeaderMap(HeaderMap& headerMap) override; 68 void SetHeaderMap(const HeaderMap& headerMap) override; 69 CefString GetHeaderByName(const CefString& name) override; 70 void SetHeaderByName(const CefString& name, 71 const CefString& value, 72 bool overwrite) override; 73 void Set(const CefString& url, 74 const CefString& method, 75 CefRefPtr<CefPostData> postData, 76 const HeaderMap& headerMap) override; 77 int GetFlags() override; 78 void SetFlags(int flags) override; 79 CefString GetFirstPartyForCookies() override; 80 void SetFirstPartyForCookies(const CefString& url) override; 81 ResourceType GetResourceType() override; 82 TransitionType GetTransitionType() override; 83 uint64 GetIdentifier() override; 84 85 // Populate this object from the ResourceRequest object. 86 void Set(const network::ResourceRequest* request, uint64 identifier); 87 88 // Populate the ResourceRequest object from this object. 89 // If |changed_only| is true then only the changed fields will be updated. 90 void Get(network::ResourceRequest* request, bool changed_only) const; 91 92 // Populate this object from the RedirectInfo object. 93 void Set(const net::RedirectInfo& redirect_info); 94 95 // Populate this object from teh HttpRequestHeaders object. 96 void Set(const net::HttpRequestHeaders& headers); 97 98 // Populate this object from the NavigationParams object. 99 // TODO(cef): Remove the |is_main_frame| argument once NavigationParams is 100 // reliable in reporting that value. 101 // Called from content_browser_client.cc NavigationOnUIThread(). 102 void Set(const navigation_interception::NavigationParams& params, 103 bool is_main_frame); 104 105 // Populate the WebURLRequest object based on the contents of |params|. 106 // Called from CefBrowserImpl::LoadRequest(). 107 static void Get(const cef::mojom::RequestParamsPtr& params, 108 blink::WebURLRequest& request); 109 110 // Populate the RequestParams object from this object. 111 // Called from CefFrameHostImpl::LoadRequest(). 112 void Get(cef::mojom::RequestParamsPtr& params) const; 113 114 void SetReadOnly(bool read_only); 115 116 // Enable or disable tracking of changes. If |track_changes| is true the 117 // status of changes will be tracked, and retrievable via GetChanges(). If 118 // |backup_on_change| is true the original value will be backed up before the 119 // first change. The original values can later be restored by calling 120 // RevertChanges() before calling SetTrackChanges(false). 121 void SetTrackChanges(bool track_changes, bool backup_on_change = false); 122 void RevertChanges(); 123 void DiscardChanges(); 124 uint8_t GetChanges() const; 125 126 static network::mojom::ReferrerPolicy NetReferrerPolicyToBlinkReferrerPolicy( 127 cef_referrer_policy_t net_policy); 128 static cef_referrer_policy_t BlinkReferrerPolicyToNetReferrerPolicy( 129 network::mojom::ReferrerPolicy blink_policy); 130 131 private: 132 // Mark values as changed. Must be called before the new values are assigned. 133 void Changed(uint8_t changes); 134 135 // Used with the Set() methods that export data to other object types. Returns 136 // true if the values should be set on the export object. If |changed_only| is 137 // true then only return true if the value has been changed in combination 138 // with track changes. 139 bool ShouldSet(uint8_t changes, bool changed_only) const; 140 141 void Reset(); 142 143 GURL url_; 144 std::string method_; 145 GURL referrer_url_; 146 ReferrerPolicy referrer_policy_; 147 CefRefPtr<CefPostData> postdata_; 148 HeaderMap headermap_; 149 ResourceType resource_type_; 150 TransitionType transition_type_; 151 uint64 identifier_; 152 153 // The below members are used by CefURLRequest. 154 int flags_; 155 net::SiteForCookies site_for_cookies_; 156 157 // Stores backup of values for use with track changes. 158 struct Backup { 159 // Bitmask of values that have been backed up. 160 uint8_t backups_ = kChangedNone; 161 162 GURL url_; 163 std::string method_; 164 GURL referrer_url_; 165 ReferrerPolicy referrer_policy_; 166 CefRefPtr<CefPostData> postdata_; 167 std::unique_ptr<HeaderMap> headermap_; 168 int flags_; 169 net::SiteForCookies site_for_cookies_; 170 }; 171 std::unique_ptr<Backup> backup_; 172 173 // True if this object is read-only. 174 bool read_only_ = false; 175 176 // True if this object should track changes. 177 bool track_changes_ = false; 178 179 // True if original values should be backed up when |track_changes_| is true. 180 bool backup_on_change_ = false; 181 182 // Bitmask of |Changes| values which indicate which fields have changed. 183 uint8_t changes_ = kChangedNone; 184 185 mutable base::Lock lock_; 186 187 IMPLEMENT_REFCOUNTING(CefRequestImpl); 188 }; 189 190 // Implementation of CefPostData 191 class CefPostDataImpl : public CefPostData { 192 public: 193 CefPostDataImpl(); 194 195 bool IsReadOnly() override; 196 bool HasExcludedElements() override; 197 size_t GetElementCount() override; 198 void GetElements(ElementVector& elements) override; 199 bool RemoveElement(CefRefPtr<CefPostDataElement> element) override; 200 bool AddElement(CefRefPtr<CefPostDataElement> element) override; 201 void RemoveElements() override; 202 203 void Set(const network::ResourceRequestBody& body); 204 scoped_refptr<network::ResourceRequestBody> GetBody() const; 205 206 void SetReadOnly(bool read_only); 207 208 void SetTrackChanges(bool track_changes); 209 bool HasChanges() const; 210 211 private: 212 void Changed(); 213 214 ElementVector elements_; 215 216 // True if this object is read-only. 217 bool read_only_; 218 219 // True if this object has excluded elements. 220 bool has_excluded_elements_; 221 222 // True if this object should track changes. 223 bool track_changes_; 224 225 // True if this object has changes. 226 bool has_changes_; 227 228 mutable base::Lock lock_; 229 230 IMPLEMENT_REFCOUNTING(CefPostDataImpl); 231 }; 232 233 // Implementation of CefPostDataElement 234 class CefPostDataElementImpl : public CefPostDataElement { 235 public: 236 CefPostDataElementImpl(); 237 ~CefPostDataElementImpl() override; 238 239 bool IsReadOnly() override; 240 void SetToEmpty() override; 241 void SetToFile(const CefString& fileName) override; 242 void SetToBytes(size_t size, const void* bytes) override; 243 Type GetType() override; 244 CefString GetFile() override; 245 size_t GetBytesCount() override; 246 size_t GetBytes(size_t size, void* bytes) override; 247 GetBytes()248 void* GetBytes() { return data_.bytes.bytes; } 249 250 void Set(const network::DataElement& element); 251 void Get(network::ResourceRequestBody& body) const; 252 253 void SetReadOnly(bool read_only); 254 255 void SetTrackChanges(bool track_changes); 256 bool HasChanges() const; 257 258 private: 259 void Changed(); 260 void Cleanup(); 261 262 Type type_; 263 union { 264 struct { 265 void* bytes; 266 size_t size; 267 } bytes; 268 cef_string_t filename; 269 } data_; 270 271 // True if this object is read-only. 272 bool read_only_; 273 274 // True if this object should track changes. 275 bool track_changes_; 276 277 // True if this object has changes. 278 bool has_changes_; 279 280 mutable base::Lock lock_; 281 282 IMPLEMENT_REFCOUNTING(CefPostDataElementImpl); 283 }; 284 285 #endif // CEF_LIBCEF_COMMON_REQUEST_IMPL_H_ 286