blob: 7166e0049623299801c5dd8106287e207de97989 [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"
8#include "android_webview/browser/aw_result_codes.h"
[email protected]dd1c2a72014-07-29 18:11:339#include "android_webview/native/aw_assets.h"
[email protected]197284842012-11-20 03:39:0110#include "base/android/build_info.h"
yfriedman38b461382014-10-20 19:40:1611#include "base/android/locale_utils.h"
[email protected]e1632c92013-09-30 00:47:5912#include "base/android/memory_pressure_listener_android.h"
[email protected]2df8d412014-03-15 05:28:4113#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:5214#include "base/files/file_path.h"
[email protected]17237f12012-12-06 17:37:3515#include "base/path_service.h"
[email protected]f81fa202012-10-25 23:32:2816#include "content/public/browser/render_process_host.h"
17#include "content/public/common/content_client.h"
[email protected]2df8d412014-03-15 05:28:4118#include "content/public/common/content_switches.h"
[email protected]f81fa202012-10-25 23:32:2819#include "content/public/common/result_codes.h"
[email protected]f16cc6782013-12-16 23:42:5720#include "content/public/common/url_utils.h"
[email protected]2df8d412014-03-15 05:28:4121#include "gpu/command_buffer/service/mailbox_synchronizer.h"
[email protected]f81fa202012-10-25 23:32:2822#include "net/android/network_change_notifier_factory_android.h"
23#include "net/base/network_change_notifier.h"
[email protected]dd1c2a72014-07-29 18:11:3324#include "ui/base/l10n/l10n_util.h"
[email protected]17237f12012-12-06 17:37:3525#include "ui/base/layout.h"
[email protected]f81fa202012-10-25 23:32:2826#include "ui/base/resource/resource_bundle.h"
[email protected]17237f12012-12-06 17:37:3527#include "ui/base/ui_base_paths.h"
[email protected]f81fa202012-10-25 23:32:2828
29namespace android_webview {
30
31AwBrowserMainParts::AwBrowserMainParts(AwBrowserContext* browser_context)
32 : browser_context_(browser_context) {
33}
34
35AwBrowserMainParts::~AwBrowserMainParts() {
36}
37
38void AwBrowserMainParts::PreEarlyInitialization() {
39 net::NetworkChangeNotifier::SetFactory(
40 new net::NetworkChangeNotifierFactoryAndroid());
[email protected]f81fa202012-10-25 23:32:2841
42 // Android WebView does not use default MessageLoop. It has its own
[email protected]f4d884432013-01-08 03:08:5043 // Android specific MessageLoop. Also see MainMessageLoopRun.
[email protected]f81fa202012-10-25 23:32:2844 DCHECK(!main_message_loop_.get());
[email protected]166f2ed2013-12-19 19:34:0045 main_message_loop_.reset(new base::MessageLoopForUI);
[email protected]9e1d6b12013-04-27 04:48:4446 base::MessageLoopForUI::current()->Start();
[email protected]f81fa202012-10-25 23:32:2847}
48
49int AwBrowserMainParts::PreCreateThreads() {
[email protected]dd1c2a72014-07-29 18:11:3350 int pak_fd = 0;
51 int64 pak_off = 0;
52 int64 pak_len = 0;
[email protected]17237f12012-12-06 17:37:3553
[email protected]f6f423f2014-07-30 16:51:0654 // 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.
[email protected]4ce2aa62014-08-14 01:05:4959 ui::ResourceBundle::InitSharedInstanceWithLocale(
yfriedman38b461382014-10-20 19:40:1660 base::android::GetDefaultLocale(),
[email protected]4ce2aa62014-08-14 01:05:4961 NULL,
62 ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
[email protected]f6f423f2014-07-30 16:51:0663 std::string locale = l10n_util::GetApplicationLocale(std::string()) + ".pak";
64 if (AwAssets::OpenAsset(locale, &pak_fd, &pak_off, &pak_len)) {
65 VLOG(0) << "Load from apk succesful, fd=" << pak_fd << " off=" << pak_off
66 << " len=" << pak_len;
67 ui::ResourceBundle::CleanupSharedInstance();
68 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
[email protected]9bbe2782014-08-12 15:26:3469 base::File(pak_fd), base::MemoryMappedFile::Region(pak_off, pak_len));
[email protected]f6f423f2014-07-30 16:51:0670 } else {
71 LOG(WARNING) << "Failed to load " << locale << ".pak from the apk too. "
72 "Bringing up WebView without any locale";
[email protected]dd1c2a72014-07-29 18:11:3373 }
[email protected]c11f4fe2013-02-19 19:50:4674
[email protected]dd1c2a72014-07-29 18:11:3375 // Try to directly mmap the webviewchromium.pak from the apk. Fall back to
76 // load from file, using PATH_SERVICE, otherwise.
77 if (AwAssets::OpenAsset("webviewchromium.pak", &pak_fd, &pak_off, &pak_len)) {
78 VLOG(0) << "Loading webviewchromium.pak from, fd:" << pak_fd
79 << " off:" << pak_off << " len:" << pak_len;
80 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion(
81 base::File(pak_fd),
82 base::MemoryMappedFile::Region(pak_off, pak_len),
83 ui::SCALE_FACTOR_NONE);
84 } else {
85 base::FilePath pak_path;
86 PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &pak_path);
87 LOG(WARNING) << "Cannot load webviewchromium.pak assets from the apk. "
88 "Falling back loading it from " << pak_path.MaybeAsASCII();
89 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
90 pak_path.AppendASCII("webviewchromium.pak"), ui::SCALE_FACTOR_NONE);
91 }
[email protected]17237f12012-12-06 17:37:3592
[email protected]e1632c92013-09-30 00:47:5993 base::android::MemoryPressureListenerAndroid::RegisterSystemCallback(
94 base::android::AttachCurrentThread());
95
[email protected]f81fa202012-10-25 23:32:2896 return content::RESULT_CODE_NORMAL_EXIT;
97}
98
[email protected]f2ab1b152013-01-25 04:38:4199void AwBrowserMainParts::PreMainMessageLoopRun() {
[email protected]8705654b2014-05-24 01:16:19100 // TODO(boliu): Can't support accelerated 2d canvas and WebGL with ubercomp
101 // yet: crbug.com/352424.
[email protected]02391412014-06-16 18:05:57102 if (!gpu::gles2::MailboxSynchronizer::Initialize()) {
[email protected]8705654b2014-05-24 01:16:19103 CommandLine* cl = CommandLine::ForCurrentProcess();
104 cl->AppendSwitch(switches::kDisableAccelerated2dCanvas);
105 cl->AppendSwitch(switches::kDisableExperimentalWebGL);
[email protected]2df8d412014-03-15 05:28:41106 }
107
[email protected]f2ab1b152013-01-25 04:38:41108 browser_context_->PreMainMessageLoopRun();
[email protected]f16cc6782013-12-16 23:42:57109 // This is needed for WebView Classic backwards compatibility
110 // See crbug.com/298495
111 content::SetMaxURLChars(20 * 1024 * 1024);
[email protected]f2ab1b152013-01-25 04:38:41112}
113
[email protected]f81fa202012-10-25 23:32:28114bool AwBrowserMainParts::MainMessageLoopRun(int* result_code) {
115 // Android WebView does not use default MessageLoop. It has its own
116 // Android specific MessageLoop.
117 return true;
118}
119
[email protected]f81fa202012-10-25 23:32:28120} // namespace android_webview