| 1 |
// Copyright 2015 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 Wrapper for URL and its createObjectUrl and revokeObjectUrl |
| 17 |
* methods that are part of the HTML5 File API. |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.fs.url'); |
| 21 |
|
| 22 |
|
| 23 |
/** |
| 24 |
* Creates a blob URL for a blob object. |
| 25 |
* Throws an error if the browser does not support Object Urls. |
| 26 |
* |
| 27 |
* @param {!Blob} blob The object for which to create the URL. |
| 28 |
* @return {string} The URL for the object. |
| 29 |
*/ |
| 30 |
goog.fs.url.createObjectUrl = function(blob) { |
| 31 |
return goog.fs.url.getUrlObject_().createObjectURL(blob); |
| 32 |
}; |
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* Revokes a URL created by {@link goog.fs.url.createObjectUrl}. |
| 37 |
* Throws an error if the browser does not support Object Urls. |
| 38 |
* |
| 39 |
* @param {string} url The URL to revoke. |
| 40 |
*/ |
| 41 |
goog.fs.url.revokeObjectUrl = function(url) { |
| 42 |
goog.fs.url.getUrlObject_().revokeObjectURL(url); |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* @typedef {{createObjectURL: (function(!Blob): string), |
| 48 |
* revokeObjectURL: function(string): void}} |
| 49 |
*/ |
| 50 |
goog.fs.url.UrlObject_; |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* Get the object that has the createObjectURL and revokeObjectURL functions for |
| 55 |
* this browser. |
| 56 |
* |
| 57 |
* @return {goog.fs.url.UrlObject_} The object for this browser. |
| 58 |
* @private |
| 59 |
*/ |
| 60 |
goog.fs.url.getUrlObject_ = function() { |
| 61 |
var urlObject = goog.fs.url.findUrlObject_(); |
| 62 |
if (urlObject != null) { |
| 63 |
return urlObject; |
| 64 |
} else { |
| 65 |
throw new Error('This browser doesn\'t seem to support blob URLs'); |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* Finds the object that has the createObjectURL and revokeObjectURL functions |
| 72 |
* for this browser. |
| 73 |
* |
| 74 |
* @return {?goog.fs.url.UrlObject_} The object for this browser or null if the |
| 75 |
* browser does not support Object Urls. |
| 76 |
* @private |
| 77 |
*/ |
| 78 |
goog.fs.url.findUrlObject_ = function() { |
| 79 |
// This is what the spec says to do |
| 80 |
// http://dev.w3.org/2006/webapi/FileAPI/#dfn-createObjectURL |
| 81 |
if (goog.isDef(goog.global.URL) && |
| 82 |
goog.isDef(goog.global.URL.createObjectURL)) { |
| 83 |
return /** @type {goog.fs.url.UrlObject_} */ (goog.global.URL); |
| 84 |
// This is what Chrome does (as of 10.0.648.6 dev) |
| 85 |
} else if ( |
| 86 |
goog.isDef(goog.global.webkitURL) && |
| 87 |
goog.isDef(goog.global.webkitURL.createObjectURL)) { |
| 88 |
return /** @type {goog.fs.url.UrlObject_} */ (goog.global.webkitURL); |
| 89 |
// This is what the spec used to say to do |
| 90 |
} else if (goog.isDef(goog.global.createObjectURL)) { |
| 91 |
return /** @type {goog.fs.url.UrlObject_} */ (goog.global); |
| 92 |
} else { |
| 93 |
return null; |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
/** |
| 99 |
* Checks whether this browser supports Object Urls. If not, calls to |
| 100 |
* createObjectUrl and revokeObjectUrl will result in an error. |
| 101 |
* |
| 102 |
* @return {boolean} True if this browser supports Object Urls. |
| 103 |
*/ |
| 104 |
goog.fs.url.browserSupportsObjectUrls = function() { |
| 105 |
return goog.fs.url.findUrlObject_() != null; |
| 106 |
}; |