| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/api/tabs/tabs_event_router.h" | 5 #include "chrome/browser/extensions/api/tabs/tabs_event_router.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "chrome/browser/ui/browser_list.h" | 22 #include "chrome/browser/ui/browser_list.h" |
| 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 24 #include "chrome/common/extensions/extension_constants.h" | 24 #include "chrome/common/extensions/extension_constants.h" |
| 25 #include "components/favicon/content/content_favicon_driver.h" | 25 #include "components/favicon/content/content_favicon_driver.h" |
| 26 #include "content/public/browser/favicon_status.h" | 26 #include "content/public/browser/favicon_status.h" |
| 27 #include "content/public/browser/navigation_entry.h" | 27 #include "content/public/browser/navigation_entry.h" |
| 28 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 29 | 29 |
| 30 using base::DictionaryValue; | 30 using base::DictionaryValue; |
| 31 using base::ListValue; | 31 using base::ListValue; |
| 32 using base::FundamentalValue; | 32 using base::Value; |
| 33 using content::WebContents; | 33 using content::WebContents; |
| 34 using zoom::ZoomController; | 34 using zoom::ZoomController; |
| 35 | 35 |
| 36 namespace extensions { | 36 namespace extensions { |
| 37 | 37 |
| 38 namespace { | 38 namespace { |
| 39 | 39 |
| 40 namespace tabs = api::tabs; | 40 namespace tabs = api::tabs; |
| 41 | 41 |
| 42 bool WillDispatchTabUpdatedEvent( | 42 bool WillDispatchTabUpdatedEvent( |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 return; | 231 return; |
| 232 } | 232 } |
| 233 | 233 |
| 234 int tab_id = ExtensionTabUtil::GetTabId(contents); | 234 int tab_id = ExtensionTabUtil::GetTabId(contents); |
| 235 std::unique_ptr<base::ListValue> args(new base::ListValue); | 235 std::unique_ptr<base::ListValue> args(new base::ListValue); |
| 236 args->AppendInteger(tab_id); | 236 args->AppendInteger(tab_id); |
| 237 | 237 |
| 238 std::unique_ptr<base::DictionaryValue> object_args( | 238 std::unique_ptr<base::DictionaryValue> object_args( |
| 239 new base::DictionaryValue()); | 239 new base::DictionaryValue()); |
| 240 object_args->Set(tabs_constants::kNewWindowIdKey, | 240 object_args->Set(tabs_constants::kNewWindowIdKey, |
| 241 new FundamentalValue( | 241 new Value(ExtensionTabUtil::GetWindowIdOfTab(contents))); |
| 242 ExtensionTabUtil::GetWindowIdOfTab(contents))); | 242 object_args->Set(tabs_constants::kNewPositionKey, new Value(index)); |
| 243 object_args->Set(tabs_constants::kNewPositionKey, | |
| 244 new FundamentalValue(index)); | |
| 245 args->Append(std::move(object_args)); | 243 args->Append(std::move(object_args)); |
| 246 | 244 |
| 247 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 245 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 248 DispatchEvent(profile, events::TABS_ON_ATTACHED, tabs::OnAttached::kEventName, | 246 DispatchEvent(profile, events::TABS_ON_ATTACHED, tabs::OnAttached::kEventName, |
| 249 std::move(args), EventRouter::USER_GESTURE_UNKNOWN); | 247 std::move(args), EventRouter::USER_GESTURE_UNKNOWN); |
| 250 } | 248 } |
| 251 | 249 |
| 252 void TabsEventRouter::TabDetachedAt(WebContents* contents, int index) { | 250 void TabsEventRouter::TabDetachedAt(WebContents* contents, int index) { |
| 253 if (!GetTabEntry(contents)) { | 251 if (!GetTabEntry(contents)) { |
| 254 // The tab was removed. Don't send detach event. | 252 // The tab was removed. Don't send detach event. |
| 255 return; | 253 return; |
| 256 } | 254 } |
| 257 | 255 |
| 258 std::unique_ptr<base::ListValue> args(new base::ListValue); | 256 std::unique_ptr<base::ListValue> args(new base::ListValue); |
| 259 args->AppendInteger(ExtensionTabUtil::GetTabId(contents)); | 257 args->AppendInteger(ExtensionTabUtil::GetTabId(contents)); |
| 260 | 258 |
| 261 std::unique_ptr<base::DictionaryValue> object_args( | 259 std::unique_ptr<base::DictionaryValue> object_args( |
| 262 new base::DictionaryValue()); | 260 new base::DictionaryValue()); |
| 263 object_args->Set(tabs_constants::kOldWindowIdKey, | 261 object_args->Set(tabs_constants::kOldWindowIdKey, |
| 264 new FundamentalValue( | 262 new Value(ExtensionTabUtil::GetWindowIdOfTab(contents))); |
| 265 ExtensionTabUtil::GetWindowIdOfTab(contents))); | 263 object_args->Set(tabs_constants::kOldPositionKey, new Value(index)); |
| 266 object_args->Set(tabs_constants::kOldPositionKey, | |
| 267 new FundamentalValue(index)); | |
| 268 args->Append(std::move(object_args)); | 264 args->Append(std::move(object_args)); |
| 269 | 265 |
| 270 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 266 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 271 DispatchEvent(profile, events::TABS_ON_DETACHED, tabs::OnDetached::kEventName, | 267 DispatchEvent(profile, events::TABS_ON_DETACHED, tabs::OnDetached::kEventName, |
| 272 std::move(args), EventRouter::USER_GESTURE_UNKNOWN); | 268 std::move(args), EventRouter::USER_GESTURE_UNKNOWN); |
| 273 } | 269 } |
| 274 | 270 |
| 275 void TabsEventRouter::TabClosingAt(TabStripModel* tab_strip_model, | 271 void TabsEventRouter::TabClosingAt(TabStripModel* tab_strip_model, |
| 276 WebContents* contents, | 272 WebContents* contents, |
| 277 int index) { | 273 int index) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 298 void TabsEventRouter::ActiveTabChanged(WebContents* old_contents, | 294 void TabsEventRouter::ActiveTabChanged(WebContents* old_contents, |
| 299 WebContents* new_contents, | 295 WebContents* new_contents, |
| 300 int index, | 296 int index, |
| 301 int reason) { | 297 int reason) { |
| 302 auto args = base::MakeUnique<base::ListValue>(); | 298 auto args = base::MakeUnique<base::ListValue>(); |
| 303 int tab_id = ExtensionTabUtil::GetTabId(new_contents); | 299 int tab_id = ExtensionTabUtil::GetTabId(new_contents); |
| 304 args->AppendInteger(tab_id); | 300 args->AppendInteger(tab_id); |
| 305 | 301 |
| 306 auto object_args = base::MakeUnique<base::DictionaryValue>(); | 302 auto object_args = base::MakeUnique<base::DictionaryValue>(); |
| 307 object_args->Set(tabs_constants::kWindowIdKey, | 303 object_args->Set(tabs_constants::kWindowIdKey, |
| 308 new FundamentalValue( | 304 new Value(ExtensionTabUtil::GetWindowIdOfTab(new_contents))); |
| 309 ExtensionTabUtil::GetWindowIdOfTab(new_contents))); | |
| 310 args->Append(object_args->CreateDeepCopy()); | 305 args->Append(object_args->CreateDeepCopy()); |
| 311 | 306 |
| 312 // The onActivated event replaced onActiveChanged and onSelectionChanged. The | 307 // The onActivated event replaced onActiveChanged and onSelectionChanged. The |
| 313 // deprecated events take two arguments: tabId, {windowId}. | 308 // deprecated events take two arguments: tabId, {windowId}. |
| 314 Profile* profile = | 309 Profile* profile = |
| 315 Profile::FromBrowserContext(new_contents->GetBrowserContext()); | 310 Profile::FromBrowserContext(new_contents->GetBrowserContext()); |
| 316 EventRouter::UserGestureState gesture = | 311 EventRouter::UserGestureState gesture = |
| 317 reason & CHANGE_REASON_USER_GESTURE | 312 reason & CHANGE_REASON_USER_GESTURE |
| 318 ? EventRouter::USER_GESTURE_ENABLED | 313 ? EventRouter::USER_GESTURE_ENABLED |
| 319 : EventRouter::USER_GESTURE_NOT_ENABLED; | 314 : EventRouter::USER_GESTURE_NOT_ENABLED; |
| 320 DispatchEvent(profile, events::TABS_ON_SELECTION_CHANGED, | 315 DispatchEvent(profile, events::TABS_ON_SELECTION_CHANGED, |
| 321 tabs::OnSelectionChanged::kEventName, args->CreateDeepCopy(), | 316 tabs::OnSelectionChanged::kEventName, args->CreateDeepCopy(), |
| 322 gesture); | 317 gesture); |
| 323 DispatchEvent(profile, events::TABS_ON_ACTIVE_CHANGED, | 318 DispatchEvent(profile, events::TABS_ON_ACTIVE_CHANGED, |
| 324 tabs::OnActiveChanged::kEventName, std::move(args), gesture); | 319 tabs::OnActiveChanged::kEventName, std::move(args), gesture); |
| 325 | 320 |
| 326 // The onActivated event takes one argument: {windowId, tabId}. | 321 // The onActivated event takes one argument: {windowId, tabId}. |
| 327 auto on_activated_args = base::MakeUnique<base::ListValue>(); | 322 auto on_activated_args = base::MakeUnique<base::ListValue>(); |
| 328 object_args->Set(tabs_constants::kTabIdKey, | 323 object_args->Set(tabs_constants::kTabIdKey, new Value(tab_id)); |
| 329 new FundamentalValue(tab_id)); | |
| 330 on_activated_args->Append(std::move(object_args)); | 324 on_activated_args->Append(std::move(object_args)); |
| 331 DispatchEvent(profile, events::TABS_ON_ACTIVATED, | 325 DispatchEvent(profile, events::TABS_ON_ACTIVATED, |
| 332 tabs::OnActivated::kEventName, std::move(on_activated_args), | 326 tabs::OnActivated::kEventName, std::move(on_activated_args), |
| 333 gesture); | 327 gesture); |
| 334 } | 328 } |
| 335 | 329 |
| 336 void TabsEventRouter::TabSelectionChanged( | 330 void TabsEventRouter::TabSelectionChanged( |
| 337 TabStripModel* tab_strip_model, | 331 TabStripModel* tab_strip_model, |
| 338 const ui::ListSelectionModel& old_model) { | 332 const ui::ListSelectionModel& old_model) { |
| 339 ui::ListSelectionModel::SelectedIndices new_selection = | 333 ui::ListSelectionModel::SelectedIndices new_selection = |
| 340 tab_strip_model->selection_model().selected_indices(); | 334 tab_strip_model->selection_model().selected_indices(); |
| 341 std::unique_ptr<base::ListValue> all_tabs(new base::ListValue); | 335 std::unique_ptr<base::ListValue> all_tabs(new base::ListValue); |
| 342 | 336 |
| 343 for (size_t i = 0; i < new_selection.size(); ++i) { | 337 for (size_t i = 0; i < new_selection.size(); ++i) { |
| 344 int index = new_selection[i]; | 338 int index = new_selection[i]; |
| 345 WebContents* contents = tab_strip_model->GetWebContentsAt(index); | 339 WebContents* contents = tab_strip_model->GetWebContentsAt(index); |
| 346 if (!contents) | 340 if (!contents) |
| 347 break; | 341 break; |
| 348 int tab_id = ExtensionTabUtil::GetTabId(contents); | 342 int tab_id = ExtensionTabUtil::GetTabId(contents); |
| 349 all_tabs->AppendInteger(tab_id); | 343 all_tabs->AppendInteger(tab_id); |
| 350 } | 344 } |
| 351 | 345 |
| 352 std::unique_ptr<base::ListValue> args(new base::ListValue); | 346 std::unique_ptr<base::ListValue> args(new base::ListValue); |
| 353 std::unique_ptr<base::DictionaryValue> select_info(new base::DictionaryValue); | 347 std::unique_ptr<base::DictionaryValue> select_info(new base::DictionaryValue); |
| 354 | 348 |
| 355 select_info->Set( | 349 select_info->Set( |
| 356 tabs_constants::kWindowIdKey, | 350 tabs_constants::kWindowIdKey, |
| 357 new FundamentalValue( | 351 new Value(ExtensionTabUtil::GetWindowIdOfTabStripModel(tab_strip_model))); |
| 358 ExtensionTabUtil::GetWindowIdOfTabStripModel(tab_strip_model))); | |
| 359 | 352 |
| 360 select_info->Set(tabs_constants::kTabIdsKey, all_tabs.release()); | 353 select_info->Set(tabs_constants::kTabIdsKey, all_tabs.release()); |
| 361 args->Append(std::move(select_info)); | 354 args->Append(std::move(select_info)); |
| 362 | 355 |
| 363 // The onHighlighted event replaced onHighlightChanged. | 356 // The onHighlighted event replaced onHighlightChanged. |
| 364 Profile* profile = tab_strip_model->profile(); | 357 Profile* profile = tab_strip_model->profile(); |
| 365 DispatchEvent(profile, events::TABS_ON_HIGHLIGHT_CHANGED, | 358 DispatchEvent(profile, events::TABS_ON_HIGHLIGHT_CHANGED, |
| 366 tabs::OnHighlightChanged::kEventName, | 359 tabs::OnHighlightChanged::kEventName, |
| 367 std::unique_ptr<base::ListValue>(args->DeepCopy()), | 360 std::unique_ptr<base::ListValue>(args->DeepCopy()), |
| 368 EventRouter::USER_GESTURE_UNKNOWN); | 361 EventRouter::USER_GESTURE_UNKNOWN); |
| 369 DispatchEvent(profile, events::TABS_ON_HIGHLIGHTED, | 362 DispatchEvent(profile, events::TABS_ON_HIGHLIGHTED, |
| 370 tabs::OnHighlighted::kEventName, std::move(args), | 363 tabs::OnHighlighted::kEventName, std::move(args), |
| 371 EventRouter::USER_GESTURE_UNKNOWN); | 364 EventRouter::USER_GESTURE_UNKNOWN); |
| 372 } | 365 } |
| 373 | 366 |
| 374 void TabsEventRouter::TabMoved(WebContents* contents, | 367 void TabsEventRouter::TabMoved(WebContents* contents, |
| 375 int from_index, | 368 int from_index, |
| 376 int to_index) { | 369 int to_index) { |
| 377 std::unique_ptr<base::ListValue> args(new base::ListValue); | 370 std::unique_ptr<base::ListValue> args(new base::ListValue); |
| 378 args->AppendInteger(ExtensionTabUtil::GetTabId(contents)); | 371 args->AppendInteger(ExtensionTabUtil::GetTabId(contents)); |
| 379 | 372 |
| 380 std::unique_ptr<base::DictionaryValue> object_args( | 373 std::unique_ptr<base::DictionaryValue> object_args( |
| 381 new base::DictionaryValue()); | 374 new base::DictionaryValue()); |
| 382 object_args->Set(tabs_constants::kWindowIdKey, | 375 object_args->Set(tabs_constants::kWindowIdKey, |
| 383 new FundamentalValue( | 376 new Value(ExtensionTabUtil::GetWindowIdOfTab(contents))); |
| 384 ExtensionTabUtil::GetWindowIdOfTab(contents))); | 377 object_args->Set(tabs_constants::kFromIndexKey, new Value(from_index)); |
| 385 object_args->Set(tabs_constants::kFromIndexKey, | 378 object_args->Set(tabs_constants::kToIndexKey, new Value(to_index)); |
| 386 new FundamentalValue(from_index)); | |
| 387 object_args->Set(tabs_constants::kToIndexKey, | |
| 388 new FundamentalValue(to_index)); | |
| 389 args->Append(std::move(object_args)); | 379 args->Append(std::move(object_args)); |
| 390 | 380 |
| 391 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 381 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 392 DispatchEvent(profile, events::TABS_ON_MOVED, tabs::OnMoved::kEventName, | 382 DispatchEvent(profile, events::TABS_ON_MOVED, tabs::OnMoved::kEventName, |
| 393 std::move(args), EventRouter::USER_GESTURE_UNKNOWN); | 383 std::move(args), EventRouter::USER_GESTURE_UNKNOWN); |
| 394 } | 384 } |
| 395 | 385 |
| 396 void TabsEventRouter::TabUpdated(TabEntry* entry, | 386 void TabsEventRouter::TabUpdated(TabEntry* entry, |
| 397 std::set<std::string> changed_property_names) { | 387 std::set<std::string> changed_property_names) { |
| 398 bool audible = entry->web_contents()->WasRecentlyAudible(); | 388 bool audible = entry->web_contents()->WasRecentlyAudible(); |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 } | 552 } |
| 563 | 553 |
| 564 void TabsEventRouter::OnAutoDiscardableStateChange(WebContents* contents, | 554 void TabsEventRouter::OnAutoDiscardableStateChange(WebContents* contents, |
| 565 bool is_auto_discardable) { | 555 bool is_auto_discardable) { |
| 566 std::set<std::string> changed_property_names; | 556 std::set<std::string> changed_property_names; |
| 567 changed_property_names.insert(tabs_constants::kAutoDiscardableKey); | 557 changed_property_names.insert(tabs_constants::kAutoDiscardableKey); |
| 568 DispatchTabUpdatedEvent(contents, std::move(changed_property_names)); | 558 DispatchTabUpdatedEvent(contents, std::move(changed_property_names)); |
| 569 } | 559 } |
| 570 | 560 |
| 571 } // namespace extensions | 561 } // namespace extensions |
| OLD | NEW |