aw_main_delegate.cc revision 58537e28ecd584eab876aee8be7156509866d23a
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/gpu_memory_buffer_factory_impl.h"
9#include "android_webview/browser/in_process_view_renderer.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_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 "cc/base/switches.h"
22#include "content/public/browser/browser_main_runner.h"
23#include "content/public/browser/browser_thread.h"
24#include "content/public/common/content_switches.h"
25#include "gpu/command_buffer/client/gl_in_process_context.h"
26#include "gpu/command_buffer/service/in_process_command_buffer.h"
27#include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
28
29namespace android_webview {
30
31namespace {
32
33// TODO(boliu): Remove this global Allow once the underlying issues are
34// resolved - http://crbug.com/240453. See AwMainDelegate::RunProcess below.
35base::LazyInstance<scoped_ptr<ScopedAllowWaitForLegacyWebViewApi> >
36    g_allow_wait_in_ui_thread = LAZY_INSTANCE_INITIALIZER;
37
38}
39
40AwMainDelegate::AwMainDelegate()
41    : gpu_memory_buffer_factory_(new GpuMemoryBufferFactoryImpl) {
42}
43
44AwMainDelegate::~AwMainDelegate() {
45}
46
47bool AwMainDelegate::BasicStartupComplete(int* exit_code) {
48  content::SetContentClient(&content_client_);
49
50  gpu::InProcessCommandBuffer::SetGpuMemoryBufferFactory(
51      gpu_memory_buffer_factory_.get());
52  gpu::InProcessCommandBuffer::EnableVirtualizedContext();
53
54  InProcessViewRenderer::CalculateTileMemoryPolicy();
55
56  CommandLine* cl = CommandLine::ForCurrentProcess();
57  cl->AppendSwitch(switches::kEnableBeginFrameScheduling);
58  cl->AppendSwitch(cc::switches::kEnableMapImage);
59
60  // WebView uses the Android system's scrollbars and overscroll glow.
61  cl->AppendSwitch(switches::kHideScrollbars);
62  cl->AppendSwitch(switches::kDisableOverscrollEdgeEffect);
63
64  // Not yet supported in single-process mode.
65  cl->AppendSwitch(switches::kDisableExperimentalWebGL);
66  cl->AppendSwitch(switches::kDisableSharedWorkers);
67
68  // Ganesh backed 2D-Canvas is not yet working and causes crashes.
69  cl->AppendSwitch(switches::kDisableAccelerated2dCanvas);
70
71  // File system API not supported (requires some new API; internal bug 6930981)
72  cl->AppendSwitch(switches::kDisableFileSystem);
73
74  // Enable D-PAD navigation for application compatibility.
75  cl->AppendSwitch(switches::kEnableSpatialNavigation);
76
77  return false;
78}
79
80void AwMainDelegate::PreSandboxStartup() {
81  // TODO(torne): When we have a separate renderer process, we need to handle
82  // being passed open FDs for the resource paks here.
83}
84
85void AwMainDelegate::SandboxInitialized(const std::string& process_type) {
86  // TODO(torne): Adjust linux OOM score here.
87}
88
89int AwMainDelegate::RunProcess(
90    const std::string& process_type,
91    const content::MainFunctionParams& main_function_params) {
92  if (process_type.empty()) {
93    AwBrowserDependencyFactoryImpl::InstallInstance();
94
95    browser_runner_.reset(content::BrowserMainRunner::Create());
96    int exit_code = browser_runner_->Initialize(main_function_params);
97    DCHECK(exit_code < 0);
98
99    g_allow_wait_in_ui_thread.Get().reset(
100        new ScopedAllowWaitForLegacyWebViewApi);
101
102    // Return 0 so that we do NOT trigger the default behavior. On Android, the
103    // UI message loop is managed by the Java application.
104    return 0;
105  }
106
107  return -1;
108}
109
110void AwMainDelegate::ProcessExiting(const std::string& process_type) {
111  // TODO(torne): Clean up resources when we handle them.
112
113  logging::CloseLogFile();
114}
115
116content::ContentBrowserClient*
117    AwMainDelegate::CreateContentBrowserClient() {
118  content_browser_client_.reset(new AwContentBrowserClient(this));
119  return content_browser_client_.get();
120}
121
122content::ContentRendererClient*
123    AwMainDelegate::CreateContentRendererClient() {
124  content_renderer_client_.reset(new AwContentRendererClient());
125  return content_renderer_client_.get();
126}
127
128AwQuotaManagerBridge* AwMainDelegate::CreateAwQuotaManagerBridge(
129    AwBrowserContext* browser_context) {
130  return new AwQuotaManagerBridgeImpl(browser_context);
131}
132
133content::GeolocationPermissionContext*
134    AwMainDelegate::CreateGeolocationPermission(
135        AwBrowserContext* browser_context) {
136  return AwGeolocationPermissionContext::Create(browser_context);
137}
138
139content::WebContentsViewDelegate* AwMainDelegate::CreateViewDelegate(
140    content::WebContents* web_contents) {
141  return AwWebContentsViewDelegate::Create(web_contents);
142}
143
144}  // namespace android_webview
145