1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
6 #include "xwalk/runtime/common/xwalk_content_client.h"
8 #include "base/command_line.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/path_service.h"
12 #include "base/strings/string_piece.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "components/nacl/common/nacl_process_type.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/common/user_agent.h"
17 #if !defined(DISABLE_NACL)
18 #include "content/public/common/pepper_plugin_info.h"
19 #include "ppapi/native_client/src/trusted/plugin/ppapi_entrypoints.h"
20 #include "ppapi/shared_impl/ppapi_permissions.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "xwalk/application/common/constants.h"
25 #include "xwalk/runtime/common/xwalk_switches.h"
26 #include "xwalk/runtime/common/xwalk_paths.h"
27 #if (defined(OS_TIZEN))
28 #include "xwalk/runtime/renderer/xwalk_content_renderer_client.h"
29 #include "xwalk/runtime/renderer/tizen/xwalk_content_renderer_client_tizen.h"
32 const char* const xwalk::XWalkContentClient::kNaClPluginName = "Native Client";
36 #if !defined(DISABLE_NACL)
37 const char kNaClPluginMimeType[] = "application/x-nacl";
38 const char kNaClPluginExtension[] = "";
39 const char kNaClPluginDescription[] = "Native Client Executable";
40 const uint32 kNaClPluginPermissions = ppapi::PERMISSION_PRIVATE |
41 ppapi::PERMISSION_DEV;
43 const char kPnaclPluginMimeType[] = "application/x-pnacl";
44 const char kPnaclPluginExtension[] = "";
45 const char kPnaclPluginDescription[] = "Portable Native Client Executable";
52 std::string GetProduct() {
53 return "Chrome/" CHROME_VERSION;
56 std::string GetUserAgent() {
57 std::string product = GetProduct();
58 #if (defined(OS_TIZEN_MOBILE) || defined(OS_ANDROID))
59 product += " Mobile Crosswalk/" XWALK_VERSION;
61 product += " Crosswalk/" XWALK_VERSION;
63 CommandLine* command_line = CommandLine::ForCurrentProcess();
64 if (command_line->HasSwitch(switches::kUseMobileUserAgent))
66 return content::BuildUserAgentFromProduct(product);
69 XWalkContentClient::XWalkContentClient() {
72 XWalkContentClient::~XWalkContentClient() {
73 xwalk::GetUserAgent();
76 void XWalkContentClient::AddPepperPlugins(
77 std::vector<content::PepperPluginInfo>* plugins) {
78 #if !defined(DISABLE_NACL)
79 // Handle Native Client just like the PDF plugin. This means that it is
80 // enabled by default for the non-portable case. This allows apps installed
81 // from the Chrome Web Store to use NaCl even if the command line switch
82 // isn't set. For other uses of NaCl we check for the command line switch.
83 // Specifically, Portable Native Client is only enabled by the command line
86 if (PathService::Get(xwalk::FILE_NACL_PLUGIN, &path)) {
87 content::PepperPluginInfo nacl;
88 // The nacl plugin is now built into the Chromium binary.
89 nacl.is_internal = true;
91 nacl.name = XWalkContentClient::kNaClPluginName;
92 content::WebPluginMimeType nacl_mime_type(kNaClPluginMimeType,
94 kNaClPluginDescription);
95 nacl.mime_types.push_back(nacl_mime_type);
96 if (!CommandLine::ForCurrentProcess()->HasSwitch(
97 switches::kDisablePnacl)) {
98 content::WebPluginMimeType pnacl_mime_type(kPnaclPluginMimeType,
99 kPnaclPluginExtension,
100 kPnaclPluginDescription);
101 nacl.mime_types.push_back(pnacl_mime_type);
103 nacl.internal_entry_points.get_interface = nacl_plugin::PPP_GetInterface;
104 nacl.internal_entry_points.initialize_module =
105 nacl_plugin::PPP_InitializeModule;
106 nacl.internal_entry_points.shutdown_module =
107 nacl_plugin::PPP_ShutdownModule;
108 nacl.permissions = kNaClPluginPermissions;
109 plugins->push_back(nacl);
114 std::string XWalkContentClient::GetProduct() const {
115 return xwalk::GetProduct();
118 std::string XWalkContentClient::GetUserAgent() const {
119 #if (defined(OS_TIZEN))
120 // TODO(jizydorczyk):
121 // const_cast below is required to invoke ContentClient::renderer() method,
122 // I think there is no reason for ContentClient::renderer() in content API
123 // to be non-const as it doesn't change any data, so it should be changed in
124 // chromium code later.
125 XWalkContentClient* content_client = const_cast<XWalkContentClient*>(this);
126 content::ContentRendererClient* content_renderer_client =
127 content_client->renderer();
128 if (content_renderer_client) {
129 XWalkContentRendererClientTizen* content_renderer_client_tizen =
130 static_cast<XWalkContentRendererClientTizen*>(
131 content_renderer_client);
132 const std::string& user_agent_string = content_renderer_client_tizen->
133 GetOverridenUserAgent();
134 if (!user_agent_string.empty())
135 return user_agent_string;
138 return xwalk::GetUserAgent();
141 base::string16 XWalkContentClient::GetLocalizedString(int message_id) const {
142 return l10n_util::GetStringUTF16(message_id);
145 base::StringPiece XWalkContentClient::GetDataResource(
147 ui::ScaleFactor scale_factor) const {
148 return ResourceBundle::GetSharedInstance().GetRawDataResourceForScale(
149 resource_id, scale_factor);
152 base::RefCountedStaticMemory* XWalkContentClient::GetDataResourceBytes(
153 int resource_id) const {
154 return ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id);
157 gfx::Image& XWalkContentClient::GetNativeImageNamed(int resource_id) const {
158 return ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id);
161 void XWalkContentClient::AddAdditionalSchemes(
162 std::vector<std::string>* standard_schemes,
163 std::vector<std::string>* savable_schemes) {
164 standard_schemes->push_back(application::kApplicationScheme);
165 savable_schemes->push_back(application::kApplicationScheme);
168 std::string XWalkContentClient::GetProcessTypeNameInEnglish(int type) {
170 case PROCESS_TYPE_NACL_LOADER:
171 return "Native Client module";
172 case PROCESS_TYPE_NACL_BROKER:
173 return "Native Client broker";
176 DCHECK(false) << "Unknown child process type!";