aw_main_delegate.cc revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
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/in_process_renderer/in_process_renderer_client.h"
9#include "android_webview/browser/scoped_allow_wait_for_legacy_web_view_api.h"
10#include "android_webview/common/aw_switches.h"
11#include "android_webview/lib/aw_browser_dependency_factory_impl.h"
12#include "android_webview/native/aw_geolocation_permission_context.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/renderer/aw_content_renderer_client.h"
16#include "base/command_line.h"
17#include "base/lazy_instance.h"
18#include "base/logging.h"
19#include "base/memory/scoped_ptr.h"
20#include "base/threading/thread_restrictions.h"
21#include "content/public/browser/browser_main_runner.h"
22#include "content/public/browser/browser_thread.h"
23#include "content/public/common/content_switches.h"
24#include "webkit/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
25
26namespace android_webview {
27
28namespace {
29
30// TODO(boliu): Remove these global Allows once the underlying issues
31// are resolved. See AwMainDelegate::RunProcess below.
32
33base::LazyInstance<scoped_ptr<ScopedAllowWaitForLegacyWebViewApi> >
34    g_allow_wait_in_ui_thread = LAZY_INSTANCE_INITIALIZER;
35
36base::LazyInstance<scoped_ptr<base::ThreadRestrictions::ScopedAllowIO> >
37    g_allow_io_in_ui_thread = LAZY_INSTANCE_INITIALIZER;
38
39bool UIAndRendererCompositorThreadsNotMerged() {
40  return CommandLine::ForCurrentProcess()->HasSwitch(
41      switches::kNoMergeUIAndRendererCompositorThreads);
42}
43}
44
45AwMainDelegate::AwMainDelegate() {
46}
47
48AwMainDelegate::~AwMainDelegate() {
49}
50
51bool AwMainDelegate::BasicStartupComplete(int* exit_code) {
52  content::SetContentClient(&content_client_);
53
54  webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl
55      ::EnableVirtualizedContext();
56
57  CommandLine* cl = CommandLine::ForCurrentProcess();
58
59  // Temporarily disable merged thread mode until proper hardware init is done.
60  // Currently hardware draw with incomplete init is making invalid GL calls
61  // that is crashing in graphics driver on Nexus 7.
62  if (!cl->HasSwitch("merge-ui-and-compositor-threads"))
63    cl->AppendSwitch(switches::kNoMergeUIAndRendererCompositorThreads);
64
65  if (UIAndRendererCompositorThreadsNotMerged()) {
66    cl->AppendSwitch(switches::kEnableWebViewSynchronousAPIs);
67  } else {
68    // Set the command line to enable synchronous API compatibility.
69    cl->AppendSwitch(switches::kEnableSynchronousRendererCompositor);
70  }
71  return false;
72}
73
74void AwMainDelegate::PreSandboxStartup() {
75  // TODO(torne): When we have a separate renderer process, we need to handle
76  // being passed open FDs for the resource paks here.
77}
78
79void AwMainDelegate::SandboxInitialized(const std::string& process_type) {
80  // TODO(torne): Adjust linux OOM score here.
81}
82
83int AwMainDelegate::RunProcess(
84    const std::string& process_type,
85    const content::MainFunctionParams& main_function_params) {
86  if (process_type.empty()) {
87    AwBrowserDependencyFactoryImpl::InstallInstance();
88
89    browser_runner_.reset(content::BrowserMainRunner::Create());
90    int exit_code = browser_runner_->Initialize(main_function_params);
91    DCHECK(exit_code < 0);
92
93    if (!UIAndRendererCompositorThreadsNotMerged()) {
94      // This is temporary until we remove the browser compositor
95      g_allow_wait_in_ui_thread.Get().reset(
96          new ScopedAllowWaitForLegacyWebViewApi);
97
98      // TODO(boliu): This is a HUGE hack to work around the fact that
99      // cc::WorkerPool joins on worker threads on the UI thread.
100      // See crbug.com/239423.
101      g_allow_io_in_ui_thread.Get().reset(
102          new base::ThreadRestrictions::ScopedAllowIO);
103    }
104
105    // Return 0 so that we do NOT trigger the default behavior. On Android, the
106    // UI message loop is managed by the Java application.
107    return 0;
108  }
109
110  return -1;
111}
112
113void AwMainDelegate::ProcessExiting(const std::string& process_type) {
114  // TODO(torne): Clean up resources when we handle them.
115
116  logging::CloseLogFile();
117}
118
119content::ContentBrowserClient*
120    AwMainDelegate::CreateContentBrowserClient() {
121  content_browser_client_.reset(new AwContentBrowserClient(this));
122  return content_browser_client_.get();
123}
124
125content::ContentRendererClient*
126    AwMainDelegate::CreateContentRendererClient() {
127  // None of this makes sense for multiprocess.
128  DCHECK(CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess));
129  // During transition period allow running in either threading mode; eventually
130  // only the compositor/UI thread merge mode will be supported.
131  const bool no_merge_threads = UIAndRendererCompositorThreadsNotMerged();
132  content_renderer_client_.reset(
133      no_merge_threads ? new AwContentRendererClient() :
134                         new InProcessRendererClient());
135  return content_renderer_client_.get();
136}
137
138AwQuotaManagerBridge* AwMainDelegate::CreateAwQuotaManagerBridge(
139    AwBrowserContext* browser_context) {
140  return new AwQuotaManagerBridgeImpl(browser_context);
141}
142
143content::GeolocationPermissionContext*
144    AwMainDelegate::CreateGeolocationPermission(
145        AwBrowserContext* browser_context) {
146  return AwGeolocationPermissionContext::Create(browser_context);
147}
148
149content::WebContentsViewDelegate* AwMainDelegate::CreateViewDelegate(
150    content::WebContents* web_contents) {
151  return AwWebContentsViewDelegate::Create(web_contents);
152}
153
154}  // namespace android_webview
155