| 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 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 } | 185 } |
| 186 } | 186 } |
| 187 | 187 |
| 188 static bool WillDispatchTabCreatedEvent( | 188 static bool WillDispatchTabCreatedEvent( |
| 189 WebContents* contents, | 189 WebContents* contents, |
| 190 bool active, | 190 bool active, |
| 191 content::BrowserContext* context, | 191 content::BrowserContext* context, |
| 192 const Extension* extension, | 192 const Extension* extension, |
| 193 Event* event, | 193 Event* event, |
| 194 const base::DictionaryValue* listener_filter) { | 194 const base::DictionaryValue* listener_filter) { |
| 195 base::DictionaryValue* tab_value = | |
| 196 ExtensionTabUtil::CreateTabObject(contents, extension) | |
| 197 ->ToValue() | |
| 198 .release(); | |
| 199 event->event_args->Clear(); | 195 event->event_args->Clear(); |
| 200 event->event_args->Append(tab_value); | 196 std::unique_ptr<base::DictionaryValue> tab_value = |
| 197 ExtensionTabUtil::CreateTabObject(contents, extension)->ToValue(); |
| 201 tab_value->SetBoolean(tabs_constants::kSelectedKey, active); | 198 tab_value->SetBoolean(tabs_constants::kSelectedKey, active); |
| 202 tab_value->SetBoolean(tabs_constants::kActiveKey, active); | 199 tab_value->SetBoolean(tabs_constants::kActiveKey, active); |
| 200 event->event_args->Append(std::move(tab_value)); |
| 203 return true; | 201 return true; |
| 204 } | 202 } |
| 205 | 203 |
| 206 void TabsEventRouter::TabCreatedAt(WebContents* contents, | 204 void TabsEventRouter::TabCreatedAt(WebContents* contents, |
| 207 int index, | 205 int index, |
| 208 bool active) { | 206 bool active) { |
| 209 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 207 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 210 std::unique_ptr<base::ListValue> args(new base::ListValue); | 208 std::unique_ptr<base::ListValue> args(new base::ListValue); |
| 211 std::unique_ptr<Event> event(new Event( | 209 std::unique_ptr<Event> event(new Event( |
| 212 events::TABS_ON_CREATED, tabs::OnCreated::kEventName, std::move(args))); | 210 events::TABS_ON_CREATED, tabs::OnCreated::kEventName, std::move(args))); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 DispatchEvent(profile, events::TABS_ON_REMOVED, tabs::OnRemoved::kEventName, | 291 DispatchEvent(profile, events::TABS_ON_REMOVED, tabs::OnRemoved::kEventName, |
| 294 std::move(args), EventRouter::USER_GESTURE_UNKNOWN); | 292 std::move(args), EventRouter::USER_GESTURE_UNKNOWN); |
| 295 | 293 |
| 296 UnregisterForTabNotifications(contents); | 294 UnregisterForTabNotifications(contents); |
| 297 } | 295 } |
| 298 | 296 |
| 299 void TabsEventRouter::ActiveTabChanged(WebContents* old_contents, | 297 void TabsEventRouter::ActiveTabChanged(WebContents* old_contents, |
| 300 WebContents* new_contents, | 298 WebContents* new_contents, |
| 301 int index, | 299 int index, |
| 302 int reason) { | 300 int reason) { |
| 303 std::unique_ptr<base::ListValue> args(new base::ListValue); | 301 auto args = base::MakeUnique<base::ListValue>(); |
| 304 int tab_id = ExtensionTabUtil::GetTabId(new_contents); | 302 int tab_id = ExtensionTabUtil::GetTabId(new_contents); |
| 305 args->AppendInteger(tab_id); | 303 args->AppendInteger(tab_id); |
| 306 | 304 |
| 307 base::DictionaryValue* object_args = new base::DictionaryValue(); | 305 auto object_args = base::MakeUnique<base::DictionaryValue>(); |
| 308 object_args->Set(tabs_constants::kWindowIdKey, | 306 object_args->Set(tabs_constants::kWindowIdKey, |
| 309 new FundamentalValue( | 307 new FundamentalValue( |
| 310 ExtensionTabUtil::GetWindowIdOfTab(new_contents))); | 308 ExtensionTabUtil::GetWindowIdOfTab(new_contents))); |
| 311 args->Append(object_args); | 309 args->Append(object_args->CreateDeepCopy()); |
| 312 | 310 |
| 313 // The onActivated event replaced onActiveChanged and onSelectionChanged. The | 311 // The onActivated event replaced onActiveChanged and onSelectionChanged. The |
| 314 // deprecated events take two arguments: tabId, {windowId}. | 312 // deprecated events take two arguments: tabId, {windowId}. |
| 315 Profile* profile = | 313 Profile* profile = |
| 316 Profile::FromBrowserContext(new_contents->GetBrowserContext()); | 314 Profile::FromBrowserContext(new_contents->GetBrowserContext()); |
| 317 EventRouter::UserGestureState gesture = | 315 EventRouter::UserGestureState gesture = |
| 318 reason & CHANGE_REASON_USER_GESTURE | 316 reason & CHANGE_REASON_USER_GESTURE |
| 319 ? EventRouter::USER_GESTURE_ENABLED | 317 ? EventRouter::USER_GESTURE_ENABLED |
| 320 : EventRouter::USER_GESTURE_NOT_ENABLED; | 318 : EventRouter::USER_GESTURE_NOT_ENABLED; |
| 321 DispatchEvent(profile, events::TABS_ON_SELECTION_CHANGED, | 319 DispatchEvent(profile, events::TABS_ON_SELECTION_CHANGED, |
| 322 tabs::OnSelectionChanged::kEventName, | 320 tabs::OnSelectionChanged::kEventName, args->CreateDeepCopy(), |
| 323 std::unique_ptr<base::ListValue>(args->DeepCopy()), gesture); | 321 gesture); |
| 324 DispatchEvent(profile, events::TABS_ON_ACTIVE_CHANGED, | 322 DispatchEvent(profile, events::TABS_ON_ACTIVE_CHANGED, |
| 325 tabs::OnActiveChanged::kEventName, | 323 tabs::OnActiveChanged::kEventName, std::move(args), gesture); |
| 326 std::unique_ptr<base::ListValue>(args->DeepCopy()), gesture); | |
| 327 | 324 |
| 328 // The onActivated event takes one argument: {windowId, tabId}. | 325 // The onActivated event takes one argument: {windowId, tabId}. |
| 329 args->Remove(0, NULL); | 326 auto on_activated_args = base::MakeUnique<base::ListValue>(); |
| 330 object_args->Set(tabs_constants::kTabIdKey, | 327 object_args->Set(tabs_constants::kTabIdKey, |
| 331 new FundamentalValue(tab_id)); | 328 new FundamentalValue(tab_id)); |
| 329 on_activated_args->Append(std::move(object_args)); |
| 332 DispatchEvent(profile, events::TABS_ON_ACTIVATED, | 330 DispatchEvent(profile, events::TABS_ON_ACTIVATED, |
| 333 tabs::OnActivated::kEventName, std::move(args), gesture); | 331 tabs::OnActivated::kEventName, std::move(on_activated_args), |
| 332 gesture); |
| 334 } | 333 } |
| 335 | 334 |
| 336 void TabsEventRouter::TabSelectionChanged( | 335 void TabsEventRouter::TabSelectionChanged( |
| 337 TabStripModel* tab_strip_model, | 336 TabStripModel* tab_strip_model, |
| 338 const ui::ListSelectionModel& old_model) { | 337 const ui::ListSelectionModel& old_model) { |
| 339 ui::ListSelectionModel::SelectedIndices new_selection = | 338 ui::ListSelectionModel::SelectedIndices new_selection = |
| 340 tab_strip_model->selection_model().selected_indices(); | 339 tab_strip_model->selection_model().selected_indices(); |
| 341 std::unique_ptr<base::ListValue> all_tabs(new base::ListValue); | 340 std::unique_ptr<base::ListValue> all_tabs(new base::ListValue); |
| 342 | 341 |
| 343 for (size_t i = 0; i < new_selection.size(); ++i) { | 342 for (size_t i = 0; i < new_selection.size(); ++i) { |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 575 int tab_index = -1; | 574 int tab_index = -1; |
| 576 | 575 |
| 577 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) { | 576 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) { |
| 578 std::set<std::string> changed_property_names; | 577 std::set<std::string> changed_property_names; |
| 579 changed_property_names.insert(tabs_constants::kAutoDiscardableKey); | 578 changed_property_names.insert(tabs_constants::kAutoDiscardableKey); |
| 580 DispatchTabUpdatedEvent(contents, std::move(changed_property_names)); | 579 DispatchTabUpdatedEvent(contents, std::move(changed_property_names)); |
| 581 } | 580 } |
| 582 } | 581 } |
| 583 | 582 |
| 584 } // namespace extensions | 583 } // namespace extensions |
| OLD | NEW |