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/browser_thread.h"
28 #include "content/public/browser/child_process_security_policy.h"
29 #include "content/public/browser/client_certificate_delegate.h"
30 #include "content/public/browser/render_frame_host.h"
31 #include "content/public/browser/render_process_host.h"
32 #include "content/public/browser/render_view_host.h"
33 #include "content/public/browser/web_contents.h"
34 #include "content/public/common/url_constants.h"
35 #include "content/public/common/web_preferences.h"
36 #include "net/android/network_library.h"
37 #include "net/ssl/ssl_cert_request_info.h"
38 #include "net/ssl/ssl_info.h"
39 #include "ui/base/resource/resource_bundle.h"
40 #include "ui/resources/grit/ui_resources.h"
42 using content::BrowserThread
;
43 using content::ResourceType
;
45 namespace android_webview
{
48 // TODO(sgurun) move this to its own file.
49 // This class filters out incoming aw_contents related IPC messages for the
50 // renderer process on the IPC thread.
51 class AwContentsMessageFilter
: public content::BrowserMessageFilter
{
53 explicit AwContentsMessageFilter(int process_id
);
55 // BrowserMessageFilter methods.
56 void OverrideThreadForMessage(const IPC::Message
& message
,
57 BrowserThread::ID
* thread
) override
;
58 bool OnMessageReceived(const IPC::Message
& message
) override
;
60 void OnShouldOverrideUrlLoading(int routing_id
,
61 const base::string16
& url
,
62 bool* ignore_navigation
);
63 void OnSubFrameCreated(int parent_render_frame_id
, int child_render_frame_id
);
66 ~AwContentsMessageFilter() override
;
70 DISALLOW_COPY_AND_ASSIGN(AwContentsMessageFilter
);
73 AwContentsMessageFilter::AwContentsMessageFilter(int process_id
)
74 : BrowserMessageFilter(AndroidWebViewMsgStart
),
75 process_id_(process_id
) {
78 AwContentsMessageFilter::~AwContentsMessageFilter() {
81 void AwContentsMessageFilter::OverrideThreadForMessage(
82 const IPC::Message
& message
, BrowserThread::ID
* thread
) {
83 if (message
.type() == AwViewHostMsg_ShouldOverrideUrlLoading::ID
) {
84 *thread
= BrowserThread::UI
;
88 bool AwContentsMessageFilter::OnMessageReceived(const IPC::Message
& message
) {
90 IPC_BEGIN_MESSAGE_MAP(AwContentsMessageFilter
, message
)
91 IPC_MESSAGE_HANDLER(AwViewHostMsg_ShouldOverrideUrlLoading
,
92 OnShouldOverrideUrlLoading
)
93 IPC_MESSAGE_HANDLER(AwViewHostMsg_SubFrameCreated
, OnSubFrameCreated
)
94 IPC_MESSAGE_UNHANDLED(handled
= false)
99 void AwContentsMessageFilter::OnShouldOverrideUrlLoading(
101 const base::string16
& url
,
102 bool* ignore_navigation
) {
103 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
104 *ignore_navigation
= false;
105 AwContentsClientBridgeBase
* client
=
106 AwContentsClientBridgeBase::FromID(process_id_
, render_frame_id
);
108 *ignore_navigation
= client
->ShouldOverrideUrlLoading(url
);
110 LOG(WARNING
) << "Failed to find the associated render view host for url: "
115 void AwContentsMessageFilter::OnSubFrameCreated(int parent_render_frame_id
,
116 int child_render_frame_id
) {
117 AwContentsIoThreadClient::SubFrameCreated(
118 process_id_
, parent_render_frame_id
, child_render_frame_id
);
121 class AwAccessTokenStore
: public content::AccessTokenStore
{
123 AwAccessTokenStore() { }
125 // content::AccessTokenStore implementation
126 void LoadAccessTokens(const LoadAccessTokensCallbackType
& request
) override
{
127 AccessTokenStore::AccessTokenSet access_token_set
;
128 // AccessTokenSet and net::URLRequestContextGetter not used on Android,
129 // but Run needs to be called to finish the geolocation setup.
130 request
.Run(access_token_set
, NULL
);
132 void SaveAccessToken(const GURL
& server_url
,
133 const base::string16
& access_token
) override
{}
136 ~AwAccessTokenStore() override
{}
138 DISALLOW_COPY_AND_ASSIGN(AwAccessTokenStore
);
141 } // anonymous namespace
143 std::string
AwContentBrowserClient::GetAcceptLangsImpl() {
144 // Start with the currnet locale.
145 std::string langs
= base::android::GetDefaultLocale();
147 // If we're not en-US, add in en-US which will be
148 // used with a lower q-value.
149 if (base::StringToLowerASCII(langs
) != "en-us") {
155 AwBrowserContext
* AwContentBrowserClient::GetAwBrowserContext() {
156 return AwBrowserContext::GetDefault();
159 AwContentBrowserClient::AwContentBrowserClient(
160 JniDependencyFactory
* native_factory
)
161 : native_factory_(native_factory
) {
162 base::FilePath user_data_dir
;
163 if (!PathService::Get(base::DIR_ANDROID_APP_DATA
, &user_data_dir
)) {
164 NOTREACHED() << "Failed to get app data directory for Android WebView";
166 browser_context_
.reset(
167 new AwBrowserContext(user_data_dir
, native_factory_
));
170 AwContentBrowserClient::~AwContentBrowserClient() {
173 void AwContentBrowserClient::AddCertificate(net::CertificateMimeType cert_type
,
174 const void* cert_data
,
176 int render_process_id
,
177 int render_frame_id
) {
179 net::android::StoreCertificate(cert_type
, cert_data
, cert_size
);
182 content::BrowserMainParts
* AwContentBrowserClient::CreateBrowserMainParts(
183 const content::MainFunctionParams
& parameters
) {
184 return new AwBrowserMainParts(browser_context_
.get());
187 content::WebContentsViewDelegate
*
188 AwContentBrowserClient::GetWebContentsViewDelegate(
189 content::WebContents
* web_contents
) {
190 return native_factory_
->CreateViewDelegate(web_contents
);
193 void AwContentBrowserClient::RenderProcessWillLaunch(
194 content::RenderProcessHost
* host
) {
195 // If WebView becomes multi-process capable, this may be insecure.
196 // More benefit can be derived from the ChildProcessSecurotyPolicy by
197 // deferring the GrantScheme calls until we know that a given child process
198 // really does need that priviledge. Check here to ensure we rethink this
199 // when the time comes. See crbug.com/156062.
200 CHECK(content::RenderProcessHost::run_renderer_in_process());
202 // Grant content: and file: scheme to the whole process, since we impose
203 // per-view access checks.
204 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
205 host
->GetID(), android_webview::kContentScheme
);
206 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
207 host
->GetID(), url::kFileScheme
);
209 host
->AddFilter(new AwContentsMessageFilter(host
->GetID()));
210 host
->AddFilter(new cdm::CdmMessageFilterAndroid());
211 host
->AddFilter(new AwPrintingMessageFilter(host
->GetID()));
214 net::URLRequestContextGetter
* AwContentBrowserClient::CreateRequestContext(
215 content::BrowserContext
* browser_context
,
216 content::ProtocolHandlerMap
* protocol_handlers
,
217 content::URLRequestInterceptorScopedVector request_interceptors
) {
218 DCHECK_EQ(browser_context_
.get(), browser_context
);
219 return browser_context_
->CreateRequestContext(protocol_handlers
,
220 request_interceptors
.Pass());
223 net::URLRequestContextGetter
*
224 AwContentBrowserClient::CreateRequestContextForStoragePartition(
225 content::BrowserContext
* browser_context
,
226 const base::FilePath
& partition_path
,
228 content::ProtocolHandlerMap
* protocol_handlers
,
229 content::URLRequestInterceptorScopedVector request_interceptors
) {
230 DCHECK_EQ(browser_context_
.get(), browser_context
);
231 // TODO(mkosiba,kinuko): request_interceptors should be hooked up in the
232 // downstream. (crbug.com/350286)
233 return browser_context_
->CreateRequestContextForStoragePartition(
234 partition_path
, in_memory
, protocol_handlers
,
235 request_interceptors
.Pass());
238 std::string
AwContentBrowserClient::GetCanonicalEncodingNameByAliasName(
239 const std::string
& alias_name
) {
243 void AwContentBrowserClient::AppendExtraCommandLineSwitches(
244 base::CommandLine
* command_line
,
245 int child_process_id
) {
246 NOTREACHED() << "Android WebView does not support multi-process yet";
249 std::string
AwContentBrowserClient::GetApplicationLocale() {
250 return base::android::GetDefaultLocale();
253 std::string
AwContentBrowserClient::GetAcceptLangs(
254 content::BrowserContext
* context
) {
255 return GetAcceptLangsImpl();
258 const gfx::ImageSkia
* AwContentBrowserClient::GetDefaultFavicon() {
259 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
260 // TODO(boliu): Bundle our own default favicon?
261 return rb
.GetImageSkiaNamed(IDR_DEFAULT_FAVICON
);
264 bool AwContentBrowserClient::AllowAppCache(const GURL
& manifest_url
,
265 const GURL
& first_party
,
266 content::ResourceContext
* context
) {
267 // WebView doesn't have a per-site policy for locally stored data,
268 // instead AppCache can be disabled for individual WebViews.
273 bool AwContentBrowserClient::AllowGetCookie(const GURL
& url
,
274 const GURL
& first_party
,
275 const net::CookieList
& cookie_list
,
276 content::ResourceContext
* context
,
277 int render_process_id
,
278 int render_frame_id
) {
279 return AwCookieAccessPolicy::GetInstance()->AllowGetCookie(url
,
287 bool AwContentBrowserClient::AllowSetCookie(const GURL
& url
,
288 const GURL
& first_party
,
289 const std::string
& cookie_line
,
290 content::ResourceContext
* context
,
291 int render_process_id
,
293 net::CookieOptions
* options
) {
294 return AwCookieAccessPolicy::GetInstance()->AllowSetCookie(url
,
303 bool AwContentBrowserClient::AllowWorkerDatabase(
305 const base::string16
& name
,
306 const base::string16
& display_name
,
307 unsigned long estimated_size
,
308 content::ResourceContext
* context
,
309 const std::vector
<std::pair
<int, int> >& render_frames
) {
310 // Android WebView does not yet support web workers.
314 void AwContentBrowserClient::AllowWorkerFileSystem(
316 content::ResourceContext
* context
,
317 const std::vector
<std::pair
<int, int> >& render_frames
,
318 base::Callback
<void(bool)> callback
) {
319 // Android WebView does not yet support web workers.
323 bool AwContentBrowserClient::AllowWorkerIndexedDB(
325 const base::string16
& name
,
326 content::ResourceContext
* context
,
327 const std::vector
<std::pair
<int, int> >& render_frames
) {
328 // Android WebView does not yet support web workers.
332 content::QuotaPermissionContext
*
333 AwContentBrowserClient::CreateQuotaPermissionContext() {
334 return new AwQuotaPermissionContext
;
337 void AwContentBrowserClient::AllowCertificateError(
338 int render_process_id
,
341 const net::SSLInfo
& ssl_info
,
342 const GURL
& request_url
,
343 ResourceType resource_type
,
345 bool strict_enforcement
,
346 bool expired_previous_decision
,
347 const base::Callback
<void(bool)>& callback
,
348 content::CertificateRequestResultType
* result
) {
349 AwContentsClientBridgeBase
* client
=
350 AwContentsClientBridgeBase::FromID(render_process_id
, render_frame_id
);
351 bool cancel_request
= true;
353 client
->AllowCertificateError(cert_error
,
359 *result
= content::CERTIFICATE_REQUEST_RESULT_TYPE_DENY
;
362 void AwContentBrowserClient::SelectClientCertificate(
363 content::WebContents
* web_contents
,
364 net::SSLCertRequestInfo
* cert_request_info
,
365 scoped_ptr
<content::ClientCertificateDelegate
> delegate
) {
366 AwContentsClientBridgeBase
* client
=
367 AwContentsClientBridgeBase::FromWebContents(web_contents
);
369 client
->SelectClientCertificate(cert_request_info
, delegate
.Pass());
372 bool AwContentBrowserClient::CanCreateWindow(
373 const GURL
& opener_url
,
374 const GURL
& opener_top_level_frame_url
,
375 const GURL
& source_origin
,
376 WindowContainerType container_type
,
377 const GURL
& target_url
,
378 const content::Referrer
& referrer
,
379 WindowOpenDisposition disposition
,
380 const blink::WebWindowFeatures
& features
,
382 bool opener_suppressed
,
383 content::ResourceContext
* context
,
384 int render_process_id
,
386 bool* no_javascript_access
) {
387 // We unconditionally allow popup windows at this stage and will give
388 // the embedder the opporunity to handle displaying of the popup in
389 // WebContentsDelegate::AddContents (via the
390 // AwContentsClient.onCreateWindow callback).
391 // Note that if the embedder has blocked support for creating popup
392 // windows through AwSettings, then we won't get to this point as
393 // the popup creation will have been blocked at the WebKit level.
394 if (no_javascript_access
) {
395 *no_javascript_access
= false;
400 void AwContentBrowserClient::ResourceDispatcherHostCreated() {
401 AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated();
404 net::NetLog
* AwContentBrowserClient::GetNetLog() {
405 return browser_context_
->GetAwURLRequestContext()->GetNetLog();
408 content::AccessTokenStore
* AwContentBrowserClient::CreateAccessTokenStore() {
409 return new AwAccessTokenStore();
412 bool AwContentBrowserClient::IsFastShutdownPossible() {
413 NOTREACHED() << "Android WebView is single process, so IsFastShutdownPossible"
414 << " should never be called";
418 void AwContentBrowserClient::ClearCache(content::RenderFrameHost
* rfh
) {
419 RemoveHttpDiskCache(rfh
->GetProcess()->GetBrowserContext(),
420 rfh
->GetProcess()->GetID());
423 void AwContentBrowserClient::ClearCookies(content::RenderFrameHost
* rfh
) {
424 // TODO(boliu): Implement.
428 base::FilePath
AwContentBrowserClient::GetDefaultDownloadDirectory() {
429 // Android WebView does not currently use the Chromium downloads system.
430 // Download requests are cancelled immedately when recognized; see
431 // AwResourceDispatcherHost::CreateResourceHandlerForDownload. However the
432 // download system still tries to start up and calls this before recognizing
433 // the request has been cancelled.
434 return base::FilePath();
437 std::string
AwContentBrowserClient::GetDefaultDownloadName() {
438 NOTREACHED() << "Android WebView does not use chromium downloads";
439 return std::string();
442 void AwContentBrowserClient::DidCreatePpapiPlugin(
443 content::BrowserPpapiHost
* browser_host
) {
444 NOTREACHED() << "Android WebView does not support plugins";
447 bool AwContentBrowserClient::AllowPepperSocketAPI(
448 content::BrowserContext
* browser_context
,
451 const content::SocketPermissionRequest
* params
) {
452 NOTREACHED() << "Android WebView does not support plugins";
456 void AwContentBrowserClient::OverrideWebkitPrefs(
457 content::RenderViewHost
* rvh
,
458 content::WebPreferences
* web_prefs
) {
459 if (!preferences_populater_
.get()) {
460 preferences_populater_
= make_scoped_ptr(native_factory_
->
461 CreateWebPreferencesPopulater());
463 preferences_populater_
->PopulateFor(
464 content::WebContents::FromRenderViewHost(rvh
), web_prefs
);
467 #if defined(VIDEO_HOLE)
468 content::ExternalVideoSurfaceContainer
*
469 AwContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
470 content::WebContents
* web_contents
) {
471 return native_factory_
->CreateExternalVideoSurfaceContainer(web_contents
);
475 } // namespace android_webview