OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/browser/extensions/browser_event_router.h" | 5 #include "chrome/browser/extensions/browser_event_router.h" |
6 | 6 |
7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
10 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api_constants.h" | 10 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api_constants.h" |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 // If tab is new, send created event. | 194 // If tab is new, send created event. |
195 int tab_id = ExtensionTabUtil::GetTabId(contents); | 195 int tab_id = ExtensionTabUtil::GetTabId(contents); |
196 if (!GetTabEntry(contents)) { | 196 if (!GetTabEntry(contents)) { |
197 tab_entries_[tab_id] = TabEntry(); | 197 tab_entries_[tab_id] = TabEntry(); |
198 | 198 |
199 TabCreatedAt(contents, index, active); | 199 TabCreatedAt(contents, index, active); |
200 return; | 200 return; |
201 } | 201 } |
202 | 202 |
203 scoped_ptr<base::ListValue> args(new base::ListValue()); | 203 scoped_ptr<base::ListValue> args(new base::ListValue()); |
204 args->Append(Value::CreateIntegerValue(tab_id)); | 204 args->Append(new base::FundamentalValue(tab_id)); |
205 | 205 |
206 DictionaryValue* object_args = new DictionaryValue(); | 206 DictionaryValue* object_args = new DictionaryValue(); |
207 object_args->Set(tab_keys::kNewWindowIdKey, Value::CreateIntegerValue( | 207 object_args->Set(tab_keys::kNewWindowIdKey, new base::FundamentalValue( |
208 ExtensionTabUtil::GetWindowIdOfTab(contents))); | 208 ExtensionTabUtil::GetWindowIdOfTab(contents))); |
209 object_args->Set(tab_keys::kNewPositionKey, Value::CreateIntegerValue( | 209 object_args->Set(tab_keys::kNewPositionKey, new base::FundamentalValue( |
210 index)); | 210 index)); |
211 args->Append(object_args); | 211 args->Append(object_args); |
212 | 212 |
213 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 213 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
214 DispatchEvent(profile, events::kOnTabAttached, args.Pass(), | 214 DispatchEvent(profile, events::kOnTabAttached, args.Pass(), |
215 EventRouter::USER_GESTURE_UNKNOWN); | 215 EventRouter::USER_GESTURE_UNKNOWN); |
216 } | 216 } |
217 | 217 |
218 void BrowserEventRouter::TabDetachedAt(WebContents* contents, int index) { | 218 void BrowserEventRouter::TabDetachedAt(WebContents* contents, int index) { |
219 if (!GetTabEntry(contents)) { | 219 if (!GetTabEntry(contents)) { |
220 // The tab was removed. Don't send detach event. | 220 // The tab was removed. Don't send detach event. |
221 return; | 221 return; |
222 } | 222 } |
223 | 223 |
224 scoped_ptr<base::ListValue> args(new base::ListValue()); | 224 scoped_ptr<base::ListValue> args(new base::ListValue()); |
225 args->Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); | 225 args->Append( |
| 226 new base::FundamentalValue(ExtensionTabUtil::GetTabId(contents))); |
226 | 227 |
227 DictionaryValue* object_args = new DictionaryValue(); | 228 DictionaryValue* object_args = new DictionaryValue(); |
228 object_args->Set(tab_keys::kOldWindowIdKey, Value::CreateIntegerValue( | 229 object_args->Set(tab_keys::kOldWindowIdKey, new base::FundamentalValue( |
229 ExtensionTabUtil::GetWindowIdOfTab(contents))); | 230 ExtensionTabUtil::GetWindowIdOfTab(contents))); |
230 object_args->Set(tab_keys::kOldPositionKey, Value::CreateIntegerValue( | 231 object_args->Set(tab_keys::kOldPositionKey, new base::FundamentalValue( |
231 index)); | 232 index)); |
232 args->Append(object_args); | 233 args->Append(object_args); |
233 | 234 |
234 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 235 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
235 DispatchEvent(profile, events::kOnTabDetached, args.Pass(), | 236 DispatchEvent(profile, events::kOnTabDetached, args.Pass(), |
236 EventRouter::USER_GESTURE_UNKNOWN); | 237 EventRouter::USER_GESTURE_UNKNOWN); |
237 } | 238 } |
238 | 239 |
239 void BrowserEventRouter::TabClosingAt(TabStripModel* tab_strip_model, | 240 void BrowserEventRouter::TabClosingAt(TabStripModel* tab_strip_model, |
240 WebContents* contents, | 241 WebContents* contents, |
241 int index) { | 242 int index) { |
242 int tab_id = ExtensionTabUtil::GetTabId(contents); | 243 int tab_id = ExtensionTabUtil::GetTabId(contents); |
243 | 244 |
244 scoped_ptr<base::ListValue> args(new base::ListValue()); | 245 scoped_ptr<base::ListValue> args(new base::ListValue()); |
245 args->Append(Value::CreateIntegerValue(tab_id)); | 246 args->Append(new base::FundamentalValue(tab_id)); |
246 | 247 |
247 DictionaryValue* object_args = new DictionaryValue(); | 248 DictionaryValue* object_args = new DictionaryValue(); |
248 object_args->SetInteger(tab_keys::kWindowIdKey, | 249 object_args->SetInteger(tab_keys::kWindowIdKey, |
249 ExtensionTabUtil::GetWindowIdOfTab(contents)); | 250 ExtensionTabUtil::GetWindowIdOfTab(contents)); |
250 object_args->SetBoolean(tab_keys::kWindowClosing, | 251 object_args->SetBoolean(tab_keys::kWindowClosing, |
251 tab_strip_model->closing_all()); | 252 tab_strip_model->closing_all()); |
252 args->Append(object_args); | 253 args->Append(object_args); |
253 | 254 |
254 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 255 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
255 DispatchEvent(profile, events::kOnTabRemoved, args.Pass(), | 256 DispatchEvent(profile, events::kOnTabRemoved, args.Pass(), |
256 EventRouter::USER_GESTURE_UNKNOWN); | 257 EventRouter::USER_GESTURE_UNKNOWN); |
257 | 258 |
258 int removed_count = tab_entries_.erase(tab_id); | 259 int removed_count = tab_entries_.erase(tab_id); |
259 DCHECK_GT(removed_count, 0); | 260 DCHECK_GT(removed_count, 0); |
260 | 261 |
261 UnregisterForTabNotifications(contents); | 262 UnregisterForTabNotifications(contents); |
262 } | 263 } |
263 | 264 |
264 void BrowserEventRouter::ActiveTabChanged(WebContents* old_contents, | 265 void BrowserEventRouter::ActiveTabChanged(WebContents* old_contents, |
265 WebContents* new_contents, | 266 WebContents* new_contents, |
266 int index, | 267 int index, |
267 int reason) { | 268 int reason) { |
268 scoped_ptr<base::ListValue> args(new base::ListValue()); | 269 scoped_ptr<base::ListValue> args(new base::ListValue()); |
269 int tab_id = ExtensionTabUtil::GetTabId(new_contents); | 270 int tab_id = ExtensionTabUtil::GetTabId(new_contents); |
270 args->Append(Value::CreateIntegerValue(tab_id)); | 271 args->Append(new base::FundamentalValue(tab_id)); |
271 | 272 |
272 DictionaryValue* object_args = new DictionaryValue(); | 273 DictionaryValue* object_args = new DictionaryValue(); |
273 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( | 274 object_args->Set(tab_keys::kWindowIdKey, new base::FundamentalValue( |
274 ExtensionTabUtil::GetWindowIdOfTab(new_contents))); | 275 ExtensionTabUtil::GetWindowIdOfTab(new_contents))); |
275 args->Append(object_args); | 276 args->Append(object_args); |
276 | 277 |
277 // The onActivated event replaced onActiveChanged and onSelectionChanged. The | 278 // The onActivated event replaced onActiveChanged and onSelectionChanged. The |
278 // deprecated events take two arguments: tabId, {windowId}. | 279 // deprecated events take two arguments: tabId, {windowId}. |
279 Profile* profile = | 280 Profile* profile = |
280 Profile::FromBrowserContext(new_contents->GetBrowserContext()); | 281 Profile::FromBrowserContext(new_contents->GetBrowserContext()); |
281 EventRouter::UserGestureState gesture = | 282 EventRouter::UserGestureState gesture = |
282 reason & CHANGE_REASON_USER_GESTURE | 283 reason & CHANGE_REASON_USER_GESTURE |
283 ? EventRouter::USER_GESTURE_ENABLED | 284 ? EventRouter::USER_GESTURE_ENABLED |
284 : EventRouter::USER_GESTURE_NOT_ENABLED; | 285 : EventRouter::USER_GESTURE_NOT_ENABLED; |
285 DispatchEvent(profile, events::kOnTabSelectionChanged, | 286 DispatchEvent(profile, events::kOnTabSelectionChanged, |
286 scoped_ptr<base::ListValue>(args->DeepCopy()), gesture); | 287 scoped_ptr<base::ListValue>(args->DeepCopy()), gesture); |
287 DispatchEvent(profile, events::kOnTabActiveChanged, | 288 DispatchEvent(profile, events::kOnTabActiveChanged, |
288 scoped_ptr<base::ListValue>(args->DeepCopy()), gesture); | 289 scoped_ptr<base::ListValue>(args->DeepCopy()), gesture); |
289 | 290 |
290 // The onActivated event takes one argument: {windowId, tabId}. | 291 // The onActivated event takes one argument: {windowId, tabId}. |
291 args->Remove(0, NULL); | 292 args->Remove(0, NULL); |
292 object_args->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id)); | 293 object_args->Set(tab_keys::kTabIdKey, new base::FundamentalValue(tab_id)); |
293 DispatchEvent(profile, events::kOnTabActivated, args.Pass(), gesture); | 294 DispatchEvent(profile, events::kOnTabActivated, args.Pass(), gesture); |
294 } | 295 } |
295 | 296 |
296 void BrowserEventRouter::TabSelectionChanged( | 297 void BrowserEventRouter::TabSelectionChanged( |
297 TabStripModel* tab_strip_model, | 298 TabStripModel* tab_strip_model, |
298 const ui::ListSelectionModel& old_model) { | 299 const ui::ListSelectionModel& old_model) { |
299 ui::ListSelectionModel::SelectedIndices new_selection = | 300 ui::ListSelectionModel::SelectedIndices new_selection = |
300 tab_strip_model->selection_model().selected_indices(); | 301 tab_strip_model->selection_model().selected_indices(); |
301 base::ListValue* all = new base::ListValue(); | 302 base::ListValue* all = new base::ListValue(); |
302 | 303 |
303 for (size_t i = 0; i < new_selection.size(); ++i) { | 304 for (size_t i = 0; i < new_selection.size(); ++i) { |
304 int index = new_selection[i]; | 305 int index = new_selection[i]; |
305 WebContents* contents = tab_strip_model->GetWebContentsAt(index); | 306 WebContents* contents = tab_strip_model->GetWebContentsAt(index); |
306 if (!contents) | 307 if (!contents) |
307 break; | 308 break; |
308 int tab_id = ExtensionTabUtil::GetTabId(contents); | 309 int tab_id = ExtensionTabUtil::GetTabId(contents); |
309 all->Append(Value::CreateIntegerValue(tab_id)); | 310 all->Append(new base::FundamentalValue(tab_id)); |
310 } | 311 } |
311 | 312 |
312 scoped_ptr<base::ListValue> args(new base::ListValue()); | 313 scoped_ptr<base::ListValue> args(new base::ListValue()); |
313 DictionaryValue* select_info = new DictionaryValue(); | 314 DictionaryValue* select_info = new DictionaryValue(); |
314 | 315 |
315 select_info->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( | 316 select_info->Set(tab_keys::kWindowIdKey, new base::FundamentalValue( |
316 ExtensionTabUtil::GetWindowIdOfTabStripModel(tab_strip_model))); | 317 ExtensionTabUtil::GetWindowIdOfTabStripModel(tab_strip_model))); |
317 | 318 |
318 select_info->Set(tab_keys::kTabIdsKey, all); | 319 select_info->Set(tab_keys::kTabIdsKey, all); |
319 args->Append(select_info); | 320 args->Append(select_info); |
320 | 321 |
321 // The onHighlighted event replaced onHighlightChanged. | 322 // The onHighlighted event replaced onHighlightChanged. |
322 Profile* profile = tab_strip_model->profile(); | 323 Profile* profile = tab_strip_model->profile(); |
323 DispatchEvent(profile, events::kOnTabHighlightChanged, | 324 DispatchEvent(profile, events::kOnTabHighlightChanged, |
324 scoped_ptr<base::ListValue>(args->DeepCopy()), | 325 scoped_ptr<base::ListValue>(args->DeepCopy()), |
325 EventRouter::USER_GESTURE_UNKNOWN); | 326 EventRouter::USER_GESTURE_UNKNOWN); |
326 DispatchEvent(profile, events::kOnTabHighlighted, args.Pass(), | 327 DispatchEvent(profile, events::kOnTabHighlighted, args.Pass(), |
327 EventRouter::USER_GESTURE_UNKNOWN); | 328 EventRouter::USER_GESTURE_UNKNOWN); |
328 } | 329 } |
329 | 330 |
330 void BrowserEventRouter::TabMoved(WebContents* contents, | 331 void BrowserEventRouter::TabMoved(WebContents* contents, |
331 int from_index, | 332 int from_index, |
332 int to_index) { | 333 int to_index) { |
333 scoped_ptr<base::ListValue> args(new base::ListValue()); | 334 scoped_ptr<base::ListValue> args(new base::ListValue()); |
334 args->Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); | 335 args->Append( |
| 336 new base::FundamentalValue(ExtensionTabUtil::GetTabId(contents))); |
335 | 337 |
336 DictionaryValue* object_args = new DictionaryValue(); | 338 DictionaryValue* object_args = new DictionaryValue(); |
337 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( | 339 object_args->Set(tab_keys::kWindowIdKey, new base::FundamentalValue( |
338 ExtensionTabUtil::GetWindowIdOfTab(contents))); | 340 ExtensionTabUtil::GetWindowIdOfTab(contents))); |
339 object_args->Set(tab_keys::kFromIndexKey, Value::CreateIntegerValue( | 341 object_args->Set(tab_keys::kFromIndexKey, new base::FundamentalValue( |
340 from_index)); | 342 from_index)); |
341 object_args->Set(tab_keys::kToIndexKey, Value::CreateIntegerValue( | 343 object_args->Set(tab_keys::kToIndexKey, new base::FundamentalValue( |
342 to_index)); | 344 to_index)); |
343 args->Append(object_args); | 345 args->Append(object_args); |
344 | 346 |
345 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 347 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
346 DispatchEvent(profile, events::kOnTabMoved, args.Pass(), | 348 DispatchEvent(profile, events::kOnTabMoved, args.Pass(), |
347 EventRouter::USER_GESTURE_UNKNOWN); | 349 EventRouter::USER_GESTURE_UNKNOWN); |
348 } | 350 } |
349 | 351 |
350 void BrowserEventRouter::TabUpdated(WebContents* contents, bool did_navigate) { | 352 void BrowserEventRouter::TabUpdated(WebContents* contents, bool did_navigate) { |
351 TabEntry* entry = GetTabEntry(contents); | 353 TabEntry* entry = GetTabEntry(contents); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 ExtensionSystem::Get(profile)->event_router()-> | 410 ExtensionSystem::Get(profile)->event_router()-> |
409 DispatchEventToExtension(extension_id, event.Pass()); | 411 DispatchEventToExtension(extension_id, event.Pass()); |
410 } | 412 } |
411 | 413 |
412 void BrowserEventRouter::DispatchSimpleBrowserEvent( | 414 void BrowserEventRouter::DispatchSimpleBrowserEvent( |
413 Profile* profile, const int window_id, const char* event_name) { | 415 Profile* profile, const int window_id, const char* event_name) { |
414 if (!profile_->IsSameProfile(profile)) | 416 if (!profile_->IsSameProfile(profile)) |
415 return; | 417 return; |
416 | 418 |
417 scoped_ptr<base::ListValue> args(new base::ListValue()); | 419 scoped_ptr<base::ListValue> args(new base::ListValue()); |
418 args->Append(Value::CreateIntegerValue(window_id)); | 420 args->Append(new base::FundamentalValue(window_id)); |
419 | 421 |
420 DispatchEvent(profile, event_name, args.Pass(), | 422 DispatchEvent(profile, event_name, args.Pass(), |
421 EventRouter::USER_GESTURE_UNKNOWN); | 423 EventRouter::USER_GESTURE_UNKNOWN); |
422 } | 424 } |
423 | 425 |
424 static void WillDispatchTabUpdatedEvent( | 426 static void WillDispatchTabUpdatedEvent( |
425 WebContents* contents, | 427 WebContents* contents, |
426 const DictionaryValue* changed_properties, | 428 const DictionaryValue* changed_properties, |
427 Profile* profile, | 429 Profile* profile, |
428 const Extension* extension, | 430 const Extension* extension, |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 | 513 |
512 void BrowserEventRouter::TabReplacedAt(TabStripModel* tab_strip_model, | 514 void BrowserEventRouter::TabReplacedAt(TabStripModel* tab_strip_model, |
513 WebContents* old_contents, | 515 WebContents* old_contents, |
514 WebContents* new_contents, | 516 WebContents* new_contents, |
515 int index) { | 517 int index) { |
516 // Notify listeners that the next tabs closing or being added are due to | 518 // Notify listeners that the next tabs closing or being added are due to |
517 // WebContents being swapped. | 519 // WebContents being swapped. |
518 const int new_tab_id = ExtensionTabUtil::GetTabId(new_contents); | 520 const int new_tab_id = ExtensionTabUtil::GetTabId(new_contents); |
519 const int old_tab_id = ExtensionTabUtil::GetTabId(old_contents); | 521 const int old_tab_id = ExtensionTabUtil::GetTabId(old_contents); |
520 scoped_ptr<base::ListValue> args(new base::ListValue()); | 522 scoped_ptr<base::ListValue> args(new base::ListValue()); |
521 args->Append(Value::CreateIntegerValue(new_tab_id)); | 523 args->Append(new base::FundamentalValue(new_tab_id)); |
522 args->Append(Value::CreateIntegerValue(old_tab_id)); | 524 args->Append(new base::FundamentalValue(old_tab_id)); |
523 | 525 |
524 DispatchEvent(Profile::FromBrowserContext(new_contents->GetBrowserContext()), | 526 DispatchEvent(Profile::FromBrowserContext(new_contents->GetBrowserContext()), |
525 events::kOnTabReplaced, | 527 events::kOnTabReplaced, |
526 args.Pass(), | 528 args.Pass(), |
527 EventRouter::USER_GESTURE_UNKNOWN); | 529 EventRouter::USER_GESTURE_UNKNOWN); |
528 | 530 |
529 // Update tab_entries_. | 531 // Update tab_entries_. |
530 const int removed_count = tab_entries_.erase(old_tab_id); | 532 const int removed_count = tab_entries_.erase(old_tab_id); |
531 DCHECK_GT(removed_count, 0); | 533 DCHECK_GT(removed_count, 0); |
532 UnregisterForTabNotifications(old_contents); | 534 UnregisterForTabNotifications(old_contents); |
(...skipping 20 matching lines...) Expand all Loading... |
553 void BrowserEventRouter::TabStripEmpty() {} | 555 void BrowserEventRouter::TabStripEmpty() {} |
554 | 556 |
555 void BrowserEventRouter::DispatchOldPageActionEvent( | 557 void BrowserEventRouter::DispatchOldPageActionEvent( |
556 Profile* profile, | 558 Profile* profile, |
557 const std::string& extension_id, | 559 const std::string& extension_id, |
558 const std::string& page_action_id, | 560 const std::string& page_action_id, |
559 int tab_id, | 561 int tab_id, |
560 const std::string& url, | 562 const std::string& url, |
561 int button) { | 563 int button) { |
562 scoped_ptr<base::ListValue> args(new base::ListValue()); | 564 scoped_ptr<base::ListValue> args(new base::ListValue()); |
563 args->Append(Value::CreateStringValue(page_action_id)); | 565 args->Append(new base::StringValue(page_action_id)); |
564 | 566 |
565 DictionaryValue* data = new DictionaryValue(); | 567 DictionaryValue* data = new DictionaryValue(); |
566 data->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id)); | 568 data->Set(tab_keys::kTabIdKey, new base::FundamentalValue(tab_id)); |
567 data->Set(tab_keys::kTabUrlKey, Value::CreateStringValue(url)); | 569 data->Set(tab_keys::kTabUrlKey, new base::StringValue(url)); |
568 data->Set(page_actions_keys::kButtonKey, | 570 data->Set(page_actions_keys::kButtonKey, |
569 Value::CreateIntegerValue(button)); | 571 new base::FundamentalValue(button)); |
570 args->Append(data); | 572 args->Append(data); |
571 | 573 |
572 DispatchEventToExtension(profile, extension_id, "pageActions", args.Pass(), | 574 DispatchEventToExtension(profile, extension_id, "pageActions", args.Pass(), |
573 EventRouter::USER_GESTURE_ENABLED); | 575 EventRouter::USER_GESTURE_ENABLED); |
574 } | 576 } |
575 | 577 |
576 void BrowserEventRouter::BrowserActionExecuted( | 578 void BrowserEventRouter::BrowserActionExecuted( |
577 const ExtensionAction& browser_action, | 579 const ExtensionAction& browser_action, |
578 Browser* browser) { | 580 Browser* browser) { |
579 Profile* profile = browser->profile(); | 581 Profile* profile = browser->profile(); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
639 | 641 |
640 DispatchEventToExtension(profile, | 642 DispatchEventToExtension(profile, |
641 extension_action.extension_id(), | 643 extension_action.extension_id(), |
642 event_name, | 644 event_name, |
643 args.Pass(), | 645 args.Pass(), |
644 EventRouter::USER_GESTURE_ENABLED); | 646 EventRouter::USER_GESTURE_ENABLED); |
645 } | 647 } |
646 } | 648 } |
647 | 649 |
648 } // namespace extensions | 650 } // namespace extensions |
OLD | NEW |