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_contents_client_bridge_base.h"
10 #include "android_webview/browser/aw_contents_io_thread_client.h"
11 #include "android_webview/browser/aw_cookie_access_policy.h"
12 #include "android_webview/browser/aw_dev_tools_manager_delegate.h"
13 #include "android_webview/browser/aw_printing_message_filter.h"
14 #include "android_webview/browser/aw_quota_permission_context.h"
15 #include "android_webview/browser/aw_web_preferences_populater.h"
16 #include "android_webview/browser/jni_dependency_factory.h"
17 #include "android_webview/browser/net/aw_url_request_context_getter.h"
18 #include "android_webview/browser/net_disk_cache_remover.h"
19 #include "android_webview/browser/renderer_host/aw_resource_dispatcher_host_delegate.h"
20 #include "android_webview/common/render_view_messages.h"
21 #include "android_webview/common/url_constants.h"
22 #include "base/android/locale_utils.h"
23 #include "base/base_paths_android.h"
24 #include "base/path_service.h"
25 #include "components/cdm/browser/cdm_message_filter_android.h"
26 #include "content/public/browser/access_token_store.h"
27 #include "content/public/browser/browser_message_filter.h"
28 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/child_process_security_policy.h"
30 #include "content/public/browser/client_certificate_delegate.h"
31 #include "content/public/browser/render_frame_host.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
{
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
{
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
);
67 ~AwContentsMessageFilter() override
;
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
) {
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)
100 void AwContentsMessageFilter::OnShouldOverrideUrlLoading(
102 const base::string16
& url
,
103 bool* ignore_navigation
) {
104 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
105 *ignore_navigation
= false;
106 AwContentsClientBridgeBase
* client
=
107 AwContentsClientBridgeBase::FromID(process_id_
, render_frame_id
);
109 *ignore_navigation
= client
->ShouldOverrideUrlLoading(url
);
111 LOG(WARNING
) << "Failed to find the associated render view host for 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
{
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
{}
137 ~AwAccessTokenStore() override
{}
139 DISALLOW_COPY_AND_ASSIGN(AwAccessTokenStore
);
142 } // anonymous 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") {
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
,
177 int render_process_id
,
178 int render_frame_id
) {
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
,
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
) {
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.
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
,
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
,
294 net::CookieOptions
* options
) {
295 return AwCookieAccessPolicy::GetInstance()->AllowSetCookie(url
,
304 bool AwContentBrowserClient::AllowWorkerDatabase(
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.
315 void AwContentBrowserClient::AllowWorkerFileSystem(
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.
324 bool AwContentBrowserClient::AllowWorkerIndexedDB(
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.
333 content::QuotaPermissionContext
*
334 AwContentBrowserClient::CreateQuotaPermissionContext() {
335 return new AwQuotaPermissionContext
;
338 void AwContentBrowserClient::AllowCertificateError(
339 int render_process_id
,
342 const net::SSLInfo
& ssl_info
,
343 const GURL
& request_url
,
344 ResourceType resource_type
,
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;
354 client
->AllowCertificateError(cert_error
,
360 *result
= content::CERTIFICATE_REQUEST_RESULT_TYPE_DENY
;
363 void AwContentBrowserClient::SelectClientCertificate(
364 content::WebContents
* web_contents
,
365 net::SSLCertRequestInfo
* cert_request_info
,
366 scoped_ptr
<content::ClientCertificateDelegate
> delegate
) {
367 AwContentsClientBridgeBase
* client
=
368 AwContentsClientBridgeBase::FromWebContents(web_contents
);
370 client
->SelectClientCertificate(cert_request_info
, delegate
.Pass());
373 bool AwContentBrowserClient::CanCreateWindow(
374 const GURL
& opener_url
,
375 const GURL
& opener_top_level_frame_url
,
376 const GURL
& source_origin
,
377 WindowContainerType container_type
,
378 const GURL
& target_url
,
379 const content::Referrer
& referrer
,
380 WindowOpenDisposition disposition
,
381 const blink::WebWindowFeatures
& features
,
383 bool opener_suppressed
,
384 content::ResourceContext
* context
,
385 int render_process_id
,
387 bool* no_javascript_access
) {
388 // We unconditionally allow popup windows at this stage and will give
389 // the embedder the opporunity to handle displaying of the popup in
390 // WebContentsDelegate::AddContents (via the
391 // AwContentsClient.onCreateWindow callback).
392 // Note that if the embedder has blocked support for creating popup
393 // windows through AwSettings, then we won't get to this point as
394 // the popup creation will have been blocked at the WebKit level.
395 if (no_javascript_access
) {
396 *no_javascript_access
= false;
401 void AwContentBrowserClient::ResourceDispatcherHostCreated() {
402 AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated();
405 net::NetLog
* AwContentBrowserClient::GetNetLog() {
406 return browser_context_
->GetAwURLRequestContext()->GetNetLog();
409 content::AccessTokenStore
* AwContentBrowserClient::CreateAccessTokenStore() {
410 return new AwAccessTokenStore();
413 bool AwContentBrowserClient::IsFastShutdownPossible() {
414 NOTREACHED() << "Android WebView is single process, so IsFastShutdownPossible"
415 << " should never be called";
419 void AwContentBrowserClient::ClearCache(content::RenderFrameHost
* rfh
) {
420 RemoveHttpDiskCache(rfh
->GetProcess()->GetBrowserContext(),
421 rfh
->GetProcess()->GetID());
424 void AwContentBrowserClient::ClearCookies(content::RenderFrameHost
* rfh
) {
425 // TODO(boliu): Implement.
429 base::FilePath
AwContentBrowserClient::GetDefaultDownloadDirectory() {
430 // Android WebView does not currently use the Chromium downloads system.
431 // Download requests are cancelled immedately when recognized; see
432 // AwResourceDispatcherHost::CreateResourceHandlerForDownload. However the
433 // download system still tries to start up and calls this before recognizing
434 // the request has been cancelled.
435 return base::FilePath();
438 std::string
AwContentBrowserClient::GetDefaultDownloadName() {
439 NOTREACHED() << "Android WebView does not use chromium downloads";
440 return std::string();
443 void AwContentBrowserClient::DidCreatePpapiPlugin(
444 content::BrowserPpapiHost
* browser_host
) {
445 NOTREACHED() << "Android WebView does not support plugins";
448 bool AwContentBrowserClient::AllowPepperSocketAPI(
449 content::BrowserContext
* browser_context
,
452 const content::SocketPermissionRequest
* params
) {
453 NOTREACHED() << "Android WebView does not support plugins";
457 void AwContentBrowserClient::OverrideWebkitPrefs(
458 content::RenderViewHost
* rvh
,
459 content::WebPreferences
* web_prefs
) {
460 if (!preferences_populater_
.get()) {
461 preferences_populater_
= make_scoped_ptr(native_factory_
->
462 CreateWebPreferencesPopulater());
464 preferences_populater_
->PopulateFor(
465 content::WebContents::FromRenderViewHost(rvh
), web_prefs
);
468 #if defined(VIDEO_HOLE)
469 content::ExternalVideoSurfaceContainer
*
470 AwContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
471 content::WebContents
* web_contents
) {
472 return native_factory_
->CreateExternalVideoSurfaceContainer(web_contents
);
476 content::DevToolsManagerDelegate
*
477 AwContentBrowserClient::GetDevToolsManagerDelegate() {
478 return new AwDevToolsManagerDelegate();
481 } // namespace android_webview