Update V8 to version 4.5.107.
[chromium-blink-merge.git] / android_webview / browser / aw_browser_main_parts.cc
blobc312757c424302acad071e884018cdaa5f4187a3
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.
5 #include "android_webview/browser/aw_browser_main_parts.h"
7 #include "android_webview/browser/aw_browser_context.h"
8 #include "android_webview/browser/aw_dev_tools_discovery_provider.h"
9 #include "android_webview/browser/aw_media_client_android.h"
10 #include "android_webview/browser/aw_result_codes.h"
11 #include "android_webview/common/aw_resource.h"
12 #include "base/android/apk_assets.h"
13 #include "base/android/build_info.h"
14 #include "base/android/locale_utils.h"
15 #include "base/android/memory_pressure_listener_android.h"
16 #include "base/files/file_path.h"
17 #include "base/path_service.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "content/public/common/content_client.h"
20 #include "content/public/common/content_switches.h"
21 #include "content/public/common/result_codes.h"
22 #include "content/public/common/url_utils.h"
23 #include "media/base/android/media_client_android.h"
24 #include "net/android/network_change_notifier_factory_android.h"
25 #include "net/base/network_change_notifier.h"
26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/base/layout.h"
28 #include "ui/base/resource/resource_bundle.h"
29 #include "ui/base/resource/resource_bundle_android.h"
30 #include "ui/base/ui_base_paths.h"
31 #include "ui/gl/gl_surface.h"
33 namespace android_webview {
35 AwBrowserMainParts::AwBrowserMainParts(AwBrowserContext* browser_context)
36 : browser_context_(browser_context) {
39 AwBrowserMainParts::~AwBrowserMainParts() {
42 void AwBrowserMainParts::PreEarlyInitialization() {
43 net::NetworkChangeNotifier::SetFactory(
44 new net::NetworkChangeNotifierFactoryAndroid());
46 // Android WebView does not use default MessageLoop. It has its own
47 // Android specific MessageLoop. Also see MainMessageLoopRun.
48 DCHECK(!main_message_loop_.get());
49 main_message_loop_.reset(new base::MessageLoopForUI);
50 base::MessageLoopForUI::current()->Start();
53 int AwBrowserMainParts::PreCreateThreads() {
54 base::MemoryMappedFile::Region pak_region =
55 base::MemoryMappedFile::Region::kWholeFile;
57 // TODO(primiano, mkosiba): GetApplicationLocale requires a ResourceBundle
58 // instance to be present to work correctly so we call this (knowing it will
59 // fail) just to create the ResourceBundle instance. We should refactor
60 // ResourceBundle/GetApplicationLocale to not require an instance to be
61 // initialized.
62 ui::SetLocalePaksStoredInApk(true);
63 ui::ResourceBundle::InitSharedInstanceWithLocale(
64 base::android::GetDefaultLocale(),
65 NULL,
66 ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
67 std::string locale = l10n_util::GetApplicationLocale(std::string());
68 std::string pak_path = ui::GetPathForAndroidLocalePakWithinApk(locale);
69 int pak_fd = base::android::OpenApkAsset(pak_path, &pak_region);
70 if (pak_fd != -1) {
71 ui::ResourceBundle::CleanupSharedInstance();
72 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
73 base::File(pak_fd), pak_region);
74 } else {
75 LOG(WARNING) << "Failed to load " << locale << ".pak from the apk. "
76 "Bringing up WebView without any locale";
79 // Try to directly mmap the webviewchromium.pak from the apk. Fall back to
80 // load from file, using PATH_SERVICE, otherwise.
81 pak_fd = base::android::OpenApkAsset("assets/webviewchromium.pak",
82 &pak_region);
83 if (pak_fd != -1) {
84 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion(
85 base::File(pak_fd), pak_region, ui::SCALE_FACTOR_NONE);
86 } else {
87 base::FilePath pak_path;
88 PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &pak_path);
89 LOG(WARNING) << "Cannot load webviewchromium.pak assets from the apk. "
90 "Falling back loading it from " << pak_path.MaybeAsASCII();
91 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
92 pak_path.AppendASCII("webviewchromium.pak"), ui::SCALE_FACTOR_NONE);
95 base::android::MemoryPressureListenerAndroid::RegisterSystemCallback(
96 base::android::AttachCurrentThread());
98 return content::RESULT_CODE_NORMAL_EXIT;
101 void AwBrowserMainParts::PreMainMessageLoopRun() {
102 browser_context_->PreMainMessageLoopRun();
104 AwDevToolsDiscoveryProvider::Install();
106 media::SetMediaClientAndroid(
107 new AwMediaClientAndroid(AwResource::GetConfigKeySystemUuidMapping()));
109 gfx::GLSurface::InitializeOneOff();
111 // This is needed for WebView Classic backwards compatibility
112 // See crbug.com/298495
113 content::SetMaxURLChars(20 * 1024 * 1024);
116 bool AwBrowserMainParts::MainMessageLoopRun(int* result_code) {
117 // Android WebView does not use default MessageLoop. It has its own
118 // Android specific MessageLoop.
119 return true;
122 } // namespace android_webview