summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/chat_manager/js/centralroster.js
blob: 449a3dad590e06917c88e0d2c1e0f38f04b8cbfa (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
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var MIN_RETRY_MILLISECONDS = 1 * 1000;
var MAX_RETRY_MILLISECONDS = 4 * 60 * 1000;
var retryTime;
var retryTimer;
var chatClient = null;

document.title = chrome.i18n.getMessage('CHAT_MANAGER_NAME');
var args = {
  'protocol': 'https',
  'host': 'talkgadget.google.com',
  'jsmode': 'pre',
  'hl': chrome.i18n.getMessage('@@ui_locale')
};

// Read args.
var urlParts = window.location.href.split(/[?&#]/);
for (var i = 1; i < urlParts.length; i++) {
  var argParts = urlParts[i].split('=');
  if (argParts.length == 2) {
    args[argParts[0]] = argParts[1];
  }
}
var notifierScriptUrl =
    args['protocol'] + '://' + args['host'] +
    '/talkgadget/notifier-js?silent=true&host=' +
    args['protocol'] + '://' + args['host'] +
    '/talkgadget/notifier-js' +
    (args['jsmode'] != '' ? ('&jsmode=' + args['jsmode']) : '');

// Implement Singleton pattern, where there is only one central roster.
function makeSingleton() {
  // This will keep a list of all central roster panels.
  var chat_popups = [];

  chrome.windows.getAll({populate: true}, function(wins) {
    // Find and remember all central roster panels.
    for (var winIdx = 0, win = wins[winIdx]; win; win = wins[++winIdx]) {
      var tabs = win.tabs;
      for (var tabIdx = 0, tab = tabs[tabIdx]; tab; tab = tabs[++tabIdx]) {
        if (tab.url === location.href) {
          chat_popups.push({tabId: tab.id, winId: win.id});
        }
      }
    }
    if (chat_popups.length > 0) {
      // Multiple panels are executing this function at the same time so we need
      // a global criteria to pickup a "winner": "keep only the tab with
      // smallest id".
      chat_popups.sort(function(t1, t2) { return t1.tabId - t2.tabId; });
      for (var i = 0, l = chat_popups.length; i < l; i++) {
        if (i == 0) {
          chrome.windows.update(chat_popups[i].winId, {focused: true});
        } else {
          chrome.tabs.remove(chat_popups[i].tabId);
        }
      }
    }
  });
}
makeSingleton();

function runGTalkScript() {
  var script = document.createElement('script');
  script.src = notifierScriptUrl;
  script.onload = loadGTalk;
  script.onerror = loadGTalk;
  document.body.appendChild(script);
}

function retryConnection() {
  location.reload();
}

function retryConnectionCountdown() {
  var seconds = retryTime / 1000;
  var minutes = Math.floor(seconds / 60);
  seconds -= minutes * 60;

  document.getElementById('retryStatus').textContent =
      chrome.i18n.getMessage('CHAT_MANAGER_RETRYING_IN',
          [minutes, (seconds < 10 ? '0' : '') + seconds]);

  if (retryTime <= 0) {
    retryConnection();
  } else {
    retryTimer = setTimeout(retryConnectionCountdown, 1000);
    retryTime -= 1000;
  }
}

function loadGTalk() {
  if (window.GTalkNotifier) {
    document.getElementById('retryInfo').style.display = 'none';
    var baseUrl = args['protocol'] + '://' + args['host'] + '/talkgadget/';
    chatClient = new window.GTalkNotifier(
        {
          'clientBaseUrl': baseUrl,
          'clientUrl': 'notifierclient' +
              (args['jsmode'] != '' ? ('?jsmode=' + args['jsmode']) : ''),
          'propertyName': 'ChromeOS',
          'xpcRelay': baseUrl + 'xpc_relay',
          'xpcBlank': baseUrl + 'xpc_blank',
          'locale': args['hl'],
          'isCentralRoster': true,
          'hideProfileCard': true,
          'isFullFrame': true
        }
    );
    delete localStorage.retryStartTime;
  } else {
    if (!localStorage.retryStartTime) {
      localStorage.retryStartTime = MIN_RETRY_MILLISECONDS;
    } else if (localStorage.retryStartTime < MAX_RETRY_MILLISECONDS) {
      localStorage.retryStartTime = Math.min(localStorage.retryStartTime * 2,
          MAX_RETRY_MILLISECONDS);
    }
    retryTime = localStorage.retryStartTime;
    document.getElementById('retryInfo').style.display = 'inline';
    retryConnectionCountdown();
  }
}

function onPageLoaded() {
  // Localizing the page content.
  document.getElementById('retryMessage').textContent =
      chrome.i18n.getMessage('CHAT_MANAGER_COULD_NOT_CONNECT');
  document.getElementById('retryButton').value =
      chrome.i18n.getMessage('CHAT_MANAGER_RETRY_NOW');

  if (localStorage.hasOpenedInViewer) {
    runGTalkScript();
  }
}