mirror of
				https://bitbucket.org/chromiumembedded/cef
				synced 2025-06-05 21:39:12 +02:00 
			
		
		
		
	Add Google SafeSearch support (issue #1917)
This commit is contained in:
		
							
								
								
									
										4
									
								
								cef.gyp
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								cef.gyp
									
									
									
									
									
								
							| @@ -953,6 +953,7 @@ | |||||||
|         '<(DEPTH)/components/components.gyp:data_use_measurement_core', |         '<(DEPTH)/components/components.gyp:data_use_measurement_core', | ||||||
|         '<(DEPTH)/components/components.gyp:devtools_discovery', |         '<(DEPTH)/components/components.gyp:devtools_discovery', | ||||||
|         '<(DEPTH)/components/components.gyp:devtools_http_handler', |         '<(DEPTH)/components/components.gyp:devtools_http_handler', | ||||||
|  |         '<(DEPTH)/components/components.gyp:google_core_browser', | ||||||
|         '<(DEPTH)/components/components.gyp:keyed_service_content', |         '<(DEPTH)/components/components.gyp:keyed_service_content', | ||||||
|         '<(DEPTH)/components/components.gyp:keyed_service_core', |         '<(DEPTH)/components/components.gyp:keyed_service_core', | ||||||
|         '<(DEPTH)/components/components.gyp:navigation_interception', |         '<(DEPTH)/components/components.gyp:navigation_interception', | ||||||
| @@ -1489,6 +1490,9 @@ | |||||||
|         # Include sources for permissions support. |         # Include sources for permissions support. | ||||||
|         '<(DEPTH)/chrome/browser/permissions/permission_request_id.h', |         '<(DEPTH)/chrome/browser/permissions/permission_request_id.h', | ||||||
|         '<(DEPTH)/chrome/browser/permissions/permission_request_id.cc', |         '<(DEPTH)/chrome/browser/permissions/permission_request_id.cc', | ||||||
|  |         # Include sources for SafeSearch support. | ||||||
|  |         '<(DEPTH)/chrome/browser/net/safe_search_util.cc', | ||||||
|  |         '<(DEPTH)/chrome/browser/net/safe_search_util.h', | ||||||
|       ], |       ], | ||||||
|       'conditions': [ |       'conditions': [ | ||||||
|         ['OS=="win"', { |         ['OS=="win"', { | ||||||
|   | |||||||
| @@ -209,12 +209,14 @@ CefBrowserContextImpl::~CefBrowserContextImpl() { | |||||||
|  |  | ||||||
|   pref_proxy_config_tracker_->DetachFromPrefService(); |   pref_proxy_config_tracker_->DetachFromPrefService(); | ||||||
|  |  | ||||||
|   if (host_content_settings_map_.get()) |   if (url_request_getter_) | ||||||
|  |     url_request_getter_->ShutdownOnUIThread(); | ||||||
|  |   if (host_content_settings_map_) | ||||||
|     host_content_settings_map_->ShutdownOnUIThread(); |     host_content_settings_map_->ShutdownOnUIThread(); | ||||||
|  |  | ||||||
|   // Delete the download manager delegate here because otherwise we'll crash |   // Delete the download manager delegate here because otherwise we'll crash | ||||||
|   // when it's accessed from the content::BrowserContext destructor. |   // when it's accessed from the content::BrowserContext destructor. | ||||||
|   if (download_manager_delegate_.get()) |   if (download_manager_delegate_) | ||||||
|     download_manager_delegate_.reset(NULL); |     download_manager_delegate_.reset(NULL); | ||||||
|  |  | ||||||
|   g_manager.Get().RemoveImpl(this, cache_path_); |   g_manager.Get().RemoveImpl(this, cache_path_); | ||||||
|   | |||||||
| @@ -18,6 +18,9 @@ | |||||||
| #include "base/command_line.h" | #include "base/command_line.h" | ||||||
| #include "base/metrics/field_trial.h" | #include "base/metrics/field_trial.h" | ||||||
| #include "base/strings/string_util.h" | #include "base/strings/string_util.h" | ||||||
|  | #include "chrome/browser/net/safe_search_util.h" | ||||||
|  | #include "components/prefs/pref_member.h" | ||||||
|  | #include "components/prefs/pref_service.h" | ||||||
| #include "content/public/common/content_switches.h" | #include "content/public/common/content_switches.h" | ||||||
| #include "net/base/net_errors.h" | #include "net/base/net_errors.h" | ||||||
| #include "net/filter/filter.h" | #include "net/filter/filter.h" | ||||||
| @@ -38,10 +41,12 @@ class CefBeforeResourceLoadCallbackImpl : public CefRequestCallback { | |||||||
|       CefRefPtr<CefRequestImpl> cef_request, |       CefRefPtr<CefRequestImpl> cef_request, | ||||||
|       GURL* new_url, |       GURL* new_url, | ||||||
|       net::URLRequest* url_request, |       net::URLRequest* url_request, | ||||||
|  |       bool force_google_safesearch, | ||||||
|       const CallbackType& callback) |       const CallbackType& callback) | ||||||
|       : cef_request_(cef_request), |       : cef_request_(cef_request), | ||||||
|         new_url_(new_url), |         new_url_(new_url), | ||||||
|         url_request_(url_request), |         url_request_(url_request), | ||||||
|  |         force_google_safesearch_(force_google_safesearch), | ||||||
|         callback_(callback) { |         callback_(callback) { | ||||||
|     DCHECK(new_url); |     DCHECK(new_url); | ||||||
|     DCHECK(url_request_); |     DCHECK(url_request_); | ||||||
| @@ -54,11 +59,13 @@ class CefBeforeResourceLoadCallbackImpl : public CefRequestCallback { | |||||||
|     if (!callback_.is_null()) { |     if (!callback_.is_null()) { | ||||||
|       // The callback is still pending. Cancel it now. |       // The callback is still pending. Cancel it now. | ||||||
|       if (CEF_CURRENTLY_ON_IOT()) { |       if (CEF_CURRENTLY_ON_IOT()) { | ||||||
|         RunNow(cef_request_, new_url_, url_request_, callback_, false); |         RunNow(cef_request_, new_url_, url_request_, callback_, | ||||||
|  |                force_google_safesearch_, false); | ||||||
|       } else { |       } else { | ||||||
|         CEF_POST_TASK(CEF_IOT, |         CEF_POST_TASK(CEF_IOT, | ||||||
|             base::Bind(&CefBeforeResourceLoadCallbackImpl::RunNow, |             base::Bind(&CefBeforeResourceLoadCallbackImpl::RunNow, | ||||||
|                        cef_request_, new_url_, url_request_, callback_, false)); |                        cef_request_, new_url_, url_request_, callback_, | ||||||
|  |                        force_google_safesearch_, false)); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| @@ -77,7 +84,8 @@ class CefBeforeResourceLoadCallbackImpl : public CefRequestCallback { | |||||||
|   void ContinueNow(bool allow) { |   void ContinueNow(bool allow) { | ||||||
|     CEF_REQUIRE_IOT(); |     CEF_REQUIRE_IOT(); | ||||||
|     if (!callback_.is_null()) { |     if (!callback_.is_null()) { | ||||||
|       RunNow(cef_request_, new_url_, url_request_, callback_, allow); |       RunNow(cef_request_, new_url_, url_request_, callback_, | ||||||
|  |              force_google_safesearch_, allow); | ||||||
|       Disconnect(); |       Disconnect(); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| @@ -115,6 +123,7 @@ class CefBeforeResourceLoadCallbackImpl : public CefRequestCallback { | |||||||
|                      GURL* new_url, |                      GURL* new_url, | ||||||
|                      net::URLRequest* request, |                      net::URLRequest* request, | ||||||
|                      const CallbackType& callback, |                      const CallbackType& callback, | ||||||
|  |                      bool force_google_safesearch, | ||||||
|                      bool allow) { |                      bool allow) { | ||||||
|     CEF_REQUIRE_IOT(); |     CEF_REQUIRE_IOT(); | ||||||
|  |  | ||||||
| @@ -139,8 +148,12 @@ class CefBeforeResourceLoadCallbackImpl : public CefRequestCallback { | |||||||
|     request->RemoveUserData(UserDataKey()); |     request->RemoveUserData(UserDataKey()); | ||||||
|  |  | ||||||
|     // Only execute the callback if the request has not been canceled. |     // Only execute the callback if the request has not been canceled. | ||||||
|     if (request->status().status() != net::URLRequestStatus::CANCELED) |     if (request->status().status() != net::URLRequestStatus::CANCELED) { | ||||||
|  |       if (force_google_safesearch && allow && new_url->is_empty()) | ||||||
|  |         safe_search_util::ForceGoogleSafeSearch(request, new_url); | ||||||
|  |  | ||||||
|       callback.Run(allow ? net::OK : net::ERR_ABORTED); |       callback.Run(allow ? net::OK : net::ERR_ABORTED); | ||||||
|  |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   static inline void* UserDataKey() { |   static inline void* UserDataKey() { | ||||||
| @@ -151,6 +164,7 @@ class CefBeforeResourceLoadCallbackImpl : public CefRequestCallback { | |||||||
|   const GURL old_url_; |   const GURL old_url_; | ||||||
|   GURL* new_url_; |   GURL* new_url_; | ||||||
|   net::URLRequest* url_request_; |   net::URLRequest* url_request_; | ||||||
|  |   bool force_google_safesearch_; | ||||||
|   CallbackType callback_; |   CallbackType callback_; | ||||||
|  |  | ||||||
|   // The user data key. |   // The user data key. | ||||||
| @@ -224,7 +238,8 @@ class CefAuthCallbackImpl : public CefAuthCallback { | |||||||
|  |  | ||||||
| }  // namespace | }  // namespace | ||||||
|  |  | ||||||
| CefNetworkDelegate::CefNetworkDelegate() { | CefNetworkDelegate::CefNetworkDelegate() | ||||||
|  |   : force_google_safesearch_(nullptr) { | ||||||
| } | } | ||||||
|  |  | ||||||
| CefNetworkDelegate::~CefNetworkDelegate() { | CefNetworkDelegate::~CefNetworkDelegate() { | ||||||
| @@ -255,6 +270,9 @@ int CefNetworkDelegate::OnBeforeURLRequest( | |||||||
|     net::URLRequest* request, |     net::URLRequest* request, | ||||||
|     const net::CompletionCallback& callback, |     const net::CompletionCallback& callback, | ||||||
|     GURL* new_url) { |     GURL* new_url) { | ||||||
|  |   const bool force_google_safesearch = | ||||||
|  |       (force_google_safesearch_ && force_google_safesearch_->GetValue()); | ||||||
|  |  | ||||||
|   CefRefPtr<CefBrowserHostImpl> browser = |   CefRefPtr<CefBrowserHostImpl> browser = | ||||||
|       CefBrowserHostImpl::GetBrowserForRequest(request); |       CefBrowserHostImpl::GetBrowserForRequest(request); | ||||||
|   if (browser.get()) { |   if (browser.get()) { | ||||||
| @@ -279,6 +297,7 @@ int CefNetworkDelegate::OnBeforeURLRequest( | |||||||
|  |  | ||||||
|         CefRefPtr<CefBeforeResourceLoadCallbackImpl> callbackImpl( |         CefRefPtr<CefBeforeResourceLoadCallbackImpl> callbackImpl( | ||||||
|             new CefBeforeResourceLoadCallbackImpl(requestPtr, new_url, request, |             new CefBeforeResourceLoadCallbackImpl(requestPtr, new_url, request, | ||||||
|  |                                                   force_google_safesearch, | ||||||
|                                                   callback)); |                                                   callback)); | ||||||
|  |  | ||||||
|         // Give the client an opportunity to evaluate the request. |         // Give the client an opportunity to evaluate the request. | ||||||
| @@ -298,6 +317,9 @@ int CefNetworkDelegate::OnBeforeURLRequest( | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   if (force_google_safesearch && new_url->is_empty()) | ||||||
|  |     safe_search_util::ForceGoogleSafeSearch(request, new_url); | ||||||
|  |  | ||||||
|   // Continue the request immediately. |   // Continue the request immediately. | ||||||
|   return net::OK; |   return net::OK; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -9,6 +9,9 @@ | |||||||
| #include "base/macros.h" | #include "base/macros.h" | ||||||
| #include "net/base/network_delegate_impl.h" | #include "net/base/network_delegate_impl.h" | ||||||
|  |  | ||||||
|  | template<class T> class PrefMember; | ||||||
|  | typedef PrefMember<bool> BooleanPrefMember; | ||||||
|  |  | ||||||
| // Used for intercepting resource requests, redirects and responses. The single | // Used for intercepting resource requests, redirects and responses. The single | ||||||
| // instance of this class is managed by CefURLRequestContextGetter. | // instance of this class is managed by CefURLRequestContextGetter. | ||||||
| class CefNetworkDelegate : public net::NetworkDelegateImpl { | class CefNetworkDelegate : public net::NetworkDelegateImpl { | ||||||
| @@ -21,6 +24,11 @@ class CefNetworkDelegate : public net::NetworkDelegateImpl { | |||||||
|   static bool AreExperimentalCookieFeaturesEnabled(); |   static bool AreExperimentalCookieFeaturesEnabled(); | ||||||
|   static bool AreStrictSecureCookiesEnabled(); |   static bool AreStrictSecureCookiesEnabled(); | ||||||
|  |  | ||||||
|  |   void set_force_google_safesearch( | ||||||
|  |       BooleanPrefMember* force_google_safesearch) { | ||||||
|  |     force_google_safesearch_ = force_google_safesearch; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  private: |  private: | ||||||
|   // net::NetworkDelegate methods. |   // net::NetworkDelegate methods. | ||||||
|   int OnBeforeURLRequest(net::URLRequest* request, |   int OnBeforeURLRequest(net::URLRequest* request, | ||||||
| @@ -39,6 +47,9 @@ class CefNetworkDelegate : public net::NetworkDelegateImpl { | |||||||
|   net::Filter* SetupFilter(net::URLRequest* request, |   net::Filter* SetupFilter(net::URLRequest* request, | ||||||
|                            net::Filter* filter_list) override; |                            net::Filter* filter_list) override; | ||||||
|  |  | ||||||
|  |   // Weak, owned by our owner (CefURLRequestContextGetterImpl). | ||||||
|  |   BooleanPrefMember* force_google_safesearch_; | ||||||
|  |  | ||||||
|   DISALLOW_COPY_AND_ASSIGN(CefNetworkDelegate); |   DISALLOW_COPY_AND_ASSIGN(CefNetworkDelegate); | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -120,6 +120,10 @@ CefURLRequestContextGetterImpl::CefURLRequestContextGetterImpl( | |||||||
|  |  | ||||||
|   std::swap(protocol_handlers_, *protocol_handlers); |   std::swap(protocol_handlers_, *protocol_handlers); | ||||||
|  |  | ||||||
|  |   force_google_safesearch_.Init(prefs::kForceGoogleSafeSearch, pref_service); | ||||||
|  |   force_google_safesearch_.MoveToThread( | ||||||
|  |       BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | ||||||
|  |  | ||||||
| #if defined(OS_POSIX) && !defined(OS_ANDROID) | #if defined(OS_POSIX) && !defined(OS_ANDROID) | ||||||
|   gsapi_library_name_ = pref_service->GetString(prefs::kGSSAPILibraryName); |   gsapi_library_name_ = pref_service->GetString(prefs::kGSSAPILibraryName); | ||||||
| #endif | #endif | ||||||
| @@ -134,6 +138,11 @@ CefURLRequestContextGetterImpl::~CefURLRequestContextGetterImpl() { | |||||||
|   storage_->set_proxy_service(NULL); |   storage_->set_proxy_service(NULL); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void CefURLRequestContextGetterImpl::ShutdownOnUIThread() { | ||||||
|  |   CEF_REQUIRE_UIT(); | ||||||
|  |   force_google_safesearch_.Destroy(); | ||||||
|  | } | ||||||
|  |  | ||||||
| net::URLRequestContext* CefURLRequestContextGetterImpl::GetURLRequestContext() { | net::URLRequestContext* CefURLRequestContextGetterImpl::GetURLRequestContext() { | ||||||
|   CEF_REQUIRE_IOT(); |   CEF_REQUIRE_IOT(); | ||||||
|  |  | ||||||
| @@ -152,7 +161,10 @@ net::URLRequestContext* CefURLRequestContextGetterImpl::GetURLRequestContext() { | |||||||
|     SetCookieStoragePath(cache_path, |     SetCookieStoragePath(cache_path, | ||||||
|                          settings_.persist_session_cookies ? true : false); |                          settings_.persist_session_cookies ? true : false); | ||||||
|  |  | ||||||
|     storage_->set_network_delegate(base::WrapUnique(new CefNetworkDelegate)); |     std::unique_ptr<CefNetworkDelegate> network_delegate( | ||||||
|  |         new CefNetworkDelegate()); | ||||||
|  |     network_delegate->set_force_google_safesearch(&force_google_safesearch_); | ||||||
|  |     storage_->set_network_delegate(std::move(network_delegate)); | ||||||
|  |  | ||||||
|     storage_->set_channel_id_service(make_scoped_ptr( |     storage_->set_channel_id_service(make_scoped_ptr( | ||||||
|         new net::ChannelIDService( |         new net::ChannelIDService( | ||||||
|   | |||||||
| @@ -17,6 +17,7 @@ | |||||||
| #include "base/compiler_specific.h" | #include "base/compiler_specific.h" | ||||||
| #include "base/files/file_path.h" | #include "base/files/file_path.h" | ||||||
| #include "base/memory/ref_counted.h" | #include "base/memory/ref_counted.h" | ||||||
|  | #include "components/prefs/pref_member.h" | ||||||
| #include "content/public/browser/browser_context.h" | #include "content/public/browser/browser_context.h" | ||||||
| #include "net/url_request/url_request_job_factory.h" | #include "net/url_request/url_request_job_factory.h" | ||||||
|  |  | ||||||
| @@ -52,6 +53,9 @@ class CefURLRequestContextGetterImpl : public CefURLRequestContextGetter { | |||||||
|       content::URLRequestInterceptorScopedVector request_interceptors); |       content::URLRequestInterceptorScopedVector request_interceptors); | ||||||
|   ~CefURLRequestContextGetterImpl() override; |   ~CefURLRequestContextGetterImpl() override; | ||||||
|  |  | ||||||
|  |   // Called when the BrowserContextImpl is destroyed. | ||||||
|  |   void ShutdownOnUIThread(); | ||||||
|  |  | ||||||
|   // net::URLRequestContextGetter implementation. |   // net::URLRequestContextGetter implementation. | ||||||
|   net::URLRequestContext* GetURLRequestContext() override; |   net::URLRequestContext* GetURLRequestContext() override; | ||||||
|   scoped_refptr<base::SingleThreadTaskRunner> |   scoped_refptr<base::SingleThreadTaskRunner> | ||||||
| @@ -102,6 +106,9 @@ class CefURLRequestContextGetterImpl : public CefURLRequestContextGetter { | |||||||
|  |  | ||||||
|   std::vector<CefRefPtr<CefRequestContextHandler> > handler_list_; |   std::vector<CefRefPtr<CefRequestContextHandler> > handler_list_; | ||||||
|  |  | ||||||
|  |   // Member variables which are pointed to by the various context objects. | ||||||
|  |   mutable BooleanPrefMember force_google_safesearch_; | ||||||
|  |  | ||||||
|   DISALLOW_COPY_AND_ASSIGN(CefURLRequestContextGetterImpl); |   DISALLOW_COPY_AND_ASSIGN(CefURLRequestContextGetterImpl); | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -161,6 +161,8 @@ std::unique_ptr<PrefService> CreatePrefService(const base::FilePath& pref_path) | |||||||
|   registry->RegisterBooleanPref(prefs::kPluginsAlwaysAuthorize, false); |   registry->RegisterBooleanPref(prefs::kPluginsAlwaysAuthorize, false); | ||||||
|  |  | ||||||
|   // Network preferences. |   // Network preferences. | ||||||
|  |   // Based on ProfileImpl::RegisterProfilePrefs. | ||||||
|  |   registry->RegisterBooleanPref(prefs::kForceGoogleSafeSearch, false); | ||||||
|   // Based on IOThread::RegisterPrefs. |   // Based on IOThread::RegisterPrefs. | ||||||
| #if defined(OS_POSIX) && !defined(OS_ANDROID) | #if defined(OS_POSIX) && !defined(OS_ANDROID) | ||||||
|   registry->RegisterStringPref(prefs::kGSSAPILibraryName, std::string()); |   registry->RegisterStringPref(prefs::kGSSAPILibraryName, std::string()); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user