Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / android_webview / browser / aw_content_browser_client.cc
blob759c844b9ba903e6e713e0a2af56b669c2867384
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_locale_manager.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/aw_descriptors.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/command_line.h"
26 #include "base/path_service.h"
27 #include "components/cdm/browser/cdm_message_filter_android.h"
28 #include "content/public/browser/access_token_store.h"
29 #include "content/public/browser/browser_message_filter.h"
30 #include "content/public/browser/child_process_security_policy.h"
31 #include "content/public/browser/client_certificate_delegate.h"
32 #include "content/public/browser/render_frame_host.h"
33 #include "content/public/browser/render_process_host.h"
34 #include "content/public/browser/render_view_host.h"
35 #include "content/public/browser/web_contents.h"
36 #include "content/public/common/content_switches.h"
37 #include "content/public/common/url_constants.h"
38 #include "content/public/common/web_preferences.h"
39 #include "net/android/network_library.h"
40 #include "net/ssl/ssl_cert_request_info.h"
41 #include "net/ssl/ssl_info.h"
42 #include "ui/base/resource/resource_bundle.h"
43 #include "ui/base/resource/resource_bundle_android.h"
44 #include "ui/resources/grit/ui_resources.h"
46 using content::ResourceType;
48 namespace android_webview {
49 namespace {
51 // TODO(sgurun) move this to its own file.
52 // This class filters out incoming aw_contents related IPC messages for the
53 // renderer process on the IPC thread.
54 class AwContentsMessageFilter : public content::BrowserMessageFilter {
55 public:
56 explicit AwContentsMessageFilter(int process_id);
58 // BrowserMessageFilter methods.
59 bool OnMessageReceived(const IPC::Message& message) override;
61 void OnSubFrameCreated(int parent_render_frame_id, int child_render_frame_id);
63 private:
64 ~AwContentsMessageFilter() override;
66 int process_id_;
68 DISALLOW_COPY_AND_ASSIGN(AwContentsMessageFilter);
71 AwContentsMessageFilter::AwContentsMessageFilter(int process_id)
72 : BrowserMessageFilter(AndroidWebViewMsgStart),
73 process_id_(process_id) {
76 AwContentsMessageFilter::~AwContentsMessageFilter() {
79 bool AwContentsMessageFilter::OnMessageReceived(const IPC::Message& message) {
80 bool handled = true;
81 IPC_BEGIN_MESSAGE_MAP(AwContentsMessageFilter, message)
82 IPC_MESSAGE_HANDLER(AwViewHostMsg_SubFrameCreated, OnSubFrameCreated)
83 IPC_MESSAGE_UNHANDLED(handled = false)
84 IPC_END_MESSAGE_MAP()
85 return handled;
88 void AwContentsMessageFilter::OnSubFrameCreated(int parent_render_frame_id,
89 int child_render_frame_id) {
90 AwContentsIoThreadClient::SubFrameCreated(
91 process_id_, parent_render_frame_id, child_render_frame_id);
94 class AwAccessTokenStore : public content::AccessTokenStore {
95 public:
96 AwAccessTokenStore() { }
98 // content::AccessTokenStore implementation
99 void LoadAccessTokens(const LoadAccessTokensCallbackType& request) override {
100 AccessTokenStore::AccessTokenSet access_token_set;
101 // AccessTokenSet and net::URLRequestContextGetter not used on Android,
102 // but Run needs to be called to finish the geolocation setup.
103 request.Run(access_token_set, NULL);
105 void SaveAccessToken(const GURL& server_url,
106 const base::string16& access_token) override {}
108 private:
109 ~AwAccessTokenStore() override {}
111 DISALLOW_COPY_AND_ASSIGN(AwAccessTokenStore);
114 AwLocaleManager* g_locale_manager = NULL;
116 } // anonymous namespace
118 // static
119 std::string AwContentBrowserClient::GetAcceptLangsImpl() {
120 // Start with the current locale.
121 std::string langs = g_locale_manager->GetLocale();
123 // If we're not en-US, add in en-US which will be
124 // used with a lower q-value.
125 if (base::ToLowerASCII(langs) != "en-us") {
126 langs += ",en-US";
128 return langs;
131 // static
132 AwBrowserContext* AwContentBrowserClient::GetAwBrowserContext() {
133 return AwBrowserContext::GetDefault();
136 AwContentBrowserClient::AwContentBrowserClient(
137 JniDependencyFactory* native_factory)
138 : native_factory_(native_factory) {
139 base::FilePath user_data_dir;
140 if (!PathService::Get(base::DIR_ANDROID_APP_DATA, &user_data_dir)) {
141 NOTREACHED() << "Failed to get app data directory for Android WebView";
143 browser_context_.reset(
144 new AwBrowserContext(user_data_dir, native_factory_));
145 g_locale_manager = native_factory->CreateAwLocaleManager();
148 AwContentBrowserClient::~AwContentBrowserClient() {
149 delete g_locale_manager;
150 g_locale_manager = NULL;
153 void AwContentBrowserClient::AddCertificate(net::CertificateMimeType cert_type,
154 const void* cert_data,
155 size_t cert_size,
156 int render_process_id,
157 int render_frame_id) {
158 if (cert_size > 0)
159 net::android::StoreCertificate(cert_type, cert_data, cert_size);
162 content::BrowserMainParts* AwContentBrowserClient::CreateBrowserMainParts(
163 const content::MainFunctionParams& parameters) {
164 return new AwBrowserMainParts(browser_context_.get());
167 content::WebContentsViewDelegate*
168 AwContentBrowserClient::GetWebContentsViewDelegate(
169 content::WebContents* web_contents) {
170 return native_factory_->CreateViewDelegate(web_contents);
173 void AwContentBrowserClient::RenderProcessWillLaunch(
174 content::RenderProcessHost* host) {
175 // Grant content: scheme access to the whole renderer process, since we impose
176 // per-view access checks, and access is granted by default (see
177 // AwSettings.mAllowContentUrlAccess).
178 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
179 host->GetID(), url::kContentScheme);
181 host->AddFilter(new AwContentsMessageFilter(host->GetID()));
182 host->AddFilter(new cdm::CdmMessageFilterAndroid());
183 host->AddFilter(new AwPrintingMessageFilter(host->GetID()));
186 net::URLRequestContextGetter* AwContentBrowserClient::CreateRequestContext(
187 content::BrowserContext* browser_context,
188 content::ProtocolHandlerMap* protocol_handlers,
189 content::URLRequestInterceptorScopedVector request_interceptors) {
190 DCHECK_EQ(browser_context_.get(), browser_context);
191 return browser_context_->CreateRequestContext(protocol_handlers,
192 request_interceptors.Pass());
195 net::URLRequestContextGetter*
196 AwContentBrowserClient::CreateRequestContextForStoragePartition(
197 content::BrowserContext* browser_context,
198 const base::FilePath& partition_path,
199 bool in_memory,
200 content::ProtocolHandlerMap* protocol_handlers,
201 content::URLRequestInterceptorScopedVector request_interceptors) {
202 DCHECK_EQ(browser_context_.get(), browser_context);
203 // TODO(mkosiba,kinuko): request_interceptors should be hooked up in the
204 // downstream. (crbug.com/350286)
205 return browser_context_->CreateRequestContextForStoragePartition(
206 partition_path, in_memory, protocol_handlers,
207 request_interceptors.Pass());
210 bool AwContentBrowserClient::IsHandledURL(const GURL& url) {
211 if (!url.is_valid()) {
212 // We handle error cases.
213 return true;
216 const std::string scheme = url.scheme();
217 DCHECK_EQ(scheme, base::ToLowerASCII(scheme));
218 // See CreateJobFactory in aw_url_request_context_getter.cc for the
219 // list of protocols that are handled.
220 // TODO(mnaganov): Make this automatic.
221 static const char* const kProtocolList[] = {
222 url::kDataScheme,
223 url::kBlobScheme,
224 url::kFileSystemScheme,
225 content::kChromeUIScheme,
226 content::kChromeDevToolsScheme,
227 url::kContentScheme,
229 if (scheme == url::kFileScheme) {
230 // Return false for the "special" file URLs, so they can be loaded
231 // even if access to file: scheme is not granted to the child process.
232 return !IsAndroidSpecialFileUrl(url);
234 for (size_t i = 0; i < arraysize(kProtocolList); ++i) {
235 if (scheme == kProtocolList[i])
236 return true;
238 return net::URLRequest::IsHandledProtocol(scheme);
241 std::string AwContentBrowserClient::GetCanonicalEncodingNameByAliasName(
242 const std::string& alias_name) {
243 return alias_name;
246 void AwContentBrowserClient::AppendExtraCommandLineSwitches(
247 base::CommandLine* command_line,
248 int child_process_id) {
249 if (command_line->HasSwitch(switches::kSingleProcess)) {
250 NOTREACHED() << "Android WebView does not support multi-process yet";
251 } else {
252 // The only kind of a child process WebView can have is renderer.
253 DCHECK_EQ(switches::kRendererProcess,
254 command_line->GetSwitchValueASCII(switches::kProcessType));
258 std::string AwContentBrowserClient::GetApplicationLocale() {
259 return base::android::GetDefaultLocale();
262 std::string AwContentBrowserClient::GetAcceptLangs(
263 content::BrowserContext* context) {
264 return GetAcceptLangsImpl();
267 const gfx::ImageSkia* AwContentBrowserClient::GetDefaultFavicon() {
268 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
269 // TODO(boliu): Bundle our own default favicon?
270 return rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON);
273 bool AwContentBrowserClient::AllowAppCache(const GURL& manifest_url,
274 const GURL& first_party,
275 content::ResourceContext* context) {
276 // WebView doesn't have a per-site policy for locally stored data,
277 // instead AppCache can be disabled for individual WebViews.
278 return true;
282 bool AwContentBrowserClient::AllowGetCookie(const GURL& url,
283 const GURL& first_party,
284 const net::CookieList& cookie_list,
285 content::ResourceContext* context,
286 int render_process_id,
287 int render_frame_id) {
288 return AwCookieAccessPolicy::GetInstance()->AllowGetCookie(url,
289 first_party,
290 cookie_list,
291 context,
292 render_process_id,
293 render_frame_id);
296 bool AwContentBrowserClient::AllowSetCookie(const GURL& url,
297 const GURL& first_party,
298 const std::string& cookie_line,
299 content::ResourceContext* context,
300 int render_process_id,
301 int render_frame_id,
302 net::CookieOptions* options) {
303 return AwCookieAccessPolicy::GetInstance()->AllowSetCookie(url,
304 first_party,
305 cookie_line,
306 context,
307 render_process_id,
308 render_frame_id,
309 options);
312 bool AwContentBrowserClient::AllowWorkerDatabase(
313 const GURL& url,
314 const base::string16& name,
315 const base::string16& display_name,
316 unsigned long estimated_size,
317 content::ResourceContext* context,
318 const std::vector<std::pair<int, int> >& render_frames) {
319 // Android WebView does not yet support web workers.
320 return false;
323 void AwContentBrowserClient::AllowWorkerFileSystem(
324 const GURL& url,
325 content::ResourceContext* context,
326 const std::vector<std::pair<int, int> >& render_frames,
327 base::Callback<void(bool)> callback) {
328 // Android WebView does not yet support web workers.
329 callback.Run(false);
332 bool AwContentBrowserClient::AllowWorkerIndexedDB(
333 const GURL& url,
334 const base::string16& name,
335 content::ResourceContext* context,
336 const std::vector<std::pair<int, int> >& render_frames) {
337 // Android WebView does not yet support web workers.
338 return false;
341 content::QuotaPermissionContext*
342 AwContentBrowserClient::CreateQuotaPermissionContext() {
343 return new AwQuotaPermissionContext;
346 void AwContentBrowserClient::AllowCertificateError(
347 int render_process_id,
348 int render_frame_id,
349 int cert_error,
350 const net::SSLInfo& ssl_info,
351 const GURL& request_url,
352 ResourceType resource_type,
353 bool overridable,
354 bool strict_enforcement,
355 bool expired_previous_decision,
356 const base::Callback<void(bool)>& callback,
357 content::CertificateRequestResultType* result) {
358 AwContentsClientBridgeBase* client =
359 AwContentsClientBridgeBase::FromID(render_process_id, render_frame_id);
360 bool cancel_request = true;
361 if (client)
362 client->AllowCertificateError(cert_error,
363 ssl_info.cert.get(),
364 request_url,
365 callback,
366 &cancel_request);
367 if (cancel_request)
368 *result = content::CERTIFICATE_REQUEST_RESULT_TYPE_DENY;
371 void AwContentBrowserClient::SelectClientCertificate(
372 content::WebContents* web_contents,
373 net::SSLCertRequestInfo* cert_request_info,
374 scoped_ptr<content::ClientCertificateDelegate> delegate) {
375 AwContentsClientBridgeBase* client =
376 AwContentsClientBridgeBase::FromWebContents(web_contents);
377 if (client)
378 client->SelectClientCertificate(cert_request_info, delegate.Pass());
381 bool AwContentBrowserClient::CanCreateWindow(
382 const GURL& opener_url,
383 const GURL& opener_top_level_frame_url,
384 const GURL& source_origin,
385 WindowContainerType container_type,
386 const GURL& target_url,
387 const content::Referrer& referrer,
388 WindowOpenDisposition disposition,
389 const blink::WebWindowFeatures& features,
390 bool user_gesture,
391 bool opener_suppressed,
392 content::ResourceContext* context,
393 int render_process_id,
394 int opener_render_view_id,
395 int opener_render_frame_id,
396 bool* no_javascript_access) {
397 // We unconditionally allow popup windows at this stage and will give
398 // the embedder the opporunity to handle displaying of the popup in
399 // WebContentsDelegate::AddContents (via the
400 // AwContentsClient.onCreateWindow callback).
401 // Note that if the embedder has blocked support for creating popup
402 // windows through AwSettings, then we won't get to this point as
403 // the popup creation will have been blocked at the WebKit level.
404 if (no_javascript_access) {
405 *no_javascript_access = false;
407 return true;
410 void AwContentBrowserClient::ResourceDispatcherHostCreated() {
411 AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated();
414 net::NetLog* AwContentBrowserClient::GetNetLog() {
415 return browser_context_->GetAwURLRequestContext()->GetNetLog();
418 content::AccessTokenStore* AwContentBrowserClient::CreateAccessTokenStore() {
419 return new AwAccessTokenStore();
422 bool AwContentBrowserClient::IsFastShutdownPossible() {
423 NOTREACHED() << "Android WebView is single process, so IsFastShutdownPossible"
424 << " should never be called";
425 return false;
428 void AwContentBrowserClient::ClearCache(content::RenderFrameHost* rfh) {
429 RemoveHttpDiskCache(rfh->GetProcess()->GetBrowserContext(),
430 rfh->GetProcess()->GetID());
433 void AwContentBrowserClient::ClearCookies(content::RenderFrameHost* rfh) {
434 // TODO(boliu): Implement.
435 NOTIMPLEMENTED();
438 base::FilePath AwContentBrowserClient::GetDefaultDownloadDirectory() {
439 // Android WebView does not currently use the Chromium downloads system.
440 // Download requests are cancelled immedately when recognized; see
441 // AwResourceDispatcherHost::CreateResourceHandlerForDownload. However the
442 // download system still tries to start up and calls this before recognizing
443 // the request has been cancelled.
444 return base::FilePath();
447 std::string AwContentBrowserClient::GetDefaultDownloadName() {
448 NOTREACHED() << "Android WebView does not use chromium downloads";
449 return std::string();
452 void AwContentBrowserClient::DidCreatePpapiPlugin(
453 content::BrowserPpapiHost* browser_host) {
454 NOTREACHED() << "Android WebView does not support plugins";
457 bool AwContentBrowserClient::AllowPepperSocketAPI(
458 content::BrowserContext* browser_context,
459 const GURL& url,
460 bool private_api,
461 const content::SocketPermissionRequest* params) {
462 NOTREACHED() << "Android WebView does not support plugins";
463 return false;
466 void AwContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
467 const base::CommandLine& command_line,
468 int child_process_id,
469 content::FileDescriptorInfo* mappings,
470 std::map<int, base::MemoryMappedFile::Region>* regions) {
471 int fd = ui::GetMainAndroidPackFd(
472 &(*regions)[kAndroidWebViewMainPakDescriptor]);
473 mappings->Share(kAndroidWebViewMainPakDescriptor, fd);
475 fd = ui::GetLocalePackFd(&(*regions)[kAndroidWebViewLocalePakDescriptor]);
476 mappings->Share(kAndroidWebViewLocalePakDescriptor, fd);
479 void AwContentBrowserClient::OverrideWebkitPrefs(
480 content::RenderViewHost* rvh,
481 content::WebPreferences* web_prefs) {
482 if (!preferences_populater_.get()) {
483 preferences_populater_ = make_scoped_ptr(native_factory_->
484 CreateWebPreferencesPopulater());
486 preferences_populater_->PopulateFor(
487 content::WebContents::FromRenderViewHost(rvh), web_prefs);
490 #if defined(VIDEO_HOLE)
491 content::ExternalVideoSurfaceContainer*
492 AwContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
493 content::WebContents* web_contents) {
494 return native_factory_->CreateExternalVideoSurfaceContainer(web_contents);
496 #endif
498 } // namespace android_webview