url.js 3.2 KB

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