754230d326273f44e4d4dc718433f577a38df08f
[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 #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"
23
24 const char* const xwalk::XWalkContentClient::kNaClPluginName = "Native Client";
25
26 namespace {
27
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;
33
34 const char kPnaclPluginMimeType[] = "application/x-pnacl";
35 const char kPnaclPluginExtension[] = "";
36 const char kPnaclPluginDescription[] = "Portable Native Client Executable";
37
38 }  // namespace
39
40 namespace xwalk {
41
42 std::string GetProduct() {
43   return "Chrome/" CHROME_VERSION;
44 }
45
46 std::string GetUserAgent() {
47   std::string product = GetProduct();
48 #if (defined(OS_TIZEN) || defined(OS_ANDROID))
49   product += " Mobile Crosswalk/" XWALK_VERSION;
50 #else
51   product += " Crosswalk/" XWALK_VERSION;
52 #endif
53   CommandLine* command_line = CommandLine::ForCurrentProcess();
54   if (command_line->HasSwitch(switches::kUseMobileUserAgent))
55     product += " Mobile";
56   return content::BuildUserAgentFromProduct(product);
57 }
58
59 XWalkContentClient::XWalkContentClient() {
60 }
61
62 XWalkContentClient::~XWalkContentClient() {
63   xwalk::GetUserAgent();
64 }
65
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
73   // switch.
74   static bool skip_nacl_file_check = false;
75   base::FilePath path;
76   if (PathService::Get(xwalk::FILE_NACL_PLUGIN, &path)) {
77     if (skip_nacl_file_check || base::PathExists(path)) {
78       content::PepperPluginInfo nacl;
79       nacl.path = path;
80       nacl.name = XWalkContentClient::kNaClPluginName;
81       content::WebPluginMimeType nacl_mime_type(kNaClPluginMimeType,
82                                                 kNaClPluginExtension,
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);
91       }
92       nacl.permissions = kNaClPluginPermissions;
93       plugins->push_back(nacl);
94
95       skip_nacl_file_check = true;
96     }
97   }
98 }
99
100 std::string XWalkContentClient::GetProduct() const {
101   return xwalk::GetProduct();
102 }
103
104 std::string XWalkContentClient::GetUserAgent() const {
105   return xwalk::GetUserAgent();
106 }
107
108 base::string16 XWalkContentClient::GetLocalizedString(int message_id) const {
109   return l10n_util::GetStringUTF16(message_id);
110 }
111
112 base::StringPiece XWalkContentClient::GetDataResource(
113     int resource_id,
114     ui::ScaleFactor scale_factor) const {
115   return ResourceBundle::GetSharedInstance().GetRawDataResourceForScale(
116       resource_id, scale_factor);
117 }
118
119 base::RefCountedStaticMemory* XWalkContentClient::GetDataResourceBytes(
120     int resource_id) const {
121   return ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id);
122 }
123
124 gfx::Image& XWalkContentClient::GetNativeImageNamed(int resource_id) const {
125   return ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id);
126 }
127
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);
133 }
134
135 std::string XWalkContentClient::GetProcessTypeNameInEnglish(int type) {
136   switch (type) {
137     case PROCESS_TYPE_NACL_LOADER:
138       return "Native Client module";
139     case PROCESS_TYPE_NACL_BROKER:
140       return "Native Client broker";
141   }
142
143   DCHECK(false) << "Unknown child process type!";
144   return "Unknown";
145 }
146
147 }  // namespace xwalk