ProfilePolicyConnectorFactory: Refactoring from Profile to BrowserContext.
[chromium-blink-merge.git] / android_webview / browser / aw_content_browser_client.cc
blob0bddaf6468400279be9ef8948f357138ea191d81
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 #include "android_webview/browser/aw_content_browser_client.h"
7 #include "android_webview/browser/aw_browser_context.h"
8 #include "android_webview/browser/aw_browser_main_parts.h"
9 #include "android_webview/browser/aw_browser_permission_request_delegate.h"
10 #include "android_webview/browser/aw_contents_client_bridge_base.h"
11 #include "android_webview/browser/aw_contents_io_thread_client.h"
12 #include "android_webview/browser/aw_cookie_access_policy.h"
13 #include "android_webview/browser/aw_dev_tools_manager_delegate.h"
14 #include "android_webview/browser/aw_printing_message_filter.h"
15 #include "android_webview/browser/aw_quota_permission_context.h"
16 #include "android_webview/browser/aw_web_preferences_populater.h"
17 #include "android_webview/browser/jni_dependency_factory.h"
18 #include "android_webview/browser/net/aw_url_request_context_getter.h"
19 #include "android_webview/browser/net_disk_cache_remover.h"
20 #include "android_webview/browser/renderer_host/aw_resource_dispatcher_host_delegate.h"
21 #include "android_webview/common/render_view_messages.h"
22 #include "android_webview/common/url_constants.h"
23 #include "base/android/locale_utils.h"
24 #include "base/base_paths_android.h"
25 #include "base/path_service.h"
26 #include "components/cdm/browser/cdm_message_filter_android.h"
27 #include "content/public/browser/access_token_store.h"
28 #include "content/public/browser/browser_message_filter.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/child_process_security_policy.h"
31 #include "content/public/browser/permission_type.h"
32 #include "content/public/browser/render_process_host.h"
33 #include "content/public/browser/render_view_host.h"
34 #include "content/public/browser/web_contents.h"
35 #include "content/public/common/url_constants.h"
36 #include "content/public/common/web_preferences.h"
37 #include "net/android/network_library.h"
38 #include "net/ssl/ssl_cert_request_info.h"
39 #include "net/ssl/ssl_info.h"
40 #include "ui/base/resource/resource_bundle.h"
41 #include "ui/resources/grit/ui_resources.h"
43 using content::BrowserThread;
44 using content::ResourceType;
46 namespace android_webview {
47 namespace {
49 // TODO(sgurun) move this to its own file.
50 // This class filters out incoming aw_contents related IPC messages for the
51 // renderer process on the IPC thread.
52 class AwContentsMessageFilter : public content::BrowserMessageFilter {
53 public:
54 explicit AwContentsMessageFilter(int process_id);
56 // BrowserMessageFilter methods.
57 void OverrideThreadForMessage(const IPC::Message& message,
58 BrowserThread::ID* thread) override;
59 bool OnMessageReceived(const IPC::Message& message) override;
61 void OnShouldOverrideUrlLoading(int routing_id,
62 const base::string16& url,
63 bool* ignore_navigation);
64 void OnSubFrameCreated(int parent_render_frame_id, int child_render_frame_id);
66 private:
67 ~AwContentsMessageFilter() override;
69 int process_id_;
71 DISALLOW_COPY_AND_ASSIGN(AwContentsMessageFilter);
74 AwContentsMessageFilter::AwContentsMessageFilter(int process_id)
75 : BrowserMessageFilter(AndroidWebViewMsgStart),
76 process_id_(process_id) {
79 AwContentsMessageFilter::~AwContentsMessageFilter() {
82 void AwContentsMessageFilter::OverrideThreadForMessage(
83 const IPC::Message& message, BrowserThread::ID* thread) {
84 if (message.type() == AwViewHostMsg_ShouldOverrideUrlLoading::ID) {
85 *thread = BrowserThread::UI;
89 bool AwContentsMessageFilter::OnMessageReceived(const IPC::Message& message) {
90 bool handled = true;
91 IPC_BEGIN_MESSAGE_MAP(AwContentsMessageFilter, message)
92 IPC_MESSAGE_HANDLER(AwViewHostMsg_ShouldOverrideUrlLoading,
93 OnShouldOverrideUrlLoading)
94 IPC_MESSAGE_HANDLER(AwViewHostMsg_SubFrameCreated, OnSubFrameCreated)
95 IPC_MESSAGE_UNHANDLED(handled = false)
96 IPC_END_MESSAGE_MAP()
97 return handled;
100 void AwContentsMessageFilter::OnShouldOverrideUrlLoading(
101 int render_frame_id,
102 const base::string16& url,
103 bool* ignore_navigation) {
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
105 *ignore_navigation = false;
106 AwContentsClientBridgeBase* client =
107 AwContentsClientBridgeBase::FromID(process_id_, render_frame_id);
108 if (client) {
109 *ignore_navigation = client->ShouldOverrideUrlLoading(url);
110 } else {
111 LOG(WARNING) << "Failed to find the associated render view host for url: "
112 << url;
116 void AwContentsMessageFilter::OnSubFrameCreated(int parent_render_frame_id,
117 int child_render_frame_id) {
118 AwContentsIoThreadClient::SubFrameCreated(
119 process_id_, parent_render_frame_id, child_render_frame_id);
122 class AwAccessTokenStore : public content::AccessTokenStore {
123 public:
124 AwAccessTokenStore() { }
126 // content::AccessTokenStore implementation
127 void LoadAccessTokens(const LoadAccessTokensCallbackType& request) override {
128 AccessTokenStore::AccessTokenSet access_token_set;
129 // AccessTokenSet and net::URLRequestContextGetter not used on Android,
130 // but Run needs to be called to finish the geolocation setup.
131 request.Run(access_token_set, NULL);
133 void SaveAccessToken(const GURL& server_url,
134 const base::string16& access_token) override {}
136 private:
137 ~AwAccessTokenStore() override {}
139 DISALLOW_COPY_AND_ASSIGN(AwAccessTokenStore);
142 } // namespace
144 std::string AwContentBrowserClient::GetAcceptLangsImpl() {
145 // Start with the currnet locale.
146 std::string langs = base::android::GetDefaultLocale();
148 // If we're not en-US, add in en-US which will be
149 // used with a lower q-value.
150 if (base::StringToLowerASCII(langs) != "en-us") {
151 langs += ",en-US";
153 return langs;
156 AwBrowserContext* AwContentBrowserClient::GetAwBrowserContext() {
157 return AwBrowserContext::GetDefault();
160 AwContentBrowserClient::AwContentBrowserClient(
161 JniDependencyFactory* native_factory)
162 : native_factory_(native_factory) {
163 base::FilePath user_data_dir;
164 if (!PathService::Get(base::DIR_ANDROID_APP_DATA, &user_data_dir)) {
165 NOTREACHED() << "Failed to get app data directory for Android WebView";
167 browser_context_.reset(
168 new AwBrowserContext(user_data_dir, native_factory_));
171 AwContentBrowserClient::~AwContentBrowserClient() {
174 void AwContentBrowserClient::AddCertificate(net::CertificateMimeType cert_type,
175 const void* cert_data,
176 size_t cert_size,
177 int render_process_id,
178 int render_frame_id) {
179 if (cert_size > 0)
180 net::android::StoreCertificate(cert_type, cert_data, cert_size);
183 content::BrowserMainParts* AwContentBrowserClient::CreateBrowserMainParts(
184 const content::MainFunctionParams& parameters) {
185 return new AwBrowserMainParts(browser_context_.get());
188 content::WebContentsViewDelegate*
189 AwContentBrowserClient::GetWebContentsViewDelegate(
190 content::WebContents* web_contents) {
191 return native_factory_->CreateViewDelegate(web_contents);
194 void AwContentBrowserClient::RenderProcessWillLaunch(
195 content::RenderProcessHost* host) {
196 // If WebView becomes multi-process capable, this may be insecure.
197 // More benefit can be derived from the ChildProcessSecurotyPolicy by
198 // deferring the GrantScheme calls until we know that a given child process
199 // really does need that priviledge. Check here to ensure we rethink this
200 // when the time comes. See crbug.com/156062.
201 CHECK(content::RenderProcessHost::run_renderer_in_process());
203 // Grant content: and file: scheme to the whole process, since we impose
204 // per-view access checks.
205 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
206 host->GetID(), android_webview::kContentScheme);
207 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
208 host->GetID(), url::kFileScheme);
210 host->AddFilter(new AwContentsMessageFilter(host->GetID()));
211 host->AddFilter(new cdm::CdmMessageFilterAndroid());
212 host->AddFilter(new AwPrintingMessageFilter(host->GetID()));
215 net::URLRequestContextGetter* AwContentBrowserClient::CreateRequestContext(
216 content::BrowserContext* browser_context,
217 content::ProtocolHandlerMap* protocol_handlers,
218 content::URLRequestInterceptorScopedVector request_interceptors) {
219 DCHECK_EQ(browser_context_.get(), browser_context);
220 return browser_context_->CreateRequestContext(protocol_handlers,
221 request_interceptors.Pass());
224 net::URLRequestContextGetter*
225 AwContentBrowserClient::CreateRequestContextForStoragePartition(
226 content::BrowserContext* browser_context,
227 const base::FilePath& partition_path,
228 bool in_memory,
229 content::ProtocolHandlerMap* protocol_handlers,
230 content::URLRequestInterceptorScopedVector request_interceptors) {
231 DCHECK_EQ(browser_context_.get(), browser_context);
232 // TODO(mkosiba,kinuko): request_interceptors should be hooked up in the
233 // downstream. (crbug.com/350286)
234 return browser_context_->CreateRequestContextForStoragePartition(
235 partition_path, in_memory, protocol_handlers,
236 request_interceptors.Pass());
239 std::string AwContentBrowserClient::GetCanonicalEncodingNameByAliasName(
240 const std::string& alias_name) {
241 return alias_name;
244 void AwContentBrowserClient::AppendExtraCommandLineSwitches(
245 base::CommandLine* command_line,
246 int child_process_id) {
247 NOTREACHED() << "Android WebView does not support multi-process yet";
250 std::string AwContentBrowserClient::GetApplicationLocale() {
251 return base::android::GetDefaultLocale();
254 std::string AwContentBrowserClient::GetAcceptLangs(
255 content::BrowserContext* context) {
256 return GetAcceptLangsImpl();
259 const gfx::ImageSkia* AwContentBrowserClient::GetDefaultFavicon() {
260 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
261 // TODO(boliu): Bundle our own default favicon?
262 return rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON);
265 bool AwContentBrowserClient::AllowAppCache(const GURL& manifest_url,
266 const GURL& first_party,
267 content::ResourceContext* context) {
268 // WebView doesn't have a per-site policy for locally stored data,
269 // instead AppCache can be disabled for individual WebViews.
270 return true;
274 bool AwContentBrowserClient::AllowGetCookie(const GURL& url,
275 const GURL& first_party,
276 const net::CookieList& cookie_list,
277 content::ResourceContext* context,
278 int render_process_id,
279 int render_frame_id) {
280 return AwCookieAccessPolicy::GetInstance()->AllowGetCookie(url,
281 first_party,
282 cookie_list,
283 context,
284 render_process_id,
285 render_frame_id);
288 bool AwContentBrowserClient::AllowSetCookie(const GURL& url,
289 const GURL& first_party,
290 const std::string& cookie_line,
291 content::ResourceContext* context,
292 int render_process_id,
293 int render_frame_id,
294 net::CookieOptions* options) {
295 return AwCookieAccessPolicy::GetInstance()->AllowSetCookie(url,
296 first_party,
297 cookie_line,
298 context,
299 render_process_id,
300 render_frame_id,
301 options);
304 bool AwContentBrowserClient::AllowWorkerDatabase(
305 const GURL& url,
306 const base::string16& name,
307 const base::string16& display_name,
308 unsigned long estimated_size,
309 content::ResourceContext* context,
310 const std::vector<std::pair<int, int> >& render_frames) {
311 // Android WebView does not yet support web workers.
312 return false;
315 void AwContentBrowserClient::AllowWorkerFileSystem(
316 const GURL& url,
317 content::ResourceContext* context,
318 const std::vector<std::pair<int, int> >& render_frames,
319 base::Callback<void(bool)> callback) {
320 // Android WebView does not yet support web workers.
321 callback.Run(false);
324 bool AwContentBrowserClient::AllowWorkerIndexedDB(
325 const GURL& url,
326 const base::string16& name,
327 content::ResourceContext* context,
328 const std::vector<std::pair<int, int> >& render_frames) {
329 // Android WebView does not yet support web workers.
330 return false;
333 content::QuotaPermissionContext*
334 AwContentBrowserClient::CreateQuotaPermissionContext() {
335 return new AwQuotaPermissionContext;
338 void AwContentBrowserClient::AllowCertificateError(
339 int render_process_id,
340 int render_frame_id,
341 int cert_error,
342 const net::SSLInfo& ssl_info,
343 const GURL& request_url,
344 ResourceType resource_type,
345 bool overridable,
346 bool strict_enforcement,
347 bool expired_previous_decision,
348 const base::Callback<void(bool)>& callback,
349 content::CertificateRequestResultType* result) {
350 AwContentsClientBridgeBase* client =
351 AwContentsClientBridgeBase::FromID(render_process_id, render_frame_id);
352 bool cancel_request = true;
353 if (client)
354 client->AllowCertificateError(cert_error,
355 ssl_info.cert.get(),
356 request_url,
357 callback,
358 &cancel_request);
359 if (cancel_request)
360 *result = content::CERTIFICATE_REQUEST_RESULT_TYPE_DENY;
363 void AwContentBrowserClient::SelectClientCertificate(
364 int render_process_id,
365 int render_frame_id,
366 net::SSLCertRequestInfo* cert_request_info,
367 const base::Callback<void(net::X509Certificate*)>& callback) {
368 AwContentsClientBridgeBase* client =
369 AwContentsClientBridgeBase::FromID(render_process_id, render_frame_id);
370 if (client) {
371 client->SelectClientCertificate(cert_request_info, callback);
372 } else {
373 callback.Run(NULL);
377 void AwContentBrowserClient::RequestPermission(
378 content::PermissionType permission,
379 content::WebContents* web_contents,
380 int bridge_id,
381 const GURL& requesting_frame,
382 bool user_gesture,
383 const base::Callback<void(bool)>& result_callback) {
384 int render_process_id = web_contents->GetRenderProcessHost()->GetID();
385 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID();
386 GURL origin = requesting_frame.GetOrigin();
387 AwBrowserPermissionRequestDelegate* delegate =
388 AwBrowserPermissionRequestDelegate::FromID(render_process_id,
389 render_view_id);
390 switch (permission) {
391 case content::PERMISSION_GEOLOCATION:
392 if (!delegate) {
393 DVLOG(0) << "Dropping GeolocationPermission request";
394 result_callback.Run(false);
395 return;
397 delegate->RequestGeolocationPermission(origin, result_callback);
398 break;
399 case content::PERMISSION_PROTECTED_MEDIA_IDENTIFIER:
400 if (!delegate) {
401 DVLOG(0) << "Dropping ProtectedMediaIdentifierPermission request";
402 result_callback.Run(false);
403 return;
405 delegate->RequestProtectedMediaIdentifierPermission(origin,
406 result_callback);
407 break;
408 case content::PERMISSION_MIDI_SYSEX:
409 case content::PERMISSION_NOTIFICATIONS:
410 case content::PERMISSION_PUSH_MESSAGING:
411 NOTIMPLEMENTED() << "RequestPermission not implemented for "
412 << permission;
413 break;
414 case content::PERMISSION_NUM:
415 NOTREACHED() << "Invalid RequestPermission for " << permission;
416 break;
420 void AwContentBrowserClient::CancelPermissionRequest(
421 content::PermissionType permission,
422 content::WebContents* web_contents,
423 int bridge_id,
424 const GURL& origin) {
425 int render_process_id = web_contents->GetRenderProcessHost()->GetID();
426 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID();
427 AwBrowserPermissionRequestDelegate* delegate =
428 AwBrowserPermissionRequestDelegate::FromID(render_process_id,
429 render_view_id);
430 if (!delegate)
431 return;
432 switch (permission) {
433 case content::PERMISSION_GEOLOCATION:
434 delegate->CancelGeolocationPermissionRequests(origin);
435 break;
436 case content::PERMISSION_PROTECTED_MEDIA_IDENTIFIER:
437 delegate->CancelProtectedMediaIdentifierPermissionRequests(origin);
438 break;
439 case content::PERMISSION_MIDI_SYSEX:
440 case content::PERMISSION_NOTIFICATIONS:
441 case content::PERMISSION_PUSH_MESSAGING:
442 NOTIMPLEMENTED() << "CancelPermission not implemented for " << permission;
443 break;
444 case content::PERMISSION_NUM:
445 NOTREACHED() << "Invalid CancelPermission for " << permission;
446 break;
450 bool AwContentBrowserClient::CanCreateWindow(
451 const GURL& opener_url,
452 const GURL& opener_top_level_frame_url,
453 const GURL& source_origin,
454 WindowContainerType container_type,
455 const GURL& target_url,
456 const content::Referrer& referrer,
457 WindowOpenDisposition disposition,
458 const blink::WebWindowFeatures& features,
459 bool user_gesture,
460 bool opener_suppressed,
461 content::ResourceContext* context,
462 int render_process_id,
463 int opener_id,
464 bool* no_javascript_access) {
465 // We unconditionally allow popup windows at this stage and will give
466 // the embedder the opporunity to handle displaying of the popup in
467 // WebContentsDelegate::AddContents (via the
468 // AwContentsClient.onCreateWindow callback).
469 // Note that if the embedder has blocked support for creating popup
470 // windows through AwSettings, then we won't get to this point as
471 // the popup creation will have been blocked at the WebKit level.
472 if (no_javascript_access) {
473 *no_javascript_access = false;
475 return true;
478 void AwContentBrowserClient::ResourceDispatcherHostCreated() {
479 AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated();
482 net::NetLog* AwContentBrowserClient::GetNetLog() {
483 return browser_context_->GetAwURLRequestContext()->GetNetLog();
486 content::AccessTokenStore* AwContentBrowserClient::CreateAccessTokenStore() {
487 return new AwAccessTokenStore();
490 bool AwContentBrowserClient::IsFastShutdownPossible() {
491 NOTREACHED() << "Android WebView is single process, so IsFastShutdownPossible"
492 << " should never be called";
493 return false;
496 void AwContentBrowserClient::ClearCache(content::RenderViewHost* rvh) {
497 RemoveHttpDiskCache(rvh->GetProcess()->GetBrowserContext(),
498 rvh->GetProcess()->GetID());
501 void AwContentBrowserClient::ClearCookies(content::RenderViewHost* rvh) {
502 // TODO(boliu): Implement.
503 NOTIMPLEMENTED();
506 base::FilePath AwContentBrowserClient::GetDefaultDownloadDirectory() {
507 // Android WebView does not currently use the Chromium downloads system.
508 // Download requests are cancelled immedately when recognized; see
509 // AwResourceDispatcherHost::CreateResourceHandlerForDownload. However the
510 // download system still tries to start up and calls this before recognizing
511 // the request has been cancelled.
512 return base::FilePath();
515 std::string AwContentBrowserClient::GetDefaultDownloadName() {
516 NOTREACHED() << "Android WebView does not use chromium downloads";
517 return std::string();
520 void AwContentBrowserClient::DidCreatePpapiPlugin(
521 content::BrowserPpapiHost* browser_host) {
522 NOTREACHED() << "Android WebView does not support plugins";
525 bool AwContentBrowserClient::AllowPepperSocketAPI(
526 content::BrowserContext* browser_context,
527 const GURL& url,
528 bool private_api,
529 const content::SocketPermissionRequest* params) {
530 NOTREACHED() << "Android WebView does not support plugins";
531 return false;
534 void AwContentBrowserClient::OverrideWebkitPrefs(
535 content::RenderViewHost* rvh,
536 content::WebPreferences* web_prefs) {
537 if (!preferences_populater_.get()) {
538 preferences_populater_ = make_scoped_ptr(native_factory_->
539 CreateWebPreferencesPopulater());
541 preferences_populater_->PopulateFor(
542 content::WebContents::FromRenderViewHost(rvh), web_prefs);
545 #if defined(VIDEO_HOLE)
546 content::ExternalVideoSurfaceContainer*
547 AwContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
548 content::WebContents* web_contents) {
549 return native_factory_->CreateExternalVideoSurfaceContainer(web_contents);
551 #endif
553 content::DevToolsManagerDelegate*
554 AwContentBrowserClient::GetDevToolsManagerDelegate() {
555 return new AwDevToolsManagerDelegate();
558 } // namespace android_webview