aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/gears/urlcapture.js
blob: a646847ebd2192b11650e55f8281819afc1a8c13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Interface for capturing URLs to a ResourceStore on the
 * LocalServer.
 *
 */


goog.provide('goog.gears.UrlCapture');
goog.provide('goog.gears.UrlCapture.Event');
goog.provide('goog.gears.UrlCapture.EventType');

goog.require('goog.Uri');
goog.require('goog.debug.Logger');
goog.require('goog.events.Event');
goog.require('goog.events.EventTarget');
goog.require('goog.gears');



/**
 * Class capture URLs to a ResourceStore on the LocalServer.
 * @constructor
 * @extends {goog.events.EventTarget}
 * @param {string} name The name of the ResourceStore to capture the URLs to.
 * @param {?string} requiredCookie  A cookie that must be present for the
 *     managed store to be active. Should have the form "foo=bar".
 * @param {GearsResourceStore=} opt_localServer The LocalServer for gears.
 */
goog.gears.UrlCapture = function(name, requiredCookie, opt_localServer) {
  goog.events.EventTarget.call(this);

  /**
   * Name of resource store.
   * @type {string}
   * @private
   */
  this.storeName_ = goog.gears.makeSafeFileName(name);
  if (name != this.storeName_) {
    this.logger_.info(
        'local store name ' + name + '->' + this.storeName_);
  }

  /**
   * A cookie that must be present for the store to be active.
   * Should have the form "foo=bar". String cast is a safety measure since
   * Gears behaves very badly when it gets an unexpected data type.
   * @type {?string}
   * @private
   */
  this.requiredCookie_ = requiredCookie ? String(requiredCookie) : null;

  /**
   * The LocalServer for Gears.
   * @type {GearsLocalServer}
   * @private
   */
  this.localServer_ = opt_localServer ||
      goog.gears.getFactory().create('beta.localserver', '1.0');

  /**
   * Object mapping list of URIs to capture to capture id.
   * @type {Object}
   * @private
   */
  this.uris_ = {};

  /**
   * Object mapping list of URIs that had errors in the capture to capture id.
   * @type {Object}
   * @private
   */
  this.errorUris_ = {};

  /**
   * Object mapping number of URLs completed to capture id.
   * @type {Object}
   * @private
   */
  this.numCompleted_ = {};
};
goog.inherits(goog.gears.UrlCapture, goog.events.EventTarget);


/**
 * Logger.
 * @type {goog.debug.Logger}
 * @private
 */
goog.gears.UrlCapture.prototype.logger_ =
    goog.debug.Logger.getLogger('goog.gears.UrlCapture');


/**
 * The ResourceStore for gears, used to capture URLs.
 * @type {GearsResourceStore}
 * @private
 */
goog.gears.UrlCapture.prototype.resourceStore_ = null;


/**
 * Events fired during URL capture
 * @enum {string}
 */
goog.gears.UrlCapture.EventType = {
  URL_SUCCESS: 'url_success',
  URL_ERROR: 'url_error',
  COMPLETE: 'complete',
  ABORT: 'abort'
};


/**
 * Lazy initializer for resource store.
 * @return {GearsResourceStore} Gears resource store.
 * @private
 */
goog.gears.UrlCapture.prototype.getResourceStore_ = function() {
  if (!this.resourceStore_) {
    this.logger_.info('creating resource store: ' + this.storeName_);
    this.resourceStore_ = this.localServer_['createStore'](
        this.storeName_, this.requiredCookie_);
  }
  return this.resourceStore_;
};


/**
 * Determine if the UrlCapture has been created.
 * @return {boolean} True if it has been created.
 */
goog.gears.UrlCapture.prototype.exists = function() {
  if (!this.resourceStore_) {
    this.logger_.info('opening resource store: ' + this.storeName_);
    this.resourceStore_ = this.localServer_['openStore'](
        this.storeName_, this.requiredCookie_);
  }
  return !!this.resourceStore_;
};


/**
 * Remove this resource store.
 */
goog.gears.UrlCapture.prototype.removeStore = function() {
  this.logger_.info('removing resource store: ' + this.storeName_);
  this.localServer_['removeStore'](this.storeName_, this.requiredCookie_);
  this.resourceStore_ = null;
};


/**
 * Renames a Url that's been captured.
 * @param {string|goog.Uri} srcUri The source Uri.
 * @param {string|goog.Uri} dstUri The destination Uri.
 */
goog.gears.UrlCapture.prototype.rename = function(srcUri, dstUri) {
  this.getResourceStore_()['rename'](srcUri.toString(), dstUri.toString());
};


/**
 * Copies a Url that's been captured.
 * @param {string|goog.Uri} srcUri The source Uri.
 * @param {string|goog.Uri} dstUri The destination Uri.
 */
goog.gears.UrlCapture.prototype.copy = function(srcUri, dstUri) {
  this.getResourceStore_()['copy'](srcUri.toString(), dstUri.toString());
};


/**
 * Starts the capture of the given URLs. Returns immediately, and fires events
 * on success and error.
 * @param {Array.<string|goog.Uri>} uris URIs to capture.
 * @return {number} The id of the ResourceStore capture. Can be used to
 * abort, or identify events.
 */
goog.gears.UrlCapture.prototype.capture = function(uris) {
  var count = uris.length;
  this.logger_.fine('capture: count==' + count);
  if (!count) {
    throw Error('No URIs to capture');
  }

  // Convert goog.Uri objects to strings since Gears will throw an exception
  // for non-strings.
  var captureStrings = [];
  for (var i = 0; i < count; i++) {
    captureStrings.push(uris[i].toString());
  }

  var id = this.getResourceStore_()['capture'](
      captureStrings, goog.bind(this.captureCallback_, this));
  this.logger_.fine('capture started: ' + id);
  this.uris_[id] = uris;
  this.errorUris_[id] = [];
  this.numCompleted_[id] = 0;
  return id;
};


/**
 * Aborts the capture with the given id. Dispatches abort event.
 * @param {number} captureId The id of the capture to abort, from #capture.
 */
goog.gears.UrlCapture.prototype.abort = function(captureId) {
  this.logger_.fine('abort: ' + captureId);

  // TODO(user) Remove when Gears adds more rubust type handling.
  // Safety measure since Gears behaves very badly if it gets an unexpected
  // data type.
  if (typeof captureId != 'number') {
    throw Error('bad capture ID: ' + captureId);
  }

  // Only need to abort if the capture is still in progress.
  if (this.uris_[captureId] || this.numCompleted_[captureId]) {
    this.logger_.info('aborting capture: ' + captureId);
    this.getResourceStore_()['abortCapture'](captureId);
    this.cleanupCapture_(captureId);
    this.dispatchEvent(new goog.gears.UrlCapture.Event(
        goog.gears.UrlCapture.EventType.ABORT, captureId));
  }
};


/**
 * Checks if a URL is captured.
 * @param {string|goog.Uri} uri The URL to check.
 * @return {boolean} true if captured, false otherwise.
 */
goog.gears.UrlCapture.prototype.isCaptured = function(uri) {
  this.logger_.fine('isCaptured: ' + uri);
  return this.getResourceStore_()['isCaptured'](uri.toString());
};


/**
 * Removes the given URI from the store.
 * @param {string|goog.Uri} uri The URI to remove from the store.
 */
goog.gears.UrlCapture.prototype.remove = function(uri) {
  this.logger_.fine('remove: ' + uri);
  this.getResourceStore_()['remove'](uri.toString());
};


/**
 * This is the callback passed into ResourceStore.capture. It gets called
 * each time a URL is captured.
 * @param {string} url The url from gears, always a string.
 * @param {boolean} success True if capture succeeded, false otherwise.
 * @param {number} captureId The id of the capture.
 * @private
 */
goog.gears.UrlCapture.prototype.captureCallback_ = function(
    url, success, captureId) {
  this.logger_.fine('captureCallback_: ' + captureId);

  if (!this.uris_[captureId] && !this.numCompleted_[captureId]) {
    // This probably means we were aborted and then a capture event came in.
    this.cleanupCapture_(captureId);
    return;
  }

  // Dispatch success/error event for the URL
  var eventUri = this.usesGoogUri_(captureId) ? new goog.Uri(url) : url;
  var eventType = null;
  if (success) {
    eventType = goog.gears.UrlCapture.EventType.URL_SUCCESS;
  } else {
    eventType = goog.gears.UrlCapture.EventType.URL_ERROR;
    this.errorUris_[captureId].push(eventUri);
  }
  this.dispatchEvent(new goog.gears.UrlCapture.Event(
      eventType, captureId, eventUri));

  // Dispatch complete event for the entire capture, if necessary
  this.numCompleted_[captureId]++;
  if (this.numCompleted_[captureId] == this.uris_[captureId].length) {
    this.dispatchEvent(new goog.gears.UrlCapture.Event(
        goog.gears.UrlCapture.EventType.COMPLETE, captureId, null,
        this.errorUris_[captureId]));
    this.cleanupCapture_(captureId);
  }
};


/**
 * Helper function to cleanup after a capture completes or is aborted.
 * @private
 * @param {number} captureId The id of the capture to clean up.
 */
goog.gears.UrlCapture.prototype.cleanupCapture_ = function(captureId) {
  this.logger_.fine('cleanupCapture_: ' + captureId);
  delete this.uris_[captureId];
  delete this.numCompleted_[captureId];
  delete this.errorUris_[captureId];
};


/**
 * Helper function to check whether a certain capture is using URIs of type
 * String or type goog.Uri
 * @private
 * @param {number} captureId The id of the capture to check.
 * @return {boolean} True if the capture uses goog.Uri, false if it uses string
 * or there are no URIs associated with the capture.
 */
goog.gears.UrlCapture.prototype.usesGoogUri_ = function(captureId) {
  if (this.uris_[captureId] &&
      this.uris_[captureId].length > 0 &&
      this.uris_[captureId][0] instanceof goog.Uri) {
    return true;
  }
  return false;
};



/**
 * An event dispatched by UrlCapture
 * @constructor
 * @extends {goog.events.Event}
 * @param {goog.gears.UrlCapture.EventType} type Type of event to dispatch.
 * @param {number} captureId The id of the capture that fired this event.
 * @param {string|goog.Uri=} opt_uri The URI for the event.
 * @param {Array.<string|goog.Uri>=} opt_errorUris The URIs that failed to load
 * correctly.
 */
goog.gears.UrlCapture.Event = function(type, captureId, opt_uri,
    opt_errorUris) {
  goog.events.Event.call(this, type);

  /**
   * The id of the capture to dispatch the event for. This id is returned from
   * goog.gears.UrlCapture#capture
   * @type {number}
   */
  this.captureId = captureId;

  /**
   * The URI the event concerns. Valid for URL_SUCCESS and URL_ERROR events.
   * @type {string|goog.Uri|null}
   */
  this.uri = opt_uri || null;

  /**
   * A list of all the URIs that failed to load correctly. Valid for
   * COMPLETE event.
   * @type {Array.<string|goog.Uri>}
   */
  this.errorUris = opt_errorUris || [];
};
goog.inherits(goog.gears.UrlCapture.Event, goog.events.Event);