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: |
(...skipping 25 matching lines...) Expand all Loading... |
36 } | 36 } |
37 var chatJid = event.data; | 37 var chatJid = event.data; |
38 if (!centralJidBroadcasterPort) { | 38 if (!centralJidBroadcasterPort) { |
39 chrome.extension.sendRequest({msg: eventType, jid: chatJid}); | 39 chrome.extension.sendRequest({msg: eventType, jid: chatJid}); |
40 } else { | 40 } else { |
41 dispatchChatEvent(eventType, chatJid); | 41 dispatchChatEvent(eventType, chatJid); |
42 } | 42 } |
43 } | 43 } |
44 | 44 |
45 /** | 45 /** |
| 46 * Triggered on opening/closing a central roster chat. Forward to extension. |
| 47 * @param {MessageEvent} event the opened/closed event. |
| 48 */ |
| 49 function moleOpenedClosed(event) { |
| 50 var eventType = event.type; |
| 51 var chatJid = event.data; |
| 52 chrome.extension.sendRequest({msg: eventType, jid: chatJid}); |
| 53 } |
| 54 |
| 55 /** |
46 * Manage two-way communication with the central roster. Updated jid's are | 56 * Manage two-way communication with the central roster. Updated jid's are |
47 * forwarded to the background, while chats are forwarded to the page. | 57 * forwarded to the background, while chats are forwarded to the page. |
48 * @param {string} chatType the chat event type. | 58 * @param {string} eventType the event type. |
49 * @param {string} chatJid the jid to route the chat event to. | 59 * @param {string} chatJid the jid to route the chat event to. |
50 */ | 60 */ |
51 function dispatchChatEvent(chatType, chatJid) { | 61 function dispatchChatEvent(eventType, chatJid) { |
52 var showChatEvent = document.createEvent('MessageEvent'); | 62 var chatEvent = document.createEvent('MessageEvent'); |
53 showChatEvent.initMessageEvent(chatType, true, true, chatJid); | 63 chatEvent.initMessageEvent(eventType, true, true, chatJid); |
54 divRosterHandler.dispatchEvent(showChatEvent); | 64 divRosterHandler.dispatchEvent(chatEvent); |
55 } | 65 } |
56 | 66 |
57 /** | 67 /** |
58 * Forward central roster Jid to page. | 68 * Forward central roster Jid to page. |
59 * @param {string} jid the central roster Jid. | 69 * @param {string} jid the central roster Jid. |
60 */ | 70 */ |
61 function dispatchCentralJid(jid) { | 71 function dispatchCentralJid(jid) { |
62 var outgoingChatEvent = document.createEvent('MessageEvent'); | 72 dispatchChatEvent(ChatBridgeEventTypes.CENTRAL_USER_UPDATE, jid); |
63 outgoingChatEvent.initMessageEvent( | |
64 ChatBridgeEventTypes.CENTRAL_USER_UPDATE, | |
65 true, true, jid); | |
66 divRosterHandler.dispatchEvent(outgoingChatEvent); | |
67 } | 73 } |
68 | 74 |
69 /** | 75 /** |
70 * Manage two-way communication with the central roster. Updated jid's are | 76 * Manage two-way communication with the central roster. Updated jid's are |
71 * forwarded to the background, while chats are forwarded to the page. | 77 * forwarded to the background, while chats are forwarded to the page. |
72 * @param {MessageEvent} event the new central roster jid event. | 78 * @param {MessageEvent} event the new central roster jid event. |
73 */ | 79 */ |
74 function centralRosterHandler(event) { | 80 function centralRosterHandler(event) { |
75 if (!centralJidBroadcasterPort) { | 81 if (!centralJidBroadcasterPort) { |
76 centralJidBroadcasterPort = chrome.extension.connect( | 82 centralJidBroadcasterPort = chrome.extension.connect( |
77 {name: 'centralJidBroadcaster'}); | 83 {name: 'centralJidBroadcaster'}); |
78 centralJidBroadcasterPort.onMessage.addListener(function(msg) { | 84 centralJidBroadcasterPort.onMessage.addListener(function(msg) { |
79 var chatJid = msg.jid; | 85 dispatchChatEvent(msg.eventType, msg.jid); |
80 dispatchChatEvent(msg.chatType, chatJid); | |
81 }); | 86 }); |
82 } | 87 } |
83 centralRosterJid = event.data; | 88 centralRosterJid = event.data; |
84 centralJidBroadcasterPort.postMessage({jid: centralRosterJid}); | 89 centralJidBroadcasterPort.postMessage({jid: centralRosterJid}); |
85 } | 90 } |
86 | 91 |
87 /** | 92 /** |
88 * Setup central roster jid listener. | 93 * Setup central roster jid listener. |
89 * @param {MessageEvent} event the event. | 94 * @param {MessageEvent} event the event. |
90 */ | 95 */ |
91 function setupCentralRosterJidListener(event) { | 96 function setupCentralRosterJidListener(event) { |
92 if (!centralJidListenerChatPort) { | 97 if (!centralJidListenerChatPort) { |
93 if (centralRosterJid) { | 98 if (centralRosterJid) { |
94 dispatchCentralJid(centralRosterJid); | 99 dispatchCentralJid(centralRosterJid); |
95 } | 100 } |
96 centralJidListenerChatPort = chrome.extension.connect( | 101 centralJidListenerChatPort = chrome.extension.connect( |
97 {name: 'centralJidListener'}); | 102 {name: 'centralJidListener'}); |
98 centralJidListenerChatPort.onMessage.addListener(function(msg) { | 103 centralJidListenerChatPort.onMessage.addListener(function(msg) { |
99 centralRosterJid = msg.jid; | 104 if (msg.eventType == ChatBridgeEventTypes.CENTRAL_USER_UPDATE) { |
100 dispatchCentralJid(centralRosterJid); | 105 centralRosterJid = msg.jid; |
| 106 } |
| 107 dispatchChatEvent(msg.eventType, msg.jid); |
101 }); | 108 }); |
102 } | 109 } |
103 } | 110 } |
104 | 111 |
105 /** | 112 /** |
106 * When the page loads, search for the communication channel div. | 113 * When the page loads, search for the communication channel div. |
107 */ | 114 */ |
108 function onPageLoaded() { | 115 function onPageLoaded() { |
109 divRosterHandler = document.getElementById('roster_comm_link'); | 116 divRosterHandler = document.getElementById('roster_comm_link'); |
110 if (divRosterHandler) { | 117 if (divRosterHandler) { |
111 divRosterHandler.addEventListener( | 118 divRosterHandler.addEventListener( |
112 ChatBridgeEventTypes.SHOW_CHAT, | 119 ChatBridgeEventTypes.SHOW_CHAT, |
113 forwardChatEvent, false); | 120 forwardChatEvent, false); |
114 divRosterHandler.addEventListener( | 121 divRosterHandler.addEventListener( |
115 ChatBridgeEventTypes.NEW_VIDEO_CHAT, | 122 ChatBridgeEventTypes.NEW_VIDEO_CHAT, |
116 forwardChatEvent, false); | 123 forwardChatEvent, false); |
117 divRosterHandler.addEventListener( | 124 divRosterHandler.addEventListener( |
118 ChatBridgeEventTypes.NEW_VOICE_CHAT, | 125 ChatBridgeEventTypes.NEW_VOICE_CHAT, |
119 forwardChatEvent, false); | 126 forwardChatEvent, false); |
120 divRosterHandler.addEventListener( | 127 divRosterHandler.addEventListener( |
121 ChatBridgeEventTypes.CENTRAL_USER_SET, | 128 ChatBridgeEventTypes.CENTRAL_USER_SET, |
122 centralRosterHandler, false); | 129 centralRosterHandler, false); |
123 divRosterHandler.addEventListener( | 130 divRosterHandler.addEventListener( |
124 ChatBridgeEventTypes.CENTRAL_USER_WATCHER, | 131 ChatBridgeEventTypes.CENTRAL_USER_WATCHER, |
125 setupCentralRosterJidListener, false); | 132 setupCentralRosterJidListener, false); |
| 133 divRosterHandler.addEventListener( |
| 134 ChatBridgeEventTypes.OPENED_MOLE_INCOMING, |
| 135 moleOpenedClosed, false); |
| 136 divRosterHandler.addEventListener( |
| 137 ChatBridgeEventTypes.CLOSED_MOLE_INCOMING, |
| 138 moleOpenedClosed, false); |
126 } | 139 } |
127 } | 140 } |
128 | 141 |
129 // Retrieve the initial central roster Jid and cache the result. | 142 // Retrieve the initial central roster Jid and cache the result. |
130 chrome.extension.sendRequest( | 143 chrome.extension.sendRequest( |
131 {msg: ChatBridgeEventTypes.CENTRAL_USER_WATCHER}, function(response) { | 144 {msg: ChatBridgeEventTypes.CENTRAL_USER_WATCHER}, function(response) { |
132 centralRosterJid = response.jid; | 145 centralRosterJid = response.jid; |
133 | 146 |
134 // The initial centralRosterJid is sent in setupCentralRosterJidListener, | 147 // The initial centralRosterJid is sent in setupCentralRosterJidListener, |
135 // but if it's already been called, send it here. | 148 // but if it's already been called, send it here. |
136 if (centralJidListenerChatPort && centralRosterJid) { | 149 if (centralJidListenerChatPort && centralRosterJid) { |
137 dispatchCentralJid(centralRosterJid); | 150 dispatchCentralJid(centralRosterJid); |
138 } | 151 } |
139 } | 152 } |
140 ); | 153 ); |
141 | 154 |
142 window.addEventListener("load", onPageLoaded, false); | 155 window.addEventListener("load", onPageLoaded, false); |
OLD | NEW |