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 CHROME_BROWSER_CHROME_CONTENT_BROWSER_CLIENT_H_
6#define CHROME_BROWSER_CHROME_CONTENT_BROWSER_CLIENT_H_
7
8#include <set>
9#include <string>
10#include <utility>
11#include <vector>
12
13#include "base/compiler_specific.h"
14#include "base/gtest_prod_util.h"
15#include "base/memory/scoped_ptr.h"
16#include "base/memory/weak_ptr.h"
17#include "chrome/common/chrome_version_info.h"
18#include "content/public/browser/content_browser_client.h"
19
20class ChromeContentBrowserClientParts;
21
22namespace base {
23class CommandLine;
24}
25
26namespace content {
27class QuotaPermissionContext;
28}
29
30namespace extensions {
31class BrowserPermissionsPolicyDelegate;
32}
33
34namespace prerender {
35class PrerenderTracker;
36}
37
38namespace user_prefs {
39class PrefRegistrySyncable;
40}
41
42namespace chrome {
43
44class ChromeContentBrowserClient : public content::ContentBrowserClient {
45 public:
46  ChromeContentBrowserClient();
47  virtual ~ChromeContentBrowserClient();
48
49  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
50
51  // Notification that the application locale has changed. This allows us to
52  // update our I/O thread cache of this value.
53  static void SetApplicationLocale(const std::string& locale);
54
55  virtual content::BrowserMainParts* CreateBrowserMainParts(
56      const content::MainFunctionParams& parameters) OVERRIDE;
57  virtual std::string GetStoragePartitionIdForSite(
58      content::BrowserContext* browser_context,
59      const GURL& site) OVERRIDE;
60  virtual bool IsValidStoragePartitionId(
61      content::BrowserContext* browser_context,
62      const std::string& partition_id) OVERRIDE;
63  virtual void GetStoragePartitionConfigForSite(
64      content::BrowserContext* browser_context,
65      const GURL& site,
66      bool can_be_default,
67      std::string* partition_domain,
68      std::string* partition_name,
69      bool* in_memory) OVERRIDE;
70  virtual content::WebContentsViewDelegate* GetWebContentsViewDelegate(
71      content::WebContents* web_contents) OVERRIDE;
72  virtual void RenderProcessWillLaunch(
73      content::RenderProcessHost* host) OVERRIDE;
74  virtual bool ShouldUseProcessPerSite(content::BrowserContext* browser_context,
75                                       const GURL& effective_url) OVERRIDE;
76  virtual GURL GetEffectiveURL(content::BrowserContext* browser_context,
77                               const GURL& url) OVERRIDE;
78  virtual void GetAdditionalWebUISchemes(
79      std::vector<std::string>* additional_schemes) OVERRIDE;
80  virtual void GetAdditionalWebUIHostsToIgnoreParititionCheck(
81      std::vector<std::string>* hosts) OVERRIDE;
82  virtual net::URLRequestContextGetter* CreateRequestContext(
83      content::BrowserContext* browser_context,
84      content::ProtocolHandlerMap* protocol_handlers,
85      content::URLRequestInterceptorScopedVector request_interceptors) OVERRIDE;
86  virtual net::URLRequestContextGetter* CreateRequestContextForStoragePartition(
87      content::BrowserContext* browser_context,
88      const base::FilePath& partition_path,
89      bool in_memory,
90      content::ProtocolHandlerMap* protocol_handlers,
91      content::URLRequestInterceptorScopedVector request_interceptors) OVERRIDE;
92  virtual bool IsHandledURL(const GURL& url) OVERRIDE;
93  virtual bool CanCommitURL(content::RenderProcessHost* process_host,
94                            const GURL& url) OVERRIDE;
95  virtual bool ShouldAllowOpenURL(content::SiteInstance* site_instance,
96                                  const GURL& url) OVERRIDE;
97  virtual bool IsSuitableHost(content::RenderProcessHost* process_host,
98                              const GURL& site_url) OVERRIDE;
99  virtual bool MayReuseHost(content::RenderProcessHost* process_host) OVERRIDE;
100  virtual bool ShouldTryToUseExistingProcessHost(
101      content::BrowserContext* browser_context, const GURL& url) OVERRIDE;
102  virtual void SiteInstanceGotProcess(
103      content::SiteInstance* site_instance) OVERRIDE;
104  virtual void SiteInstanceDeleting(content::SiteInstance* site_instance)
105      OVERRIDE;
106  virtual bool ShouldSwapBrowsingInstancesForNavigation(
107      content::SiteInstance* site_instance,
108      const GURL& current_url,
109      const GURL& new_url) OVERRIDE;
110  virtual bool ShouldSwapProcessesForRedirect(
111      content::ResourceContext* resource_context,
112      const GURL& current_url,
113      const GURL& new_url) OVERRIDE;
114  virtual bool ShouldAssignSiteForURL(const GURL& url) OVERRIDE;
115  virtual std::string GetCanonicalEncodingNameByAliasName(
116      const std::string& alias_name) OVERRIDE;
117  virtual void AppendExtraCommandLineSwitches(base::CommandLine* command_line,
118                                              int child_process_id) OVERRIDE;
119  virtual std::string GetApplicationLocale() OVERRIDE;
120  virtual std::string GetAcceptLangs(
121      content::BrowserContext* context) OVERRIDE;
122  virtual const gfx::ImageSkia* GetDefaultFavicon() OVERRIDE;
123  virtual bool AllowAppCache(const GURL& manifest_url,
124                             const GURL& first_party,
125                             content::ResourceContext* context) OVERRIDE;
126  virtual bool AllowGetCookie(const GURL& url,
127                              const GURL& first_party,
128                              const net::CookieList& cookie_list,
129                              content::ResourceContext* context,
130                              int render_process_id,
131                              int render_frame_id) OVERRIDE;
132  virtual bool AllowSetCookie(const GURL& url,
133                              const GURL& first_party,
134                              const std::string& cookie_line,
135                              content::ResourceContext* context,
136                              int render_process_id,
137                              int render_frame_id,
138                              net::CookieOptions* options) OVERRIDE;
139  virtual bool AllowSaveLocalState(content::ResourceContext* context) OVERRIDE;
140  virtual bool AllowWorkerDatabase(
141      const GURL& url,
142      const base::string16& name,
143      const base::string16& display_name,
144      unsigned long estimated_size,
145      content::ResourceContext* context,
146      const std::vector<std::pair<int, int> >& render_frames) OVERRIDE;
147  virtual void AllowWorkerFileSystem(
148      const GURL& url,
149      content::ResourceContext* context,
150      const std::vector<std::pair<int, int> >& render_frames,
151      base::Callback<void(bool)> callback) OVERRIDE;
152  virtual bool AllowWorkerIndexedDB(
153      const GURL& url,
154      const base::string16& name,
155      content::ResourceContext* context,
156      const std::vector<std::pair<int, int> >& render_frames) OVERRIDE;
157  virtual net::URLRequestContext* OverrideRequestContextForURL(
158      const GURL& url, content::ResourceContext* context) OVERRIDE;
159  virtual content::QuotaPermissionContext*
160      CreateQuotaPermissionContext() OVERRIDE;
161  virtual void AllowCertificateError(
162      int render_process_id,
163      int render_frame_id,
164      int cert_error,
165      const net::SSLInfo& ssl_info,
166      const GURL& request_url,
167      content::ResourceType resource_type,
168      bool overridable,
169      bool strict_enforcement,
170      bool expired_previous_decision,
171      const base::Callback<void(bool)>& callback,
172      content::CertificateRequestResultType* request) OVERRIDE;
173  virtual void SelectClientCertificate(
174      int render_process_id,
175      int render_frame_id,
176      const net::HttpNetworkSession* network_session,
177      net::SSLCertRequestInfo* cert_request_info,
178      const base::Callback<void(net::X509Certificate*)>& callback) OVERRIDE;
179  virtual void AddCertificate(net::CertificateMimeType cert_type,
180                              const void* cert_data,
181                              size_t cert_size,
182                              int render_process_id,
183                              int render_frame_id) OVERRIDE;
184  virtual content::MediaObserver* GetMediaObserver() OVERRIDE;
185  virtual void RequestDesktopNotificationPermission(
186      const GURL& source_origin,
187      content::RenderFrameHost* render_frame_host,
188      const base::Callback<void(blink::WebNotificationPermission)>& callback)
189          OVERRIDE;
190  virtual blink::WebNotificationPermission
191      CheckDesktopNotificationPermission(
192          const GURL& source_origin,
193          content::ResourceContext* context,
194          int render_process_id) OVERRIDE;
195  virtual void ShowDesktopNotification(
196      const content::ShowDesktopNotificationHostMsgParams& params,
197      content::RenderFrameHost* render_frame_host,
198      scoped_ptr<content::DesktopNotificationDelegate> delegate,
199      base::Closure* cancel_callback) OVERRIDE;
200  virtual void RequestGeolocationPermission(
201      content::WebContents* web_contents,
202      int bridge_id,
203      const GURL& requesting_frame,
204      bool user_gesture,
205      base::Callback<void(bool)> result_callback,
206      base::Closure* cancel_callback) OVERRIDE;
207  virtual void RequestMidiSysExPermission(
208      content::WebContents* web_contents,
209      int bridge_id,
210      const GURL& requesting_frame,
211      bool user_gesture,
212      base::Callback<void(bool)> result_callback,
213      base::Closure* cancel_callback) OVERRIDE;
214  virtual void DidUseGeolocationPermission(content::WebContents* web_contents,
215                                           const GURL& frame_url,
216                                           const GURL& main_frame_url) OVERRIDE;
217  virtual void RequestProtectedMediaIdentifierPermission(
218      content::WebContents* web_contents,
219      const GURL& origin,
220      base::Callback<void(bool)> result_callback,
221      base::Closure* cancel_callback) OVERRIDE;
222  virtual bool CanCreateWindow(const GURL& opener_url,
223                               const GURL& opener_top_level_frame_url,
224                               const GURL& source_origin,
225                               WindowContainerType container_type,
226                               const GURL& target_url,
227                               const content::Referrer& referrer,
228                               WindowOpenDisposition disposition,
229                               const blink::WebWindowFeatures& features,
230                               bool user_gesture,
231                               bool opener_suppressed,
232                               content::ResourceContext* context,
233                               int render_process_id,
234                               int opener_id,
235                               bool* no_javascript_access) OVERRIDE;
236  virtual void ResourceDispatcherHostCreated() OVERRIDE;
237  virtual content::SpeechRecognitionManagerDelegate*
238      GetSpeechRecognitionManagerDelegate() OVERRIDE;
239  virtual net::NetLog* GetNetLog() OVERRIDE;
240  virtual content::AccessTokenStore* CreateAccessTokenStore() OVERRIDE;
241  virtual bool IsFastShutdownPossible() OVERRIDE;
242  virtual void OverrideWebkitPrefs(content::RenderViewHost* rvh,
243                                   const GURL& url,
244                                   content::WebPreferences* prefs) OVERRIDE;
245  virtual void BrowserURLHandlerCreated(
246      content::BrowserURLHandler* handler) OVERRIDE;
247  virtual void ClearCache(content::RenderViewHost* rvh) OVERRIDE;
248  virtual void ClearCookies(content::RenderViewHost* rvh) OVERRIDE;
249  virtual base::FilePath GetDefaultDownloadDirectory() OVERRIDE;
250  virtual std::string GetDefaultDownloadName() OVERRIDE;
251  virtual void DidCreatePpapiPlugin(
252      content::BrowserPpapiHost* browser_host) OVERRIDE;
253  virtual content::BrowserPpapiHost* GetExternalBrowserPpapiHost(
254      int plugin_process_id) OVERRIDE;
255  virtual bool AllowPepperSocketAPI(
256      content::BrowserContext* browser_context,
257      const GURL& url,
258      bool private_api,
259      const content::SocketPermissionRequest* params) OVERRIDE;
260  virtual ui::SelectFilePolicy* CreateSelectFilePolicy(
261      content::WebContents* web_contents) OVERRIDE;
262  virtual void GetAdditionalAllowedSchemesForFileSystem(
263      std::vector<std::string>* additional_schemes) OVERRIDE;
264  virtual void GetURLRequestAutoMountHandlers(
265      std::vector<storage::URLRequestAutoMountHandler>* handlers) OVERRIDE;
266  virtual void GetAdditionalFileSystemBackends(
267      content::BrowserContext* browser_context,
268      const base::FilePath& storage_partition_path,
269      ScopedVector<storage::FileSystemBackend>* additional_backends) OVERRIDE;
270  virtual content::DevToolsManagerDelegate*
271      GetDevToolsManagerDelegate() OVERRIDE;
272  virtual bool IsPluginAllowedToCallRequestOSFileHandle(
273      content::BrowserContext* browser_context,
274      const GURL& url) OVERRIDE;
275  virtual bool IsPluginAllowedToUseDevChannelAPIs(
276      content::BrowserContext* browser_context,
277      const GURL& url) OVERRIDE;
278  virtual net::CookieStore* OverrideCookieStoreForRenderProcess(
279      int render_process_id) OVERRIDE;
280
281#if defined(OS_POSIX) && !defined(OS_MACOSX)
282  virtual void GetAdditionalMappedFilesForChildProcess(
283      const base::CommandLine& command_line,
284      int child_process_id,
285      std::vector<content::FileDescriptorInfo>* mappings) OVERRIDE;
286#endif
287#if defined(OS_WIN)
288  virtual const wchar_t* GetResourceDllName() OVERRIDE;
289  virtual void PreSpawnRenderer(sandbox::TargetPolicy* policy,
290                                bool* success) OVERRIDE;
291#endif
292  virtual bool CheckMediaAccessPermission(
293      content::BrowserContext* browser_context,
294      const GURL& security_origin,
295      content::MediaStreamType type) OVERRIDE;
296
297 private:
298  friend class DisableWebRtcEncryptionFlagTest;
299
300#if defined(ENABLE_WEBRTC)
301  // Copies disable WebRTC encryption switch depending on the channel.
302  static void MaybeCopyDisableWebRtcEncryptionSwitch(
303      base::CommandLine* to_command_line,
304      const base::CommandLine& from_command_line,
305      VersionInfo::Channel channel);
306#endif
307
308  void FileSystemAccessed(
309      const GURL& url,
310      const std::vector<std::pair<int, int> >& render_frames,
311      base::Callback<void(bool)> callback,
312      bool allow);
313
314#if defined(ENABLE_EXTENSIONS)
315  void GuestPermissionRequestHelper(
316      const GURL& url,
317      const std::vector<std::pair<int, int> >& render_frames,
318      base::Callback<void(bool)> callback,
319      bool allow);
320
321  static void RequestFileSystemPermissionOnUIThread(
322      int render_process_id,
323      int render_frame_id,
324      const GURL& url,
325      bool allowed_by_default,
326      const base::Callback<void(bool)>& callback);
327#endif
328
329#if defined(ENABLE_PLUGINS)
330  // Set of origins that can use TCP/UDP private APIs from NaCl.
331  std::set<std::string> allowed_socket_origins_;
332  // Set of origins that can get a handle for FileIO from NaCl.
333  std::set<std::string> allowed_file_handle_origins_;
334  // Set of origins that can use "dev chanel" APIs from NaCl, even on stable
335  // versions of Chrome.
336  std::set<std::string> allowed_dev_channel_origins_;
337#endif
338
339  // The prerender tracker used to determine whether a render process is used
340  // for prerendering and an override cookie store must be provided.
341  // This needs to be kept as a member rather than just looked up from
342  // the profile due to initialization ordering, as well as due to threading.
343  // It is initialized on the UI thread when the ResoureDispatcherHost is
344  // created. It is used only the IO thread.
345  prerender::PrerenderTracker* prerender_tracker_;
346
347  // Vector of additional ChromeContentBrowserClientParts.
348  // Parts are deleted in the reverse order they are added.
349  std::vector<ChromeContentBrowserClientParts*> extra_parts_;
350
351  base::WeakPtrFactory<ChromeContentBrowserClient> weak_factory_;
352
353  DISALLOW_COPY_AND_ASSIGN(ChromeContentBrowserClient);
354};
355
356}  // namespace chrome
357
358#endif  // CHROME_BROWSER_CHROME_CONTENT_BROWSER_CLIENT_H_
359