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/browser/aw_browser_main_parts.h"
6
7#include "android_webview/browser/aw_browser_context.h"
8#include "android_webview/browser/aw_result_codes.h"
9#include "base/android/build_info.h"
10#include "base/android/memory_pressure_listener_android.h"
11#include "base/files/file_path.h"
12#include "base/path_service.h"
13#include "content/public/browser/android/compositor.h"
14#include "content/public/browser/render_process_host.h"
15#include "content/public/common/content_client.h"
16#include "content/public/common/result_codes.h"
17#include "net/android/network_change_notifier_factory_android.h"
18#include "net/base/network_change_notifier.h"
19#include "ui/base/l10n/l10n_util_android.h"
20#include "ui/base/layout.h"
21#include "ui/base/resource/resource_bundle.h"
22#include "ui/base/ui_base_paths.h"
23
24namespace android_webview {
25
26AwBrowserMainParts::AwBrowserMainParts(AwBrowserContext* browser_context)
27    : browser_context_(browser_context) {
28}
29
30AwBrowserMainParts::~AwBrowserMainParts() {
31}
32
33void AwBrowserMainParts::PreEarlyInitialization() {
34  net::NetworkChangeNotifier::SetFactory(
35      new net::NetworkChangeNotifierFactoryAndroid());
36  content::Compositor::InitializeWithFlags(
37      content::Compositor::DIRECT_CONTEXT_ON_DRAW_THREAD);
38
39  // Android WebView does not use default MessageLoop. It has its own
40  // Android specific MessageLoop. Also see MainMessageLoopRun.
41  DCHECK(!main_message_loop_.get());
42  main_message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_UI));
43  base::MessageLoopForUI::current()->Start();
44}
45
46int AwBrowserMainParts::PreCreateThreads() {
47  browser_context_->InitializeBeforeThreadCreation();
48
49  ui::ResourceBundle::InitSharedInstanceLocaleOnly(
50      l10n_util::GetDefaultLocale(), NULL);
51
52  base::FilePath pak_path;
53  PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &pak_path);
54
55  ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
56      pak_path.AppendASCII("webviewchromium.pak"),
57      ui::SCALE_FACTOR_NONE);
58
59  base::android::MemoryPressureListenerAndroid::RegisterSystemCallback(
60      base::android::AttachCurrentThread());
61
62  return content::RESULT_CODE_NORMAL_EXIT;
63}
64
65void AwBrowserMainParts::PreMainMessageLoopRun() {
66  browser_context_->PreMainMessageLoopRun();
67}
68
69bool AwBrowserMainParts::MainMessageLoopRun(int* result_code) {
70  // Android WebView does not use default MessageLoop. It has its own
71  // Android specific MessageLoop.
72  return true;
73}
74
75}  // namespace android_webview
76