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 #include "ppapi/shared_impl/ppapi_permissions.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "xwalk/application/common/constants.h"
21 #include "xwalk/runtime/common/xwalk_switches.h"
22 #include "xwalk/runtime/common/xwalk_paths.h"
24 const char* const xwalk::XWalkContentClient::kNaClPluginName = "Native Client";
28 const char kNaClPluginMimeType[] = "application/x-nacl";
29 const char kNaClPluginExtension[] = "";
30 const char kNaClPluginDescription[] = "Native Client Executable";
31 const uint32 kNaClPluginPermissions = ppapi::PERMISSION_PRIVATE |
32 ppapi::PERMISSION_DEV;
34 const char kPnaclPluginMimeType[] = "application/x-pnacl";
35 const char kPnaclPluginExtension[] = "";
36 const char kPnaclPluginDescription[] = "Portable Native Client Executable";
42 std::string GetProduct() {
43 return "Chrome/" CHROME_VERSION;
46 std::string GetUserAgent() {
47 std::string product = GetProduct();
48 #if (defined(OS_TIZEN_MOBILE) || defined(OS_ANDROID))
49 product += " Mobile Crosswalk/" XWALK_VERSION;
51 product += " Crosswalk/" XWALK_VERSION;
53 CommandLine* command_line = CommandLine::ForCurrentProcess();
54 if (command_line->HasSwitch(switches::kUseMobileUserAgent))
56 return content::BuildUserAgentFromProduct(product);
59 XWalkContentClient::XWalkContentClient() {
62 XWalkContentClient::~XWalkContentClient() {
63 xwalk::GetUserAgent();
66 void XWalkContentClient::AddPepperPlugins(
67 std::vector<content::PepperPluginInfo>* plugins) {
68 // Handle Native Client just like the PDF plugin. This means that it is
69 // enabled by default for the non-portable case. This allows apps installed
70 // from the Chrome Web Store to use NaCl even if the command line switch
71 // isn't set. For other uses of NaCl we check for the command line switch.
72 // Specifically, Portable Native Client is only enabled by the command line
74 static bool skip_nacl_file_check = false;
76 if (PathService::Get(xwalk::FILE_NACL_PLUGIN, &path)) {
77 if (skip_nacl_file_check || base::PathExists(path)) {
78 content::PepperPluginInfo nacl;
80 nacl.name = XWalkContentClient::kNaClPluginName;
81 content::WebPluginMimeType nacl_mime_type(kNaClPluginMimeType,
83 kNaClPluginDescription);
84 nacl.mime_types.push_back(nacl_mime_type);
85 if (!CommandLine::ForCurrentProcess()->HasSwitch(
86 switches::kDisablePnacl)) {
87 content::WebPluginMimeType pnacl_mime_type(kPnaclPluginMimeType,
88 kPnaclPluginExtension,
89 kPnaclPluginDescription);
90 nacl.mime_types.push_back(pnacl_mime_type);
92 nacl.permissions = kNaClPluginPermissions;
93 plugins->push_back(nacl);
95 skip_nacl_file_check = true;
100 std::string XWalkContentClient::GetProduct() const {
101 return xwalk::GetProduct();
104 std::string XWalkContentClient::GetUserAgent() const {
105 return xwalk::GetUserAgent();
108 base::string16 XWalkContentClient::GetLocalizedString(int message_id) const {
109 return l10n_util::GetStringUTF16(message_id);
112 base::StringPiece XWalkContentClient::GetDataResource(
114 ui::ScaleFactor scale_factor) const {
115 return ResourceBundle::GetSharedInstance().GetRawDataResourceForScale(
116 resource_id, scale_factor);
119 base::RefCountedStaticMemory* XWalkContentClient::GetDataResourceBytes(
120 int resource_id) const {
121 return ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id);
124 gfx::Image& XWalkContentClient::GetNativeImageNamed(int resource_id) const {
125 return ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id);
128 void XWalkContentClient::AddAdditionalSchemes(
129 std::vector<std::string>* standard_schemes,
130 std::vector<std::string>* savable_schemes) {
131 standard_schemes->push_back(application::kApplicationScheme);
132 savable_schemes->push_back(application::kApplicationScheme);
135 std::string XWalkContentClient::GetProcessTypeNameInEnglish(int type) {
137 case PROCESS_TYPE_NACL_LOADER:
138 return "Native Client module";
139 case PROCESS_TYPE_NACL_BROKER:
140 return "Native Client broker";
143 DCHECK(false) << "Unknown child process type!";