[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" |
| 8 | #include "android_webview/browser/aw_result_codes.h" |
[email protected] | dd1c2a7 | 2014-07-29 18:11:33 | [diff] [blame] | 9 | #include "android_webview/native/aw_assets.h" |
[email protected] | 19728484 | 2012-11-20 03:39:01 | [diff] [blame] | 10 | #include "base/android/build_info.h" |
yfriedman | 38b46138 | 2014-10-20 19:40:16 | [diff] [blame^] | 11 | #include "base/android/locale_utils.h" |
[email protected] | e1632c9 | 2013-09-30 00:47:59 | [diff] [blame] | 12 | #include "base/android/memory_pressure_listener_android.h" |
[email protected] | 2df8d41 | 2014-03-15 05:28:41 | [diff] [blame] | 13 | #include "base/command_line.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 14 | #include "base/files/file_path.h" |
[email protected] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 15 | #include "base/path_service.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 16 | #include "content/public/browser/render_process_host.h" |
| 17 | #include "content/public/common/content_client.h" |
[email protected] | 2df8d41 | 2014-03-15 05:28:41 | [diff] [blame] | 18 | #include "content/public/common/content_switches.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 19 | #include "content/public/common/result_codes.h" |
[email protected] | f16cc678 | 2013-12-16 23:42:57 | [diff] [blame] | 20 | #include "content/public/common/url_utils.h" |
[email protected] | 2df8d41 | 2014-03-15 05:28:41 | [diff] [blame] | 21 | #include "gpu/command_buffer/service/mailbox_synchronizer.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 22 | #include "net/android/network_change_notifier_factory_android.h" |
| 23 | #include "net/base/network_change_notifier.h" |
[email protected] | dd1c2a7 | 2014-07-29 18:11:33 | [diff] [blame] | 24 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 25 | #include "ui/base/layout.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 26 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 27 | #include "ui/base/ui_base_paths.h" |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 28 | |
| 29 | namespace android_webview { |
| 30 | |
| 31 | AwBrowserMainParts::AwBrowserMainParts(AwBrowserContext* browser_context) |
| 32 | : browser_context_(browser_context) { |
| 33 | } |
| 34 | |
| 35 | AwBrowserMainParts::~AwBrowserMainParts() { |
| 36 | } |
| 37 | |
| 38 | void AwBrowserMainParts::PreEarlyInitialization() { |
| 39 | net::NetworkChangeNotifier::SetFactory( |
| 40 | new net::NetworkChangeNotifierFactoryAndroid()); |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 41 | |
| 42 | // Android WebView does not use default MessageLoop. It has its own |
[email protected] | f4d88443 | 2013-01-08 03:08:50 | [diff] [blame] | 43 | // Android specific MessageLoop. Also see MainMessageLoopRun. |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 44 | DCHECK(!main_message_loop_.get()); |
[email protected] | 166f2ed | 2013-12-19 19:34:00 | [diff] [blame] | 45 | main_message_loop_.reset(new base::MessageLoopForUI); |
[email protected] | 9e1d6b1 | 2013-04-27 04:48:44 | [diff] [blame] | 46 | base::MessageLoopForUI::current()->Start(); |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | int AwBrowserMainParts::PreCreateThreads() { |
[email protected] | dd1c2a7 | 2014-07-29 18:11:33 | [diff] [blame] | 50 | int pak_fd = 0; |
| 51 | int64 pak_off = 0; |
| 52 | int64 pak_len = 0; |
[email protected] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 53 | |
[email protected] | f6f423f | 2014-07-30 16:51:06 | [diff] [blame] | 54 | // 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] | 4ce2aa6 | 2014-08-14 01:05:49 | [diff] [blame] | 59 | ui::ResourceBundle::InitSharedInstanceWithLocale( |
yfriedman | 38b46138 | 2014-10-20 19:40:16 | [diff] [blame^] | 60 | base::android::GetDefaultLocale(), |
[email protected] | 4ce2aa6 | 2014-08-14 01:05:49 | [diff] [blame] | 61 | NULL, |
| 62 | ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES); |
[email protected] | f6f423f | 2014-07-30 16:51:06 | [diff] [blame] | 63 | 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] | 9bbe278 | 2014-08-12 15:26:34 | [diff] [blame] | 69 | base::File(pak_fd), base::MemoryMappedFile::Region(pak_off, pak_len)); |
[email protected] | f6f423f | 2014-07-30 16:51:06 | [diff] [blame] | 70 | } else { |
| 71 | LOG(WARNING) << "Failed to load " << locale << ".pak from the apk too. " |
| 72 | "Bringing up WebView without any locale"; |
[email protected] | dd1c2a7 | 2014-07-29 18:11:33 | [diff] [blame] | 73 | } |
[email protected] | c11f4fe | 2013-02-19 19:50:46 | [diff] [blame] | 74 | |
[email protected] | dd1c2a7 | 2014-07-29 18:11:33 | [diff] [blame] | 75 | // 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] | 17237f1 | 2012-12-06 17:37:35 | [diff] [blame] | 92 | |
[email protected] | e1632c9 | 2013-09-30 00:47:59 | [diff] [blame] | 93 | base::android::MemoryPressureListenerAndroid::RegisterSystemCallback( |
| 94 | base::android::AttachCurrentThread()); |
| 95 | |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 96 | return content::RESULT_CODE_NORMAL_EXIT; |
| 97 | } |
| 98 | |
[email protected] | f2ab1b15 | 2013-01-25 04:38:41 | [diff] [blame] | 99 | void AwBrowserMainParts::PreMainMessageLoopRun() { |
[email protected] | 8705654b | 2014-05-24 01:16:19 | [diff] [blame] | 100 | // TODO(boliu): Can't support accelerated 2d canvas and WebGL with ubercomp |
| 101 | // yet: crbug.com/352424. |
[email protected] | 0239141 | 2014-06-16 18:05:57 | [diff] [blame] | 102 | if (!gpu::gles2::MailboxSynchronizer::Initialize()) { |
[email protected] | 8705654b | 2014-05-24 01:16:19 | [diff] [blame] | 103 | CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 104 | cl->AppendSwitch(switches::kDisableAccelerated2dCanvas); |
| 105 | cl->AppendSwitch(switches::kDisableExperimentalWebGL); |
[email protected] | 2df8d41 | 2014-03-15 05:28:41 | [diff] [blame] | 106 | } |
| 107 | |
[email protected] | f2ab1b15 | 2013-01-25 04:38:41 | [diff] [blame] | 108 | browser_context_->PreMainMessageLoopRun(); |
[email protected] | f16cc678 | 2013-12-16 23:42:57 | [diff] [blame] | 109 | // This is needed for WebView Classic backwards compatibility |
| 110 | // See crbug.com/298495 |
| 111 | content::SetMaxURLChars(20 * 1024 * 1024); |
[email protected] | f2ab1b15 | 2013-01-25 04:38:41 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 114 | bool 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] | f81fa20 | 2012-10-25 23:32:28 | [diff] [blame] | 120 | } // namespace android_webview |