Path: blob/trunk/third_party/closure/goog/fs/url.js
2868 views
// Copyright 2015 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Wrapper for URL and its createObjectUrl and revokeObjectUrl16* methods that are part of the HTML5 File API.17*/1819goog.provide('goog.fs.url');202122/**23* Creates a blob URL for a blob object.24* Throws an error if the browser does not support Object Urls.25*26* @param {!Blob} blob The object for which to create the URL.27* @return {string} The URL for the object.28*/29goog.fs.url.createObjectUrl = function(blob) {30return goog.fs.url.getUrlObject_().createObjectURL(blob);31};323334/**35* Revokes a URL created by {@link goog.fs.url.createObjectUrl}.36* Throws an error if the browser does not support Object Urls.37*38* @param {string} url The URL to revoke.39*/40goog.fs.url.revokeObjectUrl = function(url) {41goog.fs.url.getUrlObject_().revokeObjectURL(url);42};434445/**46* @typedef {{createObjectURL: (function(!Blob): string),47* revokeObjectURL: function(string): void}}48*/49goog.fs.url.UrlObject_;505152/**53* Get the object that has the createObjectURL and revokeObjectURL functions for54* this browser.55*56* @return {goog.fs.url.UrlObject_} The object for this browser.57* @private58*/59goog.fs.url.getUrlObject_ = function() {60var urlObject = goog.fs.url.findUrlObject_();61if (urlObject != null) {62return urlObject;63} else {64throw Error('This browser doesn\'t seem to support blob URLs');65}66};676869/**70* Finds the object that has the createObjectURL and revokeObjectURL functions71* for this browser.72*73* @return {?goog.fs.url.UrlObject_} The object for this browser or null if the74* browser does not support Object Urls.75* @private76*/77goog.fs.url.findUrlObject_ = function() {78// This is what the spec says to do79// http://dev.w3.org/2006/webapi/FileAPI/#dfn-createObjectURL80if (goog.isDef(goog.global.URL) &&81goog.isDef(goog.global.URL.createObjectURL)) {82return /** @type {goog.fs.url.UrlObject_} */ (goog.global.URL);83// This is what Chrome does (as of 10.0.648.6 dev)84} else if (85goog.isDef(goog.global.webkitURL) &&86goog.isDef(goog.global.webkitURL.createObjectURL)) {87return /** @type {goog.fs.url.UrlObject_} */ (goog.global.webkitURL);88// This is what the spec used to say to do89} else if (goog.isDef(goog.global.createObjectURL)) {90return /** @type {goog.fs.url.UrlObject_} */ (goog.global);91} else {92return null;93}94};959697/**98* Checks whether this browser supports Object Urls. If not, calls to99* createObjectUrl and revokeObjectUrl will result in an error.100*101* @return {boolean} True if this browser supports Object Urls.102*/103goog.fs.url.browserSupportsObjectUrls = function() {104return goog.fs.url.findUrlObject_() != null;105};106107108