1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_PUBLIC_BROWSER_CONTENT_BROWSER_CLIENT_H_
6 #define CONTENT_PUBLIC_BROWSER_CONTENT_BROWSER_CLIENT_H_
13 #include "base/callback_forward.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/values.h"
18 #include "content/public/browser/certificate_request_result_type.h"
19 #include "content/public/common/content_client.h"
20 #include "content/public/common/media_stream_request.h"
21 #include "content/public/common/resource_type.h"
22 #include "content/public/common/socket_permission_request.h"
23 #include "content/public/common/window_container_type.h"
24 #include "net/base/mime_util.h"
25 #include "net/cookies/canonical_cookie.h"
26 #include "net/url_request/url_request_interceptor.h"
27 #include "net/url_request/url_request_job_factory.h"
28 #include "storage/browser/fileapi/file_system_context.h"
29 #include "third_party/WebKit/public/platform/WebPageVisibilityState.h"
30 #include "ui/base/window_open_disposition.h"
32 #if defined(OS_POSIX) && !defined(OS_MACOSX)
33 #include "base/posix/global_descriptors.h"
37 #include "content/public/browser/file_descriptor_info.h"
44 class DictionaryValue
;
49 struct WebWindowFeatures
;
59 class SSLCertRequestInfo
;
62 class URLRequestContext
;
63 class URLRequestContextGetter
;
64 class X509Certificate
;
72 class SelectFilePolicy
;
76 class ExternalMountPoints
;
77 class FileSystemBackend
;
82 enum class PermissionType
;
83 class AccessTokenStore
;
84 class BrowserChildProcessHost
;
86 class BrowserMainParts
;
87 class BrowserPluginGuestDelegate
;
88 class BrowserPpapiHost
;
89 class BrowserURLHandler
;
90 class ClientCertificateDelegate
;
91 class DevToolsManagerDelegate
;
92 class ExternalVideoSurfaceContainer
;
93 class LocationProvider
;
95 class NavigatorConnectContext
;
96 class NavigatorConnectServiceFactory
;
97 class PlatformNotificationService
;
98 class PresentationServiceDelegate
;
99 class QuotaPermissionContext
;
100 class RenderFrameHost
;
101 class RenderProcessHost
;
102 class RenderViewHost
;
103 class ResourceContext
;
104 class ServiceRegistry
;
106 class SpeechRecognitionManagerDelegate
;
107 class TracingDelegate
;
109 class WebContentsViewDelegate
;
110 struct MainFunctionParams
;
111 struct OpenURLParams
;
113 struct WebPreferences
;
115 // A mapping from the scheme name to the protocol handler that services its
118 std::string
, linked_ptr
<net::URLRequestJobFactory::ProtocolHandler
> >
121 // A scoped vector of protocol interceptors.
122 typedef ScopedVector
<net::URLRequestInterceptor
>
123 URLRequestInterceptorScopedVector
;
125 // Embedder API (or SPI) for participating in browser logic, to be implemented
126 // by the client of the content browser. See ChromeContentBrowserClient for the
127 // principal implementation. The methods are assumed to be called on the UI
128 // thread unless otherwise specified. Use this "escape hatch" sparingly, to
129 // avoid the embedder interface ballooning and becoming very specific to Chrome.
130 // (Often, the call out to the client can happen in a different part of the code
131 // that either already has a hook out to the embedder, or calls out to one of
132 // the observer interfaces.)
133 class CONTENT_EXPORT ContentBrowserClient
{
135 virtual ~ContentBrowserClient() {}
137 // Allows the embedder to set any number of custom BrowserMainParts
138 // implementations for the browser startup code. See comments in
139 // browser_main_parts.h.
140 virtual BrowserMainParts
* CreateBrowserMainParts(
141 const MainFunctionParams
& parameters
);
143 // Allows the embedder to change the default behavior of
144 // BrowserThread::PostAfterStartupTask to better match whatever
145 // definition of "startup" the embedder has in mind. This may be
146 // called on any thread.
147 // Note: see related BrowserThread::PostAfterStartupTask.
148 virtual void PostAfterStartupTask(
149 const tracked_objects::Location
& from_here
,
150 const scoped_refptr
<base::TaskRunner
>& task_runner
,
151 const base::Closure
& task
);
153 // If content creates the WebContentsView implementation, it will ask the
154 // embedder to return an (optional) delegate to customize it. The view will
156 virtual WebContentsViewDelegate
* GetWebContentsViewDelegate(
157 WebContents
* web_contents
);
159 // Notifies that a render process will be created. This is called before
160 // the content layer adds its own BrowserMessageFilters, so that the
161 // embedder's IPC filters have priority.
162 virtual void RenderProcessWillLaunch(RenderProcessHost
* host
) {}
164 // Notifies that a BrowserChildProcessHost has been created.
165 virtual void BrowserChildProcessHostCreated(BrowserChildProcessHost
* host
) {}
167 // Get the effective URL for the given actual URL, to allow an embedder to
168 // group different url schemes in the same SiteInstance.
169 virtual GURL
GetEffectiveURL(BrowserContext
* browser_context
,
172 // Returns whether all instances of the specified effective URL should be
173 // rendered by the same process, rather than using process-per-site-instance.
174 virtual bool ShouldUseProcessPerSite(BrowserContext
* browser_context
,
175 const GURL
& effective_url
);
177 // Returns a list additional WebUI schemes, if any. These additional schemes
178 // act as aliases to the chrome: scheme. The additional schemes may or may
179 // not serve specific WebUI pages depending on the particular URLDataSource
180 // and its override of URLDataSource::ShouldServiceRequest. For all schemes
181 // returned here, view-source is allowed.
182 virtual void GetAdditionalWebUISchemes(
183 std::vector
<std::string
>* additional_schemes
) {}
185 // Returns a list of webUI hosts to ignore the storage partition check in
186 // URLRequestChromeJob::CheckStoragePartitionMatches.
187 virtual void GetAdditionalWebUIHostsToIgnoreParititionCheck(
188 std::vector
<std::string
>* hosts
) {}
190 // Creates the main net::URLRequestContextGetter. Should only be called once
191 // per ContentBrowserClient object.
192 // TODO(ajwong): Remove once http://crbug.com/159193 is resolved.
193 virtual net::URLRequestContextGetter
* CreateRequestContext(
194 BrowserContext
* browser_context
,
195 ProtocolHandlerMap
* protocol_handlers
,
196 URLRequestInterceptorScopedVector request_interceptors
);
198 // Creates the net::URLRequestContextGetter for a StoragePartition. Should
199 // only be called once per partition_path per ContentBrowserClient object.
200 // TODO(ajwong): Remove once http://crbug.com/159193 is resolved.
201 virtual net::URLRequestContextGetter
* CreateRequestContextForStoragePartition(
202 BrowserContext
* browser_context
,
203 const base::FilePath
& partition_path
,
205 ProtocolHandlerMap
* protocol_handlers
,
206 URLRequestInterceptorScopedVector request_interceptors
);
208 // Returns whether a specified URL is handled by the embedder's internal
209 // protocol handlers.
210 virtual bool IsHandledURL(const GURL
& url
);
212 // Returns whether the given process is allowed to commit |url|. This is a
213 // more conservative check than IsSuitableHost, since it is used after a
214 // navigation has committed to ensure that the process did not exceed its
216 virtual bool CanCommitURL(RenderProcessHost
* process_host
, const GURL
& url
);
218 // Returns whether a URL should be allowed to open from a specific context.
219 // This also applies in cases where the new URL will open in another process.
220 virtual bool ShouldAllowOpenURL(SiteInstance
* site_instance
, const GURL
& url
);
222 // Returns whether a new view for a given |site_url| can be launched in a
223 // given |process_host|.
224 virtual bool IsSuitableHost(RenderProcessHost
* process_host
,
225 const GURL
& site_url
);
227 // Returns whether a new view for a new site instance can be added to a
228 // given |process_host|.
229 virtual bool MayReuseHost(RenderProcessHost
* process_host
);
231 // Returns whether a new process should be created or an existing one should
232 // be reused based on the URL we want to load. This should return false,
233 // unless there is a good reason otherwise.
234 virtual bool ShouldTryToUseExistingProcessHost(
235 BrowserContext
* browser_context
, const GURL
& url
);
237 // Called when a site instance is first associated with a process.
238 virtual void SiteInstanceGotProcess(SiteInstance
* site_instance
) {}
240 // Called from a site instance's destructor.
241 virtual void SiteInstanceDeleting(SiteInstance
* site_instance
) {}
243 // Returns true if for the navigation from |current_url| to |new_url|
244 // in |site_instance|, a new SiteInstance and BrowsingInstance should be
245 // created (even if we are in a process model that doesn't usually swap.)
246 // This forces a process swap and severs script connections with existing
248 virtual bool ShouldSwapBrowsingInstancesForNavigation(
249 SiteInstance
* site_instance
,
250 const GURL
& current_url
,
251 const GURL
& new_url
);
253 // Returns true if the given navigation redirect should cause a renderer
255 // This is called on the IO thread.
256 virtual bool ShouldSwapProcessesForRedirect(ResourceContext
* resource_context
,
257 const GURL
& current_url
,
258 const GURL
& new_url
);
260 // Returns true if the passed in URL should be assigned as the site of the
261 // current SiteInstance, if it does not yet have a site.
262 virtual bool ShouldAssignSiteForURL(const GURL
& url
);
264 // See CharacterEncoding's comment.
265 virtual std::string
GetCanonicalEncodingNameByAliasName(
266 const std::string
& alias_name
);
268 // Allows the embedder to pass extra command line flags.
269 // switches::kProcessType will already be set at this point.
270 virtual void AppendExtraCommandLineSwitches(base::CommandLine
* command_line
,
271 int child_process_id
) {}
273 // Returns the locale used by the application.
274 // This is called on the UI and IO threads.
275 virtual std::string
GetApplicationLocale();
277 // Returns the languages used in the Accept-Languages HTTP header.
278 // (Not called GetAcceptLanguages so it doesn't clash with win32).
279 virtual std::string
GetAcceptLangs(BrowserContext
* context
);
281 // Returns the default favicon. The callee doesn't own the given bitmap.
282 virtual const gfx::ImageSkia
* GetDefaultFavicon();
284 // Allow the embedder to control if an AppCache can be used for the given url.
285 // This is called on the IO thread.
286 virtual bool AllowAppCache(const GURL
& manifest_url
,
287 const GURL
& first_party
,
288 ResourceContext
* context
);
290 // Allow the embedder to control if a Service Worker can be associated
291 // with the given scope.
292 // This is called on the IO thread.
293 virtual bool AllowServiceWorker(const GURL
& scope
,
294 const GURL
& first_party
,
295 content::ResourceContext
* context
);
297 // Allow the embedder to control if the given cookie can be read.
298 // This is called on the IO thread.
299 virtual bool AllowGetCookie(const GURL
& url
,
300 const GURL
& first_party
,
301 const net::CookieList
& cookie_list
,
302 ResourceContext
* context
,
303 int render_process_id
,
304 int render_frame_id
);
306 // Allow the embedder to control if the given cookie can be set.
307 // This is called on the IO thread.
308 virtual bool AllowSetCookie(const GURL
& url
,
309 const GURL
& first_party
,
310 const std::string
& cookie_line
,
311 ResourceContext
* context
,
312 int render_process_id
,
314 net::CookieOptions
* options
);
316 // This is called on the IO thread.
317 virtual bool AllowSaveLocalState(ResourceContext
* context
);
319 // Allow the embedder to control if access to web database by a shared worker
320 // is allowed. |render_frame| is a vector of pairs of
321 // RenderProcessID/RenderFrameID of RenderFrame that are using this worker.
322 // This is called on the IO thread.
323 virtual bool AllowWorkerDatabase(
325 const base::string16
& name
,
326 const base::string16
& display_name
,
327 unsigned long estimated_size
,
328 ResourceContext
* context
,
329 const std::vector
<std::pair
<int, int> >& render_frames
);
331 // Allow the embedder to control if access to file system by a shared worker
333 // This is called on the IO thread.
334 virtual void AllowWorkerFileSystem(
336 ResourceContext
* context
,
337 const std::vector
<std::pair
<int, int> >& render_frames
,
338 base::Callback
<void(bool)> callback
);
340 // Allow the embedder to control if access to IndexedDB by a shared worker
342 // This is called on the IO thread.
343 virtual bool AllowWorkerIndexedDB(
345 const base::string16
& name
,
346 ResourceContext
* context
,
347 const std::vector
<std::pair
<int, int> >& render_frames
);
349 // Allow the embedder to override the request context based on the URL for
350 // certain operations, like cookie access. Returns nullptr to indicate the
351 // regular request context should be used.
352 // This is called on the IO thread.
353 virtual net::URLRequestContext
* OverrideRequestContextForURL(
354 const GURL
& url
, ResourceContext
* context
);
356 // Allow the embedder to specify a string version of the storage partition
357 // config with a site.
358 virtual std::string
GetStoragePartitionIdForSite(
359 BrowserContext
* browser_context
,
362 // Allows the embedder to provide a validation check for |partition_id|s.
363 // This domain of valid entries should match the range of outputs for
364 // GetStoragePartitionIdForChildProcess().
365 virtual bool IsValidStoragePartitionId(BrowserContext
* browser_context
,
366 const std::string
& partition_id
);
368 // Allows the embedder to provide a storage parititon configuration for a
369 // site. A storage partition configuration includes a domain of the embedder's
370 // choice, an optional name within that domain, and whether the partition is
373 // If |can_be_default| is false, the caller is telling the embedder that the
374 // |site| is known to not be in the default partition. This is useful in
375 // some shutdown situations where the bookkeeping logic that maps sites to
376 // their partition configuration are no longer valid.
378 // The |partition_domain| is [a-z]* UTF-8 string, specifying the domain in
379 // which partitions live (similar to namespace). Within a domain, partitions
380 // can be uniquely identified by the combination of |partition_name| and
381 // |in_memory| values. When a partition is not to be persisted, the
382 // |in_memory| value must be set to true.
383 virtual void GetStoragePartitionConfigForSite(
384 BrowserContext
* browser_context
,
387 std::string
* partition_domain
,
388 std::string
* partition_name
,
391 // Create and return a new quota permission context.
392 virtual QuotaPermissionContext
* CreateQuotaPermissionContext();
394 // Informs the embedder that a certificate error has occured. If
395 // |overridable| is true and if |strict_enforcement| is false, the user
396 // can ignore the error and continue. The embedder can call the callback
397 // asynchronously. If |result| is not set to
398 // CERTIFICATE_REQUEST_RESULT_TYPE_CONTINUE, the request will be cancelled
399 // or denied immediately, and the callback won't be run.
400 virtual void AllowCertificateError(int render_process_id
,
403 const net::SSLInfo
& ssl_info
,
404 const GURL
& request_url
,
405 ResourceType resource_type
,
407 bool strict_enforcement
,
408 bool expired_previous_decision
,
409 const base::Callback
<void(bool)>& callback
,
410 CertificateRequestResultType
* result
) {}
412 // Selects a SSL client certificate and returns it to the |delegate|. Note:
413 // |delegate| may be called synchronously or asynchronously.
415 // TODO(davidben): Move this hook to WebContentsDelegate.
416 virtual void SelectClientCertificate(
417 WebContents
* web_contents
,
418 net::SSLCertRequestInfo
* cert_request_info
,
419 scoped_ptr
<ClientCertificateDelegate
> delegate
);
421 // Adds a new installable certificate or private key.
422 // Typically used to install an X.509 user certificate.
423 // Note that it's up to the embedder to verify that the data is
424 // well-formed. |cert_data| will be nullptr if |cert_size| is 0.
425 virtual void AddCertificate(net::CertificateMimeType cert_type
,
426 const void* cert_data
,
428 int render_process_id
,
429 int render_frame_id
) {}
431 // Returns a class to get notifications about media event. The embedder can
432 // return nullptr if they're not interested.
433 virtual MediaObserver
* GetMediaObserver();
435 // Returns the platform notification service, capable of displaying Web
436 // Notifications to the user. The embedder can return a nullptr if they don't
437 // support this functionality. May be called from any thread.
438 virtual PlatformNotificationService
* GetPlatformNotificationService();
440 // Returns true if the given page is allowed to open a window of the given
441 // type. If true is returned, |no_javascript_access| will indicate whether
442 // the window that is created should be scriptable/in the same process.
443 // This is called on the IO thread.
444 virtual bool CanCreateWindow(const GURL
& opener_url
,
445 const GURL
& opener_top_level_frame_url
,
446 const GURL
& source_origin
,
447 WindowContainerType container_type
,
448 const GURL
& target_url
,
449 const Referrer
& referrer
,
450 WindowOpenDisposition disposition
,
451 const blink::WebWindowFeatures
& features
,
453 bool opener_suppressed
,
454 ResourceContext
* context
,
455 int render_process_id
,
457 bool* no_javascript_access
);
459 // Notifies the embedder that the ResourceDispatcherHost has been created.
460 // This is when it can optionally add a delegate.
461 virtual void ResourceDispatcherHostCreated() {}
463 // Allows the embedder to return a delegate for the SpeechRecognitionManager.
464 // The delegate will be owned by the manager. It's valid to return nullptr.
465 virtual SpeechRecognitionManagerDelegate
*
466 CreateSpeechRecognitionManagerDelegate();
468 // Getters for common objects.
469 virtual net::NetLog
* GetNetLog();
471 // Creates a new AccessTokenStore for gelocation.
472 virtual AccessTokenStore
* CreateAccessTokenStore();
474 // Returns true if fast shutdown is possible.
475 virtual bool IsFastShutdownPossible();
477 // Called by WebContents to override the WebKit preferences that are used by
478 // the renderer. The content layer will add its own settings, and then it's up
479 // to the embedder to update it if it wants.
480 virtual void OverrideWebkitPrefs(RenderViewHost
* render_view_host
,
481 WebPreferences
* prefs
) {}
483 // Notifies that BrowserURLHandler has been created, so that the embedder can
484 // optionally add their own handlers.
485 virtual void BrowserURLHandlerCreated(BrowserURLHandler
* handler
) {}
487 // Clears browser cache.
488 virtual void ClearCache(RenderFrameHost
* rfh
) {}
490 // Clears browser cookies.
491 virtual void ClearCookies(RenderFrameHost
* rfh
) {}
493 // Returns the default download directory.
494 // This can be called on any thread.
495 virtual base::FilePath
GetDefaultDownloadDirectory();
497 // Returns the default filename used in downloads when we have no idea what
498 // else we should do with the file.
499 virtual std::string
GetDefaultDownloadName();
501 // Notification that a pepper plugin has just been spawned. This allows the
502 // embedder to add filters onto the host to implement interfaces.
503 // This is called on the IO thread.
504 virtual void DidCreatePpapiPlugin(BrowserPpapiHost
* browser_host
) {}
506 // Gets the host for an external out-of-process plugin.
507 virtual BrowserPpapiHost
* GetExternalBrowserPpapiHost(
508 int plugin_child_id
);
510 // Returns true if the socket operation specified by |params| is allowed from
511 // the given |browser_context| and |url|. If |params| is nullptr, this method
512 // checks the basic "socket" permission, which is for those operations that
513 // don't require a specific socket permission rule.
514 // |private_api| indicates whether this permission check is for the private
515 // Pepper socket API or the public one.
516 virtual bool AllowPepperSocketAPI(BrowserContext
* browser_context
,
519 const SocketPermissionRequest
* params
);
521 // Returns an implementation of a file selecition policy. Can return nullptr.
522 virtual ui::SelectFilePolicy
* CreateSelectFilePolicy(
523 WebContents
* web_contents
);
525 // Returns additional allowed scheme set which can access files in
527 virtual void GetAdditionalAllowedSchemesForFileSystem(
528 std::vector
<std::string
>* additional_schemes
) {}
530 // Returns auto mount handlers for URL requests for FileSystem APIs.
531 virtual void GetURLRequestAutoMountHandlers(
532 std::vector
<storage::URLRequestAutoMountHandler
>* handlers
) {}
534 // Returns additional file system backends for FileSystem API.
535 // |browser_context| is needed in the additional FileSystemBackends.
536 // It has mount points to create objects returned by additional
537 // FileSystemBackends, and SpecialStoragePolicy for permission granting.
538 virtual void GetAdditionalFileSystemBackends(
539 BrowserContext
* browser_context
,
540 const base::FilePath
& storage_partition_path
,
541 ScopedVector
<storage::FileSystemBackend
>* additional_backends
) {}
543 // Allows an embedder to return its own LocationProvider implementation.
544 // Return nullptr to use the default one for the platform to be created.
545 // FYI: Used by an external project; please don't remove.
546 // Contact Viatcheslav Ostapenko at sl.ostapenko@samsung.com for more
548 virtual LocationProvider
* OverrideSystemLocationProvider();
550 // Creates a new DevToolsManagerDelegate. The caller owns the returned value.
551 // It's valid to return nullptr.
552 virtual DevToolsManagerDelegate
* GetDevToolsManagerDelegate();
554 // Creates a new TracingDelegate. The caller owns the returned value.
555 // It's valid to return nullptr.
556 virtual TracingDelegate
* GetTracingDelegate();
558 // Returns true if plugin referred to by the url can use
559 // pp::FileIO::RequestOSFileHandle.
560 virtual bool IsPluginAllowedToCallRequestOSFileHandle(
561 BrowserContext
* browser_context
,
564 // Returns true if dev channel APIs are available for plugins.
565 virtual bool IsPluginAllowedToUseDevChannelAPIs(
566 BrowserContext
* browser_context
,
569 // Checks if |security_origin| has permission to access the microphone or
570 // camera. Note that this does not query the user. |type| must be
571 // MEDIA_DEVICE_AUDIO_CAPTURE or MEDIA_DEVICE_VIDEO_CAPTURE.
572 virtual bool CheckMediaAccessPermission(BrowserContext
* browser_context
,
573 const GURL
& security_origin
,
574 MediaStreamType type
);
576 // Allows to override browser Mojo services exposed through the
577 // RenderProcessHost.
578 virtual void OverrideRenderProcessMojoServices(ServiceRegistry
* registry
) {}
580 // Registers additional navigator.connect service factories available in a
581 // particular NavigatorConnectContext.
582 virtual void GetAdditionalNavigatorConnectServices(
583 const scoped_refptr
<NavigatorConnectContext
>& context
) {}
585 // Allows to override the visibility state of a RenderFrameHost.
586 // |visibility_state| should not be null. It will only be set if needed.
587 virtual void OverridePageVisibilityState(
588 RenderFrameHost
* render_frame_host
,
589 blink::WebPageVisibilityState
* visibility_state
) {}
591 // Allows an embedder to provide its own PresentationServiceDelegate
592 // implementation. Returns nullptr if unavailable.
593 virtual PresentationServiceDelegate
* GetPresentationServiceDelegate(
594 WebContents
* web_contents
);
596 // Allows programmatic opening of a new tab/window without going through
597 // another WebContents. For example, from a Worker. |callback| will be
598 // invoked with the appropriate WebContents* when available.
599 virtual void OpenURL(BrowserContext
* browser_context
,
600 const OpenURLParams
& params
,
601 const base::Callback
<void(WebContents
*)>& callback
);
603 // Allows the embedder to record |metric| for a specific |url|.
604 virtual void RecordURLMetric(const std::string
& metric
, const GURL
& url
) {}
606 #if defined(OS_POSIX) && !defined(OS_MACOSX)
607 // Populates |mappings| with all files that need to be mapped before launching
609 virtual void GetAdditionalMappedFilesForChildProcess(
610 const base::CommandLine
& command_line
,
611 int child_process_id
,
612 FileDescriptorInfo
* mappings
) {}
616 // Returns the name of the dll that contains cursors and other resources.
617 virtual const wchar_t* GetResourceDllName();
619 // This is called on the PROCESS_LAUNCHER thread before the renderer process
620 // is launched. It gives the embedder a chance to add loosen the sandbox
622 virtual void PreSpawnRenderer(sandbox::TargetPolicy
* policy
,
626 #if defined(VIDEO_HOLE)
627 // Allows an embedder to provide its own ExternalVideoSurfaceContainer
628 // implementation. Return nullptr to disable external surface video.
629 virtual ExternalVideoSurfaceContainer
*
630 OverrideCreateExternalVideoSurfaceContainer(WebContents
* web_contents
);
634 } // namespace content
636 #endif // CONTENT_PUBLIC_BROWSER_CONTENT_BROWSER_CLIENT_H_