OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/extension_browser_event_router.h" | 5 #include "chrome/browser/extensions/extension_browser_event_router.h" |
6 | 6 |
7 #include "base/json_writer.h" | 7 #include "base/json_writer.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/browser.h" | 9 #include "chrome/browser/browser.h" |
10 #include "chrome/browser/profile.h" | 10 #include "chrome/browser/profile.h" |
11 #include "chrome/browser/extensions/extension.h" | 11 #include "chrome/browser/extensions/extension.h" |
12 #include "chrome/browser/extensions/extension_message_service.h" | 12 #include "chrome/browser/extensions/extension_message_service.h" |
13 #include "chrome/browser/extensions/extension_tabs_module.h" | 13 #include "chrome/browser/extensions/extension_tabs_module.h" |
14 #include "chrome/common/notification_service.h" | 14 #include "chrome/common/notification_service.h" |
15 | 15 |
| 16 const char* kOnTabCreated = "tab-created"; |
16 const char* kOnTabMoved = "tab-moved"; | 17 const char* kOnTabMoved = "tab-moved"; |
| 18 const char* kOnTabSelectionChanged = "tab-selection-changed"; |
| 19 const char* kOnTabAttached = "tab-attached"; |
| 20 const char* kOnTabDetached = "tab-detached"; |
| 21 const char* kOnTabRemoved = "tab-removed"; |
17 | 22 |
18 ExtensionBrowserEventRouter* ExtensionBrowserEventRouter::GetInstance() { | 23 ExtensionBrowserEventRouter* ExtensionBrowserEventRouter::GetInstance() { |
19 return Singleton<ExtensionBrowserEventRouter>::get(); | 24 return Singleton<ExtensionBrowserEventRouter>::get(); |
20 } | 25 } |
21 | 26 |
| 27 static void DispatchEvent(Profile *profile, |
| 28 const char* event_name, |
| 29 const std::string json_args) { |
| 30 ExtensionMessageService::GetInstance(profile->GetRequestContext())-> |
| 31 DispatchEventToRenderers(event_name, json_args); |
| 32 } |
| 33 |
22 void ExtensionBrowserEventRouter::Init() { | 34 void ExtensionBrowserEventRouter::Init() { |
23 if (initialized_) | 35 if (initialized_) |
24 return; | 36 return; |
25 | 37 |
26 NotificationService::current()->AddObserver(this, | 38 NotificationService::current()->AddObserver(this, |
27 NotificationType::BROWSER_OPENED, NotificationService::AllSources()); | 39 NotificationType::BROWSER_OPENED, NotificationService::AllSources()); |
28 NotificationService::current()->AddObserver(this, | 40 NotificationService::current()->AddObserver(this, |
29 NotificationType::BROWSER_CLOSED, NotificationService::AllSources()); | 41 NotificationType::BROWSER_CLOSED, NotificationService::AllSources()); |
30 | 42 |
31 initialized_ = true; | 43 initialized_ = true; |
(...skipping 17 matching lines...) Expand all Loading... |
49 browser->tabstrip_model()->RemoveObserver(this); | 61 browser->tabstrip_model()->RemoveObserver(this); |
50 break; | 62 break; |
51 default: | 63 default: |
52 NOTREACHED(); | 64 NOTREACHED(); |
53 break; | 65 break; |
54 } | 66 } |
55 } | 67 } |
56 | 68 |
57 void ExtensionBrowserEventRouter::TabInsertedAt(TabContents* contents, | 69 void ExtensionBrowserEventRouter::TabInsertedAt(TabContents* contents, |
58 int index, | 70 int index, |
59 bool foreground) { } | 71 bool foreground) { |
| 72 const char* event_name = kOnTabAttached; |
| 73 // If tab is new, send tab-created event. |
| 74 int tab_id = ExtensionTabUtil::GetTabId(contents); |
| 75 if (tab_ids_.find(tab_id) == tab_ids_.end()) { |
| 76 tab_ids_.insert(tab_id); |
| 77 event_name = kOnTabCreated; |
| 78 } |
| 79 |
| 80 ListValue args; |
| 81 DictionaryValue *object_args = new DictionaryValue(); |
| 82 object_args->Set(L"tabId", Value::CreateIntegerValue(tab_id)); |
| 83 object_args->Set(L"windowId", Value::CreateIntegerValue( |
| 84 ExtensionTabUtil::GetWindowIdOfTab(contents))); |
| 85 object_args->Set(L"index", Value::CreateIntegerValue(index)); |
| 86 args.Append(object_args); |
| 87 |
| 88 std::string json_args; |
| 89 JSONWriter::Write(&args, false, &json_args); |
| 90 |
| 91 DispatchEvent(contents->profile(), event_name, json_args); |
| 92 } |
| 93 |
| 94 void ExtensionBrowserEventRouter::TabDetachedAt(TabContents* contents, |
| 95 int index) { |
| 96 int tab_id = ExtensionTabUtil::GetTabId(contents); |
| 97 if (tab_ids_.find(tab_id) == tab_ids_.end()) { |
| 98 // The tab was removed. Don't send detach event. |
| 99 return; |
| 100 } |
| 101 |
| 102 ListValue args; |
| 103 DictionaryValue *object_args = new DictionaryValue(); |
| 104 object_args->Set(L"tabId", Value::CreateIntegerValue(tab_id)); |
| 105 object_args->Set(L"windowId", Value::CreateIntegerValue( |
| 106 ExtensionTabUtil::GetWindowIdOfTab(contents))); |
| 107 object_args->Set(L"index", Value::CreateIntegerValue(index)); |
| 108 args.Append(object_args); |
| 109 |
| 110 std::string json_args; |
| 111 JSONWriter::Write(&args, false, &json_args); |
| 112 |
| 113 DispatchEvent(contents->profile(), kOnTabDetached, json_args); |
| 114 } |
60 | 115 |
61 void ExtensionBrowserEventRouter::TabClosingAt(TabContents* contents, | 116 void ExtensionBrowserEventRouter::TabClosingAt(TabContents* contents, |
62 int index) { } | 117 int index) { |
| 118 int tab_id = ExtensionTabUtil::GetTabId(contents); |
63 | 119 |
64 void ExtensionBrowserEventRouter::TabDetachedAt(TabContents* contents, | 120 ListValue args; |
65 int index) { } | 121 args.Append(Value::CreateIntegerValue(tab_id)); |
| 122 |
| 123 std::string json_args; |
| 124 JSONWriter::Write(&args, false, &json_args); |
| 125 |
| 126 DispatchEvent(contents->profile(), kOnTabRemoved, json_args); |
| 127 |
| 128 int removed_count = tab_ids_.erase(tab_id); |
| 129 DCHECK(removed_count > 0); |
| 130 } |
66 | 131 |
67 void ExtensionBrowserEventRouter::TabSelectedAt(TabContents* old_contents, | 132 void ExtensionBrowserEventRouter::TabSelectedAt(TabContents* old_contents, |
68 TabContents* new_contents, | 133 TabContents* new_contents, |
69 int index, | 134 int index, |
70 bool user_gesture) { } | 135 bool user_gesture) { |
| 136 ListValue args; |
| 137 DictionaryValue *object_args = new DictionaryValue(); |
| 138 object_args->Set(L"tabId", Value::CreateIntegerValue( |
| 139 ExtensionTabUtil::GetTabId(new_contents))); |
| 140 object_args->Set(L"windowId", Value::CreateIntegerValue( |
| 141 ExtensionTabUtil::GetWindowIdOfTab(new_contents))); |
| 142 object_args->Set(L"index", Value::CreateIntegerValue(index)); |
| 143 args.Append(object_args); |
| 144 |
| 145 std::string json_args; |
| 146 JSONWriter::Write(&args, false, &json_args); |
| 147 |
| 148 DispatchEvent(new_contents->profile(), kOnTabSelectionChanged, json_args); |
| 149 } |
71 | 150 |
72 void ExtensionBrowserEventRouter::TabMoved(TabContents* contents, | 151 void ExtensionBrowserEventRouter::TabMoved(TabContents* contents, |
73 int from_index, | 152 int from_index, |
74 int to_index) { | 153 int to_index) { |
75 Profile *profile = contents->profile(); | |
76 | |
77 ListValue args; | 154 ListValue args; |
78 DictionaryValue *object_args = new DictionaryValue(); | 155 DictionaryValue *object_args = new DictionaryValue(); |
79 | |
80 object_args->Set(L"tabId", Value::CreateIntegerValue( | 156 object_args->Set(L"tabId", Value::CreateIntegerValue( |
81 ExtensionTabUtil::GetTabId(contents))); | 157 ExtensionTabUtil::GetTabId(contents))); |
82 object_args->Set(L"windowId", Value::CreateIntegerValue( | 158 object_args->Set(L"windowId", Value::CreateIntegerValue( |
83 ExtensionTabUtil::GetWindowIdOfTab(contents))); | 159 ExtensionTabUtil::GetWindowIdOfTab(contents))); |
84 object_args->Set(L"fromIndex", Value::CreateIntegerValue(from_index)); | 160 object_args->Set(L"fromIndex", Value::CreateIntegerValue(from_index)); |
85 object_args->Set(L"toIndex", Value::CreateIntegerValue(to_index)); | 161 object_args->Set(L"toIndex", Value::CreateIntegerValue(to_index)); |
86 | |
87 args.Append(object_args); | 162 args.Append(object_args); |
88 | 163 |
89 std::string json_args; | 164 std::string json_args; |
90 JSONWriter::Write(&args, false, &json_args); | 165 JSONWriter::Write(&args, false, &json_args); |
91 | 166 |
92 ExtensionMessageService::GetInstance(profile->GetRequestContext())-> | 167 DispatchEvent(contents->profile(), kOnTabMoved, json_args); |
93 DispatchEventToRenderers(kOnTabMoved, json_args); | |
94 } | 168 } |
95 | 169 |
96 void ExtensionBrowserEventRouter::TabChangedAt(TabContents* contents, | 170 void ExtensionBrowserEventRouter::TabChangedAt(TabContents* contents, |
97 int index, | 171 int index, |
98 bool loading_only) { } | 172 bool loading_only) { } |
99 | 173 |
100 void ExtensionBrowserEventRouter::TabStripEmpty() { } | 174 void ExtensionBrowserEventRouter::TabStripEmpty() { } |
OLD | NEW |