[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "android_webview/browser/aw_browser_main_parts.h" |
| 6 | |
| 7 | #include "android_webview/browser/aw_browser_context.h" |
tobiasjs | 5b457a44 | 2017-01-10 09:35:09 | [diff] [blame] | 8 | #include "android_webview/browser/aw_browser_terminator.h" |
ben | 5be0b913 | 2016-08-03 00:17:18 | [diff] [blame] | 9 | #include "android_webview/browser/aw_content_browser_client.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 10 | #include "android_webview/browser/aw_result_codes.h" |
boliu | c5befe7 | 2015-07-21 19:08:56 | [diff] [blame] | 11 | #include "android_webview/browser/deferred_gpu_command_service.h" |
timvolodine | bbaeb5b | 2016-03-02 19:58:47 | [diff] [blame] | 12 | #include "android_webview/browser/net/aw_network_change_notifier_factory.h" |
tobiasjs | c1f4d0f3 | 2017-01-14 22:57:55 | [diff] [blame] | 13 | #include "android_webview/common/aw_descriptors.h" |
| 14 | #include "android_webview/common/aw_paths.h" |
gunsch | 2288c53 | 2015-04-30 03:58:45 | [diff] [blame] | 15 | #include "android_webview/common/aw_resource.h" |
boliu | 1463e1e | 2015-07-23 00:18:04 | [diff] [blame] | 16 | #include "android_webview/common/aw_switches.h" |
tobiasjs | c1f4d0f3 | 2017-01-14 22:57:55 | [diff] [blame] | 17 | #include "android_webview/common/crash_reporter/aw_microdump_crash_reporter.h" |
agrieve | 3b864c9 | 2015-05-27 18:08:38 | [diff] [blame] | 18 | #include "base/android/apk_assets.h" |
[email protected] | 19728484 | 2012-11-20 03:39:01 | [diff] [blame] | 19 | #include "base/android/build_info.h" |
yfriedman | 38b46138 | 2014-10-20 19:40:16 | [diff] [blame] | 20 | #include "base/android/locale_utils.h" |
[email protected] | e1632c9 | 2013-09-30 00:47:59 | [diff] [blame] | 21 | #include "base/android/memory_pressure_listener_android.h" |
boliu | 1463e1e | 2015-07-23 00:18:04 | [diff] [blame] | 22 | #include "base/command_line.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 23 | #include "base/files/file_path.h" |
boliu | 40123db | 2016-01-16 01:38:20 | [diff] [blame] | 24 | #include "base/i18n/rtl.h" |
[email protected] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 25 | #include "base/path_service.h" |
tobiasjs | c1f4d0f3 | 2017-01-14 22:57:55 | [diff] [blame] | 26 | #include "components/crash/content/browser/crash_dump_manager_android.h" |
tobiasjs | 5b457a44 | 2017-01-10 09:35:09 | [diff] [blame] | 27 | #include "components/crash/content/browser/crash_dump_observer_android.h" |
boliu | 1463e1e | 2015-07-23 00:18:04 | [diff] [blame] | 28 | #include "content/public/browser/android/synchronous_compositor.h" |
Jochen Eisinger | 14ea977 | 2015-07-24 12:04:37 | [diff] [blame] | 29 | #include "content/public/browser/render_frame_host.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 30 | #include "content/public/browser/render_process_host.h" |
| 31 | #include "content/public/common/content_client.h" |
[email protected] | 2df8d41 | 2014-03-15 05:28:41 | [diff] [blame] | 32 | #include "content/public/common/content_switches.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 33 | #include "content/public/common/result_codes.h" |
mcasas | 4e216e7 | 2016-07-28 21:28:38 | [diff] [blame] | 34 | #include "device/geolocation/access_token_store.h" |
| 35 | #include "device/geolocation/geolocation_delegate.h" |
| 36 | #include "device/geolocation/geolocation_provider.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 37 | #include "net/android/network_change_notifier_factory_android.h" |
| 38 | #include "net/base/network_change_notifier.h" |
[email protected] | dd1c2a7 | 2014-07-29 18:11:33 | [diff] [blame] | 39 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 40 | #include "ui/base/layout.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 41 | #include "ui/base/resource/resource_bundle.h" |
agrieve | bffa19f3 | 2015-06-25 19:55:34 | [diff] [blame] | 42 | #include "ui/base/resource/resource_bundle_android.h" |
[email protected] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 43 | #include "ui/base/ui_base_paths.h" |
tobiasjs | ca238b3b | 2015-06-24 22:53:54 | [diff] [blame] | 44 | #include "ui/gl/gl_surface.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 45 | |
| 46 | namespace android_webview { |
mcasas | 80ef523 | 2016-07-22 23:00:47 | [diff] [blame] | 47 | namespace { |
| 48 | |
mcasas | 4e216e7 | 2016-07-28 21:28:38 | [diff] [blame] | 49 | class AwAccessTokenStore : public device::AccessTokenStore { |
mcasas | 80ef523 | 2016-07-22 23:00:47 | [diff] [blame] | 50 | public: |
| 51 | AwAccessTokenStore() { } |
| 52 | |
mcasas | 4e216e7 | 2016-07-28 21:28:38 | [diff] [blame] | 53 | // device::AccessTokenStore implementation |
mcasas | 80ef523 | 2016-07-22 23:00:47 | [diff] [blame] | 54 | 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. |
mcasas | 4e216e7 | 2016-07-28 21:28:38 | [diff] [blame] | 70 | class AwGeolocationDelegate : public device::GeolocationDelegate { |
mcasas | 80ef523 | 2016-07-22 23:00:47 | [diff] [blame] | 71 | public: |
| 72 | AwGeolocationDelegate() = default; |
| 73 | |
mcasas | 4e216e7 | 2016-07-28 21:28:38 | [diff] [blame] | 74 | scoped_refptr<device::AccessTokenStore> CreateAccessTokenStore() final { |
mcasas | 80ef523 | 2016-07-22 23:00:47 | [diff] [blame] | 75 | return new AwAccessTokenStore(); |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | DISALLOW_COPY_AND_ASSIGN(AwGeolocationDelegate); |
| 80 | }; |
| 81 | |
| 82 | } // anonymous namespace |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 83 | |
ben | 5be0b913 | 2016-08-03 00:17:18 | [diff] [blame] | 84 | AwBrowserMainParts::AwBrowserMainParts(AwContentBrowserClient* browser_client) |
| 85 | : browser_client_(browser_client) { |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | AwBrowserMainParts::~AwBrowserMainParts() { |
| 89 | } |
| 90 | |
| 91 | void AwBrowserMainParts::PreEarlyInitialization() { |
timvolodine | bbaeb5b | 2016-03-02 19:58:47 | [diff] [blame] | 92 | net::NetworkChangeNotifier::SetFactory(new AwNetworkChangeNotifierFactory()); |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 93 | |
| 94 | // Android WebView does not use default MessageLoop. It has its own |
[email protected] | f4d88443 | 2013-01-08 03:08:50 | [diff] [blame] | 95 | // Android specific MessageLoop. Also see MainMessageLoopRun. |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 96 | DCHECK(!main_message_loop_.get()); |
[email protected] | 166f2ed | 2013-12-19 19:34:00 | [diff] [blame] | 97 | main_message_loop_.reset(new base::MessageLoopForUI); |
[email protected] | 9e1d6b1 | 2013-04-27 04:48:44 | [diff] [blame] | 98 | base::MessageLoopForUI::current()->Start(); |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | int AwBrowserMainParts::PreCreateThreads() { |
agrieve | 96cd918 | 2015-06-25 20:50:26 | [diff] [blame] | 102 | ui::SetLocalePaksStoredInApk(true); |
mnaganov | 2f73f79 | 2015-08-21 16:24:48 | [diff] [blame] | 103 | std::string locale = ui::ResourceBundle::InitSharedInstanceWithLocale( |
yirui | 9174164 | 2016-10-28 04:29:59 | [diff] [blame] | 104 | base::android::GetDefaultLocaleString(), NULL, |
aberent | 3bcea37 | 2016-08-16 12:12:53 | [diff] [blame] | 105 | ui::ResourceBundle::LOAD_COMMON_RESOURCES); |
mnaganov | 2f73f79 | 2015-08-21 16:24:48 | [diff] [blame] | 106 | if (locale.empty()) { |
| 107 | LOG(WARNING) << "Failed to load locale .pak from the apk. " |
| 108 | "Bringing up WebView without any locale"; |
[email protected] | dd1c2a7 | 2014-07-29 18:11:33 | [diff] [blame] | 109 | } |
boliu | 40123db | 2016-01-16 01:38:20 | [diff] [blame] | 110 | base::i18n::SetICUDefaultLocale(locale); |
[email protected] | c11f4fe | 2013-02-19 19:50:46 | [diff] [blame] | 111 | |
aberent | 3bcea37 | 2016-08-16 12:12:53 | [diff] [blame] | 112 | // 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] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 118 | |
[email protected] | e1632c9 | 2013-09-30 00:47:59 | [diff] [blame] | 119 | base::android::MemoryPressureListenerAndroid::RegisterSystemCallback( |
| 120 | base::android::AttachCurrentThread()); |
boliu | c5befe7 | 2015-07-21 19:08:56 | [diff] [blame] | 121 | DeferredGpuCommandService::SetInstance(); |
tobiasjs | 5b457a44 | 2017-01-10 09:35:09 | [diff] [blame] | 122 | breakpad::CrashDumpObserver::Create(); |
tobiasjs | c1f4d0f3 | 2017-01-14 22:57:55 | [diff] [blame] | 123 | |
| 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 | |
tobiasjs | 90bce3c | 2017-04-10 17:15:44 | [diff] [blame] | 135 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 136 | switches::kWebViewSandboxedRenderer)) { |
mnaganov | f322c8f | 2016-01-08 02:49:31 | [diff] [blame] | 137 | // Create the renderers crash manager on the UI thread. |
tobiasjs | 5b457a44 | 2017-01-10 09:35:09 | [diff] [blame] | 138 | breakpad::CrashDumpObserver::GetInstance()->RegisterClient( |
| 139 | base::MakeUnique<AwBrowserTerminator>()); |
mnaganov | f322c8f | 2016-01-08 02:49:31 | [diff] [blame] | 140 | } |
[email protected] | e1632c9 | 2013-09-30 00:47:59 | [diff] [blame] | 141 | |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 142 | return content::RESULT_CODE_NORMAL_EXIT; |
| 143 | } |
| 144 | |
[email protected] | f2ab1b15 | 2013-01-25 04:38:41 | [diff] [blame] | 145 | void AwBrowserMainParts::PreMainMessageLoopRun() { |
ben | 5be0b913 | 2016-08-03 00:17:18 | [diff] [blame] | 146 | browser_client_->InitBrowserContext()->PreMainMessageLoopRun(); |
dgozman | 0115d2a26 | 2015-04-22 18:39:13 | [diff] [blame] | 147 | |
mcasas | 4e216e7 | 2016-07-28 21:28:38 | [diff] [blame] | 148 | device::GeolocationProvider::SetGeolocationDelegate( |
mcasas | 80ef523 | 2016-07-22 23:00:47 | [diff] [blame] | 149 | new AwGeolocationDelegate()); |
| 150 | |
Jochen Eisinger | 14ea977 | 2015-07-24 12:04:37 | [diff] [blame] | 151 | content::RenderFrameHost::AllowInjectingJavaScriptForAndroidWebView(); |
[email protected] | f2ab1b15 | 2013-01-25 04:38:41 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 154 | bool 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] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 160 | } // namespace android_webview |