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/extensions/api/extension_action/extension_page_actions_
api_constants.h" | 9 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api_constants.h" |
10 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 10 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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/remove event. |
221 return; | 221 return; |
222 } | 222 } |
223 | 223 |
224 scoped_ptr<ListValue> args(new ListValue()); | 224 // If this detach is occuring as part of the process of closing |contents| |
225 args->Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); | 225 // then dispatch a remove event rather than a detach event. |
226 | 226 if (contents->GetWebContentsDetachedToClose()) { |
227 DictionaryValue* object_args = new DictionaryValue(); | 227 int tab_id = ExtensionTabUtil::GetTabId(contents); |
228 object_args->Set(tab_keys::kOldWindowIdKey, Value::CreateIntegerValue( | 228 // TODO BEFORE COMITTING: pass tab_strip_model->closing_all() as last |
229 ExtensionTabUtil::GetWindowIdOfTab(contents))); | 229 // parameter. |
230 object_args->Set(tab_keys::kOldPositionKey, Value::CreateIntegerValue( | 230 DispatchTabRemovedAndUnregisterNotifications(contents, tab_id, false); |
231 index)); | 231 } else { |
232 args->Append(object_args); | 232 DispatchTabDetached(contents, index); |
233 | 233 } |
234 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | |
235 DispatchEvent(profile, events::kOnTabDetached, args.Pass(), | |
236 EventRouter::USER_GESTURE_UNKNOWN); | |
237 } | 234 } |
238 | 235 |
239 void BrowserEventRouter::TabClosingAt(TabStripModel* tab_strip_model, | 236 void BrowserEventRouter::TabClosingAt(TabStripModel* tab_strip_model, |
240 WebContents* contents, | 237 WebContents* contents, |
241 int index) { | 238 int index) { |
242 int tab_id = ExtensionTabUtil::GetTabId(contents); | 239 int tab_id = ExtensionTabUtil::GetTabId(contents); |
243 | 240 DispatchTabRemovedAndUnregisterNotifications(contents, tab_id, |
244 scoped_ptr<ListValue> args(new ListValue()); | 241 tab_strip_model->closing_all()); |
245 args->Append(Value::CreateIntegerValue(tab_id)); | |
246 | |
247 DictionaryValue* object_args = new DictionaryValue(); | |
248 object_args->SetInteger(tab_keys::kWindowIdKey, | |
249 ExtensionTabUtil::GetWindowIdOfTab(contents)); | |
250 object_args->SetBoolean(tab_keys::kWindowClosing, | |
251 tab_strip_model->closing_all()); | |
252 args->Append(object_args); | |
253 | |
254 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | |
255 DispatchEvent(profile, events::kOnTabRemoved, args.Pass(), | |
256 EventRouter::USER_GESTURE_UNKNOWN); | |
257 | |
258 int removed_count = tab_entries_.erase(tab_id); | |
259 DCHECK_GT(removed_count, 0); | |
260 | |
261 UnregisterForTabNotifications(contents); | |
262 } | 242 } |
263 | 243 |
264 void BrowserEventRouter::ActiveTabChanged(WebContents* old_contents, | 244 void BrowserEventRouter::ActiveTabChanged(WebContents* old_contents, |
265 WebContents* new_contents, | 245 WebContents* new_contents, |
266 int index, | 246 int index, |
267 int reason) { | 247 int reason) { |
268 scoped_ptr<ListValue> args(new ListValue()); | 248 scoped_ptr<ListValue> args(new ListValue()); |
269 int tab_id = ExtensionTabUtil::GetTabId(new_contents); | 249 int tab_id = ExtensionTabUtil::GetTabId(new_contents); |
270 args->Append(Value::CreateIntegerValue(tab_id)); | 250 args->Append(Value::CreateIntegerValue(tab_id)); |
271 | 251 |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 | 451 |
472 BrowserEventRouter::TabEntry* BrowserEventRouter::GetTabEntry( | 452 BrowserEventRouter::TabEntry* BrowserEventRouter::GetTabEntry( |
473 const WebContents* contents) { | 453 const WebContents* contents) { |
474 int tab_id = ExtensionTabUtil::GetTabId(contents); | 454 int tab_id = ExtensionTabUtil::GetTabId(contents); |
475 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); | 455 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); |
476 if (tab_entries_.end() == i) | 456 if (tab_entries_.end() == i) |
477 return NULL; | 457 return NULL; |
478 return &i->second; | 458 return &i->second; |
479 } | 459 } |
480 | 460 |
| 461 void BrowserEventRouter::DispatchTabDetached(WebContents* contents, |
| 462 int index) { |
| 463 scoped_ptr<ListValue> args(new ListValue()); |
| 464 args->Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); |
| 465 |
| 466 DictionaryValue* object_args = new DictionaryValue(); |
| 467 object_args->Set(tab_keys::kOldWindowIdKey, Value::CreateIntegerValue( |
| 468 ExtensionTabUtil::GetWindowIdOfTab(contents))); |
| 469 object_args->Set(tab_keys::kOldPositionKey, Value::CreateIntegerValue( |
| 470 index)); |
| 471 args->Append(object_args); |
| 472 |
| 473 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 474 DispatchEvent(profile, events::kOnTabDetached, args.Pass(), |
| 475 EventRouter::USER_GESTURE_UNKNOWN); |
| 476 } |
| 477 |
| 478 void BrowserEventRouter::DispatchTabRemovedAndUnregisterNotifications( |
| 479 WebContents* contents, int tab_id, bool window_closing) { |
| 480 scoped_ptr<ListValue> args(new ListValue()); |
| 481 args->Append(Value::CreateIntegerValue(tab_id)); |
| 482 |
| 483 DictionaryValue* object_args = new DictionaryValue(); |
| 484 object_args->SetInteger(tab_keys::kWindowIdKey, |
| 485 ExtensionTabUtil::GetWindowIdOfTab(contents)); |
| 486 object_args->SetBoolean(tab_keys::kWindowClosing, |
| 487 window_closing); |
| 488 args->Append(object_args); |
| 489 |
| 490 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 491 DispatchEvent(profile, events::kOnTabRemoved, args.Pass(), |
| 492 EventRouter::USER_GESTURE_UNKNOWN); |
| 493 |
| 494 int removed_count = tab_entries_.erase(tab_id); |
| 495 DCHECK_GT(removed_count, 0); |
| 496 UnregisterForTabNotifications(contents); |
| 497 } |
| 498 |
481 void BrowserEventRouter::Observe(int type, | 499 void BrowserEventRouter::Observe(int type, |
482 const content::NotificationSource& source, | 500 const content::NotificationSource& source, |
483 const content::NotificationDetails& details) { | 501 const content::NotificationDetails& details) { |
484 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { | 502 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { |
485 NavigationController* source_controller = | 503 NavigationController* source_controller = |
486 content::Source<NavigationController>(source).ptr(); | 504 content::Source<NavigationController>(source).ptr(); |
487 TabUpdated(source_controller->GetWebContents(), true); | 505 TabUpdated(source_controller->GetWebContents(), true); |
488 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { | 506 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { |
489 // Tab was destroyed after being detached (without being re-attached). | 507 // Tab was destroyed after being detached (without being re-attached). |
490 WebContents* contents = content::Source<WebContents>(source).ptr(); | 508 WebContents* contents = content::Source<WebContents>(source).ptr(); |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
639 | 657 |
640 DispatchEventToExtension(profile, | 658 DispatchEventToExtension(profile, |
641 extension_action.extension_id(), | 659 extension_action.extension_id(), |
642 event_name, | 660 event_name, |
643 args.Pass(), | 661 args.Pass(), |
644 EventRouter::USER_GESTURE_ENABLED); | 662 EventRouter::USER_GESTURE_ENABLED); |
645 } | 663 } |
646 } | 664 } |
647 | 665 |
648 } // namespace extensions | 666 } // namespace extensions |
OLD | NEW |