content_browser_client.h revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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. 4 5#ifndef CONTENT_PUBLIC_BROWSER_CONTENT_BROWSER_CLIENT_H_ 6#define CONTENT_PUBLIC_BROWSER_CONTENT_BROWSER_CLIENT_H_ 7 8#include <map> 9#include <string> 10#include <utility> 11#include <vector> 12 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/browser/file_descriptor_info.h" 20#include "content/public/common/content_client.h" 21#include "content/public/common/socket_permission_request.h" 22#include "content/public/common/window_container_type.h" 23#include "net/base/mime_util.h" 24#include "net/cookies/canonical_cookie.h" 25#include "net/url_request/url_request_job_factory.h" 26#include "third_party/WebKit/public/web/WebNotificationPresenter.h" 27#include "ui/base/window_open_disposition.h" 28#include "webkit/common/resource_type.h" 29 30#if defined(OS_POSIX) && !defined(OS_MACOSX) 31#include "base/posix/global_descriptors.h" 32#endif 33 34class CommandLine; 35class GURL; 36struct WebPreferences; 37 38namespace WebKit { 39struct WebWindowFeatures; 40} 41 42namespace base { 43class DictionaryValue; 44class FilePath; 45} 46namespace crypto { 47class CryptoModuleBlockingPasswordDelegate; 48} 49 50namespace gfx { 51class ImageSkia; 52} 53 54namespace net { 55class CookieOptions; 56class HttpNetworkSession; 57class NetLog; 58class SSLCertRequestInfo; 59class SSLInfo; 60class URLRequest; 61class URLRequestContext; 62class URLRequestContextGetter; 63class X509Certificate; 64} 65 66namespace sandbox { 67class TargetPolicy; 68} 69 70namespace ui { 71class SelectFilePolicy; 72} 73 74namespace fileapi { 75class ExternalMountPoints; 76class FileSystemBackend; 77} 78 79namespace content { 80 81class AccessTokenStore; 82class BrowserChildProcessHost; 83class BrowserContext; 84class BrowserMainParts; 85class BrowserPluginGuestDelegate; 86class BrowserPpapiHost; 87class BrowserURLHandler; 88class LocationProvider; 89class MediaObserver; 90class QuotaPermissionContext; 91class RenderProcessHost; 92class RenderViewHost; 93class RenderViewHostDelegateView; 94class ResourceContext; 95class SiteInstance; 96class SpeechRecognitionManagerDelegate; 97class WebContents; 98class WebContentsViewDelegate; 99class WebContentsViewPort; 100struct MainFunctionParams; 101struct Referrer; 102struct ShowDesktopNotificationHostMsgParams; 103 104// A mapping from the scheme name to the protocol handler that services its 105// content. 106typedef std::map< 107 std::string, linked_ptr<net::URLRequestJobFactory::ProtocolHandler> > 108 ProtocolHandlerMap; 109 110// Embedder API (or SPI) for participating in browser logic, to be implemented 111// by the client of the content browser. See ChromeContentBrowserClient for the 112// principal implementation. The methods are assumed to be called on the UI 113// thread unless otherwise specified. Use this "escape hatch" sparingly, to 114// avoid the embedder interface ballooning and becoming very specific to Chrome. 115// (Often, the call out to the client can happen in a different part of the code 116// that either already has a hook out to the embedder, or calls out to one of 117// the observer interfaces.) 118class CONTENT_EXPORT ContentBrowserClient { 119 public: 120 virtual ~ContentBrowserClient() {} 121 122 // Allows the embedder to set any number of custom BrowserMainParts 123 // implementations for the browser startup code. See comments in 124 // browser_main_parts.h. 125 virtual BrowserMainParts* CreateBrowserMainParts( 126 const MainFunctionParams& parameters); 127 128 // Allows an embedder to return their own WebContentsViewPort implementation. 129 // Return NULL to let the default one for the platform be created. Otherwise 130 // |render_view_host_delegate_view| also needs to be provided, and it is 131 // owned by the embedder. 132 virtual WebContentsViewPort* OverrideCreateWebContentsView( 133 WebContents* web_contents, 134 RenderViewHostDelegateView** render_view_host_delegate_view); 135 136 // If content creates the WebContentsView implementation, it will ask the 137 // embedder to return an (optional) delegate to customize it. The view will 138 // own the delegate. 139 virtual WebContentsViewDelegate* GetWebContentsViewDelegate( 140 WebContents* web_contents); 141 142 // Notifies that a guest WebContents has been created. A guest WebContents 143 // represents a renderer that's hosted within a BrowserPlugin. Creation can 144 // occur an arbitrary length of time before attachment. If the new guest has 145 // an |opener_web_contents|, then it's a new window created by that opener. 146 // If the guest was created via navigation, then |extra_params| will be 147 // non-NULL. |extra_params| are parameters passed to the BrowserPlugin object 148 // element by the content embedder. These parameters may include the API to 149 // enable for the given guest. |guest_delegate| is a return parameter of 150 // the delegate in the content embedder that will service the guest in the 151 // content layer. The content layer takes ownership of the |guest_delegate|. 152 virtual void GuestWebContentsCreated( 153 WebContents* guest_web_contents, 154 WebContents* opener_web_contents, 155 BrowserPluginGuestDelegate** guest_delegate, 156 scoped_ptr<base::DictionaryValue> extra_params) {} 157 158 // Notifies that a guest WebContents has been attached to a BrowserPlugin. 159 // A guest is attached to a BrowserPlugin when the guest has acquired an 160 // embedder WebContents. This happens on initial navigation or when a new 161 // window is attached to a BrowserPlugin. |extra_params| are params sent 162 // from javascript. 163 virtual void GuestWebContentsAttached( 164 WebContents* guest_web_contents, 165 WebContents* embedder_web_contents, 166 const GURL& embedder_frame_url, 167 const base::DictionaryValue& extra_params) {} 168 169 // Notifies that a RenderProcessHost has been created. This is called before 170 // the content layer adds its own BrowserMessageFilters, so that the 171 // embedder's IPC filters have priority. 172 virtual void RenderProcessHostCreated(RenderProcessHost* host) {} 173 174 // Notifies that a BrowserChildProcessHost has been created. 175 virtual void BrowserChildProcessHostCreated(BrowserChildProcessHost* host) {} 176 177 // Get the effective URL for the given actual URL, to allow an embedder to 178 // group different url schemes in the same SiteInstance. 179 virtual GURL GetEffectiveURL(BrowserContext* browser_context, 180 const GURL& url); 181 182 // Returns whether all instances of the specified effective URL should be 183 // rendered by the same process, rather than using process-per-site-instance. 184 virtual bool ShouldUseProcessPerSite(BrowserContext* browser_context, 185 const GURL& effective_url); 186 187 // Returns a list additional WebUI schemes, if any. These additional schemes 188 // act as aliases to the chrome: scheme. The additional schemes may or may 189 // not serve specific WebUI pages depending on the particular URLDataSource 190 // and its override of URLDataSource::ShouldServiceRequest. 191 virtual void GetAdditionalWebUISchemes( 192 std::vector<std::string>* additional_schemes) {} 193 194 // Creates the main net::URLRequestContextGetter. Should only be called once 195 // per ContentBrowserClient object. 196 // TODO(ajwong): Remove once http://crbug.com/159193 is resolved. 197 virtual net::URLRequestContextGetter* CreateRequestContext( 198 BrowserContext* browser_context, 199 ProtocolHandlerMap* protocol_handlers); 200 201 // Creates the net::URLRequestContextGetter for a StoragePartition. Should 202 // only be called once per partition_path per ContentBrowserClient object. 203 // TODO(ajwong): Remove once http://crbug.com/159193 is resolved. 204 virtual net::URLRequestContextGetter* CreateRequestContextForStoragePartition( 205 BrowserContext* browser_context, 206 const base::FilePath& partition_path, 207 bool in_memory, 208 ProtocolHandlerMap* protocol_handlers); 209 210 // Returns whether a specified URL is handled by the embedder's internal 211 // protocol handlers. 212 virtual bool IsHandledURL(const GURL& url); 213 214 // Returns whether the given process is allowed to commit |url|. This is a 215 // more conservative check than IsSuitableHost, since it is used after a 216 // navigation has committed to ensure that the process did not exceed its 217 // authority. 218 virtual bool CanCommitURL(RenderProcessHost* process_host, const GURL& url); 219 220 // Returns whether a URL should be allowed to open from a specific context. 221 // This also applies in cases where the new URL will open in another process. 222 virtual bool ShouldAllowOpenURL(SiteInstance* site_instance, const GURL& url); 223 224 // Returns whether a new view for a given |site_url| can be launched in a 225 // given |process_host|. 226 virtual bool IsSuitableHost(RenderProcessHost* process_host, 227 const GURL& site_url); 228 229 // Returns whether a new process should be created or an existing one should 230 // be reused based on the URL we want to load. This should return false, 231 // unless there is a good reason otherwise. 232 virtual bool ShouldTryToUseExistingProcessHost( 233 BrowserContext* browser_context, const GURL& url); 234 235 // Called when a site instance is first associated with a process. 236 virtual void SiteInstanceGotProcess(SiteInstance* site_instance) {} 237 238 // Called from a site instance's destructor. 239 virtual void SiteInstanceDeleting(SiteInstance* site_instance) {} 240 241 // Returns true if for the navigation from |current_url| to |new_url| 242 // in |site_instance|, the process should be swapped (even if we are in a 243 // process model that doesn't usually swap). 244 virtual bool ShouldSwapProcessesForNavigation(SiteInstance* site_instance, 245 const GURL& current_url, 246 const GURL& new_url); 247 248 // Returns true if the given navigation redirect should cause a renderer 249 // process swap. 250 // This is called on the IO thread. 251 virtual bool ShouldSwapProcessesForRedirect(ResourceContext* resource_context, 252 const GURL& current_url, 253 const GURL& new_url); 254 255 // Returns true if the passed in URL should be assigned as the site of the 256 // current SiteInstance, if it does not yet have a site. 257 virtual bool ShouldAssignSiteForURL(const GURL& url); 258 259 // See CharacterEncoding's comment. 260 virtual std::string GetCanonicalEncodingNameByAliasName( 261 const std::string& alias_name); 262 263 // Allows the embedder to pass extra command line flags. 264 // switches::kProcessType will already be set at this point. 265 virtual void AppendExtraCommandLineSwitches(CommandLine* command_line, 266 int child_process_id) {} 267 268 // Returns the locale used by the application. 269 // This is called on the UI and IO threads. 270 virtual std::string GetApplicationLocale(); 271 272 // Returns the languages used in the Accept-Languages HTTP header. 273 // (Not called GetAcceptLanguages so it doesn't clash with win32). 274 virtual std::string GetAcceptLangs(BrowserContext* context); 275 276 // Returns the default favicon. The callee doesn't own the given bitmap. 277 virtual gfx::ImageSkia* GetDefaultFavicon(); 278 279 // Allow the embedder to control if an AppCache can be used for the given url. 280 // This is called on the IO thread. 281 virtual bool AllowAppCache(const GURL& manifest_url, 282 const GURL& first_party, 283 ResourceContext* context); 284 285 // Allow the embedder to control if the given cookie can be read. 286 // This is called on the IO thread. 287 virtual bool AllowGetCookie(const GURL& url, 288 const GURL& first_party, 289 const net::CookieList& cookie_list, 290 ResourceContext* context, 291 int render_process_id, 292 int render_view_id); 293 294 // Allow the embedder to control if the given cookie can be set. 295 // This is called on the IO thread. 296 virtual bool AllowSetCookie(const GURL& url, 297 const GURL& first_party, 298 const std::string& cookie_line, 299 ResourceContext* context, 300 int render_process_id, 301 int render_view_id, 302 net::CookieOptions* options); 303 304 // This is called on the IO thread. 305 virtual bool AllowSaveLocalState(ResourceContext* context); 306 307 // Allow the embedder to control if access to web database by a shared worker 308 // is allowed. |render_views| is a vector of pairs of 309 // RenderProcessID/RenderViewID of RenderViews that are using this worker. 310 // This is called on the IO thread. 311 virtual bool AllowWorkerDatabase( 312 const GURL& url, 313 const string16& name, 314 const string16& display_name, 315 unsigned long estimated_size, 316 ResourceContext* context, 317 const std::vector<std::pair<int, int> >& render_views); 318 319 // Allow the embedder to control if access to file system by a shared worker 320 // is allowed. 321 // This is called on the IO thread. 322 virtual bool AllowWorkerFileSystem( 323 const GURL& url, 324 ResourceContext* context, 325 const std::vector<std::pair<int, int> >& render_views); 326 327 // Allow the embedder to control if access to IndexedDB by a shared worker 328 // is allowed. 329 // This is called on the IO thread. 330 virtual bool AllowWorkerIndexedDB( 331 const GURL& url, 332 const string16& name, 333 ResourceContext* context, 334 const std::vector<std::pair<int, int> >& render_views); 335 336 // Allow the embedder to override the request context based on the URL for 337 // certain operations, like cookie access. Returns NULL to indicate the 338 // regular request context should be used. 339 // This is called on the IO thread. 340 virtual net::URLRequestContext* OverrideRequestContextForURL( 341 const GURL& url, ResourceContext* context); 342 343 // Allow the embedder to specify a string version of the storage partition 344 // config with a site. 345 virtual std::string GetStoragePartitionIdForSite( 346 content::BrowserContext* browser_context, 347 const GURL& site); 348 349 // Allows the embedder to provide a validation check for |partition_id|s. 350 // This domain of valid entries should match the range of outputs for 351 // GetStoragePartitionIdForChildProcess(). 352 virtual bool IsValidStoragePartitionId(BrowserContext* browser_context, 353 const std::string& partition_id); 354 355 // Allows the embedder to provide a storage parititon configuration for a 356 // site. A storage partition configuration includes a domain of the embedder's 357 // choice, an optional name within that domain, and whether the partition is 358 // in-memory only. 359 // 360 // If |can_be_default| is false, the caller is telling the embedder that the 361 // |site| is known to not be in the default partition. This is useful in 362 // some shutdown situations where the bookkeeping logic that maps sites to 363 // their partition configuration are no longer valid. 364 // 365 // The |partition_domain| is [a-z]* UTF-8 string, specifying the domain in 366 // which partitions live (similar to namespace). Within a domain, partitions 367 // can be uniquely identified by the combination of |partition_name| and 368 // |in_memory| values. When a partition is not to be persisted, the 369 // |in_memory| value must be set to true. 370 virtual void GetStoragePartitionConfigForSite( 371 content::BrowserContext* browser_context, 372 const GURL& site, 373 bool can_be_default, 374 std::string* partition_domain, 375 std::string* partition_name, 376 bool* in_memory); 377 378 // Create and return a new quota permission context. 379 virtual QuotaPermissionContext* CreateQuotaPermissionContext(); 380 381 // Informs the embedder that a certificate error has occured. If 382 // |overridable| is true and if |strict_enforcement| is false, the user 383 // can ignore the error and continue. The embedder can call the callback 384 // asynchronously. If |result| is not set to 385 // CERTIFICATE_REQUEST_RESULT_TYPE_CONTINUE, the request will be cancelled 386 // or denied immediately, and the callback won't be run. 387 virtual void AllowCertificateError( 388 int render_process_id, 389 int render_view_id, 390 int cert_error, 391 const net::SSLInfo& ssl_info, 392 const GURL& request_url, 393 ResourceType::Type resource_type, 394 bool overridable, 395 bool strict_enforcement, 396 const base::Callback<void(bool)>& callback, 397 CertificateRequestResultType* result) {} 398 399 // Selects a SSL client certificate and returns it to the |callback|. If no 400 // certificate was selected NULL is returned to the |callback|. 401 virtual void SelectClientCertificate( 402 int render_process_id, 403 int render_view_id, 404 const net::HttpNetworkSession* network_session, 405 net::SSLCertRequestInfo* cert_request_info, 406 const base::Callback<void(net::X509Certificate*)>& callback) {} 407 408 // Adds a new installable certificate or private key. 409 // Typically used to install an X.509 user certificate. 410 // Note that it's up to the embedder to verify that the data is 411 // well-formed. |cert_data| will be NULL if file_size is 0. 412 virtual void AddCertificate( 413 net::URLRequest* request, 414 net::CertificateMimeType cert_type, 415 const void* cert_data, 416 size_t cert_size, 417 int render_process_id, 418 int render_view_id) {} 419 420 // Returns a class to get notifications about media event. The embedder can 421 // return NULL if they're not interested. 422 virtual MediaObserver* GetMediaObserver(); 423 424 // Asks permission to show desktop notifications. 425 virtual void RequestDesktopNotificationPermission( 426 const GURL& source_origin, 427 int callback_context, 428 int render_process_id, 429 int render_view_id) {} 430 431 // Checks if the given page has permission to show desktop notifications. 432 // This is called on the IO thread. 433 virtual WebKit::WebNotificationPresenter::Permission 434 CheckDesktopNotificationPermission( 435 const GURL& source_url, 436 ResourceContext* context, 437 int render_process_id); 438 439 // Show a desktop notification. If |worker| is true, the request came from an 440 // HTML5 web worker, otherwise, it came from a renderer. 441 virtual void ShowDesktopNotification( 442 const ShowDesktopNotificationHostMsgParams& params, 443 int render_process_id, 444 int render_view_id, 445 bool worker) {} 446 447 // Cancels a displayed desktop notification. 448 virtual void CancelDesktopNotification( 449 int render_process_id, 450 int render_view_id, 451 int notification_id) {} 452 453 // Returns true if the given page is allowed to open a window of the given 454 // type. If true is returned, |no_javascript_access| will indicate whether 455 // the window that is created should be scriptable/in the same process. 456 // This is called on the IO thread. 457 virtual bool CanCreateWindow(const GURL& opener_url, 458 const GURL& opener_top_level_frame_url, 459 const GURL& source_origin, 460 WindowContainerType container_type, 461 const GURL& target_url, 462 const content::Referrer& referrer, 463 WindowOpenDisposition disposition, 464 const WebKit::WebWindowFeatures& features, 465 bool user_gesture, 466 bool opener_suppressed, 467 content::ResourceContext* context, 468 int render_process_id, 469 bool is_guest, 470 int opener_id, 471 bool* no_javascript_access); 472 473 // Returns a title string to use in the task manager for a process host with 474 // the given URL, or the empty string to fall back to the default logic. 475 // This is called on the IO thread. 476 virtual std::string GetWorkerProcessTitle(const GURL& url, 477 ResourceContext* context); 478 479 // Notifies the embedder that the ResourceDispatcherHost has been created. 480 // This is when it can optionally add a delegate. 481 virtual void ResourceDispatcherHostCreated() {} 482 483 // Allows the embedder to return a delegate for the SpeechRecognitionManager. 484 // The delegate will be owned by the manager. It's valid to return NULL. 485 virtual SpeechRecognitionManagerDelegate* 486 GetSpeechRecognitionManagerDelegate(); 487 488 // Getters for common objects. 489 virtual net::NetLog* GetNetLog(); 490 491 // Creates a new AccessTokenStore for gelocation. 492 virtual AccessTokenStore* CreateAccessTokenStore(); 493 494 // Returns true if fast shutdown is possible. 495 virtual bool IsFastShutdownPossible(); 496 497 // Called by WebContents to override the WebKit preferences that are used by 498 // the renderer. The content layer will add its own settings, and then it's up 499 // to the embedder to update it if it wants. 500 virtual void OverrideWebkitPrefs(RenderViewHost* render_view_host, 501 const GURL& url, 502 WebPreferences* prefs) {} 503 504 // Inspector setting was changed and should be persisted. 505 virtual void UpdateInspectorSetting(RenderViewHost* rvh, 506 const std::string& key, 507 const std::string& value) {} 508 509 // Notifies that BrowserURLHandler has been created, so that the embedder can 510 // optionally add their own handlers. 511 virtual void BrowserURLHandlerCreated(BrowserURLHandler* handler) {} 512 513 // Clears browser cache. 514 virtual void ClearCache(RenderViewHost* rvh) {} 515 516 // Clears browser cookies. 517 virtual void ClearCookies(RenderViewHost* rvh) {} 518 519 // Returns the default download directory. 520 // This can be called on any thread. 521 virtual base::FilePath GetDefaultDownloadDirectory(); 522 523 // Returns the default filename used in downloads when we have no idea what 524 // else we should do with the file. 525 virtual std::string GetDefaultDownloadName(); 526 527 // Notification that a pepper plugin has just been spawned. This allows the 528 // embedder to add filters onto the host to implement interfaces. 529 // This is called on the IO thread. 530 virtual void DidCreatePpapiPlugin(BrowserPpapiHost* browser_host) {} 531 532 // Gets the host for an external out-of-process plugin. 533 virtual content::BrowserPpapiHost* GetExternalBrowserPpapiHost( 534 int plugin_child_id); 535 536 // Returns true if the given browser_context and site_url support hosting 537 // BrowserPlugins. 538 virtual bool SupportsBrowserPlugin(BrowserContext* browser_context, 539 const GURL& site_url); 540 541 // Returns true if the socket operation specified by |params| is allowed from 542 // the given |browser_context| and |url|. If |params| is NULL, this method 543 // checks the basic "socket" permission, which is for those operations that 544 // don't require a specific socket permission rule. 545 // |private_api| indicates whether this permission check is for the private 546 // Pepper socket API or the public one. 547 virtual bool AllowPepperSocketAPI(BrowserContext* browser_context, 548 const GURL& url, 549 bool private_api, 550 const SocketPermissionRequest* params); 551 552 // Returns an implementation of a file selecition policy. Can return NULL. 553 virtual ui::SelectFilePolicy* CreateSelectFilePolicy( 554 WebContents* web_contents); 555 556 // Returns additional allowed scheme set which can access files in 557 // FileSystem API. 558 virtual void GetAdditionalAllowedSchemesForFileSystem( 559 std::vector<std::string>* additional_schemes) {} 560 561 // Returns additional file system backends for FileSystem API. 562 // |browser_context| is needed in the additional FileSystemBackends. 563 // It has mount points to create objects returned by additional 564 // FileSystemBackends, and SpecialStoragePolicy for permission granting. 565 virtual void GetAdditionalFileSystemBackends( 566 BrowserContext* browser_context, 567 const base::FilePath& storage_partition_path, 568 ScopedVector<fileapi::FileSystemBackend>* additional_backends) {} 569 570 // Allows an embedder to return its own LocationProvider implementation. 571 // Return NULL to use the default one for the platform to be created. 572 virtual LocationProvider* OverrideSystemLocationProvider(); 573 574#if defined(OS_POSIX) && !defined(OS_MACOSX) 575 // Populates |mappings| with all files that need to be mapped before launching 576 // a child process. 577 virtual void GetAdditionalMappedFilesForChildProcess( 578 const CommandLine& command_line, 579 int child_process_id, 580 std::vector<FileDescriptorInfo>* mappings) {} 581#endif 582 583#if defined(OS_WIN) 584 // Returns the name of the dll that contains cursors and other resources. 585 virtual const wchar_t* GetResourceDllName(); 586 587 // This is called on the PROCESS_LAUNCHER thread before the renderer process 588 // is launched. It gives the embedder a chance to add loosen the sandbox 589 // policy. 590 virtual void PreSpawnRenderer(sandbox::TargetPolicy* policy, 591 bool* success) {} 592#endif 593 594#if defined(USE_NSS) 595 // Return a delegate to authenticate and unlock |module|. 596 // This is called on a worker thread. 597 virtual 598 crypto::CryptoModuleBlockingPasswordDelegate* GetCryptoPasswordDelegate( 599 const GURL& url); 600#endif 601 602 // Returns true if plugin referred to by the url can use 603 // pp::FileIO::RequestOSFileHandle. 604 virtual bool IsPluginAllowedToCallRequestOSFileHandle( 605 content::BrowserContext* browser_context, 606 const GURL& url); 607}; 608 609} // namespace content 610 611#endif // CONTENT_PUBLIC_BROWSER_CONTENT_BROWSER_CLIENT_H_ 612