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