1 // Copyright (c) 2013 Intel Corporation. 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.
5 #include "xwalk/runtime/browser/xwalk_browser_main_parts_android.h"
9 #include "base/android/path_utils.h"
10 #include "base/base_paths_android.h"
11 #include "base/files/file_path.h"
12 #include "base/file_util.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/path_service.h"
15 #include "base/threading/sequenced_worker_pool.h"
16 #include "cc/base/switches.h"
17 #include "content/public/browser/android/compositor.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/cookie_store_factory.h"
20 #include "content/public/common/content_switches.h"
21 #include "content/public/common/result_codes.h"
22 #include "grit/net_resources.h"
23 #include "net/android/network_change_notifier_factory_android.h"
24 #include "net/base/network_change_notifier.h"
25 #include "net/base/net_module.h"
26 #include "net/base/net_util.h"
27 #include "net/cookies/cookie_monster.h"
28 #include "net/cookies/cookie_store.h"
29 #include "ui/base/layout.h"
30 #include "ui/base/l10n/l10n_util_android.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "xwalk/extensions/browser/xwalk_extension_service.h"
33 #include "xwalk/extensions/common/xwalk_extension.h"
34 #include "xwalk/extensions/common/xwalk_extension_switches.h"
35 #include "xwalk/runtime/browser/android/cookie_manager.h"
36 #include "xwalk/runtime/browser/runtime_context.h"
37 #include "xwalk/runtime/browser/xwalk_runner.h"
38 #include "xwalk/runtime/common/xwalk_runtime_features.h"
39 #include "xwalk/runtime/common/xwalk_switches.h"
43 base::StringPiece PlatformResourceProvider(int key) {
44 if (key == IDR_DIR_HEADER_HTML) {
45 base::StringPiece html_data =
46 ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
50 return base::StringPiece();
57 using content::BrowserThread;
58 using extensions::XWalkExtension;
60 XWalkBrowserMainPartsAndroid::XWalkBrowserMainPartsAndroid(
61 const content::MainFunctionParams& parameters)
62 : XWalkBrowserMainParts(parameters) {
65 XWalkBrowserMainPartsAndroid::~XWalkBrowserMainPartsAndroid() {
68 void XWalkBrowserMainPartsAndroid::PreEarlyInitialization() {
69 net::NetworkChangeNotifier::SetFactory(
70 new net::NetworkChangeNotifierFactoryAndroid());
72 CommandLine::ForCurrentProcess()->AppendSwitch(
73 cc::switches::kCompositeToMailbox);
75 // Initialize the Compositor.
76 content::Compositor::Initialize();
78 XWalkBrowserMainParts::PreEarlyInitialization();
81 void XWalkBrowserMainPartsAndroid::PreMainMessageLoopStart() {
82 CommandLine* command_line = CommandLine::ForCurrentProcess();
83 // Disable ExtensionProcess for Android.
84 // External extensions will run in the BrowserProcess (in process mode).
85 command_line->AppendSwitch(switches::kXWalkDisableExtensionProcess);
87 // Only force to enable WebGL for Android for IA platforms because
88 // we've tested the WebGL conformance test. For other platforms, just
89 // follow up the behavior defined by Chromium upstream.
90 #if defined(ARCH_CPU_X86)
91 command_line->AppendSwitch(switches::kIgnoreGpuBlacklist);
94 #if defined(ENABLE_WEBRTC)
95 // Disable HW encoding/decoding acceleration for WebRTC on Android.
96 // FIXME: Remove these switches for Android when Android OS is removed from
97 // GPU accelerated_video_decode blacklist or we stop ignoring the GPU
99 command_line->AppendSwitch(switches::kDisableWebRtcHWDecoding);
100 command_line->AppendSwitch(switches::kDisableWebRtcHWEncoding);
103 // For fullscreen video playback, the ContentVideoView is still buggy, so
104 // we switch back to ContentVideoViewLegacy for temp.
105 // TODO(shouqun): Remove this flag when ContentVideoView is ready.
106 command_line->AppendSwitch(
107 switches::kDisableOverlayFullscreenVideoSubtitle);
109 XWalkBrowserMainParts::PreMainMessageLoopStart();
111 startup_url_ = GURL();
114 void XWalkBrowserMainPartsAndroid::PostMainMessageLoopStart() {
115 base::MessageLoopForUI::current()->Start();
118 void XWalkBrowserMainPartsAndroid::PreMainMessageLoopRun() {
119 net::NetModule::SetResourceProvider(PlatformResourceProvider);
120 if (parameters_.ui_task) {
121 parameters_.ui_task->Run();
122 delete parameters_.ui_task;
123 run_default_message_loop_ = false;
126 xwalk_runner_->PreMainMessageLoopRun();
128 extension_service_ = xwalk_runner_->extension_service();
130 // Prepare the cookie store.
131 base::FilePath user_data_dir;
132 if (!PathService::Get(base::DIR_ANDROID_APP_DATA, &user_data_dir)) {
133 NOTREACHED() << "Failed to get app data directory for Crosswalk";
135 CommandLine* command_line = CommandLine::ForCurrentProcess();
136 if (command_line->HasSwitch(switches::kXWalkProfileName))
137 user_data_dir = user_data_dir.Append(
138 command_line->GetSwitchValuePath(switches::kXWalkProfileName));
140 base::FilePath cookie_store_path = user_data_dir.Append(
141 FILE_PATH_LITERAL("Cookies"));
142 scoped_refptr<base::SequencedTaskRunner> background_task_runner =
143 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
144 BrowserThread::GetBlockingPool()->GetSequenceToken());
146 content::CookieStoreConfig cookie_config(
148 content::CookieStoreConfig::RESTORED_SESSION_COOKIES,
150 cookie_config.client_task_runner =
151 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
152 cookie_config.background_task_runner = background_task_runner;
153 cookie_store_ = content::CreateCookieStore(cookie_config);
154 cookie_store_->GetCookieMonster()->SetPersistSessionCookies(true);
155 SetCookieMonsterOnNetworkStackInit(cookie_store_->GetCookieMonster());
158 void XWalkBrowserMainPartsAndroid::PostMainMessageLoopRun() {
159 XWalkBrowserMainParts::PostMainMessageLoopRun();
161 base::MessageLoopForUI::current()->Start();
164 void XWalkBrowserMainPartsAndroid::CreateInternalExtensionsForExtensionThread(
165 content::RenderProcessHost* host,
166 extensions::XWalkExtensionVector* extensions) {
167 // On Android part, the ownership of each extension object will be transferred
168 // to XWalkExtensionServer after this method is called. It is a rule enforced
169 // by extension system that XWalkExtensionServer must own the extension
170 // objects and extension instances.
171 extensions::XWalkExtensionVector::const_iterator it = extensions_.begin();
172 for (; it != extensions_.end(); ++it)
173 extensions->push_back(*it);
176 void XWalkBrowserMainPartsAndroid::RegisterExtension(
177 scoped_ptr<XWalkExtension> extension) {
178 // Since the creation of extension object is driven by Java side, and each
179 // Java extension is backed by a native extension object. However, the Java
180 // object may be destroyed by Android lifecycle management without destroying
181 // the native side object. We keep the reference to native extension object
182 // to make sure we can reuse the native object if Java extension is re-created
184 extensions_.push_back(extension.release());
187 XWalkExtension* XWalkBrowserMainPartsAndroid::LookupExtension(
188 const std::string& name) {
189 extensions::XWalkExtensionVector::const_iterator it = extensions_.begin();
190 for (; it != extensions_.end(); ++it) {
191 XWalkExtension* extension = *it;
192 if (name == extension->name()) return extension;