Home | History | Annotate | Download | only in browser
      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"
      9 #include "android_webview/native/aw_assets.h"
     10 #include "base/android/build_info.h"
     11 #include "base/android/memory_pressure_listener_android.h"
     12 #include "base/command_line.h"
     13 #include "base/files/file_path.h"
     14 #include "base/path_service.h"
     15 #include "content/public/browser/render_process_host.h"
     16 #include "content/public/common/content_client.h"
     17 #include "content/public/common/content_switches.h"
     18 #include "content/public/common/result_codes.h"
     19 #include "content/public/common/url_utils.h"
     20 #include "gpu/command_buffer/service/mailbox_synchronizer.h"
     21 #include "net/android/network_change_notifier_factory_android.h"
     22 #include "net/base/network_change_notifier.h"
     23 #include "ui/base/l10n/l10n_util.h"
     24 #include "ui/base/l10n/l10n_util_android.h"
     25 #include "ui/base/layout.h"
     26 #include "ui/base/resource/resource_bundle.h"
     27 #include "ui/base/ui_base_paths.h"
     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());
     41 
     42   // Android WebView does not use default MessageLoop. It has its own
     43   // Android specific MessageLoop. Also see MainMessageLoopRun.
     44   DCHECK(!main_message_loop_.get());
     45   main_message_loop_.reset(new base::MessageLoopForUI);
     46   base::MessageLoopForUI::current()->Start();
     47 }
     48 
     49 int AwBrowserMainParts::PreCreateThreads() {
     50   int pak_fd = 0;
     51   int64 pak_off = 0;
     52   int64 pak_len = 0;
     53 
     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.
     59   ui::ResourceBundle::InitSharedInstanceLocaleOnly(
     60       l10n_util::GetDefaultLocale(), NULL);
     61   std::string locale = l10n_util::GetApplicationLocale(std::string()) + ".pak";
     62   if (AwAssets::OpenAsset(locale, &pak_fd, &pak_off, &pak_len)) {
     63     VLOG(0) << "Load from apk succesful, fd=" << pak_fd << " off=" << pak_off
     64             << " len=" << pak_len;
     65     ui::ResourceBundle::CleanupSharedInstance();
     66     ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
     67         base::File(pak_fd),
     68         base::MemoryMappedFile::Region(pak_off, pak_len),
     69         /*should_load_common_resources=*/false);
     70   } else {
     71     LOG(WARNING) << "Failed to load " << locale << ".pak from the apk too. "
     72                     "Bringing up WebView without any locale";
     73   }
     74 
     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   }
     92 
     93   base::android::MemoryPressureListenerAndroid::RegisterSystemCallback(
     94       base::android::AttachCurrentThread());
     95 
     96   return content::RESULT_CODE_NORMAL_EXIT;
     97 }
     98 
     99 void AwBrowserMainParts::PreMainMessageLoopRun() {
    100   // TODO(boliu): Can't support accelerated 2d canvas and WebGL with ubercomp
    101   // yet: crbug.com/352424.
    102   if (!gpu::gles2::MailboxSynchronizer::Initialize()) {
    103     CommandLine* cl = CommandLine::ForCurrentProcess();
    104     cl->AppendSwitch(switches::kDisableAccelerated2dCanvas);
    105     cl->AppendSwitch(switches::kDisableExperimentalWebGL);
    106   }
    107 
    108   browser_context_->PreMainMessageLoopRun();
    109   // This is needed for WebView Classic backwards compatibility
    110   // See crbug.com/298495
    111   content::SetMaxURLChars(20 * 1024 * 1024);
    112 }
    113 
    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 
    120 }  // namespace android_webview
    121