- add sources.
[platform/framework/web/crosswalk.git] / src / webkit / common / user_agent / user_agent_util.cc
1 // Copyright (c) 2012 The Chromium Authors. 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 "webkit/common/user_agent/user_agent_util.h"
6
7 #if defined(OS_POSIX) && !defined(OS_MACOSX)
8 #include <sys/utsname.h>
9 #endif
10
11 #include "base/lazy_instance.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/sys_info.h"
15
16 #if defined(OS_WIN)
17 #include "base/win/windows_version.h"
18 #endif
19
20 // Generated
21 #include "webkit_version.h"  // NOLINT
22
23 namespace webkit_glue {
24
25 std::string GetWebKitVersion() {
26   return base::StringPrintf("%d.%d (%s)",
27                             WEBKIT_VERSION_MAJOR,
28                             WEBKIT_VERSION_MINOR,
29                             WEBKIT_SVN_REVISION);
30 }
31
32 std::string GetWebKitRevision() {
33   return WEBKIT_SVN_REVISION;
34 }
35
36 #if defined(OS_ANDROID)
37 std::string GetAndroidDeviceName() {
38   std::string android_device_name = base::SysInfo::GetDeviceName();
39 #if defined(GOOGLE_TV)
40   android_device_name += " TV";
41 #endif
42   return android_device_name;
43 }
44 #endif
45
46 std::string BuildOSCpuInfo() {
47   std::string os_cpu;
48
49 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) ||\
50     defined(OS_ANDROID)
51   int32 os_major_version = 0;
52   int32 os_minor_version = 0;
53   int32 os_bugfix_version = 0;
54   base::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
55                                                &os_minor_version,
56                                                &os_bugfix_version);
57 #endif
58
59 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
60   // Should work on any Posix system.
61   struct utsname unixinfo;
62   uname(&unixinfo);
63
64   std::string cputype;
65   // special case for biarch systems
66   if (strcmp(unixinfo.machine, "x86_64") == 0 &&
67       sizeof(void*) == sizeof(int32)) {  // NOLINT
68     cputype.assign("i686 (x86_64)");
69   } else {
70     cputype.assign(unixinfo.machine);
71   }
72 #endif
73
74 #if defined(OS_WIN)
75   std::string architecture_token;
76   base::win::OSInfo* os_info = base::win::OSInfo::GetInstance();
77   if (os_info->wow64_status() == base::win::OSInfo::WOW64_ENABLED) {
78     architecture_token = "; WOW64";
79   } else {
80     base::win::OSInfo::WindowsArchitecture windows_architecture =
81         os_info->architecture();
82     if (windows_architecture == base::win::OSInfo::X64_ARCHITECTURE)
83       architecture_token = "; Win64; x64";
84     else if (windows_architecture == base::win::OSInfo::IA64_ARCHITECTURE)
85       architecture_token = "; Win64; IA64";
86   }
87 #endif
88
89 #if defined(OS_ANDROID)
90   std::string android_version_str;
91   base::StringAppendF(
92       &android_version_str, "%d.%d", os_major_version, os_minor_version);
93   if (os_bugfix_version != 0)
94     base::StringAppendF(&android_version_str, ".%d", os_bugfix_version);
95
96   std::string android_info_str;
97
98   // Send information about the device.
99   bool semicolon_inserted = false;
100   std::string android_build_codename = base::SysInfo::GetAndroidBuildCodename();
101   std::string android_device_name = GetAndroidDeviceName();
102   if ("REL" == android_build_codename && android_device_name.size() > 0) {
103     android_info_str += "; " + android_device_name;
104     semicolon_inserted = true;
105   }
106
107   // Append the build ID.
108   std::string android_build_id = base::SysInfo::GetAndroidBuildID();
109   if (android_build_id.size() > 0) {
110     if (!semicolon_inserted) {
111       android_info_str += ";";
112     }
113     android_info_str += " Build/" + android_build_id;
114   }
115 #endif
116
117   base::StringAppendF(
118       &os_cpu,
119 #if defined(OS_WIN)
120       "Windows NT %d.%d%s",
121       os_major_version,
122       os_minor_version,
123       architecture_token.c_str()
124 #elif defined(OS_MACOSX)
125       "Intel Mac OS X %d_%d_%d",
126       os_major_version,
127       os_minor_version,
128       os_bugfix_version
129 #elif defined(OS_CHROMEOS)
130       "CrOS "
131       "%s %d.%d.%d",
132       cputype.c_str(),   // e.g. i686
133       os_major_version,
134       os_minor_version,
135       os_bugfix_version
136 #elif defined(OS_ANDROID)
137       "Android %s%s",
138       android_version_str.c_str(),
139       android_info_str.c_str()
140 #else
141       "%s %s",
142       unixinfo.sysname,  // e.g. Linux
143       cputype.c_str()    // e.g. i686
144 #endif
145   );  // NOLINT
146
147   return os_cpu;
148 }
149
150 int GetWebKitMajorVersion() {
151   return WEBKIT_VERSION_MAJOR;
152 }
153
154 int GetWebKitMinorVersion() {
155   return WEBKIT_VERSION_MINOR;
156 }
157
158 std::string BuildUserAgentFromProduct(const std::string& product) {
159   const char kUserAgentPlatform[] =
160 #if defined(OS_WIN)
161       "";
162 #elif defined(OS_MACOSX)
163       "Macintosh; ";
164 #elif defined(USE_X11)
165       "X11; ";           // strange, but that's what Firefox uses
166 #elif defined(OS_ANDROID)
167       "Linux; ";
168 #else
169       "Unknown; ";
170 #endif
171
172   std::string os_info;
173   base::StringAppendF(&os_info, "%s%s", kUserAgentPlatform,
174                       webkit_glue::BuildOSCpuInfo().c_str());
175   return BuildUserAgentFromOSAndProduct(os_info, product);
176 }
177
178 std::string BuildUserAgentFromOSAndProduct(const std::string& os_info,
179                                            const std::string& product) {
180   // Derived from Safari's UA string.
181   // This is done to expose our product name in a manner that is maximally
182   // compatible with Safari, we hope!!
183   std::string user_agent;
184   base::StringAppendF(
185       &user_agent,
186       "Mozilla/5.0 (%s) AppleWebKit/%d.%d (KHTML, like Gecko) %s Safari/%d.%d",
187       os_info.c_str(),
188       WEBKIT_VERSION_MAJOR,
189       WEBKIT_VERSION_MINOR,
190       product.c_str(),
191       WEBKIT_VERSION_MAJOR,
192       WEBKIT_VERSION_MINOR);
193   return user_agent;
194 }
195
196 }  // namespace webkit_glue