f92880449a095dee5a6eb07ae9e9dea94bac6d84
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / xwalk_content_browser_client.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_content_browser_client.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/command_line.h"
11 #include "base/path_service.h"
12 #include "base/platform_file.h"
13 #include "content/public/browser/browser_main_parts.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/resource_context.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/main_function_params.h"
18 #include "content/public/common/show_desktop_notification_params.h"
19 #include "net/url_request/url_request_context_getter.h"
20 #include "xwalk/extensions/common/xwalk_extension_switches.h"
21 #include "xwalk/runtime/browser/xwalk_browser_main_parts.h"
22 #include "xwalk/runtime/browser/geolocation/xwalk_access_token_store.h"
23 #include "xwalk/runtime/browser/media/media_capture_devices_dispatcher.h"
24 #include "xwalk/runtime/browser/runtime_context.h"
25 #include "xwalk/runtime/browser/runtime_quota_permission_context.h"
26 #include "xwalk/runtime/browser/speech/speech_recognition_manager_delegate.h"
27 #include "xwalk/runtime/browser/xwalk_runner.h"
28
29 #if defined(OS_ANDROID)
30 #include "base/android/path_utils.h"
31 #include "base/base_paths_android.h"
32 #include "xwalk/runtime/browser/android/xwalk_cookie_access_policy.h"
33 #include "xwalk/runtime/browser/android/xwalk_contents_client_bridge.h"
34 #include "xwalk/runtime/browser/runtime_resource_dispatcher_host_delegate_android.h"
35 #include "xwalk/runtime/browser/xwalk_browser_main_parts_android.h"
36 #include "xwalk/runtime/common/android/xwalk_globals_android.h"
37 #endif
38
39 #if defined(OS_MACOSX)
40 #include "xwalk/runtime/browser/xwalk_browser_main_parts_mac.h"
41 #endif
42
43 #if defined(OS_TIZEN)
44 #include "xwalk/runtime/browser/xwalk_browser_main_parts_tizen.h"
45 #endif
46
47 namespace xwalk {
48
49 namespace {
50
51 // The application-wide singleton of ContentBrowserClient impl.
52 XWalkContentBrowserClient* g_browser_client = NULL;
53
54 }  // namespace
55
56 // static
57 XWalkContentBrowserClient* XWalkContentBrowserClient::Get() {
58   return g_browser_client;
59 }
60
61
62 XWalkContentBrowserClient::XWalkContentBrowserClient(XWalkRunner* xwalk_runner)
63     : xwalk_runner_(xwalk_runner),
64       url_request_context_getter_(NULL),
65       main_parts_(NULL) {
66   DCHECK(!g_browser_client);
67   g_browser_client = this;
68 }
69
70 XWalkContentBrowserClient::~XWalkContentBrowserClient() {
71   DCHECK(g_browser_client);
72   g_browser_client = NULL;
73 }
74
75 content::BrowserMainParts* XWalkContentBrowserClient::CreateBrowserMainParts(
76     const content::MainFunctionParams& parameters) {
77 #if defined(OS_MACOSX)
78   main_parts_ = new XWalkBrowserMainPartsMac(parameters);
79 #elif defined(OS_ANDROID)
80   main_parts_ = new XWalkBrowserMainPartsAndroid(parameters);
81 #elif defined(OS_TIZEN)
82   main_parts_ = new XWalkBrowserMainPartsTizen(parameters);
83 #else
84   main_parts_ = new XWalkBrowserMainParts(parameters);
85 #endif
86
87   return main_parts_;
88 }
89
90 net::URLRequestContextGetter* XWalkContentBrowserClient::CreateRequestContext(
91     content::BrowserContext* browser_context,
92     content::ProtocolHandlerMap* protocol_handlers) {
93   url_request_context_getter_ = static_cast<RuntimeContext*>(browser_context)->
94       CreateRequestContext(protocol_handlers);
95   return url_request_context_getter_;
96 }
97
98 net::URLRequestContextGetter*
99 XWalkContentBrowserClient::CreateRequestContextForStoragePartition(
100     content::BrowserContext* browser_context,
101     const base::FilePath& partition_path,
102     bool in_memory,
103     content::ProtocolHandlerMap* protocol_handlers) {
104   return static_cast<RuntimeContext*>(browser_context)->
105       CreateRequestContextForStoragePartition(
106           partition_path, in_memory, protocol_handlers);
107 }
108
109 // This allow us to append extra command line switches to the child
110 // process we launch.
111 void XWalkContentBrowserClient::AppendExtraCommandLineSwitches(
112     CommandLine* command_line, int child_process_id) {
113   CommandLine* browser_process_cmd_line = CommandLine::ForCurrentProcess();
114   const int extra_switches_count = 1;
115   const char* extra_switches[extra_switches_count] = {
116     switches::kXWalkDisableExtensionProcess
117   };
118
119   for (int i = 0; i < extra_switches_count; i++) {
120     if (browser_process_cmd_line->HasSwitch(extra_switches[i]))
121       command_line->AppendSwitch(extra_switches[i]);
122   }
123 }
124
125 content::QuotaPermissionContext*
126 XWalkContentBrowserClient::CreateQuotaPermissionContext() {
127   return new RuntimeQuotaPermissionContext();
128 }
129
130 content::AccessTokenStore* XWalkContentBrowserClient::CreateAccessTokenStore() {
131   return new XWalkAccessTokenStore(url_request_context_getter_);
132 }
133
134 content::WebContentsViewDelegate*
135 XWalkContentBrowserClient::GetWebContentsViewDelegate(
136     content::WebContents* web_contents) {
137   return NULL;
138 }
139
140 void XWalkContentBrowserClient::RenderProcessWillLaunch(
141     content::RenderProcessHost* host) {
142   xwalk_runner_->OnRenderProcessWillLaunch(host);
143 }
144
145 content::MediaObserver* XWalkContentBrowserClient::GetMediaObserver() {
146   return XWalkMediaCaptureDevicesDispatcher::GetInstance();
147 }
148
149 bool XWalkContentBrowserClient::AllowGetCookie(
150     const GURL& url,
151     const GURL& first_party,
152     const net::CookieList& cookie_list,
153     content::ResourceContext* context,
154     int render_process_id,
155     int render_view_id) {
156 #if defined(OS_ANDROID)
157   return XWalkCookieAccessPolicy::GetInstance()->AllowGetCookie(
158       url,
159       first_party,
160       cookie_list,
161       context,
162       render_process_id,
163       render_view_id);
164 #else
165   return true;
166 #endif
167 }
168
169 bool XWalkContentBrowserClient::AllowSetCookie(
170     const GURL& url,
171     const GURL& first_party,
172     const std::string& cookie_line,
173     content::ResourceContext* context,
174     int render_process_id,
175     int render_view_id,
176     net::CookieOptions* options) {
177 #if defined(OS_ANDROID)
178   return XWalkCookieAccessPolicy::GetInstance()->AllowSetCookie(
179       url,
180       first_party,
181       cookie_line,
182       context,
183       render_process_id,
184       render_view_id,
185       options);
186 #else
187   return true;
188 #endif
189 }
190
191 void XWalkContentBrowserClient::RequestDesktopNotificationPermission(
192     const GURL& source_origin,
193     int callback_context,
194     int render_process_id,
195     int render_view_id) {
196 }
197
198 blink::WebNotificationPresenter::Permission
199 XWalkContentBrowserClient::CheckDesktopNotificationPermission(
200     const GURL& source_url,
201     content::ResourceContext* context,
202     int render_process_id) {
203 #if defined(OS_ANDROID)
204   return blink::WebNotificationPresenter::PermissionAllowed;
205 #else
206   return blink::WebNotificationPresenter::PermissionNotAllowed;
207 #endif
208 }
209
210 void XWalkContentBrowserClient::ShowDesktopNotification(
211     const content::ShowDesktopNotificationHostMsgParams& params,
212     int render_process_id,
213     int render_view_id,
214     bool worker) {
215 #if defined(OS_ANDROID)
216   XWalkContentsClientBridgeBase* bridge =
217       XWalkContentsClientBridgeBase::FromID(render_process_id, render_view_id);
218   bridge->ShowNotification(params, worker, render_process_id, render_view_id);
219 #endif
220 }
221
222 void XWalkContentBrowserClient::CancelDesktopNotification(
223     int render_process_id,
224     int render_view_id,
225     int notification_id) {
226 #if defined(OS_ANDROID)
227   XWalkContentsClientBridgeBase* bridge =
228       XWalkContentsClientBridgeBase::FromID(render_process_id, render_view_id);
229   bridge->CancelNotification(
230       notification_id, render_process_id, render_view_id);
231 #endif
232 }
233
234 #if defined(OS_ANDROID)
235 void XWalkContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
236     const CommandLine& command_line,
237     int child_process_id,
238     std::vector<content::FileDescriptorInfo>* mappings) {
239   int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ;
240   base::FilePath pak_file;
241   bool r = PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_file);
242   CHECK(r);
243   pak_file = pak_file.Append(FILE_PATH_LITERAL("paks"));
244   pak_file = pak_file.Append(FILE_PATH_LITERAL(kXWalkPakFilePath));
245
246   base::PlatformFile f =
247       base::CreatePlatformFile(pak_file, flags, NULL, NULL);
248   if (f == base::kInvalidPlatformFileValue) {
249     NOTREACHED() << "Failed to open file when creating renderer process: "
250                  << "xwalk.pak";
251   }
252   mappings->push_back(
253       content::FileDescriptorInfo(kXWalkPakDescriptor,
254                                   base::FileDescriptor(f, true)));
255 }
256
257 void XWalkContentBrowserClient::ResourceDispatcherHostCreated() {
258   RuntimeResourceDispatcherHostDelegateAndroid::
259   ResourceDispatcherHostCreated();
260 }
261 #endif
262
263 content::SpeechRecognitionManagerDelegate*
264     XWalkContentBrowserClient::GetSpeechRecognitionManagerDelegate() {
265   return new xwalk::XWalkSpeechRecognitionManagerDelegate();
266 }
267
268 }  // namespace xwalk