Path: blob/trunk/javascript/atoms/html5/html5_browser.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617/**18* @fileoverview Helper function to determine which HTML5 features are19* supported by browsers..20*/2122goog.provide('bot.html5');2324goog.require('bot');25goog.require('bot.Error');26goog.require('bot.ErrorCode');27goog.require('bot.userAgent');28goog.require('goog.userAgent');29goog.require('goog.userAgent.product');303132/**33* Identifier for supported HTML5 API in Webdriver.34*35* @enum {string}36*/37bot.html5.API = {38APPCACHE: 'appcache',39BROWSER_CONNECTION: 'browser_connection',40DATABASE: 'database',41GEOLOCATION: 'location',42LOCAL_STORAGE: 'local_storage',43SESSION_STORAGE: 'session_storage',44VIDEO: 'video',45AUDIO: 'audio',46CANVAS: 'canvas'47};484950/**51* True if the current browser is IE version 8 or earlier.52* @private {boolean}53* @const54*/55bot.html5.IS_IE8_OR_EARLIER_ = goog.userAgent.IE &&56!bot.userAgent.isEngineVersion(9);575859/**60* True if the current browser is Safari version 4 or earlier.61* @private {boolean}62* @const63*/64bot.html5.IS_SAFARI4_OR_EARLIER_ = goog.userAgent.product.SAFARI &&65!bot.userAgent.isProductVersion(5);666768/**69* True if the browser is Android version 2.2 (Froyo) or earlier.70* @private {boolean}71* @const72*/73bot.html5.IS_ANDROID_FROYO_OR_EARLIER_ = goog.userAgent.product.ANDROID &&74!bot.userAgent.isProductVersion(2.3);757677/**78* True if the current browser is Safari 5 on Windows.79* @private {boolean}80* @const81*/82bot.html5.IS_SAFARI_WINDOWS_ = goog.userAgent.WINDOWS &&83goog.userAgent.product.SAFARI &&84(bot.userAgent.isProductVersion(4)) &&85!bot.userAgent.isProductVersion(6);868788/**89* Checks if the browser supports an HTML5 feature.90*91* @param {bot.html5.API} api HTML5 API identifier.92* @param {!Window=} opt_window The window to be accessed;93* defaults to the main window.94* @return {boolean} Whether the browser supports the feature.95*/96bot.html5.isSupported = function(api, opt_window) {97var win = opt_window || bot.getWindow();9899switch (api) {100case bot.html5.API.APPCACHE:101// IE8 does not support application cache, though the APIs exist.102if (bot.html5.IS_IE8_OR_EARLIER_) {103return false;104}105return goog.isDefAndNotNull(win.applicationCache);106107case bot.html5.API.BROWSER_CONNECTION:108return goog.isDefAndNotNull(win.navigator) &&109goog.isDefAndNotNull(win.navigator.onLine);110111case bot.html5.API.DATABASE:112// Safari4 database API does not allow writes.113if (bot.html5.IS_SAFARI4_OR_EARLIER_) {114return false;115}116// Android Froyo does not support database, though the APIs exist.117if (bot.html5.IS_ANDROID_FROYO_OR_EARLIER_) {118return false;119}120return goog.isDefAndNotNull(win.openDatabase);121122case bot.html5.API.GEOLOCATION:123// Safari 4,5 on Windows do not support geolocation, see:124// https://discussions.apple.com/thread/3547900125if (bot.html5.IS_SAFARI_WINDOWS_) {126return false;127}128return goog.isDefAndNotNull(win.navigator) &&129goog.isDefAndNotNull(win.navigator.geolocation);130131case bot.html5.API.LOCAL_STORAGE:132// IE8 does not support local storage, though the APIs exist.133if (bot.html5.IS_IE8_OR_EARLIER_) {134return false;135}136return goog.isDefAndNotNull(win.localStorage);137138case bot.html5.API.SESSION_STORAGE:139// IE8 does not support session storage, though the APIs exist.140if (bot.html5.IS_IE8_OR_EARLIER_) {141return false;142}143return goog.isDefAndNotNull(win.sessionStorage) &&144// To avoid browsers that only support this API partially145// like some versions of FF.146goog.isDefAndNotNull(win.sessionStorage.clear);147148default:149throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR,150'Unsupported API identifier provided as parameter');151}152};153154155