blob: 2041abdde8ca62444c38f2a6e46c8ffd7d592850 [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"
tobiasjs5b457a442017-01-10 09:35:098#include "android_webview/browser/aw_browser_terminator.h"
ben5be0b9132016-08-03 00:17:189#include "android_webview/browser/aw_content_browser_client.h"
[email protected]f81fa202012-10-25 23:32:2810#include "android_webview/browser/aw_result_codes.h"
boliuc5befe72015-07-21 19:08:5611#include "android_webview/browser/deferred_gpu_command_service.h"
timvolodinebbaeb5b2016-03-02 19:58:4712#include "android_webview/browser/net/aw_network_change_notifier_factory.h"
tobiasjsc1f4d0f32017-01-14 22:57:5513#include "android_webview/common/aw_descriptors.h"
14#include "android_webview/common/aw_paths.h"
gunsch2288c532015-04-30 03:58:4515#include "android_webview/common/aw_resource.h"
boliu1463e1e2015-07-23 00:18:0416#include "android_webview/common/aw_switches.h"
tobiasjsc1f4d0f32017-01-14 22:57:5517#include "android_webview/common/crash_reporter/aw_microdump_crash_reporter.h"
agrieve3b864c92015-05-27 18:08:3818#include "base/android/apk_assets.h"
[email protected]197284842012-11-20 03:39:0119#include "base/android/build_info.h"
yfriedman38b461382014-10-20 19:40:1620#include "base/android/locale_utils.h"
[email protected]e1632c92013-09-30 00:47:5921#include "base/android/memory_pressure_listener_android.h"
boliu1463e1e2015-07-23 00:18:0422#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:5223#include "base/files/file_path.h"
boliu40123db2016-01-16 01:38:2024#include "base/i18n/rtl.h"
[email protected]17237f12012-12-06 17:37:3525#include "base/path_service.h"
tobiasjsc1f4d0f32017-01-14 22:57:5526#include "components/crash/content/browser/crash_dump_manager_android.h"
tobiasjs5b457a442017-01-10 09:35:0927#include "components/crash/content/browser/crash_dump_observer_android.h"
boliu1463e1e2015-07-23 00:18:0428#include "content/public/browser/android/synchronous_compositor.h"
Jochen Eisinger14ea9772015-07-24 12:04:3729#include "content/public/browser/render_frame_host.h"
[email protected]f81fa202012-10-25 23:32:2830#include "content/public/browser/render_process_host.h"
31#include "content/public/common/content_client.h"
[email protected]2df8d412014-03-15 05:28:4132#include "content/public/common/content_switches.h"
[email protected]f81fa202012-10-25 23:32:2833#include "content/public/common/result_codes.h"
mcasas4e216e72016-07-28 21:28:3834#include "device/geolocation/access_token_store.h"
35#include "device/geolocation/geolocation_delegate.h"
36#include "device/geolocation/geolocation_provider.h"
[email protected]f81fa202012-10-25 23:32:2837#include "net/android/network_change_notifier_factory_android.h"
38#include "net/base/network_change_notifier.h"
[email protected]dd1c2a72014-07-29 18:11:3339#include "ui/base/l10n/l10n_util.h"
[email protected]17237f12012-12-06 17:37:3540#include "ui/base/layout.h"
[email protected]f81fa202012-10-25 23:32:2841#include "ui/base/resource/resource_bundle.h"
agrievebffa19f32015-06-25 19:55:3442#include "ui/base/resource/resource_bundle_android.h"
[email protected]17237f12012-12-06 17:37:3543#include "ui/base/ui_base_paths.h"
tobiasjsca238b3b2015-06-24 22:53:5444#include "ui/gl/gl_surface.h"
[email protected]f81fa202012-10-25 23:32:2845
46namespace android_webview {
mcasas80ef5232016-07-22 23:00:4747namespace {
48
mcasas4e216e72016-07-28 21:28:3849class AwAccessTokenStore : public device::AccessTokenStore {
mcasas80ef5232016-07-22 23:00:4750 public:
51 AwAccessTokenStore() { }
52
mcasas4e216e72016-07-28 21:28:3853 // device::AccessTokenStore implementation
mcasas80ef5232016-07-22 23:00:4754 void LoadAccessTokens(const LoadAccessTokensCallback& request) override {
55 AccessTokenStore::AccessTokenMap access_token_map;
56 // AccessTokenMap and net::URLRequestContextGetter not used on Android,
57 // but Run needs to be called to finish the geolocation setup.
58 request.Run(access_token_map, NULL);
59 }
60 void SaveAccessToken(const GURL& server_url,
61 const base::string16& access_token) override {}
62
63 private:
64 ~AwAccessTokenStore() override {}
65
66 DISALLOW_COPY_AND_ASSIGN(AwAccessTokenStore);
67};
68
69// A provider of Geolocation services to override AccessTokenStore.
mcasas4e216e72016-07-28 21:28:3870class AwGeolocationDelegate : public device::GeolocationDelegate {
mcasas80ef5232016-07-22 23:00:4771 public:
72 AwGeolocationDelegate() = default;
73
mcasas4e216e72016-07-28 21:28:3874 scoped_refptr<device::AccessTokenStore> CreateAccessTokenStore() final {
mcasas80ef5232016-07-22 23:00:4775 return new AwAccessTokenStore();
76 }
77
78 private:
79 DISALLOW_COPY_AND_ASSIGN(AwGeolocationDelegate);
80};
81
82} // anonymous namespace
[email protected]f81fa202012-10-25 23:32:2883
ben5be0b9132016-08-03 00:17:1884AwBrowserMainParts::AwBrowserMainParts(AwContentBrowserClient* browser_client)
85 : browser_client_(browser_client) {
[email protected]f81fa202012-10-25 23:32:2886}
87
88AwBrowserMainParts::~AwBrowserMainParts() {
89}
90
91void AwBrowserMainParts::PreEarlyInitialization() {
timvolodinebbaeb5b2016-03-02 19:58:4792 net::NetworkChangeNotifier::SetFactory(new AwNetworkChangeNotifierFactory());
[email protected]f81fa202012-10-25 23:32:2893
94 // Android WebView does not use default MessageLoop. It has its own
[email protected]f4d884432013-01-08 03:08:5095 // Android specific MessageLoop. Also see MainMessageLoopRun.
[email protected]f81fa202012-10-25 23:32:2896 DCHECK(!main_message_loop_.get());
[email protected]166f2ed2013-12-19 19:34:0097 main_message_loop_.reset(new base::MessageLoopForUI);
[email protected]9e1d6b12013-04-27 04:48:4498 base::MessageLoopForUI::current()->Start();
[email protected]f81fa202012-10-25 23:32:2899}
100
101int AwBrowserMainParts::PreCreateThreads() {
agrieve96cd9182015-06-25 20:50:26102 ui::SetLocalePaksStoredInApk(true);
mnaganov2f73f792015-08-21 16:24:48103 std::string locale = ui::ResourceBundle::InitSharedInstanceWithLocale(
yirui91741642016-10-28 04:29:59104 base::android::GetDefaultLocaleString(), NULL,
aberent3bcea372016-08-16 12:12:53105 ui::ResourceBundle::LOAD_COMMON_RESOURCES);
mnaganov2f73f792015-08-21 16:24:48106 if (locale.empty()) {
107 LOG(WARNING) << "Failed to load locale .pak from the apk. "
108 "Bringing up WebView without any locale";
[email protected]dd1c2a72014-07-29 18:11:33109 }
boliu40123db2016-01-16 01:38:20110 base::i18n::SetICUDefaultLocale(locale);
[email protected]c11f4fe2013-02-19 19:50:46111
aberent3bcea372016-08-16 12:12:53112 // Try to directly mmap the resources.pak from the apk. Fall back to load
113 // from file, using PATH_SERVICE, otherwise.
114 base::FilePath pak_file_path;
115 PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &pak_file_path);
116 pak_file_path = pak_file_path.AppendASCII("resources.pak");
117 ui::LoadMainAndroidPackFile("assets/resources.pak", pak_file_path);
[email protected]17237f12012-12-06 17:37:35118
[email protected]e1632c92013-09-30 00:47:59119 base::android::MemoryPressureListenerAndroid::RegisterSystemCallback(
120 base::android::AttachCurrentThread());
boliuc5befe72015-07-21 19:08:56121 DeferredGpuCommandService::SetInstance();
tobiasjs5b457a442017-01-10 09:35:09122 breakpad::CrashDumpObserver::Create();
tobiasjsc1f4d0f32017-01-14 22:57:55123
124 if (crash_reporter::IsCrashReporterEnabled()) {
125 base::FilePath crash_dir;
126 if (PathService::Get(android_webview::DIR_CRASH_DUMPS, &crash_dir)) {
127 if (!base::PathExists(crash_dir))
128 base::CreateDirectory(crash_dir);
129 breakpad::CrashDumpObserver::GetInstance()->RegisterClient(
130 base::MakeUnique<breakpad::CrashDumpManager>(
131 crash_dir, kAndroidMinidumpDescriptor));
132 }
133 }
134
tobiasjs90bce3c2017-04-10 17:15:44135 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
136 switches::kWebViewSandboxedRenderer)) {
mnaganovf322c8f2016-01-08 02:49:31137 // Create the renderers crash manager on the UI thread.
tobiasjs5b457a442017-01-10 09:35:09138 breakpad::CrashDumpObserver::GetInstance()->RegisterClient(
139 base::MakeUnique<AwBrowserTerminator>());
mnaganovf322c8f2016-01-08 02:49:31140 }
[email protected]e1632c92013-09-30 00:47:59141
[email protected]f81fa202012-10-25 23:32:28142 return content::RESULT_CODE_NORMAL_EXIT;
143}
144
[email protected]f2ab1b152013-01-25 04:38:41145void AwBrowserMainParts::PreMainMessageLoopRun() {
ben5be0b9132016-08-03 00:17:18146 browser_client_->InitBrowserContext()->PreMainMessageLoopRun();
dgozman0115d2a262015-04-22 18:39:13147
mcasas4e216e72016-07-28 21:28:38148 device::GeolocationProvider::SetGeolocationDelegate(
mcasas80ef5232016-07-22 23:00:47149 new AwGeolocationDelegate());
150
Jochen Eisinger14ea9772015-07-24 12:04:37151 content::RenderFrameHost::AllowInjectingJavaScriptForAndroidWebView();
[email protected]f2ab1b152013-01-25 04:38:41152}
153
[email protected]f81fa202012-10-25 23:32:28154bool AwBrowserMainParts::MainMessageLoopRun(int* result_code) {
155 // Android WebView does not use default MessageLoop. It has its own
156 // Android specific MessageLoop.
157 return true;
158}
159
[email protected]f81fa202012-10-25 23:32:28160} // namespace android_webview