| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Port used for: | 5 // Port used for: |
| 6 // 1. forwarding central user requests from the gmail page to the background. | 6 // 1. forwarding central user requests from the gmail page to the background. |
| 7 // 2. forwarding the central user from the background to the gmail page. | 7 // 2. forwarding the central user from the background to the gmail page. |
| 8 var centralJidListenerGmailPort; | 8 var centralJidListenerGmailPort; |
| 9 | 9 |
| 10 // The gmail page div used to funnel events through. | 10 // The gmail page div used to funnel events through. |
| 11 var divGmailHandler; | 11 var divGmailHandler; |
| 12 | 12 |
| 13 // The current central roster Jid. | 13 // The current central roster Jid. |
| 14 // Requested and cached as early as possible. | 14 // Requested and cached as early as possible. |
| 15 var centralRosterJid; | 15 var centralRosterJid; |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * Triggered on a user initiated chat request. Forward to extension to be | 18 * Triggered on a user initiated chat request. Forward to extension to be |
| 19 * processed by the Chrome central roster. | 19 * processed by the Chrome central roster. |
| 20 * @param {MessageEvent} event the new chat event. | 20 * @param {MessageEvent} event the new chat event. |
| 21 */ | 21 */ |
| 22 function forwardChatEvent(event) { | 22 function forwardChatEvent(event) { |
| 23 var chatJid = event.data; | 23 var chatJid = event.data; |
| 24 chrome.extension.sendRequest({msg: event.type, jid: chatJid}); | 24 chrome.extension.sendRequest({msg: event.type, jid: chatJid}); |
| 25 } | 25 } |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * @param {string} eventType the event type. |
| 29 * @param {string} chatJid the jid to route the chat event to. |
| 30 * TODO(seh): Move into a common JS file and reference from chatbridgehook.js. |
| 31 */ |
| 32 function dispatchChatEvent(eventType, chatJid) { |
| 33 var chatEvent = document.createEvent('MessageEvent'); |
| 34 chatEvent.initMessageEvent(eventType, true, true, chatJid); |
| 35 divGmailHandler.dispatchEvent(chatEvent); |
| 36 } |
| 37 |
| 38 /** |
| 28 * Forward central roster Jid to page. | 39 * Forward central roster Jid to page. |
| 29 * @param {string} jid the central roster Jid. | 40 * @param {string} jid the central roster Jid. |
| 30 */ | 41 */ |
| 31 function dispatchCentralJid(jid) { | 42 function dispatchCentralJid(jid) { |
| 32 var outgoingChatEvent = document.createEvent('MessageEvent'); | 43 dispatchChatEvent(ChatBridgeEventTypes.CENTRAL_USER_UPDATE, jid); |
| 33 outgoingChatEvent.initMessageEvent( | |
| 34 ChatBridgeEventTypes.CENTRAL_USER_UPDATE, | |
| 35 true, true, jid); | |
| 36 divGmailHandler.dispatchEvent(outgoingChatEvent); | |
| 37 } | 44 } |
| 38 | 45 |
| 39 /** | 46 /** |
| 40 * Setup central roster jid listener. | 47 * Setup central roster jid listener. |
| 41 * @param {MessageEvent} event the event. | 48 * @param {MessageEvent} event the event. |
| 42 */ | 49 */ |
| 43 function setupCentralRosterJidListener(event) { | 50 function setupCentralRosterJidListener(event) { |
| 44 if (!centralJidListenerGmailPort) { | 51 if (!centralJidListenerGmailPort) { |
| 45 if (centralRosterJid) { | 52 if (centralRosterJid) { |
| 46 dispatchCentralJid(centralRosterJid); | 53 dispatchCentralJid(centralRosterJid); |
| 47 } | 54 } |
| 48 centralJidListenerGmailPort = chrome.extension.connect( | 55 centralJidListenerGmailPort = chrome.extension.connect( |
| 49 {name: 'centralJidListener'}); | 56 {name: 'centralJidListener'}); |
| 50 centralJidListenerGmailPort.onMessage.addListener(function(msg) { | 57 centralJidListenerGmailPort.onMessage.addListener(function(msg) { |
| 51 centralRosterJid = msg.jid; | 58 if (msg.eventType == ChatBridgeEventTypes.CENTRAL_USER_UPDATE) { |
| 52 dispatchCentralJid(centralRosterJid); | 59 centralRosterJid = msg.jid; |
| 60 } |
| 61 dispatchChatEvent(msg.eventType, msg.jid); |
| 53 }); | 62 }); |
| 54 } | 63 } |
| 55 } | 64 } |
| 56 | 65 |
| 57 /** | 66 /** |
| 58 * When the page loads, search for the communication channel div. | 67 * When the page loads, search for the communication channel div. |
| 59 */ | 68 */ |
| 60 function onPageLoaded() { | 69 function onPageLoaded() { |
| 61 divGmailHandler = document.getElementById('mainElement'); | 70 divGmailHandler = document.getElementById('mainElement'); |
| 62 if (divGmailHandler) { | 71 if (divGmailHandler) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 82 | 91 |
| 83 // The initial centralRosterJid is sent in setupCentralRosterJidListener, | 92 // The initial centralRosterJid is sent in setupCentralRosterJidListener, |
| 84 // but if it's already been called, send it here. | 93 // but if it's already been called, send it here. |
| 85 if (centralJidListenerGmailPort && centralRosterJid) { | 94 if (centralJidListenerGmailPort && centralRosterJid) { |
| 86 dispatchCentralJid(centralRosterJid); | 95 dispatchCentralJid(centralRosterJid); |
| 87 } | 96 } |
| 88 } | 97 } |
| 89 ); | 98 ); |
| 90 | 99 |
| 91 window.addEventListener("load", onPageLoaded, false); | 100 window.addEventListener("load", onPageLoaded, false); |
| OLD | NEW |