Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / android_webview / browser / aw_content_browser_client.cc
blob87a1b932882e6235414e619b61b32cd11766cb2e
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 {
44 namespace {
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 {
50 public:
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);
58 private:
59 ~AwContentsMessageFilter() override;
61 int process_id_;
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) {
75 bool handled = true;
76 IPC_BEGIN_MESSAGE_MAP(AwContentsMessageFilter, message)
77 IPC_MESSAGE_HANDLER(AwViewHostMsg_SubFrameCreated, OnSubFrameCreated)
78 IPC_MESSAGE_UNHANDLED(handled = false)
79 IPC_END_MESSAGE_MAP()
80 return handled;
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 {
90 public:
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 {}
103 private:
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") {
118 langs += ",en-US";
120 return langs;
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,
143 size_t cert_size,
144 int render_process_id,
145 int render_frame_id) {
146 if (cert_size > 0)
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,
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 std::string AwContentBrowserClient::GetCanonicalEncodingNameByAliasName(
207 const std::string& alias_name) {
208 return 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.
237 return true;
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,
248 first_party,
249 cookie_list,
250 context,
251 render_process_id,
252 render_frame_id);
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,
260 int render_frame_id,
261 net::CookieOptions* options) {
262 return AwCookieAccessPolicy::GetInstance()->AllowSetCookie(url,
263 first_party,
264 cookie_line,
265 context,
266 render_process_id,
267 render_frame_id,
268 options);
271 bool AwContentBrowserClient::AllowWorkerDatabase(
272 const GURL& url,
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.
279 return false;
282 void AwContentBrowserClient::AllowWorkerFileSystem(
283 const GURL& url,
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.
288 callback.Run(false);
291 bool AwContentBrowserClient::AllowWorkerIndexedDB(
292 const GURL& url,
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.
297 return false;
300 content::QuotaPermissionContext*
301 AwContentBrowserClient::CreateQuotaPermissionContext() {
302 return new AwQuotaPermissionContext;
305 void AwContentBrowserClient::AllowCertificateError(
306 int render_process_id,
307 int render_frame_id,
308 int cert_error,
309 const net::SSLInfo& ssl_info,
310 const GURL& request_url,
311 ResourceType resource_type,
312 bool overridable,
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;
320 if (client)
321 client->AllowCertificateError(cert_error,
322 ssl_info.cert.get(),
323 request_url,
324 callback,
325 &cancel_request);
326 if (cancel_request)
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);
336 if (client)
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,
349 bool user_gesture,
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;
366 return true;
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";
384 return false;
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.
394 NOTIMPLEMENTED();
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,
418 const GURL& url,
419 bool private_api,
420 const content::SocketPermissionRequest* params) {
421 NOTREACHED() << "Android WebView does not support plugins";
422 return false;
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);
442 #endif
444 } // namespace android_webview