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/common/xwalk_content_client.h"
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/path_service.h"
11 #include "base/strings/string_piece.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "components/nacl/common/nacl_process_type.h"
14 #include "content/public/common/content_switches.h"
15 #include "content/public/common/user_agent.h"
16 #include "content/public/common/pepper_plugin_info.h"
17 #if !defined(DISABLE_NACL)
18 #include "ppapi/native_client/src/trusted/plugin/ppapi_entrypoints.h"
19 #include "ppapi/shared_impl/ppapi_permissions.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/base/resource/resource_bundle.h"
23 #include "xwalk/application/common/constants.h"
24 #include "xwalk/runtime/common/xwalk_switches.h"
25 #include "xwalk/runtime/common/xwalk_paths.h"
27 const char* const xwalk::XWalkContentClient::kNaClPluginName = "Native Client";
31 #if !defined(DISABLE_NACL)
32 const char kNaClPluginMimeType[] = "application/x-nacl";
33 const char kNaClPluginExtension[] = "";
34 const char kNaClPluginDescription[] = "Native Client Executable";
35 const uint32 kNaClPluginPermissions = ppapi::PERMISSION_PRIVATE |
36 ppapi::PERMISSION_DEV;
38 const char kPnaclPluginMimeType[] = "application/x-pnacl";
39 const char kPnaclPluginExtension[] = "";
40 const char kPnaclPluginDescription[] = "Portable Native Client Executable";
47 std::string GetProduct() {
48 return "Chrome/" CHROME_VERSION;
51 std::string GetUserAgent() {
52 std::string product = GetProduct();
53 #if (defined(OS_TIZEN_MOBILE) || defined(OS_ANDROID))
54 product += " Mobile Crosswalk/" XWALK_VERSION;
56 product += " Crosswalk/" XWALK_VERSION;
58 CommandLine* command_line = CommandLine::ForCurrentProcess();
59 if (command_line->HasSwitch(switches::kUseMobileUserAgent))
61 return content::BuildUserAgentFromProduct(product);
64 XWalkContentClient::XWalkContentClient() {
67 XWalkContentClient::~XWalkContentClient() {
68 xwalk::GetUserAgent();
71 void XWalkContentClient::AddPepperPlugins(
72 std::vector<content::PepperPluginInfo>* plugins) {
73 #if !defined(DISABLE_NACL)
74 // Handle Native Client just like the PDF plugin. This means that it is
75 // enabled by default for the non-portable case. This allows apps installed
76 // from the Chrome Web Store to use NaCl even if the command line switch
77 // isn't set. For other uses of NaCl we check for the command line switch.
78 // Specifically, Portable Native Client is only enabled by the command line
81 if (PathService::Get(xwalk::FILE_NACL_PLUGIN, &path)) {
82 content::PepperPluginInfo nacl;
83 // The nacl plugin is now built into the Chromium binary.
84 nacl.is_internal = true;
86 nacl.name = XWalkContentClient::kNaClPluginName;
87 content::WebPluginMimeType nacl_mime_type(kNaClPluginMimeType,
89 kNaClPluginDescription);
90 nacl.mime_types.push_back(nacl_mime_type);
91 if (!CommandLine::ForCurrentProcess()->HasSwitch(
92 switches::kDisablePnacl)) {
93 content::WebPluginMimeType pnacl_mime_type(kPnaclPluginMimeType,
94 kPnaclPluginExtension,
95 kPnaclPluginDescription);
96 nacl.mime_types.push_back(pnacl_mime_type);
98 nacl.internal_entry_points.get_interface = nacl_plugin::PPP_GetInterface;
99 nacl.internal_entry_points.initialize_module =
100 nacl_plugin::PPP_InitializeModule;
101 nacl.internal_entry_points.shutdown_module =
102 nacl_plugin::PPP_ShutdownModule;
103 nacl.permissions = kNaClPluginPermissions;
104 plugins->push_back(nacl);
109 std::string XWalkContentClient::GetProduct() const {
110 return xwalk::GetProduct();
113 std::string XWalkContentClient::GetUserAgent() const {
114 return xwalk::GetUserAgent();
117 base::string16 XWalkContentClient::GetLocalizedString(int message_id) const {
118 return l10n_util::GetStringUTF16(message_id);
121 base::StringPiece XWalkContentClient::GetDataResource(
123 ui::ScaleFactor scale_factor) const {
124 return ResourceBundle::GetSharedInstance().GetRawDataResourceForScale(
125 resource_id, scale_factor);
128 base::RefCountedStaticMemory* XWalkContentClient::GetDataResourceBytes(
129 int resource_id) const {
130 return ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id);
133 gfx::Image& XWalkContentClient::GetNativeImageNamed(int resource_id) const {
134 return ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id);
137 void XWalkContentClient::AddAdditionalSchemes(
138 std::vector<std::string>* standard_schemes,
139 std::vector<std::string>* savable_schemes) {
140 standard_schemes->push_back(application::kApplicationScheme);
141 savable_schemes->push_back(application::kApplicationScheme);
144 std::string XWalkContentClient::GetProcessTypeNameInEnglish(int type) {
146 case PROCESS_TYPE_NACL_LOADER:
147 return "Native Client module";
148 case PROCESS_TYPE_NACL_BROKER:
149 return "Native Client broker";
152 DCHECK(false) << "Unknown child process type!";