aw_main_delegate.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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.
4
5#include "android_webview/lib/main/aw_main_delegate.h"
6
7#include "android_webview/browser/aw_content_browser_client.h"
8#include "android_webview/browser/browser_view_renderer.h"
9#include "android_webview/browser/gpu_memory_buffer_factory_impl.h"
10#include "android_webview/browser/scoped_allow_wait_for_legacy_web_view_api.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_quota_manager_bridge_impl.h"
14#include "android_webview/native/aw_web_contents_view_delegate.h"
15#include "android_webview/native/aw_web_preferences_populater_impl.h"
16#include "android_webview/native/external_video_surface_container_impl.h"
17#include "android_webview/renderer/aw_content_renderer_client.h"
18#include "base/command_line.h"
19#include "base/cpu.h"
20#include "base/lazy_instance.h"
21#include "base/logging.h"
22#include "base/memory/scoped_ptr.h"
23#include "base/threading/thread_restrictions.h"
24#include "content/browser/media/android/browser_media_player_manager.h"
25#include "content/public/browser/browser_main_runner.h"
26#include "content/public/browser/browser_thread.h"
27#include "content/public/common/content_switches.h"
28#include "gpu/command_buffer/client/gl_in_process_context.h"
29#include "media/base/media_switches.h"
30#include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
31
32namespace android_webview {
33
34namespace {
35
36// TODO(boliu): Remove this global Allow once the underlying issues are
37// resolved - http://crbug.com/240453. See AwMainDelegate::RunProcess below.
38base::LazyInstance<scoped_ptr<ScopedAllowWaitForLegacyWebViewApi> >
39    g_allow_wait_in_ui_thread = LAZY_INSTANCE_INITIALIZER;
40
41}
42
43AwMainDelegate::AwMainDelegate()
44    : gpu_memory_buffer_factory_(new GpuMemoryBufferFactoryImpl) {
45}
46
47AwMainDelegate::~AwMainDelegate() {
48}
49
50bool AwMainDelegate::BasicStartupComplete(int* exit_code) {
51  content::SetContentClient(&content_client_);
52
53  CommandLine* cl = CommandLine::ForCurrentProcess();
54  bool zero_copy_disabled_by_switch = cl->HasSwitch(switches::kDisableZeroCopy);
55  bool use_zero_copy = !zero_copy_disabled_by_switch &&
56                       cl->HasSwitch(switches::kEnableZeroCopy) &&
57                       gpu_memory_buffer_factory_.get()->Initialize();
58
59  if (use_zero_copy) {
60    cl->AppendSwitch(switches::kEnableZeroCopy);
61  } else if (!zero_copy_disabled_by_switch) {
62    cl->AppendSwitch(switches::kDisableZeroCopy);
63  }
64
65  content::BrowserMediaPlayerManager::RegisterMediaUrlInterceptor(
66      new AwMediaUrlInterceptor());
67
68  BrowserViewRenderer::CalculateTileMemoryPolicy(use_zero_copy);
69
70  cl->AppendSwitch(switches::kEnableBeginFrameScheduling);
71  cl->AppendSwitch(switches::kEnableImplSidePainting);
72
73  // WebView uses the Android system's scrollbars and overscroll glow.
74  cl->AppendSwitch(switches::kDisableOverscrollEdgeEffect);
75
76  // Not yet supported in single-process mode.
77  cl->AppendSwitch(switches::kDisableSharedWorkers);
78
79  // File system API not supported (requires some new API; internal bug 6930981)
80  cl->AppendSwitch(switches::kDisableFileSystem);
81
82  // For fullscreen video we create a new container view to host the
83  // WebContents, ie. the FullscreenView. As a result we cannot reuse the
84  // embedded video blocker attached to the old container view, so we create a
85  // new blocker instead attached to the ContentVideoView.
86  cl->AppendSwitch(switches::kEnableContentVideoViewPowerSaveBlocker);
87
88#if defined(VIDEO_HOLE)
89  // Support EME/L1 with hole-punching.
90  cl->AppendSwitch(switches::kMediaDrmEnableNonCompositing);
91#endif
92
93  // WebRTC hardware decoding is not supported, internal bug 15075307
94  cl->AppendSwitch(switches::kDisableWebRtcHWDecoding);
95
96  return false;
97}
98
99void AwMainDelegate::PreSandboxStartup() {
100  // TODO(torne): When we have a separate renderer process, we need to handle
101  // being passed open FDs for the resource paks here.
102#if defined(ARCH_CPU_ARM_FAMILY)
103  // Create an instance of the CPU class to parse /proc/cpuinfo and cache
104  // cpu_brand info.
105  base::CPU cpu_info;
106#endif
107}
108
109void AwMainDelegate::SandboxInitialized(const std::string& process_type) {
110  // TODO(torne): Adjust linux OOM score here.
111}
112
113int AwMainDelegate::RunProcess(
114    const std::string& process_type,
115    const content::MainFunctionParams& main_function_params) {
116  if (process_type.empty()) {
117    AwBrowserDependencyFactoryImpl::InstallInstance();
118
119    browser_runner_.reset(content::BrowserMainRunner::Create());
120    int exit_code = browser_runner_->Initialize(main_function_params);
121    DCHECK_LT(exit_code, 0);
122
123    g_allow_wait_in_ui_thread.Get().reset(
124        new ScopedAllowWaitForLegacyWebViewApi);
125
126    // Return 0 so that we do NOT trigger the default behavior. On Android, the
127    // UI message loop is managed by the Java application.
128    return 0;
129  }
130
131  return -1;
132}
133
134void AwMainDelegate::ProcessExiting(const std::string& process_type) {
135  // TODO(torne): Clean up resources when we handle them.
136
137  logging::CloseLogFile();
138}
139
140content::ContentBrowserClient*
141    AwMainDelegate::CreateContentBrowserClient() {
142  content_browser_client_.reset(new AwContentBrowserClient(this));
143  return content_browser_client_.get();
144}
145
146content::ContentRendererClient*
147    AwMainDelegate::CreateContentRendererClient() {
148  content_renderer_client_.reset(new AwContentRendererClient());
149  return content_renderer_client_.get();
150}
151
152scoped_refptr<AwQuotaManagerBridge> AwMainDelegate::CreateAwQuotaManagerBridge(
153    AwBrowserContext* browser_context) {
154  return AwQuotaManagerBridgeImpl::Create(browser_context);
155}
156
157content::WebContentsViewDelegate* AwMainDelegate::CreateViewDelegate(
158    content::WebContents* web_contents) {
159  return AwWebContentsViewDelegate::Create(web_contents);
160}
161
162AwWebPreferencesPopulater* AwMainDelegate::CreateWebPreferencesPopulater() {
163  return new AwWebPreferencesPopulaterImpl();
164}
165
166#if defined(VIDEO_HOLE)
167content::ExternalVideoSurfaceContainer*
168AwMainDelegate::CreateExternalVideoSurfaceContainer(
169    content::WebContents* web_contents) {
170  return new ExternalVideoSurfaceContainerImpl(web_contents);
171}
172#endif
173
174}  // namespace android_webview
175