mirror of
				https://bitbucket.org/chromiumembedded/cef
				synced 2025-06-05 21:39:12 +02:00 
			
		
		
		
	Add CefRequestHandler::OnBeforeBrowse callback (issue #1076).
git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@1440 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
		
							
								
								
									
										9
									
								
								cef.gyp
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								cef.gyp
									
									
									
									
									
								
							| @@ -774,6 +774,10 @@ | ||||
|         '<(SHARED_INTERMEDIATE_DIR)/webkit', | ||||
|       ], | ||||
|       'dependencies': [ | ||||
|         '<(DEPTH)/base/base.gyp:base', | ||||
|         '<(DEPTH)/base/base.gyp:base_prefs', | ||||
|         '<(DEPTH)/base/third_party/dynamic_annotations/dynamic_annotations.gyp:dynamic_annotations', | ||||
|         '<(DEPTH)/components/components.gyp:navigation_interception', | ||||
|         '<(DEPTH)/content/content.gyp:content_app_both', | ||||
|         '<(DEPTH)/content/content.gyp:content_browser', | ||||
|         '<(DEPTH)/content/content.gyp:content_common', | ||||
| @@ -784,17 +788,14 @@ | ||||
|         '<(DEPTH)/content/content.gyp:content_utility', | ||||
|         '<(DEPTH)/content/content.gyp:content_worker', | ||||
|         '<(DEPTH)/content/content_resources.gyp:content_resources', | ||||
|         '<(DEPTH)/base/base.gyp:base', | ||||
|         '<(DEPTH)/base/base.gyp:base_prefs', | ||||
|         '<(DEPTH)/base/third_party/dynamic_annotations/dynamic_annotations.gyp:dynamic_annotations', | ||||
|         '<(DEPTH)/ipc/ipc.gyp:ipc', | ||||
|         '<(DEPTH)/media/media.gyp:media', | ||||
|         '<(DEPTH)/net/net.gyp:net', | ||||
|         '<(DEPTH)/net/net.gyp:net_with_v8', | ||||
|         '<(DEPTH)/skia/skia.gyp:skia', | ||||
|         '<(DEPTH)/third_party/libxml/libxml.gyp:libxml', | ||||
|         '<(DEPTH)/third_party/WebKit/Source/core/core.gyp:webcore', | ||||
|         '<(DEPTH)/third_party/WebKit/public/blink.gyp:blink', | ||||
|         '<(DEPTH)/third_party/WebKit/Source/core/core.gyp:webcore', | ||||
|         '<(DEPTH)/third_party/zlib/zlib.gyp:minizip', | ||||
|         '<(DEPTH)/ui/gl/gl.gyp:gl', | ||||
|         '<(DEPTH)/ui/ui.gyp:ui', | ||||
|   | ||||
| @@ -56,7 +56,10 @@ typedef struct _cef_display_handler_t { | ||||
|   cef_base_t base; | ||||
|  | ||||
|   /// | ||||
|   // Called when the loading state has changed. | ||||
|   // Called when the loading state has changed. This callback will be executed | ||||
|   // twice -- once when loading is initiated either programmatically or by user | ||||
|   // action, and once when loading is terminated due to completion, cancellation | ||||
|   // of failure. | ||||
|   /// | ||||
|   void (CEF_CALLBACK *on_loading_state_change)( | ||||
|       struct _cef_display_handler_t* self, struct _cef_browser_t* browser, | ||||
|   | ||||
| @@ -61,7 +61,8 @@ typedef struct _cef_load_handler_t { | ||||
|   // main frame. Multiple frames may be loading at the same time. Sub-frames may | ||||
|   // start or continue loading after the main frame load has ended. This | ||||
|   // function may not be called for a particular frame if the load request for | ||||
|   // that frame fails. | ||||
|   // that frame fails. For notification of overall browser load status use | ||||
|   // cef_display_handler_t:: OnLoadingStateChange instead. | ||||
|   /// | ||||
|   void (CEF_CALLBACK *on_load_start)(struct _cef_load_handler_t* self, | ||||
|       struct _cef_browser_t* browser, struct _cef_frame_t* frame); | ||||
| @@ -79,10 +80,10 @@ typedef struct _cef_load_handler_t { | ||||
|       int httpStatusCode); | ||||
|  | ||||
|   /// | ||||
|   // Called when the browser fails to load a resource. |errorCode| is the error | ||||
|   // code number, |errorText| is the error text and and |failedUrl| is the URL | ||||
|   // that failed to load. See net\base\net_error_list.h for complete | ||||
|   // descriptions of the error codes. | ||||
|   // Called when the resource load for a navigation fails or is canceled. | ||||
|   // |errorCode| is the error code number, |errorText| is the error text and | ||||
|   // |failedUrl| is the URL that failed to load. See net\base\net_error_list.h | ||||
|   // for complete descriptions of the error codes. | ||||
|   /// | ||||
|   void (CEF_CALLBACK *on_load_error)(struct _cef_load_handler_t* self, | ||||
|       struct _cef_browser_t* browser, struct _cef_frame_t* frame, | ||||
|   | ||||
| @@ -96,6 +96,20 @@ typedef struct _cef_request_handler_t { | ||||
|   /// | ||||
|   cef_base_t base; | ||||
|  | ||||
|   /// | ||||
|   // Called on the UI thread before browser navigation. Return true (1) to | ||||
|   // cancel the navigation or false (0) to allow the navigation to proceed. The | ||||
|   // |request| object cannot be modified in this callback. | ||||
|   // cef_display_handler_t::OnLoadingStateChange will be called twice in all | ||||
|   // cases. If the navigation is allowed cef_load_handler_t::OnLoadStart and | ||||
|   // cef_load_handler_t::OnLoadEnd will be called. If the navigation is canceled | ||||
|   // cef_load_handler_t::OnLoadError will be called with an |errorCode| value of | ||||
|   // ERR_ABORTED. | ||||
|   /// | ||||
|   int (CEF_CALLBACK *on_before_browse)(struct _cef_request_handler_t* self, | ||||
|       struct _cef_browser_t* browser, struct _cef_frame_t* frame, | ||||
|       struct _cef_request_t* request, int is_redirect); | ||||
|  | ||||
|   /// | ||||
|   // Called on the IO thread before a resource request is loaded. The |request| | ||||
|   // object may be modified. To cancel the request return true (1) otherwise | ||||
|   | ||||
| @@ -50,7 +50,10 @@ | ||||
| class CefDisplayHandler : public virtual CefBase { | ||||
|  public: | ||||
|   /// | ||||
|   // Called when the loading state has changed. | ||||
|   // Called when the loading state has changed. This callback will be executed | ||||
|   // twice -- once when loading is initiated either programmatically or by user | ||||
|   // action, and once when loading is terminated due to completion, cancellation | ||||
|   // of failure. | ||||
|   /// | ||||
|   /*--cef()--*/ | ||||
|   virtual void OnLoadingStateChange(CefRefPtr<CefBrowser> browser, | ||||
|   | ||||
| @@ -58,7 +58,8 @@ class CefLoadHandler : public virtual CefBase { | ||||
|   // main frame. Multiple frames may be loading at the same time. Sub-frames may | ||||
|   // start or continue loading after the main frame load has ended. This method | ||||
|   // may not be called for a particular frame if the load request for that frame | ||||
|   // fails. | ||||
|   // fails. For notification of overall browser load status use | ||||
|   // CefDisplayHandler:: OnLoadingStateChange instead. | ||||
|   /// | ||||
|   /*--cef()--*/ | ||||
|   virtual void OnLoadStart(CefRefPtr<CefBrowser> browser, | ||||
| @@ -78,10 +79,10 @@ class CefLoadHandler : public virtual CefBase { | ||||
|                          int httpStatusCode) {} | ||||
|  | ||||
|   /// | ||||
|   // Called when the browser fails to load a resource. |errorCode| is the error | ||||
|   // code number, |errorText| is the error text and and |failedUrl| is the URL | ||||
|   // that failed to load. See net\base\net_error_list.h for complete | ||||
|   // descriptions of the error codes. | ||||
|   // Called when the resource load for a navigation fails or is canceled. | ||||
|   // |errorCode| is the error code number, |errorText| is the error text and | ||||
|   // |failedUrl| is the URL that failed to load. See net\base\net_error_list.h | ||||
|   // for complete descriptions of the error codes. | ||||
|   /// | ||||
|   /*--cef(optional_param=errorText)--*/ | ||||
|   virtual void OnLoadError(CefRefPtr<CefBrowser> browser, | ||||
|   | ||||
| @@ -91,6 +91,24 @@ class CefAllowCertificateErrorCallback : public virtual CefBase { | ||||
| /*--cef(source=client)--*/ | ||||
| class CefRequestHandler : public virtual CefBase { | ||||
|  public: | ||||
|   /// | ||||
|   // Called on the UI thread before browser navigation. Return true to cancel | ||||
|   // the navigation or false to allow the navigation to proceed. The |request| | ||||
|   // object cannot be modified in this callback. | ||||
|   // CefDisplayHandler::OnLoadingStateChange will be called twice in all cases. | ||||
|   // If the navigation is allowed CefLoadHandler::OnLoadStart and | ||||
|   // CefLoadHandler::OnLoadEnd will be called. If the navigation is canceled | ||||
|   // CefLoadHandler::OnLoadError will be called with an |errorCode| value of | ||||
|   // ERR_ABORTED. | ||||
|   /// | ||||
|   /*--cef()--*/ | ||||
|   virtual bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser, | ||||
|                               CefRefPtr<CefFrame> frame, | ||||
|                               CefRefPtr<CefRequest> request, | ||||
|                               bool is_redirect) { | ||||
|     return false; | ||||
|   } | ||||
|  | ||||
|   /// | ||||
|   // Called on the IO thread before a resource request is loaded. The |request| | ||||
|   // object may be modified. To cancel the request return true otherwise return | ||||
|   | ||||
| @@ -5,16 +5,93 @@ | ||||
| #include "libcef/browser/resource_dispatcher_host_delegate.h" | ||||
| #include "libcef/browser/browser_host_impl.h" | ||||
| #include "libcef/browser/origin_whitelist_impl.h" | ||||
| #include "libcef/browser/thread_util.h" | ||||
| #include "libcef/common/request_impl.h" | ||||
|  | ||||
| #include "base/memory/scoped_vector.h" | ||||
| #include "components/navigation_interception/intercept_navigation_resource_throttle.h" | ||||
| #include "components/navigation_interception/navigation_params.h" | ||||
| #include "content/public/browser/resource_request_info.h" | ||||
| #include "content/public/common/resource_response.h" | ||||
| #include "net/http/http_response_headers.h" | ||||
| #include "net/url_request/url_request.h" | ||||
|  | ||||
| namespace { | ||||
|  | ||||
| bool NavigationOnUIThread( | ||||
|     int64 frame_id, | ||||
|     CefRefPtr<CefRequestImpl> request, | ||||
|     content::RenderViewHost* source, | ||||
|     const navigation_interception::NavigationParams& params) { | ||||
|   CEF_REQUIRE_UIT(); | ||||
|  | ||||
|   bool ignore_navigation = false; | ||||
|  | ||||
|   CefRefPtr<CefBrowserHostImpl> browser = | ||||
|       CefBrowserHostImpl::GetBrowserForHost(source); | ||||
|   DCHECK(browser.get()); | ||||
|   if (browser.get()) { | ||||
|     CefRefPtr<CefClient> client = browser->GetClient(); | ||||
|     if (client.get()) { | ||||
|       CefRefPtr<CefRequestHandler> handler = client->GetRequestHandler(); | ||||
|       if (handler.get()) { | ||||
|         CefRefPtr<CefFrame> frame; | ||||
|         if (frame_id >= 0) | ||||
|           frame = browser->GetFrame(frame_id); | ||||
|         DCHECK(frame.get()); | ||||
|         if (frame.get()) { | ||||
|           ignore_navigation = handler->OnBeforeBrowse( | ||||
|               browser.get(), frame, request.get(), params.is_redirect()); | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   return ignore_navigation; | ||||
| } | ||||
|  | ||||
| }  // namespace | ||||
|  | ||||
| CefResourceDispatcherHostDelegate::CefResourceDispatcherHostDelegate() { | ||||
| } | ||||
|  | ||||
| CefResourceDispatcherHostDelegate::~CefResourceDispatcherHostDelegate() { | ||||
| } | ||||
|  | ||||
| void CefResourceDispatcherHostDelegate::RequestBeginning( | ||||
|     net::URLRequest* request, | ||||
|     content::ResourceContext* resource_context, | ||||
|     appcache::AppCacheService* appcache_service, | ||||
|     ResourceType::Type resource_type, | ||||
|     int child_id, | ||||
|     int route_id, | ||||
|     bool is_continuation_of_transferred_request, | ||||
|     ScopedVector<content::ResourceThrottle>* throttles) { | ||||
|   if (resource_type == ResourceType::MAIN_FRAME || | ||||
|       resource_type == ResourceType::SUB_FRAME) { | ||||
|     int64 frame_id = -1; | ||||
|  | ||||
|     // ResourceRequestInfo will not exist for requests originating from | ||||
|     // WebURLLoader in the render process. | ||||
|     const content::ResourceRequestInfo* info = | ||||
|         content::ResourceRequestInfo::ForRequest(request); | ||||
|     if (info) | ||||
|       frame_id = info->GetFrameID(); | ||||
|  | ||||
|     if (frame_id >= 0) { | ||||
|       CefRefPtr<CefRequestImpl> cef_request(new CefRequestImpl); | ||||
|       cef_request->Set(request); | ||||
|       cef_request->SetReadOnly(true); | ||||
|  | ||||
|       content::ResourceThrottle* throttle = | ||||
|         new navigation_interception::InterceptNavigationResourceThrottle( | ||||
|             request, | ||||
|             base::Bind(&NavigationOnUIThread, frame_id, cef_request)); | ||||
|       throttles->push_back(throttle); | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  | ||||
| bool CefResourceDispatcherHostDelegate::HandleExternalProtocol(const GURL& url, | ||||
|                                                                int child_id, | ||||
|                                                                int route_id) { | ||||
|   | ||||
| @@ -17,6 +17,15 @@ class CefResourceDispatcherHostDelegate | ||||
|   virtual ~CefResourceDispatcherHostDelegate(); | ||||
|  | ||||
|   // ResourceDispatcherHostDelegate methods. | ||||
|   virtual void RequestBeginning( | ||||
|       net::URLRequest* request, | ||||
|       content::ResourceContext* resource_context, | ||||
|       appcache::AppCacheService* appcache_service, | ||||
|       ResourceType::Type resource_type, | ||||
|       int child_id, | ||||
|       int route_id, | ||||
|       bool is_continuation_of_transferred_request, | ||||
|       ScopedVector<content::ResourceThrottle>* throttles) OVERRIDE; | ||||
|   virtual bool HandleExternalProtocol(const GURL& url, | ||||
|                                       int child_id, | ||||
|                                       int route_id) OVERRIDE; | ||||
|   | ||||
| @@ -23,6 +23,38 @@ | ||||
|  | ||||
| // MEMBER FUNCTIONS - Body may be edited by hand. | ||||
|  | ||||
| int CEF_CALLBACK request_handler_on_before_browse( | ||||
|     struct _cef_request_handler_t* self, cef_browser_t* browser, | ||||
|     cef_frame_t* frame, cef_request_t* request, int is_redirect) { | ||||
|   // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING | ||||
|  | ||||
|   DCHECK(self); | ||||
|   if (!self) | ||||
|     return 0; | ||||
|   // Verify param: browser; type: refptr_diff | ||||
|   DCHECK(browser); | ||||
|   if (!browser) | ||||
|     return 0; | ||||
|   // Verify param: frame; type: refptr_diff | ||||
|   DCHECK(frame); | ||||
|   if (!frame) | ||||
|     return 0; | ||||
|   // Verify param: request; type: refptr_diff | ||||
|   DCHECK(request); | ||||
|   if (!request) | ||||
|     return 0; | ||||
|  | ||||
|   // Execute | ||||
|   bool _retval = CefRequestHandlerCppToC::Get(self)->OnBeforeBrowse( | ||||
|       CefBrowserCToCpp::Wrap(browser), | ||||
|       CefFrameCToCpp::Wrap(frame), | ||||
|       CefRequestCToCpp::Wrap(request), | ||||
|       is_redirect?true:false); | ||||
|  | ||||
|   // Return type: bool | ||||
|   return _retval; | ||||
| } | ||||
|  | ||||
| int CEF_CALLBACK request_handler_on_before_resource_load( | ||||
|     struct _cef_request_handler_t* self, cef_browser_t* browser, | ||||
|     cef_frame_t* frame, cef_request_t* request) { | ||||
| @@ -302,6 +334,7 @@ int CEF_CALLBACK request_handler_on_certificate_error( | ||||
| CefRequestHandlerCppToC::CefRequestHandlerCppToC(CefRequestHandler* cls) | ||||
|     : CefCppToC<CefRequestHandlerCppToC, CefRequestHandler, | ||||
|         cef_request_handler_t>(cls) { | ||||
|   struct_.struct_.on_before_browse = request_handler_on_before_browse; | ||||
|   struct_.struct_.on_before_resource_load = | ||||
|       request_handler_on_before_resource_load; | ||||
|   struct_.struct_.get_resource_handler = request_handler_get_resource_handler; | ||||
|   | ||||
| @@ -23,6 +23,38 @@ | ||||
|  | ||||
| // VIRTUAL METHODS - Body may be edited by hand. | ||||
|  | ||||
| bool CefRequestHandlerCToCpp::OnBeforeBrowse(CefRefPtr<CefBrowser> browser, | ||||
|     CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, | ||||
|     bool is_redirect) { | ||||
|   if (CEF_MEMBER_MISSING(struct_, on_before_browse)) | ||||
|     return false; | ||||
|  | ||||
|   // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING | ||||
|  | ||||
|   // Verify param: browser; type: refptr_diff | ||||
|   DCHECK(browser.get()); | ||||
|   if (!browser.get()) | ||||
|     return false; | ||||
|   // Verify param: frame; type: refptr_diff | ||||
|   DCHECK(frame.get()); | ||||
|   if (!frame.get()) | ||||
|     return false; | ||||
|   // Verify param: request; type: refptr_diff | ||||
|   DCHECK(request.get()); | ||||
|   if (!request.get()) | ||||
|     return false; | ||||
|  | ||||
|   // Execute | ||||
|   int _retval = struct_->on_before_browse(struct_, | ||||
|       CefBrowserCppToC::Wrap(browser), | ||||
|       CefFrameCppToC::Wrap(frame), | ||||
|       CefRequestCppToC::Wrap(request), | ||||
|       is_redirect); | ||||
|  | ||||
|   // Return type: bool | ||||
|   return _retval?true:false; | ||||
| } | ||||
|  | ||||
| bool CefRequestHandlerCToCpp::OnBeforeResourceLoad( | ||||
|     CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, | ||||
|     CefRefPtr<CefRequest> request) { | ||||
|   | ||||
| @@ -34,6 +34,9 @@ class CefRequestHandlerCToCpp | ||||
|   virtual ~CefRequestHandlerCToCpp() {} | ||||
|  | ||||
|   // CefRequestHandler methods | ||||
|   virtual bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser, | ||||
|       CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, | ||||
|       bool is_redirect) OVERRIDE; | ||||
|   virtual bool OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser, | ||||
|       CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request) OVERRIDE; | ||||
|   virtual CefRefPtr<CefResourceHandler> GetResourceHandler( | ||||
|   | ||||
| @@ -214,6 +214,35 @@ class HistoryNavTestHandler : public TestHandler { | ||||
|     RunNav(browser); | ||||
|   } | ||||
|  | ||||
|   virtual bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser, | ||||
|                               CefRefPtr<CefFrame> frame, | ||||
|                               CefRefPtr<CefRequest> request, | ||||
|                               bool is_redirect) OVERRIDE { | ||||
|     const NavListItem& item = kHNavList[nav_]; | ||||
|  | ||||
|     got_before_browse_[nav_].yes(); | ||||
|  | ||||
|     std::string url = request->GetURL(); | ||||
|     EXPECT_STREQ(item.target, url.c_str()); | ||||
|  | ||||
|     EXPECT_EQ(RT_MAIN_FRAME, request->GetResourceType()); | ||||
|     if (item.action == NA_LOAD) | ||||
|       EXPECT_EQ(TT_EXPLICIT, request->GetTransitionType()); | ||||
|     else if (item.action == NA_BACK || item.action == NA_FORWARD) | ||||
|       EXPECT_EQ(TT_EXPLICIT | TT_FORWARD_BACK_FLAG, request->GetTransitionType()); | ||||
|  | ||||
|     if (nav_ > 0) { | ||||
|       const NavListItem& last_item = kHNavList[nav_ - 1]; | ||||
|       EXPECT_EQ(last_item.can_go_back, browser->CanGoBack()); | ||||
|       EXPECT_EQ(last_item.can_go_forward, browser->CanGoForward()); | ||||
|     } else { | ||||
|       EXPECT_FALSE(browser->CanGoBack()); | ||||
|       EXPECT_FALSE(browser->CanGoForward()); | ||||
|     } | ||||
|  | ||||
|     return false; | ||||
|   } | ||||
|  | ||||
|   virtual bool OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser, | ||||
|                                     CefRefPtr<CefFrame> frame, | ||||
|                                     CefRefPtr<CefRequest> request) OVERRIDE { | ||||
| @@ -313,6 +342,7 @@ class HistoryNavTestHandler : public TestHandler { | ||||
|   bool load_end_confirmation_; | ||||
|   bool renderer_confirmation_; | ||||
|  | ||||
|   TrackCallback got_before_browse_[NAV_LIST_SIZE()]; | ||||
|   TrackCallback got_before_navigation_[NAV_LIST_SIZE()]; | ||||
|   TrackCallback got_before_resource_load_[NAV_LIST_SIZE()]; | ||||
|   TrackCallback got_correct_target_[NAV_LIST_SIZE()]; | ||||
| @@ -339,6 +369,7 @@ TEST(NavigationTest, History) { | ||||
|  | ||||
|   for (size_t i = 0; i < NAV_LIST_SIZE(); ++i) { | ||||
|     if (kHNavList[i].action != NA_CLEAR) { | ||||
|       ASSERT_TRUE(handler->got_before_browse_[i]) << "i = " << i; | ||||
|       ASSERT_TRUE(handler->got_before_navigation_[i]) << "i = " << i; | ||||
|       ASSERT_TRUE(handler->got_before_resource_load_[i]) << "i = " << i; | ||||
|       ASSERT_TRUE(handler->got_correct_target_[i]) << "i = " << i; | ||||
| @@ -1059,6 +1090,35 @@ class OrderNavTestHandler : public TestHandler { | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   virtual bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser, | ||||
|                               CefRefPtr<CefFrame> frame, | ||||
|                               CefRefPtr<CefRequest> request, | ||||
|                               bool is_redirect) OVERRIDE { | ||||
|     EXPECT_EQ(RT_MAIN_FRAME, request->GetResourceType()); | ||||
|  | ||||
|     if (browser->IsPopup()) { | ||||
|       EXPECT_EQ(TT_LINK, request->GetTransitionType()); | ||||
|       EXPECT_GT(browser->GetIdentifier(), 0); | ||||
|       EXPECT_EQ(browser_id_popup_, browser->GetIdentifier()); | ||||
|       got_before_browse_popup_.yes(); | ||||
|     } else { | ||||
|       EXPECT_EQ(TT_EXPLICIT, request->GetTransitionType()); | ||||
|       EXPECT_GT(browser->GetIdentifier(), 0); | ||||
|       EXPECT_EQ(browser_id_main_, browser->GetIdentifier()); | ||||
|       got_before_browse_main_.yes(); | ||||
|     } | ||||
|  | ||||
|     std::string url = request->GetURL(); | ||||
|     if (url == KONav1) | ||||
|       EXPECT_FALSE(browser->IsPopup()); | ||||
|     else if (url == KONav2) | ||||
|       EXPECT_TRUE(browser->IsPopup()); | ||||
|     else | ||||
|       EXPECT_TRUE(false);  // not reached | ||||
|  | ||||
|     return false; | ||||
|   } | ||||
|  | ||||
|   virtual bool OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser, | ||||
|                                     CefRefPtr<CefFrame> frame, | ||||
|                                     CefRefPtr<CefRequest> request) OVERRIDE { | ||||
| @@ -1146,10 +1206,21 @@ class OrderNavTestHandler : public TestHandler { | ||||
|   } | ||||
|  | ||||
|  protected: | ||||
|   virtual void DestroyTest() OVERRIDE { | ||||
|     // Verify test expectations. | ||||
|     EXPECT_TRUE(got_before_browse_main_); | ||||
|     EXPECT_TRUE(got_before_browse_popup_); | ||||
|  | ||||
|     TestHandler::DestroyTest(); | ||||
|   } | ||||
|  | ||||
|   int browser_id_main_; | ||||
|   int browser_id_popup_; | ||||
|   CefRefPtr<CefBrowser> browser_popup_; | ||||
|  | ||||
|   TrackCallback got_before_browse_main_; | ||||
|   TrackCallback got_before_browse_popup_; | ||||
|  | ||||
|   bool got_message_; | ||||
|   bool got_load_end_; | ||||
| }; | ||||
| @@ -1401,6 +1472,19 @@ class CrossOriginNavTestHandler : public TestHandler { | ||||
|     EXPECT_GT(browser_id_current_, 0); | ||||
|   } | ||||
|  | ||||
|   virtual bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser, | ||||
|                               CefRefPtr<CefFrame> frame, | ||||
|                               CefRefPtr<CefRequest> request, | ||||
|                               bool is_redirect) OVERRIDE { | ||||
|     EXPECT_EQ(RT_MAIN_FRAME, request->GetResourceType()); | ||||
|     EXPECT_EQ(TT_EXPLICIT, request->GetTransitionType()); | ||||
|  | ||||
|     EXPECT_GT(browser_id_current_, 0); | ||||
|     EXPECT_EQ(browser_id_current_, browser->GetIdentifier()); | ||||
|  | ||||
|     return false; | ||||
|   } | ||||
|  | ||||
|   virtual bool OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser, | ||||
|                                     CefRefPtr<CefFrame> frame, | ||||
|                                     CefRefPtr<CefRequest> request) OVERRIDE { | ||||
| @@ -1578,6 +1662,167 @@ TEST(NavigationTest, PopupDeny) { | ||||
| } | ||||
|  | ||||
|  | ||||
| namespace { | ||||
|  | ||||
| const char kBrowseNavPageUrl[] = "http://tests-browsenav/nav.html"; | ||||
|  | ||||
| // Browser side. | ||||
| class BrowseNavTestHandler : public TestHandler { | ||||
|  public: | ||||
|   BrowseNavTestHandler(bool allow) | ||||
|       : allow_(allow), | ||||
|         destroyed_(false) {} | ||||
|  | ||||
|   virtual void RunTest() OVERRIDE { | ||||
|    AddResource(kBrowseNavPageUrl, "<html>Test</html>", "text/html"); | ||||
|  | ||||
|     // Create the browser. | ||||
|     CreateBrowser(kBrowseNavPageUrl); | ||||
|  | ||||
|     // Time out the test after a reasonable period of time. | ||||
|     CefPostDelayedTask(TID_UI, | ||||
|           NewCefRunnableMethod(this, &BrowseNavTestHandler::DestroyTest), | ||||
|           2000); | ||||
|   } | ||||
|  | ||||
|   virtual bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser, | ||||
|                               CefRefPtr<CefFrame> frame, | ||||
|                               CefRefPtr<CefRequest> request, | ||||
|                               bool is_redirect) OVERRIDE { | ||||
|     const std::string& url = request->GetURL(); | ||||
|     EXPECT_STREQ(kBrowseNavPageUrl, url.c_str()); | ||||
|     EXPECT_EQ(GetBrowserId(), browser->GetIdentifier()); | ||||
|     EXPECT_TRUE(frame->IsMain()); | ||||
|      | ||||
|     got_before_browse_.yes(); | ||||
|      | ||||
|     return !allow_; | ||||
|   } | ||||
|  | ||||
|   virtual void OnLoadStart(CefRefPtr<CefBrowser> browser, | ||||
|                            CefRefPtr<CefFrame> frame) OVERRIDE { | ||||
|     const std::string& url = frame->GetURL(); | ||||
|     EXPECT_STREQ(kBrowseNavPageUrl, url.c_str()); | ||||
|     EXPECT_EQ(GetBrowserId(), browser->GetIdentifier()); | ||||
|     EXPECT_TRUE(frame->IsMain()); | ||||
|  | ||||
|     got_load_start_.yes(); | ||||
|   } | ||||
|  | ||||
|   virtual void OnLoadEnd(CefRefPtr<CefBrowser> browser, | ||||
|                          CefRefPtr<CefFrame> frame, | ||||
|                          int httpStatusCode) OVERRIDE { | ||||
|     const std::string& url = frame->GetURL(); | ||||
|     EXPECT_STREQ(kBrowseNavPageUrl, url.c_str()); | ||||
|     EXPECT_EQ(GetBrowserId(), browser->GetIdentifier()); | ||||
|     EXPECT_TRUE(frame->IsMain()); | ||||
|  | ||||
|     got_load_end_.yes(); | ||||
|     DestroyTestIfDone(); | ||||
|   } | ||||
|  | ||||
|   virtual void OnLoadError(CefRefPtr<CefBrowser> browser, | ||||
|                            CefRefPtr<CefFrame> frame, | ||||
|                            ErrorCode errorCode, | ||||
|                            const CefString& errorText, | ||||
|                            const CefString& failedUrl) OVERRIDE { | ||||
|     const std::string& url = frame->GetURL(); | ||||
|     EXPECT_STREQ("", url.c_str()); | ||||
|     EXPECT_EQ(GetBrowserId(), browser->GetIdentifier()); | ||||
|     EXPECT_TRUE(frame->IsMain()); | ||||
|  | ||||
|     EXPECT_EQ(ERR_ABORTED, errorCode); | ||||
|     EXPECT_STREQ(kBrowseNavPageUrl, failedUrl.ToString().c_str()); | ||||
|  | ||||
|     got_load_error_.yes(); | ||||
|     DestroyTestIfDone(); | ||||
|   } | ||||
|  | ||||
|   virtual void OnLoadingStateChange(CefRefPtr<CefBrowser> browser, | ||||
|                                     bool isLoading, | ||||
|                                     bool canGoBack, | ||||
|                                     bool canGoForward) OVERRIDE { | ||||
|     const std::string& url = browser->GetMainFrame()->GetURL(); | ||||
|     EXPECT_EQ(GetBrowserId(), browser->GetIdentifier()); | ||||
|  | ||||
|     if (isLoading) { | ||||
|       EXPECT_STREQ("", url.c_str()); | ||||
|  | ||||
|       got_loading_state_changed_start_.yes(); | ||||
|     } else { | ||||
|       if (allow_) | ||||
|         EXPECT_STREQ(kBrowseNavPageUrl, url.c_str()); | ||||
|       else | ||||
|         EXPECT_STREQ("", url.c_str()); | ||||
|  | ||||
|       got_loading_state_changed_end_.yes(); | ||||
|       DestroyTestIfDone(); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   void DestroyTestIfDone() { | ||||
|     if (destroyed_) | ||||
|       return; | ||||
|  | ||||
|     if (got_loading_state_changed_end_) { | ||||
|       if (allow_) { | ||||
|         if (got_load_end_) | ||||
|           DestroyTest(); | ||||
|       } else if (got_load_error_) { | ||||
|         DestroyTest(); | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   virtual void DestroyTest() OVERRIDE { | ||||
|     if (destroyed_) | ||||
|       return; | ||||
|     destroyed_ = true; | ||||
|  | ||||
|     EXPECT_TRUE(got_before_browse_); | ||||
|     EXPECT_TRUE(got_loading_state_changed_start_); | ||||
|     EXPECT_TRUE(got_loading_state_changed_end_); | ||||
|  | ||||
|     if (allow_) { | ||||
|       EXPECT_TRUE(got_load_start_); | ||||
|       EXPECT_TRUE(got_load_end_); | ||||
|       EXPECT_FALSE(got_load_error_); | ||||
|     } else { | ||||
|       EXPECT_FALSE(got_load_start_); | ||||
|       EXPECT_FALSE(got_load_end_); | ||||
|       EXPECT_TRUE(got_load_error_); | ||||
|     } | ||||
|  | ||||
|     TestHandler::DestroyTest(); | ||||
|   } | ||||
|  | ||||
|   bool allow_; | ||||
|   bool destroyed_; | ||||
|  | ||||
|   TrackCallback got_before_browse_; | ||||
|   TrackCallback got_load_start_; | ||||
|   TrackCallback got_load_end_; | ||||
|   TrackCallback got_load_error_; | ||||
|   TrackCallback got_loading_state_changed_start_; | ||||
|   TrackCallback got_loading_state_changed_end_; | ||||
| }; | ||||
|  | ||||
| }  // namespace | ||||
|  | ||||
| // Test allowing navigation. | ||||
| TEST(NavigationTest, BrowseAllow) { | ||||
|   CefRefPtr<BrowseNavTestHandler> handler = new BrowseNavTestHandler(true); | ||||
|   handler->ExecuteTest(); | ||||
| } | ||||
|  | ||||
| // Test denying navigation. | ||||
| TEST(NavigationTest, BrowseDeny) { | ||||
|   CefRefPtr<BrowseNavTestHandler> handler = new BrowseNavTestHandler(false); | ||||
|   handler->ExecuteTest(); | ||||
| } | ||||
|  | ||||
|  | ||||
| // Entry point for creating navigation browser test objects. | ||||
| // Called from client_app_delegates.cc. | ||||
| void CreateNavigationBrowserTests(ClientApp::BrowserDelegateSet& delegates) { | ||||
|   | ||||
| @@ -207,41 +207,44 @@ const char kTypeTestOrigin[] = "http://tests-requesttt.com/"; | ||||
| static struct TypeExpected { | ||||
|   const char* file; | ||||
|   bool browser_side;  // True if this expectation applies to the browser side. | ||||
|   bool navigation; // True if this expectation represents a navigation. | ||||
|   cef_transition_type_t transition_type; | ||||
|   cef_resource_type_t resource_type; | ||||
|   int expected_count; | ||||
| } g_type_expected[] = { | ||||
|   // Initial main frame load due to browser creation. | ||||
|   {"main.html", true,  TT_EXPLICIT, RT_MAIN_FRAME,   1}, | ||||
|   {"main.html", false, TT_EXPLICIT, RT_SUB_RESOURCE, 1}, | ||||
|   {"main.html", true,  true, TT_EXPLICIT, RT_MAIN_FRAME,   1}, | ||||
|   {"main.html", false, true, TT_EXPLICIT, RT_SUB_RESOURCE, 1}, | ||||
|  | ||||
|   // Sub frame load. | ||||
|   {"sub.html", true,  TT_LINK,     RT_SUB_FRAME,    1}, | ||||
|   {"sub.html", false, TT_EXPLICIT, RT_SUB_RESOURCE, 1}, | ||||
|   {"sub.html", true,  true, TT_LINK,     RT_SUB_FRAME,    1}, | ||||
|   {"sub.html", false, true, TT_EXPLICIT, RT_SUB_RESOURCE, 1}, | ||||
|  | ||||
|   // Stylesheet load. | ||||
|   {"style.css", true, TT_LINK, RT_STYLESHEET, 1}, | ||||
|   {"style.css", true, false, TT_LINK, RT_STYLESHEET, 1}, | ||||
|  | ||||
|   // Script load. | ||||
|   {"script.js", true, TT_LINK, RT_SCRIPT, 1}, | ||||
|   {"script.js", true, false, TT_LINK, RT_SCRIPT, 1}, | ||||
|  | ||||
|   // Image load. | ||||
|   {"image.png", true, TT_LINK, RT_IMAGE, 1}, | ||||
|   {"image.png", true, false, TT_LINK, RT_IMAGE, 1}, | ||||
|  | ||||
|   // Font load. | ||||
|   {"font.ttf", true, TT_LINK, RT_FONT_RESOURCE, 1}, | ||||
|   {"font.ttf", true, false, TT_LINK, RT_FONT_RESOURCE, 1}, | ||||
|  | ||||
|   // XHR load. | ||||
|   {"xhr.html", true, TT_LINK, RT_XHR, 1}, | ||||
|   {"xhr.html", true, false, TT_LINK, RT_XHR, 1}, | ||||
| }; | ||||
|  | ||||
| class TypeExpectations { | ||||
|  public: | ||||
|   TypeExpectations(bool browser_side) | ||||
|       : browser_side_(browser_side) { | ||||
|   TypeExpectations(bool browser_side, bool navigation) | ||||
|       : browser_side_(browser_side), | ||||
|         navigation_(navigation) { | ||||
|     // Build the map of relevant requests. | ||||
|     for (size_t i = 0; i < sizeof(g_type_expected) / sizeof(TypeExpected); ++i) { | ||||
|       if (g_type_expected[i].browser_side != browser_side_) | ||||
|       if (g_type_expected[i].browser_side != browser_side_ || | ||||
|           (navigation_ && g_type_expected[i].navigation != navigation_)) | ||||
|         continue; | ||||
|  | ||||
|       request_count_.insert(std::make_pair(i, 0)); | ||||
| @@ -263,6 +266,7 @@ class TypeExpectations { | ||||
|     EXPECT_GE(index, 0) | ||||
|         << "File: " << file.c_str() | ||||
|         << "; Browser Side: " << browser_side_ | ||||
|         << "; Navigation: " << navigation_ | ||||
|         << "; Transition Type: " << transition_type | ||||
|         << "; Resource Type: " << resource_type; | ||||
|  | ||||
| @@ -274,6 +278,7 @@ class TypeExpectations { | ||||
|     EXPECT_LE(actual_count, expected_count) | ||||
|         << "File: " << file.c_str() | ||||
|         << "; Browser Side: " << browser_side_ | ||||
|         << "; Navigation: " << navigation_ | ||||
|         << "; Transition Type: " << transition_type | ||||
|         << "; Resource Type: " << resource_type; | ||||
|  | ||||
| @@ -283,7 +288,8 @@ class TypeExpectations { | ||||
|   // Test if all expectations have been met. | ||||
|   bool IsDone(bool assert) { | ||||
|     for (size_t i = 0; i < sizeof(g_type_expected) / sizeof(TypeExpected); ++i) { | ||||
|       if (g_type_expected[i].browser_side != browser_side_) | ||||
|       if (g_type_expected[i].browser_side != browser_side_ || | ||||
|           (navigation_ && g_type_expected[i].navigation != navigation_)) | ||||
|         continue; | ||||
|  | ||||
|       RequestCount::const_iterator it = request_count_.find(i); | ||||
| @@ -293,6 +299,7 @@ class TypeExpectations { | ||||
|           EXPECT_EQ(g_type_expected[i].expected_count, it->second) | ||||
|               << "File: " << g_type_expected[i].file | ||||
|               << "; Browser Side: " << browser_side_ | ||||
|               << "; Navigation: " << navigation_ | ||||
|               << "; Transition Type: " << g_type_expected[i].transition_type | ||||
|               << "; Resource Type: " << g_type_expected[i].resource_type; | ||||
|         } | ||||
| @@ -310,6 +317,7 @@ class TypeExpectations { | ||||
|     for (size_t i = 0; i < sizeof(g_type_expected) / sizeof(TypeExpected); ++i) { | ||||
|       if (g_type_expected[i].file == file && | ||||
|           g_type_expected[i].browser_side == browser_side_ && | ||||
|           (!navigation_ || g_type_expected[i].navigation == navigation_) && | ||||
|           g_type_expected[i].transition_type == transition_type && | ||||
|           g_type_expected[i].resource_type == resource_type) { | ||||
|         return i; | ||||
| @@ -319,6 +327,7 @@ class TypeExpectations { | ||||
|   } | ||||
|  | ||||
|   bool browser_side_; | ||||
|   bool navigation_; | ||||
|  | ||||
|   // Map of TypeExpected index to actual request count. | ||||
|   typedef std::map<int, int> RequestCount; | ||||
| @@ -329,7 +338,7 @@ class TypeExpectations { | ||||
| class TypeRendererTest : public ClientApp::RenderDelegate { | ||||
|  public: | ||||
|   TypeRendererTest() : | ||||
|       expectations_(false) {} | ||||
|       expectations_(false, true) {} | ||||
|  | ||||
|   virtual bool OnBeforeNavigation(CefRefPtr<ClientApp> app, | ||||
|                                   CefRefPtr<CefBrowser> browser, | ||||
| @@ -366,11 +375,12 @@ class TypeRendererTest : public ClientApp::RenderDelegate { | ||||
| class TypeTestHandler : public TestHandler { | ||||
|  public: | ||||
|   TypeTestHandler() : | ||||
|       before_expectations_(true), | ||||
|       get_expectations_(true), | ||||
|       browse_expectations_(true, true), | ||||
|       load_expectations_(true, false), | ||||
|       get_expectations_(true, false), | ||||
|       completed_browser_side_(false), | ||||
|       completed_render_side_(false), | ||||
|       timed_out_(false) {} | ||||
|       destroyed_(false) {} | ||||
|  | ||||
|   virtual void RunTest() OVERRIDE { | ||||
|     AddResource(std::string(kTypeTestOrigin) + "main.html", | ||||
| @@ -416,14 +426,23 @@ class TypeTestHandler : public TestHandler { | ||||
|  | ||||
|     // Time out the test after a reasonable period of time. | ||||
|     CefPostDelayedTask(TID_UI, | ||||
|           NewCefRunnableMethod(this, &TypeTestHandler::DestroyTestInTimeout), | ||||
|           NewCefRunnableMethod(this, &TypeTestHandler::DestroyTest), | ||||
|           2000); | ||||
|   } | ||||
|  | ||||
|   virtual bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser, | ||||
|                               CefRefPtr<CefFrame> frame, | ||||
|                               CefRefPtr<CefRequest> request, | ||||
|                               bool is_redirect) OVERRIDE { | ||||
|     browse_expectations_.GotRequest(request); | ||||
|  | ||||
|     return false; | ||||
|   } | ||||
|  | ||||
|   virtual bool OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser, | ||||
|                                     CefRefPtr<CefFrame> frame, | ||||
|                                     CefRefPtr<CefRequest> request) OVERRIDE { | ||||
|     before_expectations_.GotRequest(request); | ||||
|     load_expectations_.GotRequest(request); | ||||
|      | ||||
|     return false; | ||||
|   } | ||||
| @@ -465,35 +484,35 @@ class TypeTestHandler : public TestHandler { | ||||
|  | ||||
|  private: | ||||
|   void DestroyTestIfComplete() { | ||||
|     if (timed_out_) | ||||
|     if (destroyed_) | ||||
|       return; | ||||
|  | ||||
|     if (completed_browser_side_ && completed_render_side_) | ||||
|       DestroyTest(); | ||||
|   } | ||||
|  | ||||
|   void DestroyTestInTimeout() { | ||||
|     if (completed_browser_side_ && completed_render_side_) | ||||
|       return; | ||||
|     timed_out_ = true; | ||||
|     DestroyTest(); | ||||
|   } | ||||
|  | ||||
|   virtual void DestroyTest() OVERRIDE { | ||||
|     if (destroyed_) | ||||
|       return; | ||||
|     destroyed_ = true; | ||||
|  | ||||
|     // Verify test expectations. | ||||
|     EXPECT_TRUE(completed_browser_side_); | ||||
|     EXPECT_TRUE(completed_render_side_); | ||||
|     EXPECT_TRUE(before_expectations_.IsDone(true)); | ||||
|     EXPECT_TRUE(browse_expectations_.IsDone(true)); | ||||
|     EXPECT_TRUE(load_expectations_.IsDone(true)); | ||||
|     EXPECT_TRUE(get_expectations_.IsDone(true)); | ||||
|  | ||||
|     TestHandler::DestroyTest(); | ||||
|   } | ||||
|  | ||||
|   TypeExpectations before_expectations_; | ||||
|   TypeExpectations browse_expectations_; | ||||
|   TypeExpectations load_expectations_; | ||||
|   TypeExpectations get_expectations_; | ||||
|  | ||||
|   bool completed_browser_side_; | ||||
|   bool completed_render_side_; | ||||
|   bool timed_out_; | ||||
|   bool destroyed_; | ||||
| }; | ||||
|  | ||||
| }  // namespace | ||||
|   | ||||
		Reference in New Issue
	
	Block a user