browser_main.cc 8.67 KB
Newer Older
1 2 3 4 5
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "libcef/browser/browser_main.h"
6

7 8
#include <stdint.h>

9 10
#include <string>

11
#include "libcef/browser/browser_context_impl.h"
12
#include "libcef/browser/browser_context_keyed_service_factories.h"
13
#include "libcef/browser/content_browser_client.h"
14
#include "libcef/browser/context.h"
15
#include "libcef/browser/devtools/devtools_manager_delegate.h"
16
#include "libcef/browser/extensions/extension_system_factory.h"
17
#include "libcef/browser/extensions/extensions_browser_client.h"
18
#include "libcef/browser/net/chrome_scheme_handler.h"
19
#include "libcef/browser/printing/printing_message_filter.h"
20
#include "libcef/browser/thread_util.h"
21 22
#include "libcef/common/extensions/extensions_client.h"
#include "libcef/common/extensions/extensions_util.h"
23
#include "libcef/common/net/net_resource_provider.h"
24 25

#include "base/bind.h"
26
#include "base/message_loop/message_loop.h"
27
#include "base/strings/string_number_conversions.h"
28
#include "base/task/post_task.h"
29
#include "chrome/browser/chrome_browser_main_extra_parts.h"
30
#include "chrome/browser/plugins/plugin_finder.h"
31
#include "content/public/browser/gpu_data_manager.h"
32 33
#include "extensions/browser/extension_system.h"
#include "extensions/common/constants.h"
34
#include "net/base/net_module.h"
35
#include "services/service_manager/embedder/result_codes.h"
36
#include "ui/base/material_design/material_design_controller.h"
37 38
#include "ui/base/resource/resource_bundle.h"

39 40 41 42
#if defined(USE_AURA) && defined(USE_X11)
#include "ui/events/devices/x11/touch_factory_x11.h"
#endif

43 44
#if defined(USE_AURA)
#include "ui/aura/env.h"
45
#include "ui/display/screen.h"
46 47
#include "ui/views/test/desktop_test_views_delegate.h"
#include "ui/views/widget/desktop_aura/desktop_screen.h"
48
#include "ui/wm/core/wm_state.h"
49 50 51 52

#if defined(OS_WIN)
#include "ui/base/cursor/cursor_loader_win.h"
#endif
53 54
#endif  // defined(USE_AURA)

55
#if defined(USE_AURA) && defined(OS_LINUX)
56 57 58
#include "ui/base/ime/input_method_initializer.h"
#endif

59 60 61 62
#if defined(OS_LINUX)
#include "libcef/browser/printing/print_dialog_linux.h"
#endif

63 64
CefBrowserMainParts::CefBrowserMainParts(
    const content::MainFunctionParams& parameters)
65
    : BrowserMainParts(), devtools_delegate_(NULL) {}
66

67 68 69 70 71
CefBrowserMainParts::~CefBrowserMainParts() {
  for (int i = static_cast<int>(chrome_extra_parts_.size()) - 1; i >= 0; --i)
    delete chrome_extra_parts_[i];
  chrome_extra_parts_.clear();
}
72

73 74
void CefBrowserMainParts::AddParts(ChromeBrowserMainExtraParts* parts) {
  chrome_extra_parts_.push_back(parts);
75 76
}

77
int CefBrowserMainParts::PreEarlyInitialization() {
78
#if defined(USE_AURA) && defined(OS_LINUX)
79 80 81 82
  // TODO(linux): Consider using a real input method or
  // views::LinuxUI::SetInstance.
  ui::InitializeInputMethodForTesting();
#endif
83 84 85 86

  for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
    chrome_extra_parts_[i]->PreEarlyInitialization();

87
  return service_manager::RESULT_CODE_NORMAL_EXIT;
88 89
}

90 91 92 93 94
void CefBrowserMainParts::PostEarlyInitialization() {
  for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
    chrome_extra_parts_[i]->PostEarlyInitialization();
}

95 96
void CefBrowserMainParts::ToolkitInitialized() {
#if defined(USE_AURA)
97
  CHECK(aura::Env::GetInstance());
98 99

  new views::DesktopTestViewsDelegate;
100

101 102
  wm_state_.reset(new wm::WMState);

103 104 105
#if defined(OS_WIN)
  ui::CursorLoaderWin::SetCursorResourceModule(
      CefContentBrowserClient::Get()->GetResourceDllName());
106
#endif
107
#endif  // defined(USE_AURA)
108 109 110 111 112 113

  for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
    chrome_extra_parts_[i]->ToolkitInitialized();
}

void CefBrowserMainParts::PreMainMessageLoopStart() {
114 115 116 117
#if defined(USE_AURA) && defined(USE_X11)
  ui::TouchFactory::SetTouchDeviceListFromCommandLine();
#endif

118 119
  for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
    chrome_extra_parts_[i]->PreMainMessageLoopStart();
120 121
}

122
void CefBrowserMainParts::PostMainMessageLoopStart() {
123 124 125
#if defined(OS_LINUX)
  printing::PrintingContextLinux::SetCreatePrintDialogFunction(
      &CefPrintDialogLinux::CreatePrintDialog);
126 127
  printing::PrintingContextLinux::SetPdfPaperSizeFunction(
      &CefPrintDialogLinux::GetPdfPaperSize);
128
#endif
129 130 131

  for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
    chrome_extra_parts_[i]->PostMainMessageLoopStart();
132 133
}

134
int CefBrowserMainParts::PreCreateThreads() {
135
#if defined(OS_WIN)
136
  PlatformInitialize();
137 138
#endif

139
  net::NetModule::SetResourceProvider(&NetResourceProvider);
140 141 142 143 144

  // Initialize the GpuDataManager before IO access restrictions are applied and
  // before the IO thread is started.
  content::GpuDataManager::GetInstance();

145 146 147
  for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
    chrome_extra_parts_[i]->PreCreateThreads();

148 149 150
  return 0;
}

151 152 153 154 155 156
void CefBrowserMainParts::ServiceManagerConnectionStarted(
    content::ServiceManagerConnection* connection) {
  for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
    chrome_extra_parts_[i]->ServiceManagerConnectionStarted(connection);
}

157
void CefBrowserMainParts::PreMainMessageLoopRun() {
158 159 160 161
#if defined(USE_AURA)
  display::Screen::SetScreenInstance(views::CreateDesktopScreen());
#endif

162 163
  ui::MaterialDesignController::Initialize();

164 165 166
  // CEF's profile is a BrowserContext.
  PreProfileInit();

167 168 169 170 171
  if (extensions::ExtensionsEnabled()) {
    // Initialize extension global objects before creating the global
    // BrowserContext.
    extensions_client_.reset(new extensions::CefExtensionsClient());
    extensions::ExtensionsClient::Set(extensions_client_.get());
172 173
    extensions_browser_client_.reset(
        new extensions::CefExtensionsBrowserClient);
174 175 176 177 178
    extensions::ExtensionsBrowserClient::Set(extensions_browser_client_.get());

    extensions::CefExtensionSystemFactory::GetInstance();
  }

179 180 181 182
  // Register additional KeyedService factories here. See
  // ChromeBrowserMainExtraPartsProfiles for details.
  cef::EnsureBrowserContextKeyedServiceFactoriesBuilt();

183 184
  printing::CefPrintingMessageFilter::EnsureShutdownNotifierFactoryBuilt();

185
  background_task_runner_ = base::CreateSingleThreadTaskRunnerWithTraits(
186
      {base::TaskPriority::BEST_EFFORT,
187 188 189 190 191 192 193 194
       base::TaskShutdownBehavior::BLOCK_SHUTDOWN, base::MayBlock()});
  user_visible_task_runner_ = base::CreateSingleThreadTaskRunnerWithTraits(
      {base::TaskPriority::USER_VISIBLE,
       base::TaskShutdownBehavior::BLOCK_SHUTDOWN, base::MayBlock()});
  user_blocking_task_runner_ = base::CreateSingleThreadTaskRunnerWithTraits(
      {base::TaskPriority::USER_BLOCKING,
       base::TaskShutdownBehavior::BLOCK_SHUTDOWN, base::MayBlock()});

195 196 197
  CefRequestContextSettings settings;
  CefContext::Get()->PopulateRequestContextSettings(&settings);

198 199 200 201 202
  // Create the global RequestContext.
  global_request_context_ =
      CefRequestContextImpl::CreateGlobalRequestContext(settings);
  CefBrowserContextImpl* browser_context = static_cast<CefBrowserContextImpl*>(
      global_request_context_->GetBrowserContext());
203

204 205
  PostProfileInit();

206
  CefDevToolsManagerDelegate::StartHttpHandler(browser_context);
207

208 209
  // Triggers initialization of the singleton instance on UI thread.
  PluginFinder::GetInstance()->Init();
210

211
  scheme::RegisterWebUIControllerFactory();
212 213 214 215 216 217 218

  // These have no equivalent in CEF.
  PreBrowserStart();
  PostBrowserStart();

  for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
    chrome_extra_parts_[i]->PreMainMessageLoopRun();
219 220 221
}

void CefBrowserMainParts::PostMainMessageLoopRun() {
222 223 224
  // NOTE: Destroy objects in reverse order of creation.
  CefDevToolsManagerDelegate::StopHttpHandler();

225 226 227
  // There should be no additional references to the global CefRequestContext
  // during shutdown. Did you forget to release a CefBrowser reference?
  DCHECK(global_request_context_->HasOneRef());
228
  global_request_context_ = NULL;
229

230 231
  for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
    chrome_extra_parts_[i]->PostMainMessageLoopRun();
232 233
}

234
void CefBrowserMainParts::PostDestroyThreads() {
235 236 237 238 239
  if (extensions::ExtensionsEnabled()) {
    extensions::ExtensionsBrowserClient::Set(NULL);
    extensions_browser_client_.reset();
  }

240
#if defined(USE_AURA)
241 242
  // Delete the DesktopTestViewsDelegate.
  delete views::ViewsDelegate::GetInstance();
243
#endif
244
}
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

void CefBrowserMainParts::PreProfileInit() {
  for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
    chrome_extra_parts_[i]->PreProfileInit();
}

void CefBrowserMainParts::PostProfileInit() {
  for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
    chrome_extra_parts_[i]->PostProfileInit();
}

void CefBrowserMainParts::PreBrowserStart() {
  for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
    chrome_extra_parts_[i]->PreBrowserStart();
}

void CefBrowserMainParts::PostBrowserStart() {
  for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
    chrome_extra_parts_[i]->PostBrowserStart();
}