[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" |
dgozman | 0115d2a26 | 2015-04-22 18:39:13 | [diff] [blame] | 8 | #include "android_webview/browser/aw_dev_tools_discovery_provider.h" |
gunsch | 2288c53 | 2015-04-30 03:58:45 | [diff] [blame] | 9 | #include "android_webview/browser/aw_media_client_android.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 10 | #include "android_webview/browser/aw_result_codes.h" |
gunsch | 2288c53 | 2015-04-30 03:58:45 | [diff] [blame] | 11 | #include "android_webview/common/aw_resource.h" |
agrieve | 3b864c9 | 2015-05-27 18:08:38 | [diff] [blame] | 12 | #include "base/android/apk_assets.h" |
[email protected] | 19728484 | 2012-11-20 03:39:01 | [diff] [blame] | 13 | #include "base/android/build_info.h" |
yfriedman | 38b46138 | 2014-10-20 19:40:16 | [diff] [blame] | 14 | #include "base/android/locale_utils.h" |
[email protected] | e1632c9 | 2013-09-30 00:47:59 | [diff] [blame] | 15 | #include "base/android/memory_pressure_listener_android.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 16 | #include "base/files/file_path.h" |
[email protected] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 17 | #include "base/path_service.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 18 | #include "content/public/browser/render_process_host.h" |
| 19 | #include "content/public/common/content_client.h" |
[email protected] | 2df8d41 | 2014-03-15 05:28:41 | [diff] [blame] | 20 | #include "content/public/common/content_switches.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 21 | #include "content/public/common/result_codes.h" |
[email protected] | f16cc678 | 2013-12-16 23:42:57 | [diff] [blame] | 22 | #include "content/public/common/url_utils.h" |
gunsch | 2288c53 | 2015-04-30 03:58:45 | [diff] [blame] | 23 | #include "media/base/android/media_client_android.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 24 | #include "net/android/network_change_notifier_factory_android.h" |
| 25 | #include "net/base/network_change_notifier.h" |
[email protected] | dd1c2a7 | 2014-07-29 18:11:33 | [diff] [blame] | 26 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 27 | #include "ui/base/layout.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 28 | #include "ui/base/resource/resource_bundle.h" |
agrieve | bffa19f3 | 2015-06-25 19:55:34 | [diff] [blame] | 29 | #include "ui/base/resource/resource_bundle_android.h" |
[email protected] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 30 | #include "ui/base/ui_base_paths.h" |
tobiasjs | ca238b3b | 2015-06-24 22:53:54 | [diff] [blame] | 31 | #include "ui/gl/gl_surface.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 32 | |
| 33 | namespace android_webview { |
| 34 | |
| 35 | AwBrowserMainParts::AwBrowserMainParts(AwBrowserContext* browser_context) |
| 36 | : browser_context_(browser_context) { |
| 37 | } |
| 38 | |
| 39 | AwBrowserMainParts::~AwBrowserMainParts() { |
| 40 | } |
| 41 | |
| 42 | void AwBrowserMainParts::PreEarlyInitialization() { |
| 43 | net::NetworkChangeNotifier::SetFactory( |
| 44 | new net::NetworkChangeNotifierFactoryAndroid()); |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 45 | |
| 46 | // Android WebView does not use default MessageLoop. It has its own |
[email protected] | f4d88443 | 2013-01-08 03:08:50 | [diff] [blame] | 47 | // Android specific MessageLoop. Also see MainMessageLoopRun. |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 48 | DCHECK(!main_message_loop_.get()); |
[email protected] | 166f2ed | 2013-12-19 19:34:00 | [diff] [blame] | 49 | main_message_loop_.reset(new base::MessageLoopForUI); |
[email protected] | 9e1d6b1 | 2013-04-27 04:48:44 | [diff] [blame] | 50 | base::MessageLoopForUI::current()->Start(); |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | int AwBrowserMainParts::PreCreateThreads() { |
agrieve | 3b864c9 | 2015-05-27 18:08:38 | [diff] [blame] | 54 | base::MemoryMappedFile::Region pak_region = |
| 55 | base::MemoryMappedFile::Region::kWholeFile; |
[email protected] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 56 | |
[email protected] | f6f423f | 2014-07-30 16:51:06 | [diff] [blame] | 57 | // 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. |
agrieve | 96cd918 | 2015-06-25 20:50:26 | [diff] [blame^] | 62 | ui::SetLocalePaksStoredInApk(true); |
[email protected] | 4ce2aa6 | 2014-08-14 01:05:49 | [diff] [blame] | 63 | ui::ResourceBundle::InitSharedInstanceWithLocale( |
yfriedman | 38b46138 | 2014-10-20 19:40:16 | [diff] [blame] | 64 | base::android::GetDefaultLocale(), |
[email protected] | 4ce2aa6 | 2014-08-14 01:05:49 | [diff] [blame] | 65 | NULL, |
| 66 | ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES); |
agrieve | bffa19f3 | 2015-06-25 19:55:34 | [diff] [blame] | 67 | 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); |
agrieve | 3b864c9 | 2015-05-27 18:08:38 | [diff] [blame] | 70 | if (pak_fd != -1) { |
[email protected] | f6f423f | 2014-07-30 16:51:06 | [diff] [blame] | 71 | ui::ResourceBundle::CleanupSharedInstance(); |
| 72 | ui::ResourceBundle::InitSharedInstanceWithPakFileRegion( |
agrieve | 3b864c9 | 2015-05-27 18:08:38 | [diff] [blame] | 73 | base::File(pak_fd), pak_region); |
[email protected] | f6f423f | 2014-07-30 16:51:06 | [diff] [blame] | 74 | } else { |
agrieve | bffa19f3 | 2015-06-25 19:55:34 | [diff] [blame] | 75 | LOG(WARNING) << "Failed to load " << locale << ".pak from the apk. " |
[email protected] | f6f423f | 2014-07-30 16:51:06 | [diff] [blame] | 76 | "Bringing up WebView without any locale"; |
[email protected] | dd1c2a7 | 2014-07-29 18:11:33 | [diff] [blame] | 77 | } |
[email protected] | c11f4fe | 2013-02-19 19:50:46 | [diff] [blame] | 78 | |
[email protected] | dd1c2a7 | 2014-07-29 18:11:33 | [diff] [blame] | 79 | // Try to directly mmap the webviewchromium.pak from the apk. Fall back to |
| 80 | // load from file, using PATH_SERVICE, otherwise. |
agrieve | 62536bc | 2015-06-05 03:01:33 | [diff] [blame] | 81 | pak_fd = base::android::OpenApkAsset("assets/webviewchromium.pak", |
| 82 | &pak_region); |
agrieve | 3b864c9 | 2015-05-27 18:08:38 | [diff] [blame] | 83 | if (pak_fd != -1) { |
[email protected] | dd1c2a7 | 2014-07-29 18:11:33 | [diff] [blame] | 84 | ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion( |
agrieve | 3b864c9 | 2015-05-27 18:08:38 | [diff] [blame] | 85 | base::File(pak_fd), pak_region, ui::SCALE_FACTOR_NONE); |
[email protected] | dd1c2a7 | 2014-07-29 18:11:33 | [diff] [blame] | 86 | } 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] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 94 | |
[email protected] | e1632c9 | 2013-09-30 00:47:59 | [diff] [blame] | 95 | base::android::MemoryPressureListenerAndroid::RegisterSystemCallback( |
| 96 | base::android::AttachCurrentThread()); |
| 97 | |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 98 | return content::RESULT_CODE_NORMAL_EXIT; |
| 99 | } |
| 100 | |
[email protected] | f2ab1b15 | 2013-01-25 04:38:41 | [diff] [blame] | 101 | void AwBrowserMainParts::PreMainMessageLoopRun() { |
| 102 | browser_context_->PreMainMessageLoopRun(); |
dgozman | 0115d2a26 | 2015-04-22 18:39:13 | [diff] [blame] | 103 | |
dgozman | ec2b982a | 2015-04-28 10:36:39 | [diff] [blame] | 104 | AwDevToolsDiscoveryProvider::Install(); |
dgozman | 0115d2a26 | 2015-04-22 18:39:13 | [diff] [blame] | 105 | |
gunsch | 2288c53 | 2015-04-30 03:58:45 | [diff] [blame] | 106 | media::SetMediaClientAndroid( |
| 107 | new AwMediaClientAndroid(AwResource::GetConfigKeySystemUuidMapping())); |
| 108 | |
tobiasjs | ca238b3b | 2015-06-24 22:53:54 | [diff] [blame] | 109 | gfx::GLSurface::InitializeOneOff(); |
| 110 | |
[email protected] | f16cc678 | 2013-12-16 23:42:57 | [diff] [blame] | 111 | // This is needed for WebView Classic backwards compatibility |
| 112 | // See crbug.com/298495 |
| 113 | content::SetMaxURLChars(20 * 1024 * 1024); |
[email protected] | f2ab1b15 | 2013-01-25 04:38:41 | [diff] [blame] | 114 | } |
| 115 | |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 116 | bool 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] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 122 | } // namespace android_webview |