summaryrefslogtreecommitdiffstats
path: root/webbrowser/components/sync/progress.js
blob: 101160fa87162eac28a291fd73df637db7f0c5cd (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://services-sync/main.js");

var gProgressBar;
var gCounter = 0;

function onLoad(event) {
  Services.obs.addObserver(onEngineSync, "weave:engine:sync:finish", false);
  Services.obs.addObserver(onEngineSync, "weave:engine:sync:error", false);
  Services.obs.addObserver(onServiceSync, "weave:service:sync:finish", false);
  Services.obs.addObserver(onServiceSync, "weave:service:sync:error", false);

  gProgressBar = document.getElementById('uploadProgressBar');

  if (Services.prefs.getPrefType("services.sync.firstSync") != Ci.nsIPrefBranch.PREF_INVALID) {
    gProgressBar.hidden = false;
  }
  else {
    gProgressBar.hidden = true;
  }
}

function onUnload(event) {
  cleanUpObservers();
}

function cleanUpObservers() {
  try {
    Services.obs.removeObserver(onEngineSync, "weave:engine:sync:finish");
    Services.obs.removeObserver(onEngineSync, "weave:engine:sync:error");
    Services.obs.removeObserver(onServiceSync, "weave:service:sync:finish");
    Services.obs.removeObserver(onServiceSync, "weave:service:sync:error");
  }
  catch (e) {
    // may be double called by unload & exit. Ignore.
  }
}

function onEngineSync(subject, topic, data) {
  // The Clients engine syncs first. At this point we don't necessarily know
  // yet how many engines will be enabled, so we'll ignore the Clients engine
  // and evaluate how many engines are enabled when the first "real" engine
  // syncs.
  if (data == "clients") {
    return;
  }

  if (!gCounter &&
      Services.prefs.getPrefType("services.sync.firstSync") != Ci.nsIPrefBranch.PREF_INVALID) {
    gProgressBar.max = Weave.Service.engineManager.getEnabled().length;
  }

  gCounter += 1;
  gProgressBar.setAttribute("value", gCounter);
}

function onServiceSync(subject, topic, data) {
  // To address the case where 0 engines are synced, we will fill the
  // progress bar so the user knows that the sync has finished.
  gProgressBar.setAttribute("value", gProgressBar.max);
  cleanUpObservers();
}

function closeTab() {
  window.close();
}