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_printing_message_filter.h"
13 #include "android_webview/browser/aw_quota_permission_context.h"
14 #include "android_webview/browser/aw_web_preferences_populater.h"
15 #include "android_webview/browser/jni_dependency_factory.h"
16 #include "android_webview/browser/net/aw_url_request_context_getter.h"
17 #include "android_webview/browser/net_disk_cache_remover.h"
18 #include "android_webview/browser/renderer_host/aw_resource_dispatcher_host_delegate.h"
19 #include "android_webview/common/render_view_messages.h"
20 #include "android_webview/common/url_constants.h"
21 #include "base/android/locale_utils.h"
22 #include "base/base_paths_android.h"
23 #include "base/path_service.h"
24 #include "components/cdm/browser/cdm_message_filter_android.h"
25 #include "content/public/browser/access_token_store.h"
26 #include "content/public/browser/browser_message_filter.h"
27 #include "content/public/browser/child_process_security_policy.h"
28 #include "content/public/browser/client_certificate_delegate.h"
29 #include "content/public/browser/render_frame_host.h"
30 #include "content/public/browser/render_process_host.h"
31 #include "content/public/browser/render_view_host.h"
32 #include "content/public/browser/web_contents.h"
33 #include "content/public/common/url_constants.h"
34 #include "content/public/common/web_preferences.h"
35 #include "net/android/network_library.h"
36 #include "net/ssl/ssl_cert_request_info.h"
37 #include "net/ssl/ssl_info.h"
38 #include "ui/base/resource/resource_bundle.h"
39 #include "ui/resources/grit/ui_resources.h"
41 using content::ResourceType
;
43 namespace android_webview
{
46 // TODO(sgurun) move this to its own file.
47 // This class filters out incoming aw_contents related IPC messages for the
48 // renderer process on the IPC thread.
49 class AwContentsMessageFilter
: public content::BrowserMessageFilter
{
51 explicit AwContentsMessageFilter(int process_id
);
53 // BrowserMessageFilter methods.
54 bool OnMessageReceived(const IPC::Message
& message
) override
;
56 void OnSubFrameCreated(int parent_render_frame_id
, int child_render_frame_id
);
59 ~AwContentsMessageFilter() override
;
63 DISALLOW_COPY_AND_ASSIGN(AwContentsMessageFilter
);
66 AwContentsMessageFilter::AwContentsMessageFilter(int process_id
)
67 : BrowserMessageFilter(AndroidWebViewMsgStart
),
68 process_id_(process_id
) {
71 AwContentsMessageFilter::~AwContentsMessageFilter() {
74 bool AwContentsMessageFilter::OnMessageReceived(const IPC::Message
& message
) {
76 IPC_BEGIN_MESSAGE_MAP(AwContentsMessageFilter
, message
)
77 IPC_MESSAGE_HANDLER(AwViewHostMsg_SubFrameCreated
, OnSubFrameCreated
)
78 IPC_MESSAGE_UNHANDLED(handled
= false)
83 void AwContentsMessageFilter::OnSubFrameCreated(int parent_render_frame_id
,
84 int child_render_frame_id
) {
85 AwContentsIoThreadClient::SubFrameCreated(
86 process_id_
, parent_render_frame_id
, child_render_frame_id
);
89 class AwAccessTokenStore
: public content::AccessTokenStore
{
91 AwAccessTokenStore() { }
93 // content::AccessTokenStore implementation
94 void LoadAccessTokens(const LoadAccessTokensCallbackType
& request
) override
{
95 AccessTokenStore::AccessTokenSet access_token_set
;
96 // AccessTokenSet and net::URLRequestContextGetter not used on Android,
97 // but Run needs to be called to finish the geolocation setup.
98 request
.Run(access_token_set
, NULL
);
100 void SaveAccessToken(const GURL
& server_url
,
101 const base::string16
& access_token
) override
{}
104 ~AwAccessTokenStore() override
{}
106 DISALLOW_COPY_AND_ASSIGN(AwAccessTokenStore
);
109 } // anonymous namespace
111 std::string
AwContentBrowserClient::GetAcceptLangsImpl() {
112 // Start with the currnet locale.
113 std::string langs
= base::android::GetDefaultLocale();
115 // If we're not en-US, add in en-US which will be
116 // used with a lower q-value.
117 if (base::StringToLowerASCII(langs
) != "en-us") {
123 AwBrowserContext
* AwContentBrowserClient::GetAwBrowserContext() {
124 return AwBrowserContext::GetDefault();
127 AwContentBrowserClient::AwContentBrowserClient(
128 JniDependencyFactory
* native_factory
)
129 : native_factory_(native_factory
) {
130 base::FilePath user_data_dir
;
131 if (!PathService::Get(base::DIR_ANDROID_APP_DATA
, &user_data_dir
)) {
132 NOTREACHED() << "Failed to get app data directory for Android WebView";
134 browser_context_
.reset(
135 new AwBrowserContext(user_data_dir
, native_factory_
));
138 AwContentBrowserClient::~AwContentBrowserClient() {
141 void AwContentBrowserClient::AddCertificate(net::CertificateMimeType cert_type
,
142 const void* cert_data
,
144 int render_process_id
,
145 int render_frame_id
) {
147 net::android::StoreCertificate(cert_type
, cert_data
, cert_size
);
150 content::BrowserMainParts
* AwContentBrowserClient::CreateBrowserMainParts(
151 const content::MainFunctionParams
& parameters
) {
152 return new AwBrowserMainParts(browser_context_
.get());
155 content::WebContentsViewDelegate
*
156 AwContentBrowserClient::GetWebContentsViewDelegate(
157 content::WebContents
* web_contents
) {
158 return native_factory_
->CreateViewDelegate(web_contents
);
161 void AwContentBrowserClient::RenderProcessWillLaunch(
162 content::RenderProcessHost
* host
) {
163 // If WebView becomes multi-process capable, this may be insecure.
164 // More benefit can be derived from the ChildProcessSecurotyPolicy by
165 // deferring the GrantScheme calls until we know that a given child process
166 // really does need that priviledge. Check here to ensure we rethink this
167 // when the time comes. See crbug.com/156062.
168 CHECK(content::RenderProcessHost::run_renderer_in_process());
170 // Grant content: and file: scheme to the whole process, since we impose
171 // per-view access checks.
172 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
173 host
->GetID(), android_webview::kContentScheme
);
174 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
175 host
->GetID(), url::kFileScheme
);
177 host
->AddFilter(new AwContentsMessageFilter(host
->GetID()));
178 host
->AddFilter(new cdm::CdmMessageFilterAndroid());
179 host
->AddFilter(new AwPrintingMessageFilter(host
->GetID()));
182 net::URLRequestContextGetter
* AwContentBrowserClient::CreateRequestContext(
183 content::BrowserContext
* browser_context
,
184 content::ProtocolHandlerMap
* protocol_handlers
,
185 content::URLRequestInterceptorScopedVector request_interceptors
) {
186 DCHECK_EQ(browser_context_
.get(), browser_context
);
187 return browser_context_
->CreateRequestContext(protocol_handlers
,
188 request_interceptors
.Pass());
191 net::URLRequestContextGetter
*
192 AwContentBrowserClient::CreateRequestContextForStoragePartition(
193 content::BrowserContext
* browser_context
,
194 const base::FilePath
& partition_path
,
196 content::ProtocolHandlerMap
* protocol_handlers
,
197 content::URLRequestInterceptorScopedVector request_interceptors
) {
198 DCHECK_EQ(browser_context_
.get(), browser_context
);
199 // TODO(mkosiba,kinuko): request_interceptors should be hooked up in the
200 // downstream. (crbug.com/350286)
201 return browser_context_
->CreateRequestContextForStoragePartition(
202 partition_path
, in_memory
, protocol_handlers
,
203 request_interceptors
.Pass());
206 std::string
AwContentBrowserClient::GetCanonicalEncodingNameByAliasName(
207 const std::string
& alias_name
) {
211 void AwContentBrowserClient::AppendExtraCommandLineSwitches(
212 base::CommandLine
* command_line
,
213 int child_process_id
) {
214 NOTREACHED() << "Android WebView does not support multi-process yet";
217 std::string
AwContentBrowserClient::GetApplicationLocale() {
218 return base::android::GetDefaultLocale();
221 std::string
AwContentBrowserClient::GetAcceptLangs(
222 content::BrowserContext
* context
) {
223 return GetAcceptLangsImpl();
226 const gfx::ImageSkia
* AwContentBrowserClient::GetDefaultFavicon() {
227 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
228 // TODO(boliu): Bundle our own default favicon?
229 return rb
.GetImageSkiaNamed(IDR_DEFAULT_FAVICON
);
232 bool AwContentBrowserClient::AllowAppCache(const GURL
& manifest_url
,
233 const GURL
& first_party
,
234 content::ResourceContext
* context
) {
235 // WebView doesn't have a per-site policy for locally stored data,
236 // instead AppCache can be disabled for individual WebViews.
241 bool AwContentBrowserClient::AllowGetCookie(const GURL
& url
,
242 const GURL
& first_party
,
243 const net::CookieList
& cookie_list
,
244 content::ResourceContext
* context
,
245 int render_process_id
,
246 int render_frame_id
) {
247 return AwCookieAccessPolicy::GetInstance()->AllowGetCookie(url
,
255 bool AwContentBrowserClient::AllowSetCookie(const GURL
& url
,
256 const GURL
& first_party
,
257 const std::string
& cookie_line
,
258 content::ResourceContext
* context
,
259 int render_process_id
,
261 net::CookieOptions
* options
) {
262 return AwCookieAccessPolicy::GetInstance()->AllowSetCookie(url
,
271 bool AwContentBrowserClient::AllowWorkerDatabase(
273 const base::string16
& name
,
274 const base::string16
& display_name
,
275 unsigned long estimated_size
,
276 content::ResourceContext
* context
,
277 const std::vector
<std::pair
<int, int> >& render_frames
) {
278 // Android WebView does not yet support web workers.
282 void AwContentBrowserClient::AllowWorkerFileSystem(
284 content::ResourceContext
* context
,
285 const std::vector
<std::pair
<int, int> >& render_frames
,
286 base::Callback
<void(bool)> callback
) {
287 // Android WebView does not yet support web workers.
291 bool AwContentBrowserClient::AllowWorkerIndexedDB(
293 const base::string16
& name
,
294 content::ResourceContext
* context
,
295 const std::vector
<std::pair
<int, int> >& render_frames
) {
296 // Android WebView does not yet support web workers.
300 content::QuotaPermissionContext
*
301 AwContentBrowserClient::CreateQuotaPermissionContext() {
302 return new AwQuotaPermissionContext
;
305 void AwContentBrowserClient::AllowCertificateError(
306 int render_process_id
,
309 const net::SSLInfo
& ssl_info
,
310 const GURL
& request_url
,
311 ResourceType resource_type
,
313 bool strict_enforcement
,
314 bool expired_previous_decision
,
315 const base::Callback
<void(bool)>& callback
,
316 content::CertificateRequestResultType
* result
) {
317 AwContentsClientBridgeBase
* client
=
318 AwContentsClientBridgeBase::FromID(render_process_id
, render_frame_id
);
319 bool cancel_request
= true;
321 client
->AllowCertificateError(cert_error
,
327 *result
= content::CERTIFICATE_REQUEST_RESULT_TYPE_DENY
;
330 void AwContentBrowserClient::SelectClientCertificate(
331 content::WebContents
* web_contents
,
332 net::SSLCertRequestInfo
* cert_request_info
,
333 scoped_ptr
<content::ClientCertificateDelegate
> delegate
) {
334 AwContentsClientBridgeBase
* client
=
335 AwContentsClientBridgeBase::FromWebContents(web_contents
);
337 client
->SelectClientCertificate(cert_request_info
, delegate
.Pass());
340 bool AwContentBrowserClient::CanCreateWindow(
341 const GURL
& opener_url
,
342 const GURL
& opener_top_level_frame_url
,
343 const GURL
& source_origin
,
344 WindowContainerType container_type
,
345 const GURL
& target_url
,
346 const content::Referrer
& referrer
,
347 WindowOpenDisposition disposition
,
348 const blink::WebWindowFeatures
& features
,
350 bool opener_suppressed
,
351 content::ResourceContext
* context
,
352 int render_process_id
,
353 int opener_render_view_id
,
354 int opener_render_frame_id
,
355 bool* no_javascript_access
) {
356 // We unconditionally allow popup windows at this stage and will give
357 // the embedder the opporunity to handle displaying of the popup in
358 // WebContentsDelegate::AddContents (via the
359 // AwContentsClient.onCreateWindow callback).
360 // Note that if the embedder has blocked support for creating popup
361 // windows through AwSettings, then we won't get to this point as
362 // the popup creation will have been blocked at the WebKit level.
363 if (no_javascript_access
) {
364 *no_javascript_access
= false;
369 void AwContentBrowserClient::ResourceDispatcherHostCreated() {
370 AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated();
373 net::NetLog
* AwContentBrowserClient::GetNetLog() {
374 return browser_context_
->GetAwURLRequestContext()->GetNetLog();
377 content::AccessTokenStore
* AwContentBrowserClient::CreateAccessTokenStore() {
378 return new AwAccessTokenStore();
381 bool AwContentBrowserClient::IsFastShutdownPossible() {
382 NOTREACHED() << "Android WebView is single process, so IsFastShutdownPossible"
383 << " should never be called";
387 void AwContentBrowserClient::ClearCache(content::RenderFrameHost
* rfh
) {
388 RemoveHttpDiskCache(rfh
->GetProcess()->GetBrowserContext(),
389 rfh
->GetProcess()->GetID());
392 void AwContentBrowserClient::ClearCookies(content::RenderFrameHost
* rfh
) {
393 // TODO(boliu): Implement.
397 base::FilePath
AwContentBrowserClient::GetDefaultDownloadDirectory() {
398 // Android WebView does not currently use the Chromium downloads system.
399 // Download requests are cancelled immedately when recognized; see
400 // AwResourceDispatcherHost::CreateResourceHandlerForDownload. However the
401 // download system still tries to start up and calls this before recognizing
402 // the request has been cancelled.
403 return base::FilePath();
406 std::string
AwContentBrowserClient::GetDefaultDownloadName() {
407 NOTREACHED() << "Android WebView does not use chromium downloads";
408 return std::string();
411 void AwContentBrowserClient::DidCreatePpapiPlugin(
412 content::BrowserPpapiHost
* browser_host
) {
413 NOTREACHED() << "Android WebView does not support plugins";
416 bool AwContentBrowserClient::AllowPepperSocketAPI(
417 content::BrowserContext
* browser_context
,
420 const content::SocketPermissionRequest
* params
) {
421 NOTREACHED() << "Android WebView does not support plugins";
425 void AwContentBrowserClient::OverrideWebkitPrefs(
426 content::RenderViewHost
* rvh
,
427 content::WebPreferences
* web_prefs
) {
428 if (!preferences_populater_
.get()) {
429 preferences_populater_
= make_scoped_ptr(native_factory_
->
430 CreateWebPreferencesPopulater());
432 preferences_populater_
->PopulateFor(
433 content::WebContents::FromRenderViewHost(rvh
), web_prefs
);
436 #if defined(VIDEO_HOLE)
437 content::ExternalVideoSurfaceContainer
*
438 AwContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
439 content::WebContents
* web_contents
) {
440 return native_factory_
->CreateExternalVideoSurfaceContainer(web_contents
);
444 } // namespace android_webview