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/script_badge_controller.h" | 5 #include "chrome/browser/extensions/script_badge_controller.h" |
6 | 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/string_util.h" |
| 9 #include "base/stringprintf.h" |
7 #include "chrome/browser/extensions/browser_event_router.h" | 10 #include "chrome/browser/extensions/browser_event_router.h" |
8 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/extensions/extension_system.h" | 12 #include "chrome/browser/extensions/extension_system.h" |
10 #include "chrome/browser/extensions/tab_helper.h" | 13 #include "chrome/browser/extensions/tab_helper.h" |
11 #include "chrome/browser/sessions/session_id.h" | 14 #include "chrome/browser/sessions/session_id.h" |
12 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 15 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
13 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
14 #include "chrome/common/extensions/extension_action.h" | 17 #include "chrome/common/extensions/extension_action.h" |
15 #include "chrome/common/extensions/extension_messages.h" | 18 #include "chrome/common/extensions/extension_messages.h" |
16 #include "chrome/common/extensions/extension_set.h" | 19 #include "chrome/common/extensions/extension_set.h" |
17 #include "chrome/common/chrome_notification_types.h" | 20 #include "chrome/common/chrome_notification_types.h" |
18 #include "content/public/browser/navigation_controller.h" | 21 #include "content/public/browser/navigation_controller.h" |
19 #include "content/public/browser/navigation_details.h" | 22 #include "content/public/browser/navigation_details.h" |
20 #include "content/public/browser/navigation_entry.h" | 23 #include "content/public/browser/navigation_entry.h" |
21 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
22 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 26 #include "googleurl/src/gurl.h" |
23 #include "ipc/ipc_message.h" | 27 #include "ipc/ipc_message.h" |
24 #include "ipc/ipc_message_macros.h" | 28 #include "ipc/ipc_message_macros.h" |
25 | 29 |
26 namespace extensions { | 30 namespace extensions { |
27 | 31 |
28 ScriptBadgeController::ScriptBadgeController(TabContents* tab_contents, | 32 ScriptBadgeController::ScriptBadgeController(TabContents* tab_contents, |
29 ScriptExecutor* script_executor) | 33 ScriptExecutor* script_executor) |
30 : ScriptExecutor::Observer(script_executor), | 34 : ScriptExecutor::Observer(script_executor), |
31 content::WebContentsObserver(tab_contents->web_contents()), | 35 content::WebContentsObserver(tab_contents->web_contents()), |
32 tab_contents_(tab_contents) { | 36 tab_contents_(tab_contents) { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 | 100 |
97 return extension->ShowConfigureContextMenus() ? | 101 return extension->ShowConfigureContextMenus() ? |
98 ACTION_SHOW_CONTEXT_MENU : ACTION_NONE; | 102 ACTION_SHOW_CONTEXT_MENU : ACTION_NONE; |
99 } | 103 } |
100 | 104 |
101 return ACTION_NONE; | 105 return ACTION_NONE; |
102 } | 106 } |
103 | 107 |
104 void ScriptBadgeController::OnExecuteScriptFinished( | 108 void ScriptBadgeController::OnExecuteScriptFinished( |
105 const std::string& extension_id, | 109 const std::string& extension_id, |
106 bool success, | |
107 int32 page_id, | |
108 const std::string& error, | 110 const std::string& error, |
| 111 int32 on_page_id, |
| 112 const GURL& on_url, |
109 const base::ListValue& script_results) { | 113 const base::ListValue& script_results) { |
110 if (success && page_id == GetPageID()) { | 114 int32 current_page_id = GetPageID(); |
| 115 |
| 116 // Tracking down http://crbug.com/138323. |
| 117 if (current_page_id < 0) { |
| 118 std::string message = base::StringPrintf( |
| 119 "Expected a page ID of %d on URL \"%s\", but there was no navigation " |
| 120 "entry. Current URL is \"%s\", extension ID is %s.", |
| 121 on_page_id, |
| 122 on_url.spec().c_str(), |
| 123 tab_contents_->web_contents()->GetURL().spec().c_str(), |
| 124 extension_id.c_str()); |
| 125 char buf[1024]; |
| 126 base::snprintf(buf, arraysize(buf), "%s", message.c_str()); |
| 127 CHECK(false) << message; |
| 128 } |
| 129 |
| 130 if (error.empty() && on_page_id == current_page_id) { |
111 if (MarkExtensionExecuting(extension_id)) | 131 if (MarkExtensionExecuting(extension_id)) |
112 NotifyChange(); | 132 NotifyChange(); |
113 } | 133 } |
114 } | 134 } |
115 | 135 |
116 ExtensionService* ScriptBadgeController::GetExtensionService() { | 136 ExtensionService* ScriptBadgeController::GetExtensionService() { |
117 return extensions::ExtensionSystem::Get( | 137 return extensions::ExtensionSystem::Get( |
118 tab_contents_->profile())->extension_service(); | 138 tab_contents_->profile())->extension_service(); |
119 } | 139 } |
120 | 140 |
121 int32 ScriptBadgeController::GetPageID() { | 141 int32 ScriptBadgeController::GetPageID() { |
122 return tab_contents_->web_contents()->GetController().GetActiveEntry()-> | 142 content::NavigationEntry* nav_entry = |
123 GetPageID(); | 143 tab_contents_->web_contents()->GetController().GetActiveEntry(); |
| 144 return nav_entry ? nav_entry->GetPageID() : -1; |
124 } | 145 } |
125 | 146 |
126 void ScriptBadgeController::NotifyChange() { | 147 void ScriptBadgeController::NotifyChange() { |
127 content::NotificationService::current()->Notify( | 148 content::NotificationService::current()->Notify( |
128 chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED, | 149 chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED, |
129 content::Source<Profile>(tab_contents_->profile()), | 150 content::Source<Profile>(tab_contents_->profile()), |
130 content::Details<TabContents>(tab_contents_)); | 151 content::Details<TabContents>(tab_contents_)); |
131 } | 152 } |
132 | 153 |
133 void ScriptBadgeController::DidNavigateMainFrame( | 154 void ScriptBadgeController::DidNavigateMainFrame( |
(...skipping 19 matching lines...) Expand all Loading... |
153 bool ScriptBadgeController::OnMessageReceived(const IPC::Message& message) { | 174 bool ScriptBadgeController::OnMessageReceived(const IPC::Message& message) { |
154 bool handled = true; | 175 bool handled = true; |
155 IPC_BEGIN_MESSAGE_MAP(ScriptBadgeController, message) | 176 IPC_BEGIN_MESSAGE_MAP(ScriptBadgeController, message) |
156 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ContentScriptsExecuting, | 177 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ContentScriptsExecuting, |
157 OnContentScriptsExecuting) | 178 OnContentScriptsExecuting) |
158 IPC_MESSAGE_UNHANDLED(handled = false) | 179 IPC_MESSAGE_UNHANDLED(handled = false) |
159 IPC_END_MESSAGE_MAP() | 180 IPC_END_MESSAGE_MAP() |
160 return handled; | 181 return handled; |
161 } | 182 } |
162 | 183 |
| 184 namespace { |
| 185 std::string JoinExtensionIDs(const std::set<std::string>& ids) { |
| 186 std::vector<std::string> as_vector(ids.begin(), ids.end()); |
| 187 return "[" + JoinString(as_vector, ',') + "]"; |
| 188 } |
| 189 } // namespace |
| 190 |
163 void ScriptBadgeController::OnContentScriptsExecuting( | 191 void ScriptBadgeController::OnContentScriptsExecuting( |
164 const std::set<std::string>& extension_ids, int32 page_id) { | 192 const std::set<std::string>& extension_ids, |
165 if (page_id != GetPageID()) | 193 int32 on_page_id, |
| 194 const GURL& on_url) { |
| 195 int32 current_page_id = GetPageID(); |
| 196 |
| 197 // Tracking down http://crbug.com/138323. |
| 198 if (current_page_id < 0) { |
| 199 std::string message = base::StringPrintf( |
| 200 "Expected a page ID of %d on URL \"%s\", but there was no navigation " |
| 201 "entry. Current URL is \"%s\", extension IDs are %s.", |
| 202 on_page_id, |
| 203 on_url.spec().c_str(), |
| 204 tab_contents_->web_contents()->GetURL().spec().c_str(), |
| 205 JoinExtensionIDs(extension_ids).c_str()); |
| 206 char buf[1024]; |
| 207 base::snprintf(buf, arraysize(buf), "%s", message.c_str()); |
| 208 CHECK(false) << message; |
| 209 } |
| 210 |
| 211 if (on_page_id != current_page_id) |
166 return; | 212 return; |
167 | 213 |
168 bool changed = false; | 214 bool changed = false; |
169 for (std::set<std::string>::const_iterator it = extension_ids.begin(); | 215 for (std::set<std::string>::const_iterator it = extension_ids.begin(); |
170 it != extension_ids.end(); ++it) { | 216 it != extension_ids.end(); ++it) { |
171 changed |= MarkExtensionExecuting(*it); | 217 changed |= MarkExtensionExecuting(*it); |
172 } | 218 } |
173 if (changed) | 219 if (changed) |
174 NotifyChange(); | 220 NotifyChange(); |
175 } | 221 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 current_actions_.erase(it); | 267 current_actions_.erase(it); |
222 break; | 268 break; |
223 } | 269 } |
224 } | 270 } |
225 | 271 |
226 CHECK_EQ(size_before, current_actions_.size() + 1); | 272 CHECK_EQ(size_before, current_actions_.size() + 1); |
227 return true; | 273 return true; |
228 } | 274 } |
229 | 275 |
230 } // namespace extensions | 276 } // namespace extensions |
OLD | NEW |