Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / android_webview / browser / aw_content_browser_client.cc
blobfb16cebeaa1f38c08469de7c520ae7bf43f3394f
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/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/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::ResourceType;
44 namespace android_webview {
45 namespace {
47 // TODO(sgurun) move this to its own file.
48 // This class filters out incoming aw_contents related IPC messages for the
49 // renderer process on the IPC thread.
50 class AwContentsMessageFilter : public content::BrowserMessageFilter {
51 public:
52 explicit AwContentsMessageFilter(int process_id);
54 // BrowserMessageFilter methods.
55 bool OnMessageReceived(const IPC::Message& message) override;
57 void OnSubFrameCreated(int parent_render_frame_id, int child_render_frame_id);
59 private:
60 ~AwContentsMessageFilter() override;
62 int process_id_;
64 DISALLOW_COPY_AND_ASSIGN(AwContentsMessageFilter);
67 AwContentsMessageFilter::AwContentsMessageFilter(int process_id)
68 : BrowserMessageFilter(AndroidWebViewMsgStart),
69 process_id_(process_id) {
72 AwContentsMessageFilter::~AwContentsMessageFilter() {
75 bool AwContentsMessageFilter::OnMessageReceived(const IPC::Message& message) {
76 bool handled = true;
77 IPC_BEGIN_MESSAGE_MAP(AwContentsMessageFilter, message)
78 IPC_MESSAGE_HANDLER(AwViewHostMsg_SubFrameCreated, OnSubFrameCreated)
79 IPC_MESSAGE_UNHANDLED(handled = false)
80 IPC_END_MESSAGE_MAP()
81 return handled;
84 void AwContentsMessageFilter::OnSubFrameCreated(int parent_render_frame_id,
85 int child_render_frame_id) {
86 AwContentsIoThreadClient::SubFrameCreated(
87 process_id_, parent_render_frame_id, child_render_frame_id);
90 class AwAccessTokenStore : public content::AccessTokenStore {
91 public:
92 AwAccessTokenStore() { }
94 // content::AccessTokenStore implementation
95 void LoadAccessTokens(const LoadAccessTokensCallbackType& request) override {
96 AccessTokenStore::AccessTokenSet access_token_set;
97 // AccessTokenSet and net::URLRequestContextGetter not used on Android,
98 // but Run needs to be called to finish the geolocation setup.
99 request.Run(access_token_set, NULL);
101 void SaveAccessToken(const GURL& server_url,
102 const base::string16& access_token) override {}
104 private:
105 ~AwAccessTokenStore() override {}
107 DISALLOW_COPY_AND_ASSIGN(AwAccessTokenStore);
110 AwLocaleManager* g_locale_manager = NULL;
112 } // anonymous namespace
114 // static
115 std::string AwContentBrowserClient::GetAcceptLangsImpl() {
116 // Start with the current locale.
117 std::string langs = g_locale_manager->GetLocale();
119 // If we're not en-US, add in en-US which will be
120 // used with a lower q-value.
121 if (base::ToLowerASCII(langs) != "en-us") {
122 langs += ",en-US";
124 return langs;
127 // static
128 AwBrowserContext* AwContentBrowserClient::GetAwBrowserContext() {
129 return AwBrowserContext::GetDefault();
132 AwContentBrowserClient::AwContentBrowserClient(
133 JniDependencyFactory* native_factory)
134 : native_factory_(native_factory) {
135 base::FilePath user_data_dir;
136 if (!PathService::Get(base::DIR_ANDROID_APP_DATA, &user_data_dir)) {
137 NOTREACHED() << "Failed to get app data directory for Android WebView";
139 browser_context_.reset(
140 new AwBrowserContext(user_data_dir, native_factory_));
141 g_locale_manager = native_factory->CreateAwLocaleManager();
144 AwContentBrowserClient::~AwContentBrowserClient() {
145 delete g_locale_manager;
146 g_locale_manager = NULL;
149 void AwContentBrowserClient::AddCertificate(net::CertificateMimeType cert_type,
150 const void* cert_data,
151 size_t cert_size,
152 int render_process_id,
153 int render_frame_id) {
154 if (cert_size > 0)
155 net::android::StoreCertificate(cert_type, cert_data, cert_size);
158 content::BrowserMainParts* AwContentBrowserClient::CreateBrowserMainParts(
159 const content::MainFunctionParams& parameters) {
160 return new AwBrowserMainParts(browser_context_.get());
163 content::WebContentsViewDelegate*
164 AwContentBrowserClient::GetWebContentsViewDelegate(
165 content::WebContents* web_contents) {
166 return native_factory_->CreateViewDelegate(web_contents);
169 void AwContentBrowserClient::RenderProcessWillLaunch(
170 content::RenderProcessHost* host) {
171 // Grant content: scheme access to the whole renderer process, since we impose
172 // per-view access checks, and access is granted by default (see
173 // AwSettings.mAllowContentUrlAccess).
174 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
175 host->GetID(), url::kContentScheme);
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,
195 bool in_memory,
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 bool AwContentBrowserClient::IsHandledURL(const GURL& url) {
207 if (!url.is_valid()) {
208 // We handle error cases.
209 return true;
212 const std::string scheme = url.scheme();
213 DCHECK_EQ(scheme, base::ToLowerASCII(scheme));
214 // See CreateJobFactory in aw_url_request_context_getter.cc for the
215 // list of protocols that are handled.
216 // TODO(mnaganov): Make this automatic.
217 static const char* const kProtocolList[] = {
218 url::kDataScheme,
219 url::kBlobScheme,
220 url::kFileSystemScheme,
221 content::kChromeUIScheme,
222 content::kChromeDevToolsScheme,
223 url::kContentScheme,
225 if (scheme == url::kFileScheme) {
226 // Return false for the "special" file URLs, so they can be loaded
227 // even if access to file: scheme is not granted to the child process.
228 return !IsAndroidSpecialFileUrl(url);
230 for (size_t i = 0; i < arraysize(kProtocolList); ++i) {
231 if (scheme == kProtocolList[i])
232 return true;
234 return net::URLRequest::IsHandledProtocol(scheme);
237 std::string AwContentBrowserClient::GetCanonicalEncodingNameByAliasName(
238 const std::string& alias_name) {
239 return alias_name;
242 void AwContentBrowserClient::AppendExtraCommandLineSwitches(
243 base::CommandLine* command_line,
244 int child_process_id) {
245 NOTREACHED() << "Android WebView does not support multi-process yet";
248 std::string AwContentBrowserClient::GetApplicationLocale() {
249 return base::android::GetDefaultLocale();
252 std::string AwContentBrowserClient::GetAcceptLangs(
253 content::BrowserContext* context) {
254 return GetAcceptLangsImpl();
257 const gfx::ImageSkia* AwContentBrowserClient::GetDefaultFavicon() {
258 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
259 // TODO(boliu): Bundle our own default favicon?
260 return rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON);
263 bool AwContentBrowserClient::AllowAppCache(const GURL& manifest_url,
264 const GURL& first_party,
265 content::ResourceContext* context) {
266 // WebView doesn't have a per-site policy for locally stored data,
267 // instead AppCache can be disabled for individual WebViews.
268 return true;
272 bool AwContentBrowserClient::AllowGetCookie(const GURL& url,
273 const GURL& first_party,
274 const net::CookieList& cookie_list,
275 content::ResourceContext* context,
276 int render_process_id,
277 int render_frame_id) {
278 return AwCookieAccessPolicy::GetInstance()->AllowGetCookie(url,
279 first_party,
280 cookie_list,
281 context,
282 render_process_id,
283 render_frame_id);
286 bool AwContentBrowserClient::AllowSetCookie(const GURL& url,
287 const GURL& first_party,
288 const std::string& cookie_line,
289 content::ResourceContext* context,
290 int render_process_id,
291 int render_frame_id,
292 net::CookieOptions* options) {
293 return AwCookieAccessPolicy::GetInstance()->AllowSetCookie(url,
294 first_party,
295 cookie_line,
296 context,
297 render_process_id,
298 render_frame_id,
299 options);
302 bool AwContentBrowserClient::AllowWorkerDatabase(
303 const GURL& url,
304 const base::string16& name,
305 const base::string16& display_name,
306 unsigned long estimated_size,
307 content::ResourceContext* context,
308 const std::vector<std::pair<int, int> >& render_frames) {
309 // Android WebView does not yet support web workers.
310 return false;
313 void AwContentBrowserClient::AllowWorkerFileSystem(
314 const GURL& url,
315 content::ResourceContext* context,
316 const std::vector<std::pair<int, int> >& render_frames,
317 base::Callback<void(bool)> callback) {
318 // Android WebView does not yet support web workers.
319 callback.Run(false);
322 bool AwContentBrowserClient::AllowWorkerIndexedDB(
323 const GURL& url,
324 const base::string16& name,
325 content::ResourceContext* context,
326 const std::vector<std::pair<int, int> >& render_frames) {
327 // Android WebView does not yet support web workers.
328 return false;
331 content::QuotaPermissionContext*
332 AwContentBrowserClient::CreateQuotaPermissionContext() {
333 return new AwQuotaPermissionContext;
336 void AwContentBrowserClient::AllowCertificateError(
337 int render_process_id,
338 int render_frame_id,
339 int cert_error,
340 const net::SSLInfo& ssl_info,
341 const GURL& request_url,
342 ResourceType resource_type,
343 bool overridable,
344 bool strict_enforcement,
345 bool expired_previous_decision,
346 const base::Callback<void(bool)>& callback,
347 content::CertificateRequestResultType* result) {
348 AwContentsClientBridgeBase* client =
349 AwContentsClientBridgeBase::FromID(render_process_id, render_frame_id);
350 bool cancel_request = true;
351 if (client)
352 client->AllowCertificateError(cert_error,
353 ssl_info.cert.get(),
354 request_url,
355 callback,
356 &cancel_request);
357 if (cancel_request)
358 *result = content::CERTIFICATE_REQUEST_RESULT_TYPE_DENY;
361 void AwContentBrowserClient::SelectClientCertificate(
362 content::WebContents* web_contents,
363 net::SSLCertRequestInfo* cert_request_info,
364 scoped_ptr<content::ClientCertificateDelegate> delegate) {
365 AwContentsClientBridgeBase* client =
366 AwContentsClientBridgeBase::FromWebContents(web_contents);
367 if (client)
368 client->SelectClientCertificate(cert_request_info, delegate.Pass());
371 bool AwContentBrowserClient::CanCreateWindow(
372 const GURL& opener_url,
373 const GURL& opener_top_level_frame_url,
374 const GURL& source_origin,
375 WindowContainerType container_type,
376 const GURL& target_url,
377 const content::Referrer& referrer,
378 WindowOpenDisposition disposition,
379 const blink::WebWindowFeatures& features,
380 bool user_gesture,
381 bool opener_suppressed,
382 content::ResourceContext* context,
383 int render_process_id,
384 int opener_render_view_id,
385 int opener_render_frame_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;
397 return true;
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";
415 return false;
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.
425 NOTIMPLEMENTED();
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,
449 const GURL& url,
450 bool private_api,
451 const content::SocketPermissionRequest* params) {
452 NOTREACHED() << "Android WebView does not support plugins";
453 return false;
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);
473 #endif
475 } // namespace android_webview