[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" | ||||
ben | 5be0b913 | 2016-08-03 00:17:18 | [diff] [blame] | 8 | #include "android_webview/browser/aw_content_browser_client.h" |
dgozman | 0115d2a26 | 2015-04-22 18:39:13 | [diff] [blame] | 9 | #include "android_webview/browser/aw_dev_tools_discovery_provider.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" |
gunsch | 2288c53 | 2015-04-30 03:58:45 | [diff] [blame] | 13 | #include "android_webview/common/aw_resource.h" |
boliu | 1463e1e | 2015-07-23 00:18:04 | [diff] [blame] | 14 | #include "android_webview/common/aw_switches.h" |
agrieve | 3b864c9 | 2015-05-27 18:08:38 | [diff] [blame] | 15 | #include "base/android/apk_assets.h" |
[email protected] | 19728484 | 2012-11-20 03:39:01 | [diff] [blame] | 16 | #include "base/android/build_info.h" |
yfriedman | 38b46138 | 2014-10-20 19:40:16 | [diff] [blame] | 17 | #include "base/android/locale_utils.h" |
[email protected] | e1632c9 | 2013-09-30 00:47:59 | [diff] [blame] | 18 | #include "base/android/memory_pressure_listener_android.h" |
boliu | 1463e1e | 2015-07-23 00:18:04 | [diff] [blame] | 19 | #include "base/command_line.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 20 | #include "base/files/file_path.h" |
boliu | 40123db | 2016-01-16 01:38:20 | [diff] [blame] | 21 | #include "base/i18n/rtl.h" |
[email protected] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 22 | #include "base/path_service.h" |
mnaganov | f322c8f | 2016-01-08 02:49:31 | [diff] [blame] | 23 | #include "components/crash/content/browser/crash_micro_dump_manager_android.h" |
boliu | 1463e1e | 2015-07-23 00:18:04 | [diff] [blame] | 24 | #include "content/public/browser/android/synchronous_compositor.h" |
Jochen Eisinger | 14ea977 | 2015-07-24 12:04:37 | [diff] [blame] | 25 | #include "content/public/browser/render_frame_host.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 26 | #include "content/public/browser/render_process_host.h" |
27 | #include "content/public/common/content_client.h" | ||||
[email protected] | 2df8d41 | 2014-03-15 05:28:41 | [diff] [blame] | 28 | #include "content/public/common/content_switches.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 29 | #include "content/public/common/result_codes.h" |
mcasas | 4e216e7 | 2016-07-28 21:28:38 | [diff] [blame] | 30 | #include "device/geolocation/access_token_store.h" |
31 | #include "device/geolocation/geolocation_delegate.h" | ||||
32 | #include "device/geolocation/geolocation_provider.h" | ||||
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 33 | #include "net/android/network_change_notifier_factory_android.h" |
34 | #include "net/base/network_change_notifier.h" | ||||
[email protected] | dd1c2a7 | 2014-07-29 18:11:33 | [diff] [blame] | 35 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 36 | #include "ui/base/layout.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 37 | #include "ui/base/resource/resource_bundle.h" |
agrieve | bffa19f3 | 2015-06-25 19:55:34 | [diff] [blame] | 38 | #include "ui/base/resource/resource_bundle_android.h" |
[email protected] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 39 | #include "ui/base/ui_base_paths.h" |
tobiasjs | ca238b3b | 2015-06-24 22:53:54 | [diff] [blame] | 40 | #include "ui/gl/gl_surface.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 41 | |
42 | namespace android_webview { | ||||
mcasas | 80ef523 | 2016-07-22 23:00:47 | [diff] [blame] | 43 | namespace { |
44 | |||||
mcasas | 4e216e7 | 2016-07-28 21:28:38 | [diff] [blame] | 45 | class AwAccessTokenStore : public device::AccessTokenStore { |
mcasas | 80ef523 | 2016-07-22 23:00:47 | [diff] [blame] | 46 | public: |
47 | AwAccessTokenStore() { } | ||||
48 | |||||
mcasas | 4e216e7 | 2016-07-28 21:28:38 | [diff] [blame] | 49 | // device::AccessTokenStore implementation |
mcasas | 80ef523 | 2016-07-22 23:00:47 | [diff] [blame] | 50 | void LoadAccessTokens(const LoadAccessTokensCallback& request) override { |
51 | AccessTokenStore::AccessTokenMap access_token_map; | ||||
52 | // AccessTokenMap and net::URLRequestContextGetter not used on Android, | ||||
53 | // but Run needs to be called to finish the geolocation setup. | ||||
54 | request.Run(access_token_map, NULL); | ||||
55 | } | ||||
56 | void SaveAccessToken(const GURL& server_url, | ||||
57 | const base::string16& access_token) override {} | ||||
58 | |||||
59 | private: | ||||
60 | ~AwAccessTokenStore() override {} | ||||
61 | |||||
62 | DISALLOW_COPY_AND_ASSIGN(AwAccessTokenStore); | ||||
63 | }; | ||||
64 | |||||
65 | // A provider of Geolocation services to override AccessTokenStore. | ||||
mcasas | 4e216e7 | 2016-07-28 21:28:38 | [diff] [blame] | 66 | class AwGeolocationDelegate : public device::GeolocationDelegate { |
mcasas | 80ef523 | 2016-07-22 23:00:47 | [diff] [blame] | 67 | public: |
68 | AwGeolocationDelegate() = default; | ||||
69 | |||||
mcasas | 4e216e7 | 2016-07-28 21:28:38 | [diff] [blame] | 70 | scoped_refptr<device::AccessTokenStore> CreateAccessTokenStore() final { |
mcasas | 80ef523 | 2016-07-22 23:00:47 | [diff] [blame] | 71 | return new AwAccessTokenStore(); |
72 | } | ||||
73 | |||||
74 | private: | ||||
75 | DISALLOW_COPY_AND_ASSIGN(AwGeolocationDelegate); | ||||
76 | }; | ||||
77 | |||||
78 | } // anonymous namespace | ||||
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 79 | |
ben | 5be0b913 | 2016-08-03 00:17:18 | [diff] [blame] | 80 | AwBrowserMainParts::AwBrowserMainParts(AwContentBrowserClient* browser_client) |
81 | : browser_client_(browser_client) { | ||||
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 82 | } |
83 | |||||
84 | AwBrowserMainParts::~AwBrowserMainParts() { | ||||
85 | } | ||||
86 | |||||
87 | void AwBrowserMainParts::PreEarlyInitialization() { | ||||
timvolodine | bbaeb5b | 2016-03-02 19:58:47 | [diff] [blame] | 88 | net::NetworkChangeNotifier::SetFactory(new AwNetworkChangeNotifierFactory()); |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 89 | |
90 | // Android WebView does not use default MessageLoop. It has its own | ||||
[email protected] | f4d88443 | 2013-01-08 03:08:50 | [diff] [blame] | 91 | // Android specific MessageLoop. Also see MainMessageLoopRun. |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 92 | DCHECK(!main_message_loop_.get()); |
[email protected] | 166f2ed | 2013-12-19 19:34:00 | [diff] [blame] | 93 | main_message_loop_.reset(new base::MessageLoopForUI); |
[email protected] | 9e1d6b1 | 2013-04-27 04:48:44 | [diff] [blame] | 94 | base::MessageLoopForUI::current()->Start(); |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 95 | } |
96 | |||||
97 | int AwBrowserMainParts::PreCreateThreads() { | ||||
agrieve | 96cd918 | 2015-06-25 20:50:26 | [diff] [blame] | 98 | ui::SetLocalePaksStoredInApk(true); |
mnaganov | 2f73f79 | 2015-08-21 16:24:48 | [diff] [blame] | 99 | std::string locale = ui::ResourceBundle::InitSharedInstanceWithLocale( |
yfriedman | 38b46138 | 2014-10-20 19:40:16 | [diff] [blame] | 100 | base::android::GetDefaultLocale(), |
[email protected] | 4ce2aa6 | 2014-08-14 01:05:49 | [diff] [blame] | 101 | NULL, |
aberent | 3bcea37 | 2016-08-16 12:12:53 | [diff] [blame^] | 102 | ui::ResourceBundle::LOAD_COMMON_RESOURCES); |
mnaganov | 2f73f79 | 2015-08-21 16:24:48 | [diff] [blame] | 103 | if (locale.empty()) { |
104 | LOG(WARNING) << "Failed to load locale .pak from the apk. " | ||||
105 | "Bringing up WebView without any locale"; | ||||
[email protected] | dd1c2a7 | 2014-07-29 18:11:33 | [diff] [blame] | 106 | } |
boliu | 40123db | 2016-01-16 01:38:20 | [diff] [blame] | 107 | base::i18n::SetICUDefaultLocale(locale); |
[email protected] | c11f4fe | 2013-02-19 19:50:46 | [diff] [blame] | 108 | |
aberent | 3bcea37 | 2016-08-16 12:12:53 | [diff] [blame^] | 109 | // Try to directly mmap the resources.pak from the apk. Fall back to load |
110 | // from file, using PATH_SERVICE, otherwise. | ||||
111 | base::FilePath pak_file_path; | ||||
112 | PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &pak_file_path); | ||||
113 | pak_file_path = pak_file_path.AppendASCII("resources.pak"); | ||||
114 | ui::LoadMainAndroidPackFile("assets/resources.pak", pak_file_path); | ||||
[email protected] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 115 | |
[email protected] | e1632c9 | 2013-09-30 00:47:59 | [diff] [blame] | 116 | base::android::MemoryPressureListenerAndroid::RegisterSystemCallback( |
117 | base::android::AttachCurrentThread()); | ||||
boliu | c5befe7 | 2015-07-21 19:08:56 | [diff] [blame] | 118 | DeferredGpuCommandService::SetInstance(); |
mnaganov | f322c8f | 2016-01-08 02:49:31 | [diff] [blame] | 119 | if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
120 | switches::kSingleProcess)) { | ||||
121 | // Create the renderers crash manager on the UI thread. | ||||
122 | breakpad::CrashMicroDumpManager::GetInstance(); | ||||
123 | } | ||||
[email protected] | e1632c9 | 2013-09-30 00:47:59 | [diff] [blame] | 124 | |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 125 | return content::RESULT_CODE_NORMAL_EXIT; |
126 | } | ||||
127 | |||||
[email protected] | f2ab1b15 | 2013-01-25 04:38:41 | [diff] [blame] | 128 | void AwBrowserMainParts::PreMainMessageLoopRun() { |
ben | 5be0b913 | 2016-08-03 00:17:18 | [diff] [blame] | 129 | browser_client_->InitBrowserContext()->PreMainMessageLoopRun(); |
dgozman | 0115d2a26 | 2015-04-22 18:39:13 | [diff] [blame] | 130 | |
mcasas | 4e216e7 | 2016-07-28 21:28:38 | [diff] [blame] | 131 | device::GeolocationProvider::SetGeolocationDelegate( |
mcasas | 80ef523 | 2016-07-22 23:00:47 | [diff] [blame] | 132 | new AwGeolocationDelegate()); |
133 | |||||
dgozman | ec2b982a | 2015-04-28 10:36:39 | [diff] [blame] | 134 | AwDevToolsDiscoveryProvider::Install(); |
dgozman | 0115d2a26 | 2015-04-22 18:39:13 | [diff] [blame] | 135 | |
Jochen Eisinger | 14ea977 | 2015-07-24 12:04:37 | [diff] [blame] | 136 | content::RenderFrameHost::AllowInjectingJavaScriptForAndroidWebView(); |
[email protected] | f2ab1b15 | 2013-01-25 04:38:41 | [diff] [blame] | 137 | } |
138 | |||||
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 139 | bool AwBrowserMainParts::MainMessageLoopRun(int* result_code) { |
140 | // Android WebView does not use default MessageLoop. It has its own | ||||
141 | // Android specific MessageLoop. | ||||
142 | return true; | ||||
143 | } | ||||
144 | |||||
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 145 | } // namespace android_webview |