localStorage: Aggressively flush to disk for Android WebView.
[chromium-blink-merge.git] / android_webview / lib / main / aw_main_delegate.cc
blobdefb379388d38ac57af7a52be47e51c9e7262a70
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/crash_reporter/aw_microdump_crash_reporter.h"
11 #include "android_webview/lib/aw_browser_dependency_factory_impl.h"
12 #include "android_webview/native/aw_media_url_interceptor.h"
13 #include "android_webview/native/aw_message_port_service_impl.h"
14 #include "android_webview/native/aw_quota_manager_bridge_impl.h"
15 #include "android_webview/native/aw_web_contents_view_delegate.h"
16 #include "android_webview/native/aw_web_preferences_populater_impl.h"
17 #include "android_webview/native/public/aw_assets.h"
18 #include "android_webview/renderer/aw_content_renderer_client.h"
19 #include "base/command_line.h"
20 #include "base/cpu.h"
21 #include "base/i18n/icu_util.h"
22 #include "base/lazy_instance.h"
23 #include "base/logging.h"
24 #include "base/memory/scoped_ptr.h"
25 #include "base/threading/thread_restrictions.h"
26 #include "cc/base/switches.h"
27 #include "components/external_video_surface/browser/android/external_video_surface_container_impl.h"
28 #include "content/public/browser/android/browser_media_player_manager.h"
29 #include "content/public/browser/browser_main_runner.h"
30 #include "content/public/browser/browser_thread.h"
31 #include "content/public/common/content_descriptors.h"
32 #include "content/public/common/content_switches.h"
33 #include "gin/public/isolate_holder.h"
34 #include "gpu/command_buffer/client/gl_in_process_context.h"
35 #include "gpu/command_buffer/service/gpu_switches.h"
36 #include "media/base/media_switches.h"
38 namespace android_webview {
40 namespace {
42 // TODO(boliu): Remove this global Allow once the underlying issues are
43 // resolved - http://crbug.com/240453. See AwMainDelegate::RunProcess below.
44 base::LazyInstance<scoped_ptr<ScopedAllowWaitForLegacyWebViewApi> >
45 g_allow_wait_in_ui_thread = LAZY_INSTANCE_INITIALIZER;
49 AwMainDelegate::AwMainDelegate() {
52 AwMainDelegate::~AwMainDelegate() {
55 bool AwMainDelegate::BasicStartupComplete(int* exit_code) {
56 content::SetContentClient(&content_client_);
58 content::RegisterMediaUrlInterceptor(new AwMediaUrlInterceptor());
60 BrowserViewRenderer::CalculateTileMemoryPolicy();
62 base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
63 cl->AppendSwitch(switches::kEnableBeginFrameScheduling);
65 // WebView uses the Android system's scrollbars and overscroll glow.
66 cl->AppendSwitch(switches::kDisableOverscrollEdgeEffect);
68 // Pull-to-refresh should never be a default WebView action.
69 cl->AppendSwitch(switches::kDisablePullToRefreshEffect);
71 // Not yet supported in single-process mode.
72 cl->AppendSwitch(switches::kDisableSharedWorkers);
74 // File system API not supported (requires some new API; internal bug 6930981)
75 cl->AppendSwitch(switches::kDisableFileSystem);
77 // Web Notification API and the Push API are not supported (crbug.com/434712)
78 cl->AppendSwitch(switches::kDisableNotifications);
80 // TODO(ddorwin): Enable unprefixed EME. See http://crbug.com/394931.
81 cl->AppendSwitch(switches::kDisableEncryptedMedia);
83 // WebRTC hardware decoding is not supported, internal bug 15075307
84 cl->AppendSwitch(switches::kDisableWebRtcHWDecoding);
85 cl->AppendSwitch(switches::kDisableAcceleratedVideoDecode);
87 // This is needed for sharing textures across the different GL threads.
88 cl->AppendSwitch(switches::kEnableThreadedTextureMailboxes);
90 // WebView does not yet support screen orientation locking.
91 cl->AppendSwitch(switches::kDisableScreenOrientationLock);
93 // WebView does not (yet) save Chromium data during shutdown, so add setting
94 // for Chrome to aggressively persist DOM Storage to minimize data loss.
95 // http://crbug.com/479767
96 cl->AppendSwitch(switches::kEnableAggressiveDOMStorageFlushing);
98 // This is needed to be able to mmap the V8 snapshot and ICU data file
99 // directly from the WebView .apk.
100 // This needs to be here so that it gets to run before the code in
101 // content_main_runner that reads these values tries to do so.
102 // In multi-process mode this code would live in
103 // AwContentBrowserClient::GetAdditionalMappedFilesForChildProcess.
104 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
105 #ifdef __LP64__
106 const char kNativesFileName[] = "natives_blob_64.bin";
107 const char kSnapshotFileName[] = "snapshot_blob_64.bin";
108 #else
109 const char kNativesFileName[] = "natives_blob_32.bin";
110 const char kSnapshotFileName[] = "snapshot_blob_32.bin";
111 #endif // __LP64__
112 // TODO(gsennton) we should use
113 // gin::IsolateHolder::kNativesFileName/kSnapshotFileName
114 // here when those files have arch specific names http://crbug.com/455699
115 CHECK(AwAssets::RegisterAssetWithGlobalDescriptors(
116 kV8NativesDataDescriptor, kNativesFileName));
117 CHECK(AwAssets::RegisterAssetWithGlobalDescriptors(
118 kV8SnapshotDataDescriptor, kSnapshotFileName));
119 #endif
120 CHECK(AwAssets::RegisterAssetWithGlobalDescriptors(
121 kAndroidICUDataDescriptor, base::i18n::kIcuDataFileName));
123 return false;
126 void AwMainDelegate::PreSandboxStartup() {
127 // TODO(torne): When we have a separate renderer process, we need to handle
128 // being passed open FDs for the resource paks here.
129 #if defined(ARCH_CPU_ARM_FAMILY)
130 // Create an instance of the CPU class to parse /proc/cpuinfo and cache
131 // cpu_brand info.
132 base::CPU cpu_info;
133 #endif
135 crash_reporter::EnableMicrodumpCrashReporter();
138 void AwMainDelegate::SandboxInitialized(const std::string& process_type) {
139 // TODO(torne): Adjust linux OOM score here.
142 int AwMainDelegate::RunProcess(
143 const std::string& process_type,
144 const content::MainFunctionParams& main_function_params) {
145 if (process_type.empty()) {
146 AwBrowserDependencyFactoryImpl::InstallInstance();
148 browser_runner_.reset(content::BrowserMainRunner::Create());
149 int exit_code = browser_runner_->Initialize(main_function_params);
150 DCHECK_LT(exit_code, 0);
152 g_allow_wait_in_ui_thread.Get().reset(
153 new ScopedAllowWaitForLegacyWebViewApi);
155 // Return 0 so that we do NOT trigger the default behavior. On Android, the
156 // UI message loop is managed by the Java application.
157 return 0;
160 return -1;
163 void AwMainDelegate::ProcessExiting(const std::string& process_type) {
164 // TODO(torne): Clean up resources when we handle them.
166 logging::CloseLogFile();
169 content::ContentBrowserClient*
170 AwMainDelegate::CreateContentBrowserClient() {
171 content_browser_client_.reset(new AwContentBrowserClient(this));
172 return content_browser_client_.get();
175 content::ContentRendererClient*
176 AwMainDelegate::CreateContentRendererClient() {
177 content_renderer_client_.reset(new AwContentRendererClient());
178 return content_renderer_client_.get();
181 scoped_refptr<AwQuotaManagerBridge> AwMainDelegate::CreateAwQuotaManagerBridge(
182 AwBrowserContext* browser_context) {
183 return AwQuotaManagerBridgeImpl::Create(browser_context);
186 content::WebContentsViewDelegate* AwMainDelegate::CreateViewDelegate(
187 content::WebContents* web_contents) {
188 return AwWebContentsViewDelegate::Create(web_contents);
191 AwWebPreferencesPopulater* AwMainDelegate::CreateWebPreferencesPopulater() {
192 return new AwWebPreferencesPopulaterImpl();
195 AwMessagePortService* AwMainDelegate::CreateAwMessagePortService() {
196 return new AwMessagePortServiceImpl();
199 #if defined(VIDEO_HOLE)
200 content::ExternalVideoSurfaceContainer*
201 AwMainDelegate::CreateExternalVideoSurfaceContainer(
202 content::WebContents* web_contents) {
203 return external_video_surface::ExternalVideoSurfaceContainerImpl::Create(
204 web_contents);
206 #endif
208 } // namespace android_webview