load_image_request.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. import {assert} from 'chrome://resources/js/assert.m.js';
  5. import {ImageOrientation, ImageTransformParam} from './image_orientation.js';
  6. /**
  7. * Response status.
  8. *
  9. * @enum {string}
  10. */
  11. export const LoadImageResponseStatus = {
  12. SUCCESS: 'success',
  13. ERROR: 'error',
  14. };
  15. /**
  16. * Structure of the response object passed to the LoadImageRequest callback.
  17. * All methods must be static since this is passed between isolated contexts.
  18. *
  19. * @struct
  20. */
  21. export class LoadImageResponse {
  22. /**
  23. * @param {!LoadImageResponseStatus} status
  24. * @param {?number} taskId or null if fulfilled by the client-side cache.
  25. * @param {{width:number, height:number, ifd:?string, data:string}=}
  26. * opt_result
  27. */
  28. constructor(status, taskId, opt_result) {
  29. /** @type {!LoadImageResponseStatus} */
  30. this.status = status;
  31. /** @type {?number} */
  32. this.taskId = taskId;
  33. if (status === LoadImageResponseStatus.ERROR) {
  34. return;
  35. }
  36. // Response result defined only when status == SUCCESS.
  37. assert(opt_result);
  38. /** @type {number|undefined} */
  39. this.width = opt_result.width;
  40. /** @type {number|undefined} */
  41. this.height = opt_result.height;
  42. /** @type {?string} */
  43. this.ifd = opt_result.ifd;
  44. /**
  45. * The (compressed) image data as a data URL.
  46. * @type {string|undefined}
  47. */
  48. this.data = opt_result.data;
  49. }
  50. /**
  51. * Returns the cacheable result value for |response|, or null for an error.
  52. *
  53. * @param {!LoadImageResponse} response Response data from the ImageLoader.
  54. * @param {number|undefined} timestamp The request timestamp. If undefined,
  55. * then null is used. Currently this disables any caching in the
  56. * ImageLoader, but disables only *expiration* in the client unless a
  57. * timestamp is presented on a later request.
  58. * @return {?{
  59. * timestamp: ?number,
  60. * width: number,
  61. * height: number,
  62. * ifd: ?string,
  63. * data: string
  64. * }}
  65. */
  66. static cacheValue(response, timestamp) {
  67. if (response.status === LoadImageResponseStatus.ERROR) {
  68. return null;
  69. }
  70. // Response result defined only when status == SUCCESS.
  71. assert(response.width);
  72. assert(response.height);
  73. assert(response.data);
  74. return {
  75. timestamp: timestamp || null,
  76. width: response.width,
  77. height: response.height,
  78. ifd: response.ifd,
  79. data: response.data,
  80. };
  81. }
  82. }
  83. /**
  84. * Encapsulates a request to load an image.
  85. * All methods must be static since this is passed between isolated contexts.
  86. *
  87. * @struct
  88. */
  89. export class LoadImageRequest {
  90. constructor() {
  91. // Parts that uniquely identify the request.
  92. /**
  93. * Url of the requested image. Undefined only for cancellations.
  94. * @type {string|undefined}
  95. */
  96. this.url;
  97. /** @type{ImageOrientation|ImageTransformParam|undefined} */
  98. this.orientation;
  99. /** @type {number|undefined} */
  100. this.scale;
  101. /** @type {number|undefined} */
  102. this.width;
  103. /** @type {number|undefined} */
  104. this.height;
  105. /** @type {number|undefined} */
  106. this.maxWidth;
  107. /** @type {number|undefined} */
  108. this.maxHeight;
  109. // Parts that control the request flow.
  110. /** @type {number|undefined} */
  111. this.taskId;
  112. /** @type {boolean|undefined} */
  113. this.cancel;
  114. /** @type {boolean|undefined} */
  115. this.crop;
  116. /** @type {number|undefined} */
  117. this.timestamp;
  118. /** @type {boolean|undefined} */
  119. this.cache;
  120. /** @type {number|undefined} */
  121. this.priority;
  122. }
  123. /**
  124. * Creates a cache key.
  125. *
  126. * @return {?string} Cache key. It may be null if the cache does not support
  127. * the request. e.g. Data URI.
  128. */
  129. static cacheKey(request) {
  130. if (/^data:/i.test(request.url)) {
  131. return null;
  132. }
  133. return JSON.stringify({
  134. url: request.url,
  135. orientation: request.orientation,
  136. scale: request.scale,
  137. width: request.width,
  138. height: request.height,
  139. maxWidth: request.maxWidth,
  140. maxHeight: request.maxHeight,
  141. });
  142. }
  143. /**
  144. * Creates a cancel request.
  145. *
  146. * @param{number} taskId The task to cancel.
  147. * @return {!LoadImageRequest}
  148. */
  149. static createCancel(taskId) {
  150. return /** @type {!LoadImageRequest} */ ({taskId: taskId, cancel: true});
  151. }
  152. /**
  153. * Creates a load request from an option map.
  154. * Only the timestamp may be undefined.
  155. *
  156. * @param {{
  157. * url: !string,
  158. * maxWidth: number,
  159. * maxHeight: number,
  160. * cache: boolean,
  161. * priority: number,
  162. * timestamp: (number|undefined),
  163. * orientation: ?ImageTransformParam,
  164. * }} params Request parameters.
  165. * @return {!LoadImageRequest}
  166. */
  167. static createRequest(params) {
  168. return /** @type {!LoadImageRequest} */ (params);
  169. }
  170. /**
  171. * Creates a request to load a full-sized image.
  172. * Only the timestamp may be undefined.
  173. *
  174. * @param {{
  175. * url: !string,
  176. * cache: boolean,
  177. * priority: number,
  178. * timestamp: (?number|undefined),
  179. * }} params Request parameters.
  180. * @return {!LoadImageRequest}
  181. */
  182. static createFullImageRequest(params) {
  183. return /** @type {!LoadImageRequest} */ (params);
  184. }
  185. /**
  186. * Creates a load request from a url string. All options are undefined.
  187. *
  188. * @param {string} url
  189. * @return {!LoadImageRequest}
  190. */
  191. static createForUrl(url) {
  192. return /** @type {!LoadImageRequest} */ ({url: url});
  193. }
  194. }