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