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/browser/deferred_gpu_command_service.h"
12 #include "android_webview/common/aw_resource.h"
13 #include "android_webview/common/aw_switches.h"
14 #include "base/android/apk_assets.h"
15 #include "base/android/build_info.h"
16 #include "base/android/locale_utils.h"
17 #include "base/android/memory_pressure_listener_android.h"
18 #include "base/command_line.h"
19 #include "base/files/file_path.h"
20 #include "base/path_service.h"
21 #include "content/public/browser/android/synchronous_compositor.h"
22 #include "content/public/browser/render_frame_host.h"
23 #include "content/public/browser/render_process_host.h"
24 #include "content/public/common/content_client.h"
25 #include "content/public/common/content_switches.h"
26 #include "content/public/common/result_codes.h"
27 #include "content/public/common/url_utils.h"
28 #include "media/base/android/media_client_android.h"
29 #include "net/android/network_change_notifier_factory_android.h"
30 #include "net/base/network_change_notifier.h"
31 #include "ui/base/l10n/l10n_util.h"
32 #include "ui/base/layout.h"
33 #include "ui/base/resource/resource_bundle.h"
34 #include "ui/base/resource/resource_bundle_android.h"
35 #include "ui/base/ui_base_paths.h"
36 #include "ui/gl/gl_surface.h"
38 namespace android_webview
{
40 AwBrowserMainParts::AwBrowserMainParts(AwBrowserContext
* browser_context
)
41 : browser_context_(browser_context
) {
44 AwBrowserMainParts::~AwBrowserMainParts() {
47 void AwBrowserMainParts::PreEarlyInitialization() {
48 net::NetworkChangeNotifier::SetFactory(
49 new net::NetworkChangeNotifierFactoryAndroid());
51 // Android WebView does not use default MessageLoop. It has its own
52 // Android specific MessageLoop. Also see MainMessageLoopRun.
53 DCHECK(!main_message_loop_
.get());
54 main_message_loop_
.reset(new base::MessageLoopForUI
);
55 base::MessageLoopForUI::current()->Start();
58 int AwBrowserMainParts::PreCreateThreads() {
59 base::MemoryMappedFile::Region pak_region
=
60 base::MemoryMappedFile::Region::kWholeFile
;
62 // TODO(primiano, mkosiba): GetApplicationLocale requires a ResourceBundle
63 // instance to be present to work correctly so we call this (knowing it will
64 // fail) just to create the ResourceBundle instance. We should refactor
65 // ResourceBundle/GetApplicationLocale to not require an instance to be
67 ui::SetLocalePaksStoredInApk(true);
68 ui::ResourceBundle::InitSharedInstanceWithLocale(
69 base::android::GetDefaultLocale(),
71 ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES
);
72 std::string locale
= l10n_util::GetApplicationLocale(std::string());
73 std::string pak_path
= ui::GetPathForAndroidLocalePakWithinApk(locale
);
74 int pak_fd
= base::android::OpenApkAsset(pak_path
, &pak_region
);
76 ui::ResourceBundle::CleanupSharedInstance();
77 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
78 base::File(pak_fd
), pak_region
);
80 LOG(WARNING
) << "Failed to load " << locale
<< ".pak from the apk. "
81 "Bringing up WebView without any locale";
84 // Try to directly mmap the webviewchromium.pak from the apk. Fall back to
85 // load from file, using PATH_SERVICE, otherwise.
86 pak_fd
= base::android::OpenApkAsset("assets/webviewchromium.pak",
89 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion(
90 base::File(pak_fd
), pak_region
, ui::SCALE_FACTOR_NONE
);
92 base::FilePath pak_path
;
93 PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID
, &pak_path
);
94 LOG(WARNING
) << "Cannot load webviewchromium.pak assets from the apk. "
95 "Falling back loading it from " << pak_path
.MaybeAsASCII();
96 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
97 pak_path
.AppendASCII("webviewchromium.pak"), ui::SCALE_FACTOR_NONE
);
100 base::android::MemoryPressureListenerAndroid::RegisterSystemCallback(
101 base::android::AttachCurrentThread());
102 DeferredGpuCommandService::SetInstance();
104 return content::RESULT_CODE_NORMAL_EXIT
;
107 void AwBrowserMainParts::PreMainMessageLoopRun() {
108 browser_context_
->PreMainMessageLoopRun();
110 AwDevToolsDiscoveryProvider::Install();
112 media::SetMediaClientAndroid(
113 new AwMediaClientAndroid(AwResource::GetConfigKeySystemUuidMapping()));
115 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
116 switches::kUseIpcCommandBuffer
)) {
117 content::SynchronousCompositor::SetUseIpcCommandBuffer();
119 gfx::GLSurface::InitializeOneOff();
122 content::RenderFrameHost::AllowInjectingJavaScriptForAndroidWebView();
124 // This is needed for WebView Classic backwards compatibility
125 // See crbug.com/298495
126 content::SetMaxURLChars(20 * 1024 * 1024);
129 bool AwBrowserMainParts::MainMessageLoopRun(int* result_code
) {
130 // Android WebView does not use default MessageLoop. It has its own
131 // Android specific MessageLoop.
135 } // namespace android_webview