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 updated central user from the chat page to the background. | 6 // 1. forwarding updated central user from the chat page to the background. |
7 // 2. forwarding chats from the background to the chat page we're attached to. | 7 // 2. forwarding chats from the background to the chat page we're attached to. |
8 var centralJidBroadcasterPort; | 8 var centralJidBroadcasterPort; |
9 | 9 |
10 // Port used for: | 10 // Port used for: |
11 // 1. forwarding central user requests from the chat page to the background. | 11 // 1. forwarding central user requests from the chat page to the background. |
12 // 2. forwarding the central user from the background to the chat page. | 12 // 2. forwarding the central user from the background to the chat page. |
13 var centralJidListenerChatPort; | 13 var centralJidListenerChatPort; |
14 | 14 |
15 // The chat page div used to funnel events through. | 15 // The chat page div used to funnel events through. |
16 var divRosterHandler; | 16 var divRosterHandler; |
17 | 17 |
18 /** | 18 /** |
| 19 * Triggered on a user initiated chat request. Forward to extension to be |
| 20 * processed by the Chrome central roster. |
| 21 * @param {MessageEvent} event the new chat event. |
| 22 */ |
| 23 function forwardChatEvent(event) { |
| 24 var eventType = event.type; |
| 25 switch (event.type) { |
| 26 case ChatBridgeEventTypes.NEW_VIDEO_CHAT: |
| 27 eventType = ChatBridgeEventTypes.START_VIDEO; |
| 28 break; |
| 29 case ChatBridgeEventTypes.NEW_VOICE_CHAT: |
| 30 eventType = ChatBridgeEventTypes.START_VOICE; |
| 31 break; |
| 32 } |
| 33 var chatJid = event.data; |
| 34 if (!centralJidBroadcasterPort) { |
| 35 chrome.extension.sendRequest({msg: eventType, jid: chatJid}); |
| 36 } else { |
| 37 dispatchChatEvent(eventType, chatJid); |
| 38 } |
| 39 } |
| 40 |
| 41 /** |
| 42 * Manage two-way communication with the central roster. Updated jid's are |
| 43 * forwarded to the background, while chats are forwarded to the page. |
| 44 * @param {string} chatType the chat event type. |
| 45 * @param {string} chatJid the jid to route the chat event to. |
| 46 */ |
| 47 function dispatchChatEvent(chatType, chatJid) { |
| 48 var showChatEvent = document.createEvent('MessageEvent'); |
| 49 showChatEvent.initMessageEvent(chatType, true, true, chatJid); |
| 50 divRosterHandler.dispatchEvent(showChatEvent); |
| 51 } |
| 52 |
| 53 /** |
19 * Manage two-way communication with the central roster. Updated jid's are | 54 * Manage two-way communication with the central roster. Updated jid's are |
20 * forwarded to the background, while chats are forwarded to the page. | 55 * forwarded to the background, while chats are forwarded to the page. |
21 * @param {MessageEvent} event the new central roster jid event. | 56 * @param {MessageEvent} event the new central roster jid event. |
22 */ | 57 */ |
23 function centralRosterHandler(event) { | 58 function centralRosterHandler(event) { |
24 if (!centralJidBroadcasterPort) { | 59 if (!centralJidBroadcasterPort) { |
25 centralJidBroadcasterPort = chrome.extension.connect( | 60 centralJidBroadcasterPort = chrome.extension.connect( |
26 {name: 'centralJidBroadcaster'}); | 61 {name: 'centralJidBroadcaster'}); |
27 centralJidBroadcasterPort.onMessage.addListener(function(msg) { | 62 centralJidBroadcasterPort.onMessage.addListener(function(msg) { |
28 var chatJid = msg.jid; | 63 var chatJid = msg.jid; |
29 var showChatEvent = document.createEvent('MessageEvent'); | 64 dispatchChatEvent(msg.chatType, chatJid); |
30 showChatEvent.initMessageEvent(msg.chatType, true, true, chatJid); | |
31 divRosterHandler.dispatchEvent(showChatEvent); | |
32 }); | 65 }); |
33 } | 66 } |
34 var centralRosterJid = event.data; | 67 var centralRosterJid = event.data; |
35 centralJidBroadcasterPort.postMessage({jid: centralRosterJid}); | 68 centralJidBroadcasterPort.postMessage({jid: centralRosterJid}); |
36 } | 69 } |
37 | 70 |
38 /** | 71 /** |
39 * Setup central roster jid listener. | 72 * Setup central roster jid listener. |
40 * @param {MessageEvent} event the event. | 73 * @param {MessageEvent} event the event. |
41 */ | 74 */ |
42 function setupCentralRosterJidListener(event) { | 75 function setupCentralRosterJidListener(event) { |
43 if (!centralJidListenerChatPort) { | 76 if (!centralJidListenerChatPort) { |
44 centralJidListenerChatPort = chrome.extension.connect( | 77 centralJidListenerChatPort = chrome.extension.connect( |
45 {name: 'centralJidListener'}); | 78 {name: 'centralJidListener'}); |
46 centralJidListenerChatPort.onMessage.addListener(function(msg) { | 79 centralJidListenerChatPort.onMessage.addListener(function(msg) { |
47 var centralRosterJid = msg.jid; | 80 var centralRosterJid = msg.jid; |
48 var outgoingChatEvent = document.createEvent('MessageEvent'); | 81 var outgoingChatEvent = document.createEvent('MessageEvent'); |
49 outgoingChatEvent.initMessageEvent( | 82 outgoingChatEvent.initMessageEvent( |
50 ChatBridgeEventTypes.CENTRAL_USER_UPDATE, | 83 ChatBridgeEventTypes.CENTRAL_USER_UPDATE, |
51 true, true, centralRosterJid); | 84 true, true, centralRosterJid); |
52 divRosterHandler.dispatchEvent(outgoingChatEvent); | 85 divRosterHandler.dispatchEvent(outgoingChatEvent); |
53 }); | 86 }); |
54 } | 87 } |
55 } | 88 } |
56 | 89 |
57 // Search for communication channel div. | 90 // Search for communication channel div. |
58 divRosterHandler = document.getElementById('roster_comm_link'); | 91 divRosterHandler = document.getElementById('roster_comm_link'); |
59 if (divRosterHandler) { | 92 if (divRosterHandler) { |
| 93 divRosterHandler.addEventListener(ChatBridgeEventTypes.SHOW_CHAT, |
| 94 forwardChatEvent, false); |
| 95 divRosterHandler.addEventListener(ChatBridgeEventTypes.NEW_VIDEO_CHAT, |
| 96 forwardChatEvent, false); |
| 97 divRosterHandler.addEventListener(ChatBridgeEventTypes.NEW_VOICE_CHAT, |
| 98 forwardChatEvent, false); |
60 divRosterHandler.addEventListener(ChatBridgeEventTypes.CENTRAL_USER_SET, | 99 divRosterHandler.addEventListener(ChatBridgeEventTypes.CENTRAL_USER_SET, |
61 centralRosterHandler, false); | 100 centralRosterHandler, false); |
62 divRosterHandler.addEventListener(ChatBridgeEventTypes.CENTRAL_USER_WATCHER, | 101 divRosterHandler.addEventListener(ChatBridgeEventTypes.CENTRAL_USER_WATCHER, |
63 setupCentralRosterJidListener, false); | 102 setupCentralRosterJidListener, false); |
64 } | 103 } |
OLD | NEW |