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/lib/main/aw_main_delegate.h"
7 #include "android_webview/browser/aw_content_browser_client.h"
8 #include "android_webview/browser/browser_view_renderer.h"
9 #include "android_webview/browser/scoped_allow_wait_for_legacy_web_view_api.h"
10 #include "android_webview/common/aw_descriptors.h"
11 #include "android_webview/common/aw_switches.h"
12 #include "android_webview/crash_reporter/aw_microdump_crash_reporter.h"
13 #include "android_webview/lib/aw_browser_dependency_factory_impl.h"
14 #include "android_webview/native/aw_locale_manager_impl.h"
15 #include "android_webview/native/aw_media_url_interceptor.h"
16 #include "android_webview/native/aw_message_port_service_impl.h"
17 #include "android_webview/native/aw_quota_manager_bridge_impl.h"
18 #include "android_webview/native/aw_web_contents_view_delegate.h"
19 #include "android_webview/native/aw_web_preferences_populater_impl.h"
20 #include "android_webview/renderer/aw_content_renderer_client.h"
21 #include "base/android/apk_assets.h"
22 #include "base/command_line.h"
24 #include "base/i18n/icu_util.h"
25 #include "base/lazy_instance.h"
26 #include "base/logging.h"
27 #include "base/memory/scoped_ptr.h"
28 #include "base/threading/thread_restrictions.h"
29 #include "cc/base/switches.h"
30 #include "components/external_video_surface/browser/android/external_video_surface_container_impl.h"
31 #include "content/public/browser/android/browser_media_player_manager_register.h"
32 #include "content/public/browser/browser_main_runner.h"
33 #include "content/public/browser/browser_thread.h"
34 #include "content/public/common/content_descriptors.h"
35 #include "content/public/common/content_switches.h"
36 #include "gin/public/isolate_holder.h"
37 #include "gpu/command_buffer/client/gl_in_process_context.h"
38 #include "gpu/command_buffer/service/gpu_switches.h"
39 #include "media/base/media_switches.h"
40 #include "ui/base/resource/resource_bundle.h"
41 #include "ui/events/gesture_detection/gesture_configuration.h"
43 namespace android_webview
{
47 // TODO(boliu): Remove this global Allow once the underlying issues are
48 // resolved - http://crbug.com/240453. See AwMainDelegate::RunProcess below.
49 base::LazyInstance
<scoped_ptr
<ScopedAllowWaitForLegacyWebViewApi
> >
50 g_allow_wait_in_ui_thread
= LAZY_INSTANCE_INITIALIZER
;
54 AwMainDelegate::AwMainDelegate() {
57 AwMainDelegate::~AwMainDelegate() {
60 bool AwMainDelegate::BasicStartupComplete(int* exit_code
) {
61 content::SetContentClient(&content_client_
);
63 content::RegisterMediaUrlInterceptor(new AwMediaUrlInterceptor());
65 BrowserViewRenderer::CalculateTileMemoryPolicy();
67 // WebView apps can override WebView#computeScroll to achieve custom
68 // scroll/fling. As a result, fling animations may not be ticked, potentially
69 // confusing the tap suppression controller. Simply disable it for WebView.
70 ui::GestureConfiguration::GetInstance()
71 ->set_fling_touchscreen_tap_suppression_enabled(false);
73 base::CommandLine
* cl
= base::CommandLine::ForCurrentProcess();
74 cl
->AppendSwitch(cc::switches::kEnableBeginFrameScheduling
);
76 // WebView uses the Android system's scrollbars and overscroll glow.
77 cl
->AppendSwitch(switches::kDisableOverscrollEdgeEffect
);
79 // Pull-to-refresh should never be a default WebView action.
80 cl
->AppendSwitch(switches::kDisablePullToRefreshEffect
);
82 // Not yet supported in single-process mode.
83 cl
->AppendSwitch(switches::kDisableSharedWorkers
);
85 // File system API not supported (requires some new API; internal bug 6930981)
86 cl
->AppendSwitch(switches::kDisableFileSystem
);
88 // Web Notification API and the Push API are not supported (crbug.com/434712)
89 cl
->AppendSwitch(switches::kDisableNotifications
);
91 // WebRTC hardware decoding is not supported, internal bug 15075307
92 cl
->AppendSwitch(switches::kDisableWebRtcHWDecoding
);
93 cl
->AppendSwitch(switches::kDisableAcceleratedVideoDecode
);
95 // This is needed for sharing textures across the different GL threads.
96 cl
->AppendSwitch(switches::kEnableThreadedTextureMailboxes
);
98 // WebView does not yet support screen orientation locking.
99 cl
->AppendSwitch(switches::kDisableScreenOrientationLock
);
101 // WebView does not currently support Web Speech API (crbug.com/487255)
102 cl
->AppendSwitch(switches::kDisableSpeechAPI
);
104 // WebView does not currently support the Permissions API (crbug.com/490120)
105 cl
->AppendSwitch(switches::kDisablePermissionsAPI
);
107 // WebView does not (yet) save Chromium data during shutdown, so add setting
108 // for Chrome to aggressively persist DOM Storage to minimize data loss.
109 // http://crbug.com/479767
110 cl
->AppendSwitch(switches::kEnableAggressiveDOMStorageFlushing
);
112 // Webview does not currently support the Presentation API, see
113 // https://crbug.com/521319
114 cl
->AppendSwitch(switches::kDisablePresentationAPI
);
116 if (cl
->GetSwitchValueASCII(switches::kProcessType
).empty()) {
117 // Browser process (no type specified).
119 // This code is needed to be able to mmap the V8 snapshot directly from
120 // the WebView .apk using architecture-specific names.
121 // This needs to be here so that it gets to run before the code in
122 // content_main_runner that reads these values tries to do so.
124 const char kNativesFileName
[] = "assets/natives_blob_64.bin";
125 const char kSnapshotFileName
[] = "assets/snapshot_blob_64.bin";
127 const char kNativesFileName
[] = "assets/natives_blob_32.bin";
128 const char kSnapshotFileName
[] = "assets/snapshot_blob_32.bin";
130 // TODO(gsennton) we should use
131 // gin::IsolateHolder::kNativesFileName/kSnapshotFileName
132 // here when those files have arch specific names http://crbug.com/455699
133 CHECK(base::android::RegisterApkAssetWithGlobalDescriptors(
134 kV8NativesDataDescriptor
, kNativesFileName
));
135 CHECK(base::android::RegisterApkAssetWithGlobalDescriptors(
136 kV8SnapshotDataDescriptor
, kSnapshotFileName
));
139 if (cl
->HasSwitch(switches::kWebViewSanboxedRenderer
)) {
140 cl
->AppendSwitch(switches::kInProcessGPU
);
141 cl
->AppendSwitchASCII(switches::kRendererProcessLimit
, "1");
147 void AwMainDelegate::PreSandboxStartup() {
148 #if defined(ARCH_CPU_ARM_FAMILY)
149 // Create an instance of the CPU class to parse /proc/cpuinfo and cache
154 const base::CommandLine
& command_line
=
155 *base::CommandLine::ForCurrentProcess();
156 std::string process_type
=
157 command_line
.GetSwitchValueASCII(switches::kProcessType
);
158 if (process_type
== switches::kRendererProcess
) {
159 auto global_descriptors
= base::GlobalDescriptors::GetInstance();
160 int pak_fd
= global_descriptors
->Get(kAndroidWebViewLocalePakDescriptor
);
161 base::MemoryMappedFile::Region pak_region
=
162 global_descriptors
->GetRegion(kAndroidWebViewLocalePakDescriptor
);
163 ResourceBundle::InitSharedInstanceWithPakFileRegion(base::File(pak_fd
),
165 pak_fd
= global_descriptors
->Get(kAndroidWebViewMainPakDescriptor
);
167 global_descriptors
->GetRegion(kAndroidWebViewMainPakDescriptor
);
168 ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion(
169 base::File(pak_fd
), pak_region
, ui::SCALE_FACTOR_NONE
);
172 crash_reporter::EnableMicrodumpCrashReporter();
175 void AwMainDelegate::SandboxInitialized(const std::string
& process_type
) {
176 // TODO(torne): Adjust linux OOM score here.
179 int AwMainDelegate::RunProcess(
180 const std::string
& process_type
,
181 const content::MainFunctionParams
& main_function_params
) {
182 if (process_type
.empty()) {
183 AwBrowserDependencyFactoryImpl::InstallInstance();
185 browser_runner_
.reset(content::BrowserMainRunner::Create());
186 int exit_code
= browser_runner_
->Initialize(main_function_params
);
187 DCHECK_LT(exit_code
, 0);
189 g_allow_wait_in_ui_thread
.Get().reset(
190 new ScopedAllowWaitForLegacyWebViewApi
);
192 // Return 0 so that we do NOT trigger the default behavior. On Android, the
193 // UI message loop is managed by the Java application.
200 void AwMainDelegate::ProcessExiting(const std::string
& process_type
) {
201 // TODO(torne): Clean up resources when we handle them.
203 logging::CloseLogFile();
206 content::ContentBrowserClient
*
207 AwMainDelegate::CreateContentBrowserClient() {
208 content_browser_client_
.reset(new AwContentBrowserClient(this));
209 return content_browser_client_
.get();
212 content::ContentRendererClient
*
213 AwMainDelegate::CreateContentRendererClient() {
214 content_renderer_client_
.reset(new AwContentRendererClient());
215 return content_renderer_client_
.get();
218 scoped_refptr
<AwQuotaManagerBridge
> AwMainDelegate::CreateAwQuotaManagerBridge(
219 AwBrowserContext
* browser_context
) {
220 return AwQuotaManagerBridgeImpl::Create(browser_context
);
223 content::WebContentsViewDelegate
* AwMainDelegate::CreateViewDelegate(
224 content::WebContents
* web_contents
) {
225 return AwWebContentsViewDelegate::Create(web_contents
);
228 AwWebPreferencesPopulater
* AwMainDelegate::CreateWebPreferencesPopulater() {
229 return new AwWebPreferencesPopulaterImpl();
232 AwMessagePortService
* AwMainDelegate::CreateAwMessagePortService() {
233 return new AwMessagePortServiceImpl();
236 AwLocaleManager
* AwMainDelegate::CreateAwLocaleManager() {
237 return new AwLocaleManagerImpl();
240 #if defined(VIDEO_HOLE)
241 content::ExternalVideoSurfaceContainer
*
242 AwMainDelegate::CreateExternalVideoSurfaceContainer(
243 content::WebContents
* web_contents
) {
244 return external_video_surface::ExternalVideoSurfaceContainerImpl::Create(
249 } // namespace android_webview