1 // Copyright (c) 2011 Marshall A. Greenblatt. All rights reserved. 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google Inc. nor the name Chromium Embedded 14 // Framework nor the names of its contributors may be used to endorse 15 // or promote products derived from this software without specific prior 16 // written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // 30 // --------------------------------------------------------------------------- 31 // 32 // The contents of this file must follow a specific format in order to 33 // support the CEF translator tool. See the translator.README.txt file in the 34 // tools directory for more information. 35 // 36 37 #ifndef CEF_INCLUDE_CEF_RESOURCE_REQUEST_HANDLER_H_ 38 #define CEF_INCLUDE_CEF_RESOURCE_REQUEST_HANDLER_H_ 39 #pragma once 40 41 #include "include/cef_base.h" 42 #include "include/cef_browser.h" 43 #include "include/cef_callback.h" 44 #include "include/cef_frame.h" 45 #include "include/cef_request.h" 46 #include "include/cef_resource_handler.h" 47 #include "include/cef_response.h" 48 #include "include/cef_response_filter.h" 49 50 class CefCookieAccessFilter; 51 52 /// 53 // Implement this interface to handle events related to browser requests. The 54 // methods of this class will be called on the IO thread unless otherwise 55 // indicated. 56 /// 57 /*--cef(source=client,no_debugct_check)--*/ 58 class CefResourceRequestHandler : public virtual CefBaseRefCounted { 59 public: 60 typedef cef_return_value_t ReturnValue; 61 typedef cef_urlrequest_status_t URLRequestStatus; 62 63 /// 64 // Called on the IO thread before a resource request is loaded. The |browser| 65 // and |frame| values represent the source of the request, and may be NULL for 66 // requests originating from service workers or CefURLRequest. To optionally 67 // filter cookies for the request return a CefCookieAccessFilter object. The 68 // |request| object cannot not be modified in this callback. 69 /// 70 /*--cef(optional_param=browser,optional_param=frame)--*/ GetCookieAccessFilter(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request)71 virtual CefRefPtr<CefCookieAccessFilter> GetCookieAccessFilter( 72 CefRefPtr<CefBrowser> browser, 73 CefRefPtr<CefFrame> frame, 74 CefRefPtr<CefRequest> request) { 75 return nullptr; 76 } 77 78 /// 79 // Called on the IO thread before a resource request is loaded. The |browser| 80 // and |frame| values represent the source of the request, and may be NULL for 81 // requests originating from service workers or CefURLRequest. To redirect or 82 // change the resource load optionally modify |request|. Modification of the 83 // request URL will be treated as a redirect. Return RV_CONTINUE to continue 84 // the request immediately. Return RV_CONTINUE_ASYNC and call CefCallback 85 // methods at a later time to continue or cancel the request asynchronously. 86 // Return RV_CANCEL to cancel the request immediately. 87 // 88 /// 89 /*--cef(optional_param=browser,optional_param=frame, 90 default_retval=RV_CONTINUE)--*/ OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request,CefRefPtr<CefCallback> callback)91 virtual ReturnValue OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser, 92 CefRefPtr<CefFrame> frame, 93 CefRefPtr<CefRequest> request, 94 CefRefPtr<CefCallback> callback) { 95 return RV_CONTINUE; 96 } 97 98 /// 99 // Called on the IO thread before a resource is loaded. The |browser| and 100 // |frame| values represent the source of the request, and may be NULL for 101 // requests originating from service workers or CefURLRequest. To allow the 102 // resource to load using the default network loader return NULL. To specify a 103 // handler for the resource return a CefResourceHandler object. The |request| 104 // object cannot not be modified in this callback. 105 /// 106 /*--cef(optional_param=browser,optional_param=frame)--*/ GetResourceHandler(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request)107 virtual CefRefPtr<CefResourceHandler> GetResourceHandler( 108 CefRefPtr<CefBrowser> browser, 109 CefRefPtr<CefFrame> frame, 110 CefRefPtr<CefRequest> request) { 111 return nullptr; 112 } 113 114 /// 115 // Called on the IO thread when a resource load is redirected. The |browser| 116 // and |frame| values represent the source of the request, and may be NULL for 117 // requests originating from service workers or CefURLRequest. The |request| 118 // parameter will contain the old URL and other request-related information. 119 // The |response| parameter will contain the response that resulted in the 120 // redirect. The |new_url| parameter will contain the new URL and can be 121 // changed if desired. The |request| and |response| objects cannot be modified 122 // in this callback. 123 /// 124 /*--cef(optional_param=browser,optional_param=frame)--*/ OnResourceRedirect(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request,CefRefPtr<CefResponse> response,CefString & new_url)125 virtual void OnResourceRedirect(CefRefPtr<CefBrowser> browser, 126 CefRefPtr<CefFrame> frame, 127 CefRefPtr<CefRequest> request, 128 CefRefPtr<CefResponse> response, 129 CefString& new_url) {} 130 131 /// 132 // Called on the IO thread when a resource response is received. The |browser| 133 // and |frame| values represent the source of the request, and may be NULL for 134 // requests originating from service workers or CefURLRequest. To allow the 135 // resource load to proceed without modification return false. To redirect or 136 // retry the resource load optionally modify |request| and return true. 137 // Modification of the request URL will be treated as a redirect. Requests 138 // handled using the default network loader cannot be redirected in this 139 // callback. The |response| object cannot be modified in this callback. 140 // 141 // WARNING: Redirecting using this method is deprecated. Use 142 // OnBeforeResourceLoad or GetResourceHandler to perform redirects. 143 /// 144 /*--cef(optional_param=browser,optional_param=frame)--*/ OnResourceResponse(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request,CefRefPtr<CefResponse> response)145 virtual bool OnResourceResponse(CefRefPtr<CefBrowser> browser, 146 CefRefPtr<CefFrame> frame, 147 CefRefPtr<CefRequest> request, 148 CefRefPtr<CefResponse> response) { 149 return false; 150 } 151 152 /// 153 // Called on the IO thread to optionally filter resource response content. The 154 // |browser| and |frame| values represent the source of the request, and may 155 // be NULL for requests originating from service workers or CefURLRequest. 156 // |request| and |response| represent the request and response respectively 157 // and cannot be modified in this callback. 158 /// 159 /*--cef(optional_param=browser,optional_param=frame)--*/ GetResourceResponseFilter(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request,CefRefPtr<CefResponse> response)160 virtual CefRefPtr<CefResponseFilter> GetResourceResponseFilter( 161 CefRefPtr<CefBrowser> browser, 162 CefRefPtr<CefFrame> frame, 163 CefRefPtr<CefRequest> request, 164 CefRefPtr<CefResponse> response) { 165 return nullptr; 166 } 167 168 /// 169 // Called on the IO thread when a resource load has completed. The |browser| 170 // and |frame| values represent the source of the request, and may be NULL for 171 // requests originating from service workers or CefURLRequest. |request| and 172 // |response| represent the request and response respectively and cannot be 173 // modified in this callback. |status| indicates the load completion status. 174 // |received_content_length| is the number of response bytes actually read. 175 // This method will be called for all requests, including requests that are 176 // aborted due to CEF shutdown or destruction of the associated browser. In 177 // cases where the associated browser is destroyed this callback may arrive 178 // after the CefLifeSpanHandler::OnBeforeClose callback for that browser. The 179 // CefFrame::IsValid method can be used to test for this situation, and care 180 // should be taken not to call |browser| or |frame| methods that modify state 181 // (like LoadURL, SendProcessMessage, etc.) if the frame is invalid. 182 /// 183 /*--cef(optional_param=browser,optional_param=frame)--*/ OnResourceLoadComplete(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request,CefRefPtr<CefResponse> response,URLRequestStatus status,int64 received_content_length)184 virtual void OnResourceLoadComplete(CefRefPtr<CefBrowser> browser, 185 CefRefPtr<CefFrame> frame, 186 CefRefPtr<CefRequest> request, 187 CefRefPtr<CefResponse> response, 188 URLRequestStatus status, 189 int64 received_content_length) {} 190 191 /// 192 // Called on the IO thread to handle requests for URLs with an unknown 193 // protocol component. The |browser| and |frame| values represent the source 194 // of the request, and may be NULL for requests originating from service 195 // workers or CefURLRequest. |request| cannot be modified in this callback. 196 // Set |allow_os_execution| to true to attempt execution via the registered OS 197 // protocol handler, if any. 198 // SECURITY WARNING: YOU SHOULD USE THIS METHOD TO ENFORCE RESTRICTIONS BASED 199 // ON SCHEME, HOST OR OTHER URL ANALYSIS BEFORE ALLOWING OS EXECUTION. 200 /// 201 /*--cef(optional_param=browser,optional_param=frame)--*/ OnProtocolExecution(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request,bool & allow_os_execution)202 virtual void OnProtocolExecution(CefRefPtr<CefBrowser> browser, 203 CefRefPtr<CefFrame> frame, 204 CefRefPtr<CefRequest> request, 205 bool& allow_os_execution) {} 206 }; 207 208 /// 209 // Implement this interface to filter cookies that may be sent or received from 210 // resource requests. The methods of this class will be called on the IO thread 211 // unless otherwise indicated. 212 /// 213 /*--cef(source=client,no_debugct_check)--*/ 214 class CefCookieAccessFilter : public virtual CefBaseRefCounted { 215 public: 216 /// 217 // Called on the IO thread before a resource request is sent. The |browser| 218 // and |frame| values represent the source of the request, and may be NULL for 219 // requests originating from service workers or CefURLRequest. |request| 220 // cannot be modified in this callback. Return true if the specified cookie 221 // can be sent with the request or false otherwise. 222 /// 223 /*--cef(optional_param=browser,optional_param=frame)--*/ CanSendCookie(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request,const CefCookie & cookie)224 virtual bool CanSendCookie(CefRefPtr<CefBrowser> browser, 225 CefRefPtr<CefFrame> frame, 226 CefRefPtr<CefRequest> request, 227 const CefCookie& cookie) { 228 return true; 229 } 230 231 /// 232 // Called on the IO thread after a resource response is received. The 233 // |browser| and |frame| values represent the source of the request, and may 234 // be NULL for requests originating from service workers or CefURLRequest. 235 // |request| cannot be modified in this callback. Return true if the specified 236 // cookie returned with the response can be saved or false otherwise. 237 /// 238 /*--cef(optional_param=browser,optional_param=frame)--*/ CanSaveCookie(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request,CefRefPtr<CefResponse> response,const CefCookie & cookie)239 virtual bool CanSaveCookie(CefRefPtr<CefBrowser> browser, 240 CefRefPtr<CefFrame> frame, 241 CefRefPtr<CefRequest> request, 242 CefRefPtr<CefResponse> response, 243 const CefCookie& cookie) { 244 return true; 245 } 246 }; 247 248 #endif // CEF_INCLUDE_CEF_RESOURCE_REQUEST_HANDLER_H_ 249