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