blob: c312757c424302acad071e884018cdaa5f4187a3 [file] [log] [blame]
[email protected]f81fa202012-10-25 23:32:281// 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"
dgozman0115d2a262015-04-22 18:39:138#include "android_webview/browser/aw_dev_tools_discovery_provider.h"
gunsch2288c532015-04-30 03:58:459#include "android_webview/browser/aw_media_client_android.h"
[email protected]f81fa202012-10-25 23:32:2810#include "android_webview/browser/aw_result_codes.h"
gunsch2288c532015-04-30 03:58:4511#include "android_webview/common/aw_resource.h"
agrieve3b864c92015-05-27 18:08:3812#include "base/android/apk_assets.h"
[email protected]197284842012-11-20 03:39:0113#include "base/android/build_info.h"
yfriedman38b461382014-10-20 19:40:1614#include "base/android/locale_utils.h"
[email protected]e1632c92013-09-30 00:47:5915#include "base/android/memory_pressure_listener_android.h"
[email protected]57999812013-02-24 05:40:5216#include "base/files/file_path.h"
[email protected]17237f12012-12-06 17:37:3517#include "base/path_service.h"
[email protected]f81fa202012-10-25 23:32:2818#include "content/public/browser/render_process_host.h"
19#include "content/public/common/content_client.h"
[email protected]2df8d412014-03-15 05:28:4120#include "content/public/common/content_switches.h"
[email protected]f81fa202012-10-25 23:32:2821#include "content/public/common/result_codes.h"
[email protected]f16cc6782013-12-16 23:42:5722#include "content/public/common/url_utils.h"
gunsch2288c532015-04-30 03:58:4523#include "media/base/android/media_client_android.h"
[email protected]f81fa202012-10-25 23:32:2824#include "net/android/network_change_notifier_factory_android.h"
25#include "net/base/network_change_notifier.h"
[email protected]dd1c2a72014-07-29 18:11:3326#include "ui/base/l10n/l10n_util.h"
[email protected]17237f12012-12-06 17:37:3527#include "ui/base/layout.h"
[email protected]f81fa202012-10-25 23:32:2828#include "ui/base/resource/resource_bundle.h"
agrievebffa19f32015-06-25 19:55:3429#include "ui/base/resource/resource_bundle_android.h"
[email protected]17237f12012-12-06 17:37:3530#include "ui/base/ui_base_paths.h"
tobiasjsca238b3b2015-06-24 22:53:5431#include "ui/gl/gl_surface.h"
[email protected]f81fa202012-10-25 23:32:2832
33namespace android_webview {
34
35AwBrowserMainParts::AwBrowserMainParts(AwBrowserContext* browser_context)
36 : browser_context_(browser_context) {
37}
38
39AwBrowserMainParts::~AwBrowserMainParts() {
40}
41
42void AwBrowserMainParts::PreEarlyInitialization() {
43 net::NetworkChangeNotifier::SetFactory(
44 new net::NetworkChangeNotifierFactoryAndroid());
[email protected]f81fa202012-10-25 23:32:2845
46 // Android WebView does not use default MessageLoop. It has its own
[email protected]f4d884432013-01-08 03:08:5047 // Android specific MessageLoop. Also see MainMessageLoopRun.
[email protected]f81fa202012-10-25 23:32:2848 DCHECK(!main_message_loop_.get());
[email protected]166f2ed2013-12-19 19:34:0049 main_message_loop_.reset(new base::MessageLoopForUI);
[email protected]9e1d6b12013-04-27 04:48:4450 base::MessageLoopForUI::current()->Start();
[email protected]f81fa202012-10-25 23:32:2851}
52
53int AwBrowserMainParts::PreCreateThreads() {
agrieve3b864c92015-05-27 18:08:3854 base::MemoryMappedFile::Region pak_region =
55 base::MemoryMappedFile::Region::kWholeFile;
[email protected]17237f12012-12-06 17:37:3556
[email protected]f6f423f2014-07-30 16:51:0657 // 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.
agrieve96cd9182015-06-25 20:50:2662 ui::SetLocalePaksStoredInApk(true);
[email protected]4ce2aa62014-08-14 01:05:4963 ui::ResourceBundle::InitSharedInstanceWithLocale(
yfriedman38b461382014-10-20 19:40:1664 base::android::GetDefaultLocale(),
[email protected]4ce2aa62014-08-14 01:05:4965 NULL,
66 ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
agrievebffa19f32015-06-25 19:55:3467 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);
agrieve3b864c92015-05-27 18:08:3870 if (pak_fd != -1) {
[email protected]f6f423f2014-07-30 16:51:0671 ui::ResourceBundle::CleanupSharedInstance();
72 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
agrieve3b864c92015-05-27 18:08:3873 base::File(pak_fd), pak_region);
[email protected]f6f423f2014-07-30 16:51:0674 } else {
agrievebffa19f32015-06-25 19:55:3475 LOG(WARNING) << "Failed to load " << locale << ".pak from the apk. "
[email protected]f6f423f2014-07-30 16:51:0676 "Bringing up WebView without any locale";
[email protected]dd1c2a72014-07-29 18:11:3377 }
[email protected]c11f4fe2013-02-19 19:50:4678
[email protected]dd1c2a72014-07-29 18:11:3379 // Try to directly mmap the webviewchromium.pak from the apk. Fall back to
80 // load from file, using PATH_SERVICE, otherwise.
agrieve62536bc2015-06-05 03:01:3381 pak_fd = base::android::OpenApkAsset("assets/webviewchromium.pak",
82 &pak_region);
agrieve3b864c92015-05-27 18:08:3883 if (pak_fd != -1) {
[email protected]dd1c2a72014-07-29 18:11:3384 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion(
agrieve3b864c92015-05-27 18:08:3885 base::File(pak_fd), pak_region, ui::SCALE_FACTOR_NONE);
[email protected]dd1c2a72014-07-29 18:11:3386 } 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);
93 }
[email protected]17237f12012-12-06 17:37:3594
[email protected]e1632c92013-09-30 00:47:5995 base::android::MemoryPressureListenerAndroid::RegisterSystemCallback(
96 base::android::AttachCurrentThread());
97
[email protected]f81fa202012-10-25 23:32:2898 return content::RESULT_CODE_NORMAL_EXIT;
99}
100
[email protected]f2ab1b152013-01-25 04:38:41101void AwBrowserMainParts::PreMainMessageLoopRun() {
102 browser_context_->PreMainMessageLoopRun();
dgozman0115d2a262015-04-22 18:39:13103
dgozmanec2b982a2015-04-28 10:36:39104 AwDevToolsDiscoveryProvider::Install();
dgozman0115d2a262015-04-22 18:39:13105
gunsch2288c532015-04-30 03:58:45106 media::SetMediaClientAndroid(
107 new AwMediaClientAndroid(AwResource::GetConfigKeySystemUuidMapping()));
108
tobiasjsca238b3b2015-06-24 22:53:54109 gfx::GLSurface::InitializeOneOff();
110
[email protected]f16cc6782013-12-16 23:42:57111 // This is needed for WebView Classic backwards compatibility
112 // See crbug.com/298495
113 content::SetMaxURLChars(20 * 1024 * 1024);
[email protected]f2ab1b152013-01-25 04:38:41114}
115
[email protected]f81fa202012-10-25 23:32:28116bool AwBrowserMainParts::MainMessageLoopRun(int* result_code) {
117 // Android WebView does not use default MessageLoop. It has its own
118 // Android specific MessageLoop.
119 return true;
120}
121
[email protected]f81fa202012-10-25 23:32:28122} // namespace android_webview