|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
|
6 |
|
7 Cu.import("resource://gre/modules/Log.jsm"); |
|
8 Cu.import("resource://services-common/rest.js"); |
|
9 Cu.import("resource://services-sync/util.js"); |
|
10 Cu.import("resource://services-sync/constants.js"); |
|
11 |
|
12 this.EXPORTED_SYMBOLS = ["SyncStorageRequest"]; |
|
13 |
|
14 const STORAGE_REQUEST_TIMEOUT = 5 * 60; // 5 minutes |
|
15 |
|
16 /** |
|
17 * RESTRequest variant for use against a Sync storage server. |
|
18 */ |
|
19 this.SyncStorageRequest = function SyncStorageRequest(uri) { |
|
20 RESTRequest.call(this, uri); |
|
21 |
|
22 this.authenticator = null; |
|
23 } |
|
24 SyncStorageRequest.prototype = { |
|
25 |
|
26 __proto__: RESTRequest.prototype, |
|
27 |
|
28 _logName: "Sync.StorageRequest", |
|
29 |
|
30 /** |
|
31 * The string to use as the base User-Agent in Sync requests. |
|
32 * These strings will look something like |
|
33 * |
|
34 * Firefox/4.0 FxSync/1.8.0.20100101.mobile |
|
35 * |
|
36 * or |
|
37 * |
|
38 * Firefox Aurora/5.0a1 FxSync/1.9.0.20110409.desktop |
|
39 */ |
|
40 userAgent: |
|
41 Services.appinfo.name + "/" + Services.appinfo.version + // Product. |
|
42 " FxSync/" + WEAVE_VERSION + "." + // Sync. |
|
43 Services.appinfo.appBuildID + ".", // Build. |
|
44 |
|
45 /** |
|
46 * Wait 5 minutes before killing a request. |
|
47 */ |
|
48 timeout: STORAGE_REQUEST_TIMEOUT, |
|
49 |
|
50 dispatch: function dispatch(method, data, onComplete, onProgress) { |
|
51 // Compose a UA string fragment from the various available identifiers. |
|
52 if (Svc.Prefs.get("sendVersionInfo", true)) { |
|
53 let ua = this.userAgent + Svc.Prefs.get("client.type", "desktop"); |
|
54 this.setHeader("user-agent", ua); |
|
55 } |
|
56 |
|
57 if (this.authenticator) { |
|
58 this.authenticator(this); |
|
59 } else { |
|
60 this._log.debug("No authenticator found."); |
|
61 } |
|
62 |
|
63 return RESTRequest.prototype.dispatch.apply(this, arguments); |
|
64 }, |
|
65 |
|
66 onStartRequest: function onStartRequest(channel) { |
|
67 RESTRequest.prototype.onStartRequest.call(this, channel); |
|
68 if (this.status == this.ABORTED) { |
|
69 return; |
|
70 } |
|
71 |
|
72 let headers = this.response.headers; |
|
73 // Save the latest server timestamp when possible. |
|
74 if (headers["x-weave-timestamp"]) { |
|
75 SyncStorageRequest.serverTime = parseFloat(headers["x-weave-timestamp"]); |
|
76 } |
|
77 |
|
78 // This is a server-side safety valve to allow slowing down |
|
79 // clients without hurting performance. |
|
80 if (headers["x-weave-backoff"]) { |
|
81 Svc.Obs.notify("weave:service:backoff:interval", |
|
82 parseInt(headers["x-weave-backoff"], 10)); |
|
83 } |
|
84 |
|
85 if (this.response.success && headers["x-weave-quota-remaining"]) { |
|
86 Svc.Obs.notify("weave:service:quota:remaining", |
|
87 parseInt(headers["x-weave-quota-remaining"], 10)); |
|
88 } |
|
89 } |
|
90 }; |