summaryrefslogtreecommitdiffstats
path: root/android_webview/browser/aw_browser_main_parts.cc
blob: 4ea656ce92c381c41e5a1c2fa18c14fff4845b14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "android_webview/browser/aw_browser_main_parts.h"

#include "android_webview/browser/aw_browser_context.h"
#include "android_webview/browser/aw_dev_tools_discovery_provider.h"
#include "android_webview/browser/aw_media_client_android.h"
#include "android_webview/browser/aw_result_codes.h"
#include "android_webview/common/aw_resource.h"
#include "base/android/apk_assets.h"
#include "base/android/build_info.h"
#include "base/android/locale_utils.h"
#include "base/android/memory_pressure_listener_android.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/result_codes.h"
#include "content/public/common/url_utils.h"
#include "media/base/android/media_client_android.h"
#include "net/android/network_change_notifier_factory_android.h"
#include "net/base/network_change_notifier.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/layout.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_paths.h"

namespace android_webview {

AwBrowserMainParts::AwBrowserMainParts(AwBrowserContext* browser_context)
    : browser_context_(browser_context) {
}

AwBrowserMainParts::~AwBrowserMainParts() {
}

void AwBrowserMainParts::PreEarlyInitialization() {
  net::NetworkChangeNotifier::SetFactory(
      new net::NetworkChangeNotifierFactoryAndroid());

  // Android WebView does not use default MessageLoop. It has its own
  // Android specific MessageLoop. Also see MainMessageLoopRun.
  DCHECK(!main_message_loop_.get());
  main_message_loop_.reset(new base::MessageLoopForUI);
  base::MessageLoopForUI::current()->Start();
}

int AwBrowserMainParts::PreCreateThreads() {
  base::MemoryMappedFile::Region pak_region =
      base::MemoryMappedFile::Region::kWholeFile;

  // TODO(primiano, mkosiba): GetApplicationLocale requires a ResourceBundle
  // instance to be present to work correctly so we call this (knowing it will
  // fail) just to create the ResourceBundle instance. We should refactor
  // ResourceBundle/GetApplicationLocale to not require an instance to be
  // initialized.
  ui::ResourceBundle::InitSharedInstanceWithLocale(
      base::android::GetDefaultLocale(),
      NULL,
      ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
  std::string locale = l10n_util::GetApplicationLocale(std::string()) + ".pak";
  int pak_fd = base::android::OpenApkAsset(locale, &pak_region);
  if (pak_fd != -1) {
    ui::ResourceBundle::CleanupSharedInstance();
    ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
        base::File(pak_fd), pak_region);
  } else {
    LOG(WARNING) << "Failed to load " << locale << ".pak from the apk too. "
                    "Bringing up WebView without any locale";
  }

  // Try to directly mmap the webviewchromium.pak from the apk. Fall back to
  // load from file, using PATH_SERVICE, otherwise.
  pak_fd = base::android::OpenApkAsset("webviewchromium.pak", &pak_region);
  if (pak_fd != -1) {
    ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion(
        base::File(pak_fd), pak_region, ui::SCALE_FACTOR_NONE);
  } else {
    base::FilePath pak_path;
    PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &pak_path);
    LOG(WARNING) << "Cannot load webviewchromium.pak assets from the apk. "
                    "Falling back loading it from " << pak_path.MaybeAsASCII();
    ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
        pak_path.AppendASCII("webviewchromium.pak"), ui::SCALE_FACTOR_NONE);
  }

  base::android::MemoryPressureListenerAndroid::RegisterSystemCallback(
      base::android::AttachCurrentThread());

  return content::RESULT_CODE_NORMAL_EXIT;
}

void AwBrowserMainParts::PreMainMessageLoopRun() {
  browser_context_->PreMainMessageLoopRun();

  AwDevToolsDiscoveryProvider::Install();

  media::SetMediaClientAndroid(
      new AwMediaClientAndroid(AwResource::GetConfigKeySystemUuidMapping()));

  // This is needed for WebView Classic backwards compatibility
  // See crbug.com/298495
  content::SetMaxURLChars(20 * 1024 * 1024);
}

bool AwBrowserMainParts::MainMessageLoopRun(int* result_code) {
  // Android WebView does not use default MessageLoop. It has its own
  // Android specific MessageLoop.
  return true;
}

}  // namespace android_webview