1 | // Copyright 2008 The Closure Library Authors. All Rights Reserved. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS-IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | /** |
16 | * @fileoverview Detects the specific browser and not just the rendering engine. |
17 | * |
18 | */ |
19 | |
20 | goog.provide('goog.userAgent.product'); |
21 | |
22 | goog.require('goog.userAgent'); |
23 | |
24 | |
25 | /** |
26 | * @define {boolean} Whether the code is running on the Firefox web browser. |
27 | */ |
28 | goog.define('goog.userAgent.product.ASSUME_FIREFOX', false); |
29 | |
30 | |
31 | /** |
32 | * @define {boolean} Whether the code is running on the Camino web browser. |
33 | */ |
34 | goog.define('goog.userAgent.product.ASSUME_CAMINO', false); |
35 | |
36 | |
37 | /** |
38 | * @define {boolean} Whether we know at compile-time that the product is an |
39 | * iPhone. |
40 | */ |
41 | goog.define('goog.userAgent.product.ASSUME_IPHONE', false); |
42 | |
43 | |
44 | /** |
45 | * @define {boolean} Whether we know at compile-time that the product is an |
46 | * iPad. |
47 | */ |
48 | goog.define('goog.userAgent.product.ASSUME_IPAD', false); |
49 | |
50 | |
51 | /** |
52 | * @define {boolean} Whether we know at compile-time that the product is an |
53 | * Android phone. |
54 | */ |
55 | goog.define('goog.userAgent.product.ASSUME_ANDROID', false); |
56 | |
57 | |
58 | /** |
59 | * @define {boolean} Whether the code is running on the Chrome web browser. |
60 | */ |
61 | goog.define('goog.userAgent.product.ASSUME_CHROME', false); |
62 | |
63 | |
64 | /** |
65 | * @define {boolean} Whether the code is running on the Safari web browser. |
66 | */ |
67 | goog.define('goog.userAgent.product.ASSUME_SAFARI', false); |
68 | |
69 | |
70 | /** |
71 | * Whether we know the product type at compile-time. |
72 | * @type {boolean} |
73 | * @private |
74 | */ |
75 | goog.userAgent.product.PRODUCT_KNOWN_ = |
76 | goog.userAgent.ASSUME_IE || |
77 | goog.userAgent.ASSUME_OPERA || |
78 | goog.userAgent.product.ASSUME_FIREFOX || |
79 | goog.userAgent.product.ASSUME_CAMINO || |
80 | goog.userAgent.product.ASSUME_IPHONE || |
81 | goog.userAgent.product.ASSUME_IPAD || |
82 | goog.userAgent.product.ASSUME_ANDROID || |
83 | goog.userAgent.product.ASSUME_CHROME || |
84 | goog.userAgent.product.ASSUME_SAFARI; |
85 | |
86 | |
87 | /** |
88 | * Right now we just focus on Tier 1-3 browsers at: |
89 | * http://wiki/Nonconf/ProductPlatformGuidelines |
90 | * As well as the YUI grade A browsers at: |
91 | * http://developer.yahoo.com/yui/articles/gbs/ |
92 | * |
93 | * @private |
94 | */ |
95 | goog.userAgent.product.init_ = function() { |
96 | |
97 | /** |
98 | * Whether the code is running on the Firefox web browser. |
99 | * @type {boolean} |
100 | * @private |
101 | */ |
102 | goog.userAgent.product.detectedFirefox_ = false; |
103 | |
104 | /** |
105 | * Whether the code is running on the Camino web browser. |
106 | * @type {boolean} |
107 | * @private |
108 | */ |
109 | goog.userAgent.product.detectedCamino_ = false; |
110 | |
111 | /** |
112 | * Whether the code is running on an iPhone or iPod touch. |
113 | * @type {boolean} |
114 | * @private |
115 | */ |
116 | goog.userAgent.product.detectedIphone_ = false; |
117 | |
118 | /** |
119 | * Whether the code is running on an iPad |
120 | * @type {boolean} |
121 | * @private |
122 | */ |
123 | goog.userAgent.product.detectedIpad_ = false; |
124 | |
125 | /** |
126 | * Whether the code is running on the default browser on an Android phone. |
127 | * @type {boolean} |
128 | * @private |
129 | */ |
130 | goog.userAgent.product.detectedAndroid_ = false; |
131 | |
132 | /** |
133 | * Whether the code is running on the Chrome web browser. |
134 | * @type {boolean} |
135 | * @private |
136 | */ |
137 | goog.userAgent.product.detectedChrome_ = false; |
138 | |
139 | /** |
140 | * Whether the code is running on the Safari web browser. |
141 | * @type {boolean} |
142 | * @private |
143 | */ |
144 | goog.userAgent.product.detectedSafari_ = false; |
145 | |
146 | var ua = goog.userAgent.getUserAgentString(); |
147 | if (!ua) { |
148 | return; |
149 | } |
150 | |
151 | // The order of the if-statements in the following code is important. |
152 | // For example, in the WebKit section, we put Chrome in front of Safari |
153 | // because the string 'Safari' is present on both of those browsers' |
154 | // userAgent strings as well as the string we are looking for. |
155 | // The idea is to prevent accidental detection of more than one client. |
156 | |
157 | if (ua.indexOf('Firefox') != -1) { |
158 | goog.userAgent.product.detectedFirefox_ = true; |
159 | } else if (ua.indexOf('Camino') != -1) { |
160 | goog.userAgent.product.detectedCamino_ = true; |
161 | } else if (ua.indexOf('iPhone') != -1 || ua.indexOf('iPod') != -1) { |
162 | goog.userAgent.product.detectedIphone_ = true; |
163 | } else if (ua.indexOf('iPad') != -1) { |
164 | goog.userAgent.product.detectedIpad_ = true; |
165 | } else if (ua.indexOf('Android') != -1) { |
166 | goog.userAgent.product.detectedAndroid_ = true; |
167 | } else if (ua.indexOf('Chrome') != -1) { |
168 | goog.userAgent.product.detectedChrome_ = true; |
169 | } else if (ua.indexOf('Safari') != -1) { |
170 | goog.userAgent.product.detectedSafari_ = true; |
171 | } |
172 | }; |
173 | |
174 | if (!goog.userAgent.product.PRODUCT_KNOWN_) { |
175 | goog.userAgent.product.init_(); |
176 | } |
177 | |
178 | |
179 | /** |
180 | * Whether the code is running on the Opera web browser. |
181 | * @type {boolean} |
182 | */ |
183 | goog.userAgent.product.OPERA = goog.userAgent.OPERA; |
184 | |
185 | |
186 | /** |
187 | * Whether the code is running on an IE web browser. |
188 | * @type {boolean} |
189 | */ |
190 | goog.userAgent.product.IE = goog.userAgent.IE; |
191 | |
192 | |
193 | /** |
194 | * Whether the code is running on the Firefox web browser. |
195 | * @type {boolean} |
196 | */ |
197 | goog.userAgent.product.FIREFOX = goog.userAgent.product.PRODUCT_KNOWN_ ? |
198 | goog.userAgent.product.ASSUME_FIREFOX : |
199 | goog.userAgent.product.detectedFirefox_; |
200 | |
201 | |
202 | /** |
203 | * Whether the code is running on the Camino web browser. |
204 | * @type {boolean} |
205 | */ |
206 | goog.userAgent.product.CAMINO = goog.userAgent.product.PRODUCT_KNOWN_ ? |
207 | goog.userAgent.product.ASSUME_CAMINO : |
208 | goog.userAgent.product.detectedCamino_; |
209 | |
210 | |
211 | /** |
212 | * Whether the code is running on an iPhone or iPod touch. |
213 | * @type {boolean} |
214 | */ |
215 | goog.userAgent.product.IPHONE = goog.userAgent.product.PRODUCT_KNOWN_ ? |
216 | goog.userAgent.product.ASSUME_IPHONE : |
217 | goog.userAgent.product.detectedIphone_; |
218 | |
219 | |
220 | /** |
221 | * Whether the code is running on an iPad. |
222 | * @type {boolean} |
223 | */ |
224 | goog.userAgent.product.IPAD = goog.userAgent.product.PRODUCT_KNOWN_ ? |
225 | goog.userAgent.product.ASSUME_IPAD : |
226 | goog.userAgent.product.detectedIpad_; |
227 | |
228 | |
229 | /** |
230 | * Whether the code is running on the default browser on an Android phone. |
231 | * @type {boolean} |
232 | */ |
233 | goog.userAgent.product.ANDROID = goog.userAgent.product.PRODUCT_KNOWN_ ? |
234 | goog.userAgent.product.ASSUME_ANDROID : |
235 | goog.userAgent.product.detectedAndroid_; |
236 | |
237 | |
238 | /** |
239 | * Whether the code is running on the Chrome web browser. |
240 | * @type {boolean} |
241 | */ |
242 | goog.userAgent.product.CHROME = goog.userAgent.product.PRODUCT_KNOWN_ ? |
243 | goog.userAgent.product.ASSUME_CHROME : |
244 | goog.userAgent.product.detectedChrome_; |
245 | |
246 | |
247 | /** |
248 | * Whether the code is running on the Safari web browser. |
249 | * @type {boolean} |
250 | */ |
251 | goog.userAgent.product.SAFARI = goog.userAgent.product.PRODUCT_KNOWN_ ? |
252 | goog.userAgent.product.ASSUME_SAFARI : |
253 | goog.userAgent.product.detectedSafari_; |