Chromium Code Reviews| 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/extension_browser_event_router.h" | 5 #include "chrome/browser/extensions/extension_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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 | 165 |
| 166 void ExtensionBrowserEventRouter::UnregisterForTabNotifications( | 166 void ExtensionBrowserEventRouter::UnregisterForTabNotifications( |
| 167 WebContents* contents) { | 167 WebContents* contents) { |
| 168 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 168 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 169 content::Source<NavigationController>(&contents->GetController())); | 169 content::Source<NavigationController>(&contents->GetController())); |
| 170 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 170 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 171 content::Source<WebContents>(contents)); | 171 content::Source<WebContents>(contents)); |
| 172 } | 172 } |
| 173 | 173 |
| 174 void ExtensionBrowserEventRouter::OnBrowserWindowReady(Browser* browser) { | 174 void ExtensionBrowserEventRouter::OnBrowserWindowReady(Browser* browser) { |
| 175 ListValue args; | 175 ListValue* args = new ListValue(); |
| 176 | 176 |
| 177 DCHECK(browser->extension_window_controller()); | 177 DCHECK(browser->extension_window_controller()); |
| 178 DictionaryValue* window_dictionary = | 178 DictionaryValue* window_dictionary = |
| 179 browser->extension_window_controller()->CreateWindowValue(); | 179 browser->extension_window_controller()->CreateWindowValue(); |
| 180 args.Append(window_dictionary); | 180 args->Append(window_dictionary); |
| 181 | 181 |
| 182 std::string json_args; | 182 DispatchEvent(browser->profile(), events::kOnWindowCreated, args); |
| 183 base::JSONWriter::Write(&args, &json_args); | |
| 184 | |
| 185 DispatchEvent(browser->profile(), events::kOnWindowCreated, json_args); | |
| 186 } | 183 } |
| 187 | 184 |
| 188 void ExtensionBrowserEventRouter::OnBrowserRemoved(Browser* browser) { | 185 void ExtensionBrowserEventRouter::OnBrowserRemoved(Browser* browser) { |
| 189 if (!profile_->IsSameProfile(browser->profile())) | 186 if (!profile_->IsSameProfile(browser->profile())) |
| 190 return; | 187 return; |
| 191 | 188 |
| 192 // Stop listening to TabStripModel events for this browser. | 189 // Stop listening to TabStripModel events for this browser. |
| 193 browser->tab_strip_model()->RemoveObserver(this); | 190 browser->tab_strip_model()->RemoveObserver(this); |
| 194 | 191 |
| 195 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY, | 192 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY, |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 226 | 223 |
| 227 if (focused_window_id_ == window_id) | 224 if (focused_window_id_ == window_id) |
| 228 return; | 225 return; |
| 229 | 226 |
| 230 // window_profile is either this profile's default profile, its | 227 // window_profile is either this profile's default profile, its |
| 231 // incognito profile, or NULL iff this profile is losing focus. | 228 // incognito profile, or NULL iff this profile is losing focus. |
| 232 Profile* previous_focused_profile = focused_profile_; | 229 Profile* previous_focused_profile = focused_profile_; |
| 233 focused_profile_ = window_profile; | 230 focused_profile_ = window_profile; |
| 234 focused_window_id_ = window_id; | 231 focused_window_id_ = window_id; |
| 235 | 232 |
| 236 ListValue real_args; | 233 ListValue* real_args = new ListValue(); |
| 237 real_args.Append(Value::CreateIntegerValue(window_id)); | 234 real_args->Append(Value::CreateIntegerValue(window_id)); |
| 238 std::string real_json_args; | |
| 239 base::JSONWriter::Write(&real_args, &real_json_args); | |
| 240 | 235 |
| 241 // When switching between windows in the default and incognitoi profiles, | 236 // When switching between windows in the default and incognitoi profiles, |
| 242 // dispatch WINDOW_ID_NONE to extensions whose profile lost focus that | 237 // dispatch WINDOW_ID_NONE to extensions whose profile lost focus that |
| 243 // can't see the new focused window across the incognito boundary. | 238 // can't see the new focused window across the incognito boundary. |
| 244 // See crbug.com/46610. | 239 // See crbug.com/46610. |
| 245 std::string none_json_args; | 240 ListValue* none_args = new ListValue(); |
| 246 if (focused_profile_ != NULL && previous_focused_profile != NULL && | 241 if (focused_profile_ != NULL && previous_focused_profile != NULL && |
| 247 focused_profile_ != previous_focused_profile) { | 242 focused_profile_ != previous_focused_profile) { |
| 248 ListValue none_args; | 243 none_args->Append( |
| 249 none_args.Append( | |
| 250 Value::CreateIntegerValue(extension_misc::kUnknownWindowId)); | 244 Value::CreateIntegerValue(extension_misc::kUnknownWindowId)); |
| 251 base::JSONWriter::Write(&none_args, &none_json_args); | |
| 252 } | 245 } |
| 253 | 246 |
| 254 DispatchEventsAcrossIncognito((focused_profile_ ? focused_profile_ : | 247 DispatchEventsAcrossIncognito((focused_profile_ ? focused_profile_ : |
| 255 previous_focused_profile), | 248 previous_focused_profile), |
| 256 events::kOnWindowFocusedChanged, | 249 events::kOnWindowFocusedChanged, |
| 257 real_json_args, | 250 real_args, |
| 258 none_json_args); | 251 none_args); |
| 259 } | 252 } |
| 260 | 253 |
| 261 void ExtensionBrowserEventRouter::TabCreatedAt(WebContents* contents, | 254 void ExtensionBrowserEventRouter::TabCreatedAt(WebContents* contents, |
| 262 int index, | 255 int index, |
| 263 bool active) { | 256 bool active) { |
| 264 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 257 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 265 DispatchEventWithTab(profile, "", events::kOnTabCreated, contents, active); | 258 DispatchEventWithTab(profile, "", events::kOnTabCreated, contents, active); |
| 266 | 259 |
| 267 RegisterForTabNotifications(contents); | 260 RegisterForTabNotifications(contents); |
| 268 } | 261 } |
| 269 | 262 |
| 270 void ExtensionBrowserEventRouter::TabInsertedAt(TabContents* contents, | 263 void ExtensionBrowserEventRouter::TabInsertedAt(TabContents* contents, |
| 271 int index, | 264 int index, |
| 272 bool active) { | 265 bool active) { |
| 273 // If tab is new, send created event. | 266 // If tab is new, send created event. |
| 274 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); | 267 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); |
| 275 if (!GetTabEntry(contents->web_contents())) { | 268 if (!GetTabEntry(contents->web_contents())) { |
| 276 tab_entries_[tab_id] = TabEntry(); | 269 tab_entries_[tab_id] = TabEntry(); |
| 277 | 270 |
| 278 TabCreatedAt(contents->web_contents(), index, active); | 271 TabCreatedAt(contents->web_contents(), index, active); |
| 279 return; | 272 return; |
| 280 } | 273 } |
| 281 | 274 |
| 282 ListValue args; | 275 ListValue* args = new ListValue(); |
| 283 args.Append(Value::CreateIntegerValue(tab_id)); | 276 args->Append(Value::CreateIntegerValue(tab_id)); |
| 284 | 277 |
| 285 DictionaryValue* object_args = new DictionaryValue(); | 278 DictionaryValue* object_args = new DictionaryValue(); |
| 286 object_args->Set(tab_keys::kNewWindowIdKey, Value::CreateIntegerValue( | 279 object_args->Set(tab_keys::kNewWindowIdKey, Value::CreateIntegerValue( |
| 287 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); | 280 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); |
| 288 object_args->Set(tab_keys::kNewPositionKey, Value::CreateIntegerValue( | 281 object_args->Set(tab_keys::kNewPositionKey, Value::CreateIntegerValue( |
| 289 index)); | 282 index)); |
| 290 args.Append(object_args); | 283 args->Append(object_args); |
| 291 | 284 |
| 292 std::string json_args; | 285 DispatchEvent(contents->profile(), events::kOnTabAttached, args); |
| 293 base::JSONWriter::Write(&args, &json_args); | |
| 294 | |
| 295 DispatchEvent(contents->profile(), events::kOnTabAttached, json_args); | |
| 296 } | 286 } |
| 297 | 287 |
| 298 void ExtensionBrowserEventRouter::TabDetachedAt(TabContents* contents, | 288 void ExtensionBrowserEventRouter::TabDetachedAt(TabContents* contents, |
| 299 int index) { | 289 int index) { |
| 300 if (!GetTabEntry(contents->web_contents())) { | 290 if (!GetTabEntry(contents->web_contents())) { |
| 301 // The tab was removed. Don't send detach event. | 291 // The tab was removed. Don't send detach event. |
| 302 return; | 292 return; |
| 303 } | 293 } |
| 304 | 294 |
| 305 ListValue args; | 295 ListValue* args = new ListValue(); |
| 306 args.Append(Value::CreateIntegerValue( | 296 args->Append(Value::CreateIntegerValue( |
| 307 ExtensionTabUtil::GetTabId(contents->web_contents()))); | 297 ExtensionTabUtil::GetTabId(contents->web_contents()))); |
| 308 | 298 |
| 309 DictionaryValue* object_args = new DictionaryValue(); | 299 DictionaryValue* object_args = new DictionaryValue(); |
| 310 object_args->Set(tab_keys::kOldWindowIdKey, Value::CreateIntegerValue( | 300 object_args->Set(tab_keys::kOldWindowIdKey, Value::CreateIntegerValue( |
| 311 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); | 301 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); |
| 312 object_args->Set(tab_keys::kOldPositionKey, Value::CreateIntegerValue( | 302 object_args->Set(tab_keys::kOldPositionKey, Value::CreateIntegerValue( |
| 313 index)); | 303 index)); |
| 314 args.Append(object_args); | 304 args->Append(object_args); |
| 315 | 305 |
| 316 std::string json_args; | 306 DispatchEvent(contents->profile(), events::kOnTabDetached, args); |
| 317 base::JSONWriter::Write(&args, &json_args); | |
| 318 | |
| 319 DispatchEvent(contents->profile(), events::kOnTabDetached, json_args); | |
| 320 } | 307 } |
| 321 | 308 |
| 322 void ExtensionBrowserEventRouter::TabClosingAt(TabStripModel* tab_strip_model, | 309 void ExtensionBrowserEventRouter::TabClosingAt(TabStripModel* tab_strip_model, |
| 323 TabContents* contents, | 310 TabContents* contents, |
| 324 int index) { | 311 int index) { |
| 325 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); | 312 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); |
| 326 | 313 |
| 327 ListValue args; | 314 ListValue* args = new ListValue(); |
| 328 args.Append(Value::CreateIntegerValue(tab_id)); | 315 args->Append(Value::CreateIntegerValue(tab_id)); |
| 329 | 316 |
| 330 DictionaryValue* object_args = new DictionaryValue(); | 317 DictionaryValue* object_args = new DictionaryValue(); |
| 331 object_args->SetBoolean(tab_keys::kWindowClosing, | 318 object_args->SetBoolean(tab_keys::kWindowClosing, |
| 332 tab_strip_model->closing_all()); | 319 tab_strip_model->closing_all()); |
| 333 args.Append(object_args); | 320 args->Append(object_args); |
| 334 | 321 |
| 335 std::string json_args; | 322 DispatchEvent(contents->profile(), events::kOnTabRemoved, args); |
| 336 base::JSONWriter::Write(&args, &json_args); | |
| 337 | |
| 338 DispatchEvent(contents->profile(), events::kOnTabRemoved, json_args); | |
| 339 | 323 |
| 340 int removed_count = tab_entries_.erase(tab_id); | 324 int removed_count = tab_entries_.erase(tab_id); |
| 341 DCHECK_GT(removed_count, 0); | 325 DCHECK_GT(removed_count, 0); |
| 342 | 326 |
| 343 UnregisterForTabNotifications(contents->web_contents()); | 327 UnregisterForTabNotifications(contents->web_contents()); |
| 344 } | 328 } |
| 345 | 329 |
| 346 void ExtensionBrowserEventRouter::ActiveTabChanged( | 330 void ExtensionBrowserEventRouter::ActiveTabChanged( |
| 347 TabContents* old_contents, | 331 TabContents* old_contents, |
| 348 TabContents* new_contents, | 332 TabContents* new_contents, |
| 349 int index, | 333 int index, |
| 350 bool user_gesture) { | 334 bool user_gesture) { |
| 351 ListValue args; | 335 ListValue* args = new ListValue(); |
| 352 int tab_id = ExtensionTabUtil::GetTabId(new_contents->web_contents()); | 336 int tab_id = ExtensionTabUtil::GetTabId(new_contents->web_contents()); |
| 353 args.Append(Value::CreateIntegerValue(tab_id)); | 337 args->Append(Value::CreateIntegerValue(tab_id)); |
| 354 | 338 |
| 355 DictionaryValue* object_args = new DictionaryValue(); | 339 DictionaryValue* object_args = new DictionaryValue(); |
| 356 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( | 340 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( |
| 357 ExtensionTabUtil::GetWindowIdOfTab(new_contents->web_contents()))); | 341 ExtensionTabUtil::GetWindowIdOfTab(new_contents->web_contents()))); |
| 358 args.Append(object_args); | 342 args->Append(object_args); |
| 359 | 343 |
| 360 // The onActivated event replaced onActiveChanged and onSelectionChanged. The | 344 // The onActivated event replaced onActiveChanged and onSelectionChanged. The |
| 361 // deprecated events take two arguments: tabId, {windowId}. | 345 // deprecated events take two arguments: tabId, {windowId}. |
| 362 std::string old_json_args; | 346 Profile* profile = new_contents->profile(); |
| 363 base::JSONWriter::Write(&args, &old_json_args); | 347 DispatchEvent(profile, events::kOnTabSelectionChanged, args->DeepCopy()); |
| 348 DispatchEvent(profile, events::kOnTabActiveChanged, args->DeepCopy()); | |
| 364 | 349 |
| 365 // The onActivated event takes one argument: {windowId, tabId}. | 350 // The onActivated event takes one argument: {windowId, tabId}. |
| 366 std::string new_json_args; | 351 args->Remove(0, NULL); |
| 367 args.Remove(0, NULL); | 352 DispatchEvent(profile, events::kOnTabActivated, args); |
| 368 object_args->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id)); | |
| 369 base::JSONWriter::Write(&args, &new_json_args); | |
| 370 | |
| 371 Profile* profile = new_contents->profile(); | |
| 372 DispatchEvent(profile, events::kOnTabSelectionChanged, old_json_args); | |
| 373 DispatchEvent(profile, events::kOnTabActiveChanged, old_json_args); | |
| 374 DispatchEvent(profile, events::kOnTabActivated, new_json_args); | |
| 375 } | 353 } |
| 376 | 354 |
| 377 void ExtensionBrowserEventRouter::TabSelectionChanged( | 355 void ExtensionBrowserEventRouter::TabSelectionChanged( |
| 378 TabStripModel* tab_strip_model, | 356 TabStripModel* tab_strip_model, |
| 379 const TabStripSelectionModel& old_model) { | 357 const TabStripSelectionModel& old_model) { |
| 380 TabStripSelectionModel::SelectedIndices new_selection = | 358 TabStripSelectionModel::SelectedIndices new_selection = |
| 381 tab_strip_model->selection_model().selected_indices(); | 359 tab_strip_model->selection_model().selected_indices(); |
| 382 ListValue* all = new ListValue(); | 360 ListValue* all = new ListValue(); |
| 383 | 361 |
| 384 for (size_t i = 0; i < new_selection.size(); ++i) { | 362 for (size_t i = 0; i < new_selection.size(); ++i) { |
| 385 int index = new_selection[i]; | 363 int index = new_selection[i]; |
| 386 TabContents* contents = tab_strip_model->GetTabContentsAt(index); | 364 TabContents* contents = tab_strip_model->GetTabContentsAt(index); |
| 387 if (!contents) | 365 if (!contents) |
| 388 break; | 366 break; |
| 389 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); | 367 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); |
| 390 all->Append(Value::CreateIntegerValue(tab_id)); | 368 all->Append(Value::CreateIntegerValue(tab_id)); |
| 391 } | 369 } |
| 392 | 370 |
| 393 ListValue args; | 371 ListValue* args = new ListValue(); |
| 394 DictionaryValue* select_info = new DictionaryValue(); | 372 DictionaryValue* select_info = new DictionaryValue(); |
| 395 | 373 |
| 396 select_info->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( | 374 select_info->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( |
| 397 ExtensionTabUtil::GetWindowIdOfTabStripModel(tab_strip_model))); | 375 ExtensionTabUtil::GetWindowIdOfTabStripModel(tab_strip_model))); |
| 398 | 376 |
| 399 select_info->Set(tab_keys::kTabIdsKey, all); | 377 select_info->Set(tab_keys::kTabIdsKey, all); |
| 400 args.Append(select_info); | 378 args->Append(select_info); |
| 401 | |
| 402 std::string json_args; | |
| 403 base::JSONWriter::Write(&args, &json_args); | |
| 404 | 379 |
| 405 // The onHighlighted event replaced onHighlightChanged. | 380 // The onHighlighted event replaced onHighlightChanged. |
| 406 Profile* profile = tab_strip_model->profile(); | 381 Profile* profile = tab_strip_model->profile(); |
| 407 DispatchEvent(profile, events::kOnTabHighlightChanged, json_args); | 382 DispatchEvent(profile, events::kOnTabHighlightChanged, args->DeepCopy()); |
| 408 DispatchEvent(profile, events::kOnTabHighlighted, json_args); | 383 DispatchEvent(profile, events::kOnTabHighlighted, args); |
| 409 } | 384 } |
| 410 | 385 |
| 411 void ExtensionBrowserEventRouter::TabMoved(TabContents* contents, | 386 void ExtensionBrowserEventRouter::TabMoved(TabContents* contents, |
| 412 int from_index, | 387 int from_index, |
| 413 int to_index) { | 388 int to_index) { |
| 414 ListValue args; | 389 ListValue* args = new ListValue(); |
| 415 args.Append(Value::CreateIntegerValue( | 390 args->Append(Value::CreateIntegerValue( |
| 416 ExtensionTabUtil::GetTabId(contents->web_contents()))); | 391 ExtensionTabUtil::GetTabId(contents->web_contents()))); |
| 417 | 392 |
| 418 DictionaryValue* object_args = new DictionaryValue(); | 393 DictionaryValue* object_args = new DictionaryValue(); |
| 419 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( | 394 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( |
| 420 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); | 395 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); |
| 421 object_args->Set(tab_keys::kFromIndexKey, Value::CreateIntegerValue( | 396 object_args->Set(tab_keys::kFromIndexKey, Value::CreateIntegerValue( |
| 422 from_index)); | 397 from_index)); |
| 423 object_args->Set(tab_keys::kToIndexKey, Value::CreateIntegerValue( | 398 object_args->Set(tab_keys::kToIndexKey, Value::CreateIntegerValue( |
| 424 to_index)); | 399 to_index)); |
| 425 args.Append(object_args); | 400 args->Append(object_args); |
| 426 | 401 |
| 427 std::string json_args; | 402 DispatchEvent(contents->profile(), events::kOnTabMoved, args); |
| 428 base::JSONWriter::Write(&args, &json_args); | |
| 429 | |
| 430 DispatchEvent(contents->profile(), events::kOnTabMoved, json_args); | |
| 431 } | 403 } |
| 432 | 404 |
| 433 void ExtensionBrowserEventRouter::TabUpdated(WebContents* contents, | 405 void ExtensionBrowserEventRouter::TabUpdated(WebContents* contents, |
| 434 bool did_navigate) { | 406 bool did_navigate) { |
| 435 TabEntry* entry = GetTabEntry(contents); | 407 TabEntry* entry = GetTabEntry(contents); |
| 436 DictionaryValue* changed_properties = NULL; | 408 DictionaryValue* changed_properties = NULL; |
| 437 | 409 |
| 438 DCHECK(entry); | 410 DCHECK(entry); |
| 439 | 411 |
| 440 if (did_navigate) | 412 if (did_navigate) |
| 441 changed_properties = entry->DidNavigate(contents); | 413 changed_properties = entry->DidNavigate(contents); |
| 442 else | 414 else |
| 443 changed_properties = entry->UpdateLoadState(contents); | 415 changed_properties = entry->UpdateLoadState(contents); |
| 444 | 416 |
| 445 if (changed_properties) | 417 if (changed_properties) |
| 446 DispatchTabUpdatedEvent(contents, changed_properties); | 418 DispatchTabUpdatedEvent(contents, changed_properties); |
| 447 } | 419 } |
| 448 | 420 |
| 449 void ExtensionBrowserEventRouter::DispatchEvent(Profile* profile, | 421 void ExtensionBrowserEventRouter::DispatchEvent(Profile* profile, |
| 450 const char* event_name, | 422 const char* event_name, |
| 451 const std::string& json_args) { | 423 base::ListValue* arguments) { |
| 452 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) | 424 if (!profile_->IsSameProfile(profile) || |
| 425 !profile->GetExtensionEventRouter()) { | |
| 426 delete arguments; | |
| 453 return; | 427 return; |
| 428 } | |
| 454 | 429 |
| 455 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | 430 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 456 event_name, json_args, profile, GURL(), extensions::EventFilteringInfo()); | 431 event_name, arguments, profile, GURL(), extensions::EventFilteringInfo()); |
| 457 } | 432 } |
| 458 | 433 |
| 459 void ExtensionBrowserEventRouter::DispatchEventToExtension( | 434 void ExtensionBrowserEventRouter::DispatchEventToExtension( |
| 460 Profile* profile, | 435 Profile* profile, |
| 461 const std::string& extension_id, | 436 const std::string& extension_id, |
| 462 const char* event_name, | 437 const char* event_name, |
| 463 const std::string& json_args) { | 438 ListValue* event_args) { |
| 464 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) | 439 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) |
|
miket_OOO
2012/07/10 22:33:19
If this guy doesn't take ownership of args, it's c
| |
| 465 return; | 440 return; |
| 466 | 441 |
| 467 profile->GetExtensionEventRouter()->DispatchEventToExtension( | 442 profile->GetExtensionEventRouter()->DispatchEventToExtension( |
| 468 extension_id, event_name, json_args, profile, GURL()); | 443 extension_id, event_name, event_args, profile, GURL()); |
| 469 } | 444 } |
| 470 | 445 |
| 471 void ExtensionBrowserEventRouter::DispatchEventsAcrossIncognito( | 446 void ExtensionBrowserEventRouter::DispatchEventsAcrossIncognito( |
| 472 Profile* profile, | 447 Profile* profile, |
| 473 const char* event_name, | 448 const char* event_name, |
| 474 const std::string& json_args, | 449 ListValue* event_args, |
| 475 const std::string& cross_incognito_args) { | 450 ListValue* cross_incognito_args) { |
| 476 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) | 451 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) |
|
miket_OOO
2012/07/10 22:33:19
Same comment about ownership/leak on return.
| |
| 477 return; | 452 return; |
| 478 | 453 |
| 479 profile->GetExtensionEventRouter()->DispatchEventsToRenderersAcrossIncognito( | 454 profile->GetExtensionEventRouter()->DispatchEventsToRenderersAcrossIncognito( |
| 480 event_name, json_args, profile, cross_incognito_args, GURL()); | 455 event_name, event_args, profile, cross_incognito_args, GURL()); |
| 481 } | 456 } |
| 482 | 457 |
| 483 void ExtensionBrowserEventRouter::DispatchEventWithTab( | 458 void ExtensionBrowserEventRouter::DispatchEventWithTab( |
| 484 Profile* profile, | 459 Profile* profile, |
| 485 const std::string& extension_id, | 460 const std::string& extension_id, |
| 486 const char* event_name, | 461 const char* event_name, |
| 487 const WebContents* web_contents, | 462 const WebContents* web_contents, |
| 488 bool active) { | 463 bool active) { |
| 489 if (!profile_->IsSameProfile(profile)) | 464 if (!profile_->IsSameProfile(profile)) |
| 490 return; | 465 return; |
| 491 | 466 |
| 492 ListValue args; | 467 ListValue* args = new ListValue(); |
| 493 args.Append(ExtensionTabUtil::CreateTabValueActive( | 468 args->Append(ExtensionTabUtil::CreateTabValueActive( |
| 494 web_contents, active)); | 469 web_contents, active)); |
| 495 std::string json_args; | |
| 496 base::JSONWriter::Write(&args, &json_args); | |
| 497 if (!extension_id.empty()) { | 470 if (!extension_id.empty()) { |
| 498 DispatchEventToExtension(profile, extension_id, event_name, json_args); | 471 DispatchEventToExtension(profile, extension_id, event_name, args); |
| 499 } else { | 472 } else { |
| 500 DispatchEvent(profile, event_name, json_args); | 473 DispatchEvent(profile, event_name, args); |
| 501 } | 474 } |
| 502 } | 475 } |
| 503 | 476 |
| 504 void ExtensionBrowserEventRouter::DispatchSimpleBrowserEvent( | 477 void ExtensionBrowserEventRouter::DispatchSimpleBrowserEvent( |
| 505 Profile* profile, const int window_id, const char* event_name) { | 478 Profile* profile, const int window_id, const char* event_name) { |
| 506 if (!profile_->IsSameProfile(profile)) | 479 if (!profile_->IsSameProfile(profile)) |
| 507 return; | 480 return; |
| 508 | 481 |
| 509 ListValue args; | 482 ListValue* args = new ListValue(); |
| 510 args.Append(Value::CreateIntegerValue(window_id)); | 483 args->Append(Value::CreateIntegerValue(window_id)); |
| 511 | 484 |
| 512 std::string json_args; | 485 DispatchEvent(profile, event_name, args); |
| 513 base::JSONWriter::Write(&args, &json_args); | |
| 514 | |
| 515 DispatchEvent(profile, event_name, json_args); | |
| 516 } | 486 } |
| 517 | 487 |
| 518 void ExtensionBrowserEventRouter::DispatchTabUpdatedEvent( | 488 void ExtensionBrowserEventRouter::DispatchTabUpdatedEvent( |
| 519 WebContents* contents, DictionaryValue* changed_properties) { | 489 WebContents* contents, DictionaryValue* changed_properties) { |
| 520 DCHECK(changed_properties); | 490 DCHECK(changed_properties); |
| 521 DCHECK(contents); | 491 DCHECK(contents); |
| 522 | 492 |
| 523 // The state of the tab (as seen from the extension point of view) has | 493 // The state of the tab (as seen from the extension point of view) has |
| 524 // changed. Send a notification to the extension. | 494 // changed. Send a notification to the extension. |
| 525 ListValue args; | 495 ListValue* args = new ListValue(); |
| 526 | 496 |
| 527 // First arg: The id of the tab that changed. | 497 // First arg: The id of the tab that changed. |
| 528 args.Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); | 498 args->Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); |
| 529 | 499 |
| 530 // Second arg: An object containing the changes to the tab state. | 500 // Second arg: An object containing the changes to the tab state. |
| 531 args.Append(changed_properties); | 501 args->Append(changed_properties); |
| 532 | 502 |
| 533 // Third arg: An object containing the state of the tab. | 503 // Third arg: An object containing the state of the tab. |
| 534 args.Append(ExtensionTabUtil::CreateTabValue(contents)); | 504 args->Append(ExtensionTabUtil::CreateTabValue(contents)); |
| 535 | |
| 536 std::string json_args; | |
| 537 base::JSONWriter::Write(&args, &json_args); | |
| 538 | 505 |
| 539 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 506 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 540 DispatchEvent(profile, events::kOnTabUpdated, json_args); | 507 DispatchEvent(profile, events::kOnTabUpdated, args); |
| 541 } | 508 } |
| 542 | 509 |
| 543 ExtensionBrowserEventRouter::TabEntry* ExtensionBrowserEventRouter::GetTabEntry( | 510 ExtensionBrowserEventRouter::TabEntry* ExtensionBrowserEventRouter::GetTabEntry( |
| 544 const WebContents* contents) { | 511 const WebContents* contents) { |
| 545 int tab_id = ExtensionTabUtil::GetTabId(contents); | 512 int tab_id = ExtensionTabUtil::GetTabId(contents); |
| 546 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); | 513 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); |
| 547 if (tab_entries_.end() == i) | 514 if (tab_entries_.end() == i) |
| 548 return NULL; | 515 return NULL; |
| 549 return &i->second; | 516 return &i->second; |
| 550 } | 517 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 608 | 575 |
| 609 void ExtensionBrowserEventRouter::TabStripEmpty() {} | 576 void ExtensionBrowserEventRouter::TabStripEmpty() {} |
| 610 | 577 |
| 611 void ExtensionBrowserEventRouter::DispatchOldPageActionEvent( | 578 void ExtensionBrowserEventRouter::DispatchOldPageActionEvent( |
| 612 Profile* profile, | 579 Profile* profile, |
| 613 const std::string& extension_id, | 580 const std::string& extension_id, |
| 614 const std::string& page_action_id, | 581 const std::string& page_action_id, |
| 615 int tab_id, | 582 int tab_id, |
| 616 const std::string& url, | 583 const std::string& url, |
| 617 int button) { | 584 int button) { |
| 618 ListValue args; | 585 ListValue* args = new ListValue(); |
| 619 args.Append(Value::CreateStringValue(page_action_id)); | 586 args->Append(Value::CreateStringValue(page_action_id)); |
| 620 | 587 |
| 621 DictionaryValue* data = new DictionaryValue(); | 588 DictionaryValue* data = new DictionaryValue(); |
| 622 data->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id)); | 589 data->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id)); |
| 623 data->Set(tab_keys::kTabUrlKey, Value::CreateStringValue(url)); | 590 data->Set(tab_keys::kTabUrlKey, Value::CreateStringValue(url)); |
| 624 data->Set(page_action_keys::kButtonKey, Value::CreateIntegerValue(button)); | 591 data->Set(page_action_keys::kButtonKey, Value::CreateIntegerValue(button)); |
| 625 args.Append(data); | 592 args->Append(data); |
| 626 | 593 |
| 627 std::string json_args; | 594 DispatchEventToExtension(profile, extension_id, "pageActions", args); |
| 628 base::JSONWriter::Write(&args, &json_args); | |
| 629 | |
| 630 DispatchEventToExtension(profile, extension_id, "pageActions", json_args); | |
| 631 } | 595 } |
| 632 | 596 |
| 633 void ExtensionBrowserEventRouter::BrowserActionExecuted( | 597 void ExtensionBrowserEventRouter::BrowserActionExecuted( |
| 634 const ExtensionAction& browser_action, | 598 const ExtensionAction& browser_action, |
| 635 Browser* browser) { | 599 Browser* browser) { |
| 636 Profile* profile = browser->profile(); | 600 Profile* profile = browser->profile(); |
| 637 TabContents* tab_contents = NULL; | 601 TabContents* tab_contents = NULL; |
| 638 int tab_id = 0; | 602 int tab_id = 0; |
| 639 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) | 603 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) |
| 640 return; | 604 return; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 666 NULL, NULL, &tab_contents, NULL)) { | 630 NULL, NULL, &tab_contents, NULL)) { |
| 667 return; | 631 return; |
| 668 } | 632 } |
| 669 ExtensionActionExecuted(profile, script_badge, tab_contents); | 633 ExtensionActionExecuted(profile, script_badge, tab_contents); |
| 670 } | 634 } |
| 671 | 635 |
| 672 void ExtensionBrowserEventRouter::CommandExecuted( | 636 void ExtensionBrowserEventRouter::CommandExecuted( |
| 673 Profile* profile, | 637 Profile* profile, |
| 674 const std::string& extension_id, | 638 const std::string& extension_id, |
| 675 const std::string& command) { | 639 const std::string& command) { |
| 676 ListValue args; | 640 ListValue* args = new ListValue(); |
| 677 args.Append(Value::CreateStringValue(command)); | 641 args->Append(Value::CreateStringValue(command)); |
| 678 std::string json_args; | |
| 679 base::JSONWriter::Write(&args, &json_args); | |
| 680 | 642 |
| 681 DispatchEventToExtension(profile, | 643 DispatchEventToExtension(profile, |
| 682 extension_id, | 644 extension_id, |
| 683 "experimental.keybinding.onCommand", | 645 "experimental.keybinding.onCommand", |
| 684 json_args); | 646 args); |
| 685 } | 647 } |
| 686 | 648 |
| 687 void ExtensionBrowserEventRouter::ExtensionActionExecuted( | 649 void ExtensionBrowserEventRouter::ExtensionActionExecuted( |
| 688 Profile* profile, | 650 Profile* profile, |
| 689 const ExtensionAction& extension_action, | 651 const ExtensionAction& extension_action, |
| 690 TabContents* tab_contents) { | 652 TabContents* tab_contents) { |
| 691 const char* event_name = NULL; | 653 const char* event_name = NULL; |
| 692 switch (extension_action.action_type()) { | 654 switch (extension_action.action_type()) { |
| 693 case ExtensionAction::TYPE_BROWSER: | 655 case ExtensionAction::TYPE_BROWSER: |
| 694 event_name = "browserAction.onClicked"; | 656 event_name = "browserAction.onClicked"; |
| 695 break; | 657 break; |
| 696 case ExtensionAction::TYPE_PAGE: | 658 case ExtensionAction::TYPE_PAGE: |
| 697 event_name = "pageAction.onClicked"; | 659 event_name = "pageAction.onClicked"; |
| 698 break; | 660 break; |
| 699 case ExtensionAction::TYPE_SCRIPT_BADGE: | 661 case ExtensionAction::TYPE_SCRIPT_BADGE: |
| 700 event_name = "scriptBadge.onClicked"; | 662 event_name = "scriptBadge.onClicked"; |
| 701 break; | 663 break; |
| 702 } | 664 } |
| 703 | 665 |
| 704 if (event_name) { | 666 if (event_name) { |
| 705 DispatchEventWithTab(profile, | 667 DispatchEventWithTab(profile, |
| 706 extension_action.extension_id(), | 668 extension_action.extension_id(), |
| 707 event_name, | 669 event_name, |
| 708 tab_contents->web_contents(), | 670 tab_contents->web_contents(), |
| 709 true); | 671 true); |
| 710 } | 672 } |
| 711 } | 673 } |
| OLD | NEW |