Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / android_webview / browser / aw_browser_main_parts.cc
blobbe793ee8c0ba0fe2c311afd1b46e0e092eb00bac
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_result_codes.h"
10 #include "android_webview/native/public/aw_assets.h"
11 #include "base/android/build_info.h"
12 #include "base/android/locale_utils.h"
13 #include "base/android/memory_pressure_listener_android.h"
14 #include "base/files/file_path.h"
15 #include "base/path_service.h"
16 #include "components/devtools_discovery/devtools_discovery_manager.h"
17 #include "content/public/browser/render_process_host.h"
18 #include "content/public/common/content_client.h"
19 #include "content/public/common/content_switches.h"
20 #include "content/public/common/result_codes.h"
21 #include "content/public/common/url_utils.h"
22 #include "net/android/network_change_notifier_factory_android.h"
23 #include "net/base/network_change_notifier.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/base/layout.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/base/ui_base_paths.h"
29 namespace android_webview {
31 AwBrowserMainParts::AwBrowserMainParts(AwBrowserContext* browser_context)
32 : browser_context_(browser_context) {
35 AwBrowserMainParts::~AwBrowserMainParts() {
38 void AwBrowserMainParts::PreEarlyInitialization() {
39 net::NetworkChangeNotifier::SetFactory(
40 new net::NetworkChangeNotifierFactoryAndroid());
42 // Android WebView does not use default MessageLoop. It has its own
43 // Android specific MessageLoop. Also see MainMessageLoopRun.
44 DCHECK(!main_message_loop_.get());
45 main_message_loop_.reset(new base::MessageLoopForUI);
46 base::MessageLoopForUI::current()->Start();
49 int AwBrowserMainParts::PreCreateThreads() {
50 int pak_fd = 0;
51 int64 pak_off = 0;
52 int64 pak_len = 0;
54 // TODO(primiano, mkosiba): GetApplicationLocale requires a ResourceBundle
55 // instance to be present to work correctly so we call this (knowing it will
56 // fail) just to create the ResourceBundle instance. We should refactor
57 // ResourceBundle/GetApplicationLocale to not require an instance to be
58 // initialized.
59 ui::ResourceBundle::InitSharedInstanceWithLocale(
60 base::android::GetDefaultLocale(),
61 NULL,
62 ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
63 std::string locale = l10n_util::GetApplicationLocale(std::string()) + ".pak";
64 if (AwAssets::OpenAsset(locale, &pak_fd, &pak_off, &pak_len)) {
65 ui::ResourceBundle::CleanupSharedInstance();
66 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
67 base::File(pak_fd), base::MemoryMappedFile::Region(pak_off, pak_len));
68 } else {
69 LOG(WARNING) << "Failed to load " << locale << ".pak from the apk too. "
70 "Bringing up WebView without any locale";
73 // Try to directly mmap the webviewchromium.pak from the apk. Fall back to
74 // load from file, using PATH_SERVICE, otherwise.
75 if (AwAssets::OpenAsset("webviewchromium.pak", &pak_fd, &pak_off, &pak_len)) {
76 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion(
77 base::File(pak_fd),
78 base::MemoryMappedFile::Region(pak_off, pak_len),
79 ui::SCALE_FACTOR_NONE);
80 } else {
81 base::FilePath pak_path;
82 PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &pak_path);
83 LOG(WARNING) << "Cannot load webviewchromium.pak assets from the apk. "
84 "Falling back loading it from " << pak_path.MaybeAsASCII();
85 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
86 pak_path.AppendASCII("webviewchromium.pak"), ui::SCALE_FACTOR_NONE);
89 base::android::MemoryPressureListenerAndroid::RegisterSystemCallback(
90 base::android::AttachCurrentThread());
92 return content::RESULT_CODE_NORMAL_EXIT;
95 void AwBrowserMainParts::PreMainMessageLoopRun() {
96 browser_context_->PreMainMessageLoopRun();
98 devtools_discovery::DevToolsDiscoveryManager* discovery_manager =
99 devtools_discovery::DevToolsDiscoveryManager::GetInstance();
100 discovery_manager->AddProvider(make_scoped_ptr(
101 new AwDevToolsDiscoveryProvider()));
103 // This is needed for WebView Classic backwards compatibility
104 // See crbug.com/298495
105 content::SetMaxURLChars(20 * 1024 * 1024);
108 bool AwBrowserMainParts::MainMessageLoopRun(int* result_code) {
109 // Android WebView does not use default MessageLoop. It has its own
110 // Android specific MessageLoop.
111 return true;
114 } // namespace android_webview