| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "android_webview/browser/aw_browser_main_parts.h" | 5 #include "android_webview/browser/aw_browser_main_parts.h" | 
| 6 | 6 | 
| 7 #include "android_webview/browser/aw_browser_context.h" | 7 #include "android_webview/browser/aw_browser_context.h" | 
| 8 #include "android_webview/browser/aw_result_codes.h" | 8 #include "android_webview/browser/aw_result_codes.h" | 
|  | 9 #include "android_webview/native/aw_assets.h" | 
| 9 #include "base/android/build_info.h" | 10 #include "base/android/build_info.h" | 
| 10 #include "base/android/memory_pressure_listener_android.h" | 11 #include "base/android/memory_pressure_listener_android.h" | 
| 11 #include "base/command_line.h" | 12 #include "base/command_line.h" | 
| 12 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" | 
| 13 #include "base/path_service.h" | 14 #include "base/path_service.h" | 
| 14 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" | 
| 15 #include "content/public/common/content_client.h" | 16 #include "content/public/common/content_client.h" | 
| 16 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" | 
| 17 #include "content/public/common/result_codes.h" | 18 #include "content/public/common/result_codes.h" | 
| 18 #include "content/public/common/url_utils.h" | 19 #include "content/public/common/url_utils.h" | 
| 19 #include "gpu/command_buffer/service/mailbox_synchronizer.h" | 20 #include "gpu/command_buffer/service/mailbox_synchronizer.h" | 
| 20 #include "net/android/network_change_notifier_factory_android.h" | 21 #include "net/android/network_change_notifier_factory_android.h" | 
| 21 #include "net/base/network_change_notifier.h" | 22 #include "net/base/network_change_notifier.h" | 
|  | 23 #include "ui/base/l10n/l10n_util.h" | 
| 22 #include "ui/base/l10n/l10n_util_android.h" | 24 #include "ui/base/l10n/l10n_util_android.h" | 
| 23 #include "ui/base/layout.h" | 25 #include "ui/base/layout.h" | 
| 24 #include "ui/base/resource/resource_bundle.h" | 26 #include "ui/base/resource/resource_bundle.h" | 
| 25 #include "ui/base/ui_base_paths.h" | 27 #include "ui/base/ui_base_paths.h" | 
| 26 | 28 | 
| 27 namespace android_webview { | 29 namespace android_webview { | 
| 28 | 30 | 
| 29 AwBrowserMainParts::AwBrowserMainParts(AwBrowserContext* browser_context) | 31 AwBrowserMainParts::AwBrowserMainParts(AwBrowserContext* browser_context) | 
| 30     : browser_context_(browser_context) { | 32     : browser_context_(browser_context) { | 
| 31 } | 33 } | 
| 32 | 34 | 
| 33 AwBrowserMainParts::~AwBrowserMainParts() { | 35 AwBrowserMainParts::~AwBrowserMainParts() { | 
| 34 } | 36 } | 
| 35 | 37 | 
| 36 void AwBrowserMainParts::PreEarlyInitialization() { | 38 void AwBrowserMainParts::PreEarlyInitialization() { | 
| 37   net::NetworkChangeNotifier::SetFactory( | 39   net::NetworkChangeNotifier::SetFactory( | 
| 38       new net::NetworkChangeNotifierFactoryAndroid()); | 40       new net::NetworkChangeNotifierFactoryAndroid()); | 
| 39 | 41 | 
| 40   // Android WebView does not use default MessageLoop. It has its own | 42   // Android WebView does not use default MessageLoop. It has its own | 
| 41   // Android specific MessageLoop. Also see MainMessageLoopRun. | 43   // Android specific MessageLoop. Also see MainMessageLoopRun. | 
| 42   DCHECK(!main_message_loop_.get()); | 44   DCHECK(!main_message_loop_.get()); | 
| 43   main_message_loop_.reset(new base::MessageLoopForUI); | 45   main_message_loop_.reset(new base::MessageLoopForUI); | 
| 44   base::MessageLoopForUI::current()->Start(); | 46   base::MessageLoopForUI::current()->Start(); | 
| 45 } | 47 } | 
| 46 | 48 | 
| 47 int AwBrowserMainParts::PreCreateThreads() { | 49 int AwBrowserMainParts::PreCreateThreads() { | 
| 48   ui::ResourceBundle::InitSharedInstanceLocaleOnly( | 50   int pak_fd = 0; | 
| 49       l10n_util::GetDefaultLocale(), NULL); | 51   int64 pak_off = 0; | 
|  | 52   int64 pak_len = 0; | 
| 50 | 53 | 
| 51   base::FilePath pak_path; | 54   // TODO(primiano): at present state there is a cyclic logic dependency | 
| 52   PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &pak_path); | 55   // between l10n_util::GetApplicationLocale and our code here. l10n_util | 
|  | 56   // decides which locale to ultimately use by scanning the pak files in the | 
|  | 57   // data folder. What we ultimately want to achieve is knowing the pak file to | 
|  | 58   // load from the apk beforehand, without looking at the data folder at all. | 
|  | 59   // Fixing this cyclic dependency involves some refactoring in ResourceBundle | 
|  | 60   // and l10n_util. | 
|  | 61   // In the meanwhile we use the mmap path only as a fallback (i.e. we check | 
|  | 62   // first for the existence of the pak files in the data dir). In the immediate | 
|  | 63   // future this order should be reversed, and we should look in the data | 
|  | 64   // directory only if we failed to mmap the pak file from the apk. | 
|  | 65   bool did_init_from_file = !ui::ResourceBundle::InitSharedInstanceLocaleOnly( | 
|  | 66                                  l10n_util::GetDefaultLocale(), NULL).empty(); | 
|  | 67   if (!did_init_from_file) { | 
|  | 68     LOG(WARNING) << "Failed to load the locale pak from PATH_SERVICE. " | 
|  | 69                     " Trying to mmap en-US.pak from the apk"; | 
|  | 70     if (AwAssets::OpenAsset("en-US.pak", &pak_fd, &pak_off, &pak_len)) { | 
|  | 71       VLOG(0) << "Load from apk succesful, fd=" << pak_fd << " off=" << pak_off | 
|  | 72               << " len=" << pak_len; | 
|  | 73       ui::ResourceBundle::CleanupSharedInstance(); | 
|  | 74       ui::ResourceBundle::InitSharedInstanceWithPakFileRegion( | 
|  | 75           base::File(pak_fd), | 
|  | 76           base::MemoryMappedFile::Region(pak_off, pak_len), | 
|  | 77           /*should_load_common_resources=*/false); | 
|  | 78     } else { | 
|  | 79       LOG(WARNING) << "Failed to load en-US.pak from the apk too. " | 
|  | 80                       "Bringing up WebView without any locale"; | 
|  | 81     } | 
|  | 82   } | 
| 53 | 83 | 
| 54   ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath( | 84   // Try to directly mmap the webviewchromium.pak from the apk. Fall back to | 
| 55       pak_path.AppendASCII("webviewchromium.pak"), | 85   // load from file, using PATH_SERVICE, otherwise. | 
| 56       ui::SCALE_FACTOR_NONE); | 86   if (AwAssets::OpenAsset("webviewchromium.pak", &pak_fd, &pak_off, &pak_len)) { | 
|  | 87     VLOG(0) << "Loading webviewchromium.pak from, fd:" << pak_fd | 
|  | 88             << " off:" << pak_off << " len:" << pak_len; | 
|  | 89     ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion( | 
|  | 90         base::File(pak_fd), | 
|  | 91         base::MemoryMappedFile::Region(pak_off, pak_len), | 
|  | 92         ui::SCALE_FACTOR_NONE); | 
|  | 93   } else { | 
|  | 94     base::FilePath pak_path; | 
|  | 95     PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &pak_path); | 
|  | 96     LOG(WARNING) << "Cannot load webviewchromium.pak assets from the apk. " | 
|  | 97                     "Falling back loading it from " << pak_path.MaybeAsASCII(); | 
|  | 98     ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath( | 
|  | 99         pak_path.AppendASCII("webviewchromium.pak"), ui::SCALE_FACTOR_NONE); | 
|  | 100   } | 
| 57 | 101 | 
| 58   base::android::MemoryPressureListenerAndroid::RegisterSystemCallback( | 102   base::android::MemoryPressureListenerAndroid::RegisterSystemCallback( | 
| 59       base::android::AttachCurrentThread()); | 103       base::android::AttachCurrentThread()); | 
| 60 | 104 | 
| 61   return content::RESULT_CODE_NORMAL_EXIT; | 105   return content::RESULT_CODE_NORMAL_EXIT; | 
| 62 } | 106 } | 
| 63 | 107 | 
| 64 void AwBrowserMainParts::PreMainMessageLoopRun() { | 108 void AwBrowserMainParts::PreMainMessageLoopRun() { | 
| 65   // TODO(boliu): Can't support accelerated 2d canvas and WebGL with ubercomp | 109   // TODO(boliu): Can't support accelerated 2d canvas and WebGL with ubercomp | 
| 66   // yet: crbug.com/352424. | 110   // yet: crbug.com/352424. | 
| 67   if (!gpu::gles2::MailboxSynchronizer::Initialize()) { | 111   if (!gpu::gles2::MailboxSynchronizer::Initialize()) { | 
| 68     CommandLine* cl = CommandLine::ForCurrentProcess(); | 112     CommandLine* cl = CommandLine::ForCurrentProcess(); | 
| 69     cl->AppendSwitch(switches::kDisableAccelerated2dCanvas); | 113     cl->AppendSwitch(switches::kDisableAccelerated2dCanvas); | 
| 70     cl->AppendSwitch(switches::kDisableExperimentalWebGL); | 114     cl->AppendSwitch(switches::kDisableExperimentalWebGL); | 
| 71   } | 115   } | 
| 72 | 116 | 
| 73   browser_context_->PreMainMessageLoopRun(); | 117   browser_context_->PreMainMessageLoopRun(); | 
| 74   // This is needed for WebView Classic backwards compatibility | 118   // This is needed for WebView Classic backwards compatibility | 
| 75   // See crbug.com/298495 | 119   // See crbug.com/298495 | 
| 76   content::SetMaxURLChars(20 * 1024 * 1024); | 120   content::SetMaxURLChars(20 * 1024 * 1024); | 
| 77 } | 121 } | 
| 78 | 122 | 
| 79 bool AwBrowserMainParts::MainMessageLoopRun(int* result_code) { | 123 bool AwBrowserMainParts::MainMessageLoopRun(int* result_code) { | 
| 80   // Android WebView does not use default MessageLoop. It has its own | 124   // Android WebView does not use default MessageLoop. It has its own | 
| 81   // Android specific MessageLoop. | 125   // Android specific MessageLoop. | 
| 82   return true; | 126   return true; | 
| 83 } | 127 } | 
| 84 | 128 | 
| 85 }  // namespace android_webview | 129 }  // namespace android_webview | 
| OLD | NEW | 
|---|