Upstream version 9.38.204.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / common / xwalk_content_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/common/xwalk_content_client.h"
6
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"
20 #endif
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"
26
27 const char* const xwalk::XWalkContentClient::kNaClPluginName = "Native Client";
28
29 namespace {
30
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;
37
38 const char kPnaclPluginMimeType[] = "application/x-pnacl";
39 const char kPnaclPluginExtension[] = "";
40 const char kPnaclPluginDescription[] = "Portable Native Client Executable";
41 #endif
42
43 }  // namespace
44
45 namespace xwalk {
46
47 std::string GetProduct() {
48   return "Chrome/" CHROME_VERSION;
49 }
50
51 std::string GetUserAgent() {
52   std::string product = GetProduct();
53 #if (defined(OS_TIZEN_MOBILE) || defined(OS_ANDROID))
54   product += " Mobile Crosswalk/" XWALK_VERSION;
55 #else
56   product += " Crosswalk/" XWALK_VERSION;
57 #endif
58   CommandLine* command_line = CommandLine::ForCurrentProcess();
59   if (command_line->HasSwitch(switches::kUseMobileUserAgent))
60     product += " Mobile";
61   return content::BuildUserAgentFromProduct(product);
62 }
63
64 XWalkContentClient::XWalkContentClient() {
65 }
66
67 XWalkContentClient::~XWalkContentClient() {
68   xwalk::GetUserAgent();
69 }
70
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
79   // switch.
80   base::FilePath path;
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;
85     nacl.path = path;
86     nacl.name = XWalkContentClient::kNaClPluginName;
87     content::WebPluginMimeType nacl_mime_type(kNaClPluginMimeType,
88                                               kNaClPluginExtension,
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);
97     }
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);
105   }
106 #endif
107 }
108
109 std::string XWalkContentClient::GetProduct() const {
110   return xwalk::GetProduct();
111 }
112
113 std::string XWalkContentClient::GetUserAgent() const {
114   return xwalk::GetUserAgent();
115 }
116
117 base::string16 XWalkContentClient::GetLocalizedString(int message_id) const {
118   return l10n_util::GetStringUTF16(message_id);
119 }
120
121 base::StringPiece XWalkContentClient::GetDataResource(
122     int resource_id,
123     ui::ScaleFactor scale_factor) const {
124   return ResourceBundle::GetSharedInstance().GetRawDataResourceForScale(
125       resource_id, scale_factor);
126 }
127
128 base::RefCountedStaticMemory* XWalkContentClient::GetDataResourceBytes(
129     int resource_id) const {
130   return ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id);
131 }
132
133 gfx::Image& XWalkContentClient::GetNativeImageNamed(int resource_id) const {
134   return ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id);
135 }
136
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);
142 }
143
144 std::string XWalkContentClient::GetProcessTypeNameInEnglish(int type) {
145   switch (type) {
146     case PROCESS_TYPE_NACL_LOADER:
147       return "Native Client module";
148     case PROCESS_TYPE_NACL_BROKER:
149       return "Native Client broker";
150   }
151
152   DCHECK(false) << "Unknown child process type!";
153   return "Unknown";
154 }
155
156 }  // namespace xwalk