You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WebBrowser.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server;
  17. import java.io.Serializable;
  18. import java.util.Date;
  19. import java.util.Locale;
  20. import java.util.TimeZone;
  21. import com.vaadin.shared.VBrowserDetails;
  22. /**
  23. * Class that provides information about the web browser the user is using.
  24. * Provides information such as browser name and version, screen resolution and
  25. * IP address.
  26. *
  27. * @author Vaadin Ltd.
  28. */
  29. public class WebBrowser implements Serializable {
  30. private int screenHeight = -1;
  31. private int screenWidth = -1;
  32. private String browserApplication = null;
  33. private Locale locale;
  34. private String address;
  35. private boolean secureConnection;
  36. private int timezoneOffset = 0;
  37. private int rawTimezoneOffset = 0;
  38. private int dstSavings;
  39. private boolean dstInEffect;
  40. private String timeZoneId;
  41. private boolean touchDevice;
  42. private VBrowserDetails browserDetails;
  43. private long clientServerTimeDelta;
  44. /**
  45. * Gets the height of the screen in pixels. This is the full screen
  46. * resolution and not the height available for the application.
  47. *
  48. * @return the height of the screen in pixels.
  49. */
  50. public int getScreenHeight() {
  51. return screenHeight;
  52. }
  53. /**
  54. * Gets the width of the screen in pixels. This is the full screen
  55. * resolution and not the width available for the application.
  56. *
  57. * @return the width of the screen in pixels.
  58. */
  59. public int getScreenWidth() {
  60. return screenWidth;
  61. }
  62. /**
  63. * Get the browser user-agent string.
  64. *
  65. * @return The raw browser userAgent string
  66. */
  67. public String getBrowserApplication() {
  68. return browserApplication;
  69. }
  70. /**
  71. * Gets the IP-address of the web browser. If the application is running
  72. * inside a portlet, this method will return null.
  73. *
  74. * @return IP-address in 1.12.123.123 -format
  75. */
  76. public String getAddress() {
  77. return address;
  78. }
  79. /** Get the default locate of the browser. */
  80. public Locale getLocale() {
  81. return locale;
  82. }
  83. /** Is the connection made using HTTPS? */
  84. public boolean isSecureConnection() {
  85. return secureConnection;
  86. }
  87. /**
  88. * Tests whether the user is using Firefox.
  89. *
  90. * @return true if the user is using Firefox, false if the user is not using
  91. * Firefox or if no information on the browser is present
  92. */
  93. public boolean isFirefox() {
  94. if (browserDetails == null) {
  95. return false;
  96. }
  97. return browserDetails.isFirefox();
  98. }
  99. /**
  100. * Tests whether the user is using Internet Explorer.
  101. *
  102. * @return true if the user is using Internet Explorer, false if the user is
  103. * not using Internet Explorer or if no information on the browser
  104. * is present
  105. */
  106. public boolean isIE() {
  107. if (browserDetails == null) {
  108. return false;
  109. }
  110. return browserDetails.isIE();
  111. }
  112. /**
  113. * Tests whether the user is using Edge.
  114. *
  115. * @since 7.5.3
  116. * @return true if the user is using Edge, false if the user is not using
  117. * Edge or if no information on the browser is present
  118. */
  119. public boolean isEdge() {
  120. if (browserDetails == null) {
  121. return false;
  122. }
  123. return browserDetails.isEdge();
  124. }
  125. /**
  126. * Tests whether the user is using Safari. Note that Chrome on iOS is not
  127. * detected as Safari but as Chrome although the underlying browser engine
  128. * is the same.
  129. *
  130. * @return true if the user is using Safari, false if the user is not using
  131. * Safari or if no information on the browser is present
  132. */
  133. public boolean isSafari() {
  134. if (browserDetails == null) {
  135. return false;
  136. }
  137. return browserDetails.isSafari();
  138. }
  139. /**
  140. * Tests whether the user is using Opera.
  141. *
  142. * @return true if the user is using Opera, false if the user is not using
  143. * Opera or if no information on the browser is present
  144. */
  145. public boolean isOpera() {
  146. if (browserDetails == null) {
  147. return false;
  148. }
  149. return browserDetails.isOpera();
  150. }
  151. /**
  152. * Tests whether the user is using Chrome.
  153. *
  154. * @return true if the user is using Chrome, false if the user is not using
  155. * Chrome or if no information on the browser is present
  156. */
  157. public boolean isChrome() {
  158. if (browserDetails == null) {
  159. return false;
  160. }
  161. return browserDetails.isChrome();
  162. }
  163. /**
  164. * Tests whether the user is using Chrome Frame.
  165. *
  166. * @return true if the user is using Chrome Frame, false if the user is not
  167. * using Chrome or if no information on the browser is present
  168. */
  169. public boolean isChromeFrame() {
  170. if (browserDetails == null) {
  171. return false;
  172. }
  173. return browserDetails.isChromeFrame();
  174. }
  175. /**
  176. * Tests whether the user's browser is Chrome Frame capable.
  177. *
  178. * @return true if the user can use Chrome Frame, false if the user can not
  179. * or if no information on the browser is present
  180. */
  181. public boolean isChromeFrameCapable() {
  182. if (browserDetails == null) {
  183. return false;
  184. }
  185. return browserDetails.isChromeFrameCapable();
  186. }
  187. /**
  188. * Gets the major version of the browser the user is using.
  189. *
  190. * <p>
  191. * Note that Internet Explorer in IE7 compatibility mode might return 8 in
  192. * some cases even though it should return 7.
  193. * </p>
  194. *
  195. * @return The major version of the browser or -1 if not known.
  196. */
  197. public int getBrowserMajorVersion() {
  198. if (browserDetails == null) {
  199. return -1;
  200. }
  201. return browserDetails.getBrowserMajorVersion();
  202. }
  203. /**
  204. * Gets the minor version of the browser the user is using.
  205. *
  206. * @see #getBrowserMajorVersion()
  207. *
  208. * @return The minor version of the browser or -1 if not known.
  209. */
  210. public int getBrowserMinorVersion() {
  211. if (browserDetails == null) {
  212. return -1;
  213. }
  214. return browserDetails.getBrowserMinorVersion();
  215. }
  216. /**
  217. * Tests whether the user is using Linux.
  218. *
  219. * @return true if the user is using Linux, false if the user is not using
  220. * Linux or if no information on the browser is present
  221. */
  222. public boolean isLinux() {
  223. return browserDetails.isLinux();
  224. }
  225. /**
  226. * Tests whether the user is using Mac OS X.
  227. *
  228. * @return true if the user is using Mac OS X, false if the user is not
  229. * using Mac OS X or if no information on the browser is present
  230. */
  231. public boolean isMacOSX() {
  232. return browserDetails.isMacOSX();
  233. }
  234. /**
  235. * Tests whether the user is using Windows.
  236. *
  237. * @return true if the user is using Windows, false if the user is not using
  238. * Windows or if no information on the browser is present
  239. */
  240. public boolean isWindows() {
  241. return browserDetails.isWindows();
  242. }
  243. /**
  244. * Tests whether the user is using Windows Phone.
  245. *
  246. * @return true if the user is using Windows Phone, false if the user is not
  247. * using Windows Phone or if no information on the browser is
  248. * present
  249. * @since 7.3.2
  250. */
  251. public boolean isWindowsPhone() {
  252. return browserDetails.isWindowsPhone();
  253. }
  254. /**
  255. * Tests if the browser is run on Android.
  256. *
  257. * @return true if run on Android false if the user is not using Android or
  258. * if no information on the browser is present
  259. */
  260. public boolean isAndroid() {
  261. return browserDetails.isAndroid();
  262. }
  263. /**
  264. * Tests if the browser is run in iOS.
  265. *
  266. * @return true if run in iOS false if the user is not using iOS or if no
  267. * information on the browser is present
  268. */
  269. public boolean isIOS() {
  270. return browserDetails.isIOS();
  271. }
  272. /**
  273. * Tests if the browser is run on IPhone.
  274. *
  275. * @return true if run on IPhone false if the user is not using IPhone or if
  276. * no information on the browser is present
  277. * @since 7.3.3
  278. */
  279. public boolean isIPhone() {
  280. return browserDetails.isIPhone();
  281. }
  282. /**
  283. * Tests if the browser is run on IPad.
  284. *
  285. * @return true if run on IPad false if the user is not using IPad or if no
  286. * information on the browser is present
  287. * @since 7.3.3
  288. */
  289. public boolean isIPad() {
  290. return browserDetails.isIPad();
  291. }
  292. /**
  293. * Tests if the browser is run on ChromeOS (e.g. a Chromebook).
  294. *
  295. * @return true if run on ChromeOS false if the user is not using ChromeOS
  296. * or if no information on the browser is present
  297. * @since 8.1.1
  298. */
  299. public boolean isChromeOS() {
  300. return browserDetails.isChromeOS();
  301. }
  302. /**
  303. * Returns the browser-reported TimeZone offset in milliseconds from GMT.
  304. * This includes possible daylight saving adjustments, to figure out which
  305. * TimeZone the user actually might be in, see
  306. * {@link #getRawTimezoneOffset()}.
  307. *
  308. * @see WebBrowser#getRawTimezoneOffset()
  309. * @return timezone offset in milliseconds, 0 if not available
  310. */
  311. public int getTimezoneOffset() {
  312. return timezoneOffset;
  313. }
  314. /**
  315. * Returns the TimeZone Id (like "Europe/Helsinki") provided by the browser
  316. * (if the browser supports this feature).
  317. *
  318. * @return the TimeZone Id if provided by the browser, null otherwise.
  319. * @see <a href=
  320. * "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/resolvedOptions">Intl.DateTimeFormat.prototype.resolvedOptions()</a>
  321. * @since 8.2
  322. */
  323. public String getTimeZoneId() {
  324. return timeZoneId;
  325. }
  326. /**
  327. * Returns the browser-reported TimeZone offset in milliseconds from GMT
  328. * ignoring possible daylight saving adjustments that may be in effect in
  329. * the browser.
  330. * <p>
  331. * You can use this to figure out which TimeZones the user could actually be
  332. * in by calling {@link TimeZone#getAvailableIDs(int)}.
  333. * </p>
  334. * <p>
  335. * If {@link #getRawTimezoneOffset()} and {@link #getTimezoneOffset()}
  336. * returns the same value, the browser is either in a zone that does not
  337. * currently have daylight saving time, or in a zone that never has daylight
  338. * saving time.
  339. * </p>
  340. *
  341. * @return timezone offset in milliseconds excluding DST, 0 if not available
  342. */
  343. public int getRawTimezoneOffset() {
  344. return rawTimezoneOffset;
  345. }
  346. /**
  347. * Returns the offset in milliseconds between the browser's GMT TimeZone and
  348. * DST.
  349. *
  350. * @return the number of milliseconds that the TimeZone shifts when DST is
  351. * in effect
  352. */
  353. public int getDSTSavings() {
  354. return dstSavings;
  355. }
  356. /**
  357. * Returns whether daylight saving time (DST) is currently in effect in the
  358. * region of the browser or not.
  359. *
  360. * @return true if the browser resides at a location that currently is in
  361. * DST
  362. */
  363. public boolean isDSTInEffect() {
  364. return dstInEffect;
  365. }
  366. /**
  367. * Returns the current date and time of the browser. This will not be
  368. * entirely accurate due to varying network latencies, but should provide a
  369. * close-enough value for most cases. Also note that the returned Date
  370. * object uses servers default time zone, not the clients.
  371. * <p>
  372. * To get the actual date and time shown in the end users computer, you can
  373. * do something like:
  374. *
  375. * <pre>
  376. * WebBrowser browser = ...;
  377. * SimpleTimeZone timeZone = new SimpleTimeZone(browser.getTimezoneOffset(), "Fake client time zone");
  378. * DateFormat format = DateFormat.getDateTimeInstance();
  379. * format.setTimeZone(timeZone);
  380. * myLabel.setValue(format.format(browser.getCurrentDate()));
  381. * </pre>
  382. *
  383. * @return the current date and time of the browser.
  384. * @see #isDSTInEffect()
  385. * @see #getDSTSavings()
  386. * @see #getTimezoneOffset()
  387. */
  388. public Date getCurrentDate() {
  389. return new Date(new Date().getTime() + clientServerTimeDelta);
  390. }
  391. /**
  392. * @return true if the browser is detected to support touch events
  393. */
  394. public boolean isTouchDevice() {
  395. return touchDevice;
  396. }
  397. /**
  398. * For internal use by VaadinServlet/VaadinPortlet only. Updates all
  399. * properties in the class according to the given information.
  400. *
  401. * @param sw
  402. * Screen width
  403. * @param sh
  404. * Screen height
  405. * @param tzo
  406. * TimeZone offset in minutes from GMT
  407. * @param rtzo
  408. * raw TimeZone offset in minutes from GMT (w/o DST adjustment)
  409. * @param dstSavings
  410. * the difference between the raw TimeZone and DST in minutes
  411. * @param dstInEffect
  412. * is DST currently active in the region or not?
  413. * @param curDate
  414. * the current date in milliseconds since the epoch
  415. * @param touchDevice
  416. */
  417. void updateClientSideDetails(String sw, String sh, String tzo, String rtzo,
  418. String dstSavings, String dstInEffect, String tzId, String curDate,
  419. boolean touchDevice) {
  420. if (sw != null) {
  421. try {
  422. screenHeight = Integer.parseInt(sh);
  423. screenWidth = Integer.parseInt(sw);
  424. } catch (final NumberFormatException e) {
  425. screenHeight = screenWidth = -1;
  426. }
  427. }
  428. if (tzo != null) {
  429. try {
  430. // browser->java conversion: min->ms, reverse sign
  431. timezoneOffset = -Integer.parseInt(tzo) * 60 * 1000;
  432. } catch (final NumberFormatException e) {
  433. timezoneOffset = 0; // default gmt+0
  434. }
  435. }
  436. if (rtzo != null) {
  437. try {
  438. // browser->java conversion: min->ms, reverse sign
  439. rawTimezoneOffset = -Integer.parseInt(rtzo) * 60 * 1000;
  440. } catch (final NumberFormatException e) {
  441. rawTimezoneOffset = 0; // default gmt+0
  442. }
  443. }
  444. if (dstSavings != null) {
  445. try {
  446. // browser->java conversion: min->ms
  447. this.dstSavings = Integer.parseInt(dstSavings) * 60 * 1000;
  448. } catch (final NumberFormatException e) {
  449. this.dstSavings = 0; // default no savings
  450. }
  451. }
  452. if (dstInEffect != null) {
  453. this.dstInEffect = Boolean.parseBoolean(dstInEffect);
  454. }
  455. if (tzId == null || "undefined".equals(tzId)) {
  456. timeZoneId = null;
  457. } else {
  458. timeZoneId = tzId;
  459. }
  460. if (curDate != null) {
  461. try {
  462. long curTime = Long.parseLong(curDate);
  463. clientServerTimeDelta = curTime - new Date().getTime();
  464. } catch (final NumberFormatException e) {
  465. clientServerTimeDelta = 0;
  466. }
  467. }
  468. this.touchDevice = touchDevice;
  469. }
  470. /**
  471. * For internal use by VaadinServlet/VaadinPortlet only. Updates all
  472. * properties in the class according to the given information.
  473. *
  474. * @param request
  475. * the Vaadin request to read the information from
  476. */
  477. public void updateRequestDetails(VaadinRequest request) {
  478. locale = request.getLocale();
  479. address = request.getRemoteAddr();
  480. secureConnection = request.isSecure();
  481. // Headers are case insensitive according to the specification but are
  482. // case sensitive in Weblogic portal...
  483. String agent = request.getHeader("User-Agent");
  484. if (agent != null) {
  485. browserApplication = agent;
  486. browserDetails = new VBrowserDetails(agent);
  487. }
  488. if (request.getParameter("v-sw") != null) {
  489. updateClientSideDetails(request.getParameter("v-sw"),
  490. request.getParameter("v-sh"), request.getParameter("v-tzo"),
  491. request.getParameter("v-rtzo"),
  492. request.getParameter("v-dstd"),
  493. request.getParameter("v-dston"),
  494. request.getParameter("v-tzid"),
  495. request.getParameter("v-curdate"),
  496. request.getParameter("v-td") != null);
  497. }
  498. }
  499. /**
  500. * Checks if the browser is so old that it simply won't work with a Vaadin
  501. * application. Can be used to redirect to an alternative page, show
  502. * alternative content or similar.
  503. *
  504. * When this method returns true chances are very high that the browser
  505. * won't work and it does not make sense to direct the user to the Vaadin
  506. * application.
  507. *
  508. * @return true if the browser won't work, false if not the browser is
  509. * supported or might work
  510. */
  511. public boolean isTooOldToFunctionProperly() {
  512. if (browserDetails == null) {
  513. // Don't know, so assume it will work
  514. return false;
  515. }
  516. return browserDetails.isTooOldToFunctionProperly();
  517. }
  518. /**
  519. * Checks if the browser supports ECMAScript 6, based on the user agent.
  520. *
  521. * @return <code>true</code> if the browser supports ES6, <code>false</code>
  522. * otherwise.
  523. * @since 8.1
  524. */
  525. public boolean isEs6Supported() {
  526. if (browserDetails == null) {
  527. // Don't know, so assume no
  528. return false;
  529. }
  530. return browserDetails.isEs6Supported();
  531. }
  532. }