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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 bool WillDispatchTabUpdatedEvent( | 42 bool WillDispatchTabUpdatedEvent( |
43 WebContents* contents, | 43 WebContents* contents, |
44 const std::set<std::string> changed_property_names, | 44 const std::set<std::string> changed_property_names, |
45 content::BrowserContext* context, | 45 content::BrowserContext* context, |
46 const Extension* extension, | 46 const Extension* extension, |
47 Event* event, | 47 Event* event, |
48 const base::DictionaryValue* listener_filter) { | 48 const base::DictionaryValue* listener_filter) { |
49 std::unique_ptr<api::tabs::Tab> tab_object = | 49 std::unique_ptr<api::tabs::Tab> tab_object = |
50 ExtensionTabUtil::CreateTabObject(contents, extension); | 50 ExtensionTabUtil::CreateTabObject(contents, extension); |
51 | 51 |
52 base::DictionaryValue* tab_value = tab_object->ToValue().release(); | 52 std::unique_ptr<base::DictionaryValue> tab_value = tab_object->ToValue(); |
53 | 53 |
54 std::unique_ptr<base::DictionaryValue> changed_properties( | 54 auto changed_properties = base::MakeUnique<base::DictionaryValue>(); |
55 new base::DictionaryValue); | |
56 const base::Value* value = nullptr; | 55 const base::Value* value = nullptr; |
57 for (const auto& property : changed_property_names) { | 56 for (const auto& property : changed_property_names) { |
58 if (tab_value->Get(property, &value)) | 57 if (tab_value->Get(property, &value)) |
59 changed_properties->Set(property, base::WrapUnique(value->DeepCopy())); | 58 changed_properties->Set(property, value->CreateDeepCopy()); |
60 } | 59 } |
61 | 60 |
62 event->event_args->Set(1, changed_properties.release()); | 61 event->event_args->Set(1, std::move(changed_properties)); |
63 event->event_args->Set(2, tab_value); | 62 event->event_args->Set(2, std::move(tab_value)); |
64 return true; | 63 return true; |
65 } | 64 } |
66 | 65 |
67 } // namespace | 66 } // namespace |
68 | 67 |
69 TabsEventRouter::TabEntry::TabEntry(TabsEventRouter* router, | 68 TabsEventRouter::TabEntry::TabEntry(TabsEventRouter* router, |
70 content::WebContents* contents) | 69 content::WebContents* contents) |
71 : WebContentsObserver(contents), | 70 : WebContentsObserver(contents), |
72 complete_waiting_on_load_(false), | 71 complete_waiting_on_load_(false), |
73 was_audible_(contents->WasRecentlyAudible()), | 72 was_audible_(contents->WasRecentlyAudible()), |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 TabCreatedAt(contents, index, active); | 228 TabCreatedAt(contents, index, active); |
230 return; | 229 return; |
231 } | 230 } |
232 | 231 |
233 int tab_id = ExtensionTabUtil::GetTabId(contents); | 232 int tab_id = ExtensionTabUtil::GetTabId(contents); |
234 std::unique_ptr<base::ListValue> args(new base::ListValue); | 233 std::unique_ptr<base::ListValue> args(new base::ListValue); |
235 args->AppendInteger(tab_id); | 234 args->AppendInteger(tab_id); |
236 | 235 |
237 std::unique_ptr<base::DictionaryValue> object_args( | 236 std::unique_ptr<base::DictionaryValue> object_args( |
238 new base::DictionaryValue()); | 237 new base::DictionaryValue()); |
239 object_args->Set(tabs_constants::kNewWindowIdKey, | 238 object_args->Set( |
240 new Value(ExtensionTabUtil::GetWindowIdOfTab(contents))); | 239 tabs_constants::kNewWindowIdKey, |
241 object_args->Set(tabs_constants::kNewPositionKey, new Value(index)); | 240 base::MakeUnique<Value>(ExtensionTabUtil::GetWindowIdOfTab(contents))); |
| 241 object_args->Set(tabs_constants::kNewPositionKey, |
| 242 base::MakeUnique<Value>(index)); |
242 args->Append(std::move(object_args)); | 243 args->Append(std::move(object_args)); |
243 | 244 |
244 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 245 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
245 DispatchEvent(profile, events::TABS_ON_ATTACHED, tabs::OnAttached::kEventName, | 246 DispatchEvent(profile, events::TABS_ON_ATTACHED, tabs::OnAttached::kEventName, |
246 std::move(args), EventRouter::USER_GESTURE_UNKNOWN); | 247 std::move(args), EventRouter::USER_GESTURE_UNKNOWN); |
247 } | 248 } |
248 | 249 |
249 void TabsEventRouter::TabDetachedAt(WebContents* contents, int index) { | 250 void TabsEventRouter::TabDetachedAt(WebContents* contents, int index) { |
250 if (!GetTabEntry(contents)) { | 251 if (!GetTabEntry(contents)) { |
251 // The tab was removed. Don't send detach event. | 252 // The tab was removed. Don't send detach event. |
252 return; | 253 return; |
253 } | 254 } |
254 | 255 |
255 std::unique_ptr<base::ListValue> args(new base::ListValue); | 256 std::unique_ptr<base::ListValue> args(new base::ListValue); |
256 args->AppendInteger(ExtensionTabUtil::GetTabId(contents)); | 257 args->AppendInteger(ExtensionTabUtil::GetTabId(contents)); |
257 | 258 |
258 std::unique_ptr<base::DictionaryValue> object_args( | 259 std::unique_ptr<base::DictionaryValue> object_args( |
259 new base::DictionaryValue()); | 260 new base::DictionaryValue()); |
260 object_args->Set(tabs_constants::kOldWindowIdKey, | 261 object_args->Set( |
261 new Value(ExtensionTabUtil::GetWindowIdOfTab(contents))); | 262 tabs_constants::kOldWindowIdKey, |
262 object_args->Set(tabs_constants::kOldPositionKey, new Value(index)); | 263 base::MakeUnique<Value>(ExtensionTabUtil::GetWindowIdOfTab(contents))); |
| 264 object_args->Set(tabs_constants::kOldPositionKey, |
| 265 base::MakeUnique<Value>(index)); |
263 args->Append(std::move(object_args)); | 266 args->Append(std::move(object_args)); |
264 | 267 |
265 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 268 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
266 DispatchEvent(profile, events::TABS_ON_DETACHED, tabs::OnDetached::kEventName, | 269 DispatchEvent(profile, events::TABS_ON_DETACHED, tabs::OnDetached::kEventName, |
267 std::move(args), EventRouter::USER_GESTURE_UNKNOWN); | 270 std::move(args), EventRouter::USER_GESTURE_UNKNOWN); |
268 } | 271 } |
269 | 272 |
270 void TabsEventRouter::TabClosingAt(TabStripModel* tab_strip_model, | 273 void TabsEventRouter::TabClosingAt(TabStripModel* tab_strip_model, |
271 WebContents* contents, | 274 WebContents* contents, |
272 int index) { | 275 int index) { |
(...skipping 20 matching lines...) Expand all Loading... |
293 void TabsEventRouter::ActiveTabChanged(WebContents* old_contents, | 296 void TabsEventRouter::ActiveTabChanged(WebContents* old_contents, |
294 WebContents* new_contents, | 297 WebContents* new_contents, |
295 int index, | 298 int index, |
296 int reason) { | 299 int reason) { |
297 auto args = base::MakeUnique<base::ListValue>(); | 300 auto args = base::MakeUnique<base::ListValue>(); |
298 int tab_id = ExtensionTabUtil::GetTabId(new_contents); | 301 int tab_id = ExtensionTabUtil::GetTabId(new_contents); |
299 args->AppendInteger(tab_id); | 302 args->AppendInteger(tab_id); |
300 | 303 |
301 auto object_args = base::MakeUnique<base::DictionaryValue>(); | 304 auto object_args = base::MakeUnique<base::DictionaryValue>(); |
302 object_args->Set(tabs_constants::kWindowIdKey, | 305 object_args->Set(tabs_constants::kWindowIdKey, |
303 new Value(ExtensionTabUtil::GetWindowIdOfTab(new_contents))); | 306 base::MakeUnique<Value>( |
| 307 ExtensionTabUtil::GetWindowIdOfTab(new_contents))); |
304 args->Append(object_args->CreateDeepCopy()); | 308 args->Append(object_args->CreateDeepCopy()); |
305 | 309 |
306 // The onActivated event replaced onActiveChanged and onSelectionChanged. The | 310 // The onActivated event replaced onActiveChanged and onSelectionChanged. The |
307 // deprecated events take two arguments: tabId, {windowId}. | 311 // deprecated events take two arguments: tabId, {windowId}. |
308 Profile* profile = | 312 Profile* profile = |
309 Profile::FromBrowserContext(new_contents->GetBrowserContext()); | 313 Profile::FromBrowserContext(new_contents->GetBrowserContext()); |
310 EventRouter::UserGestureState gesture = | 314 EventRouter::UserGestureState gesture = |
311 reason & CHANGE_REASON_USER_GESTURE | 315 reason & CHANGE_REASON_USER_GESTURE |
312 ? EventRouter::USER_GESTURE_ENABLED | 316 ? EventRouter::USER_GESTURE_ENABLED |
313 : EventRouter::USER_GESTURE_NOT_ENABLED; | 317 : EventRouter::USER_GESTURE_NOT_ENABLED; |
314 DispatchEvent(profile, events::TABS_ON_SELECTION_CHANGED, | 318 DispatchEvent(profile, events::TABS_ON_SELECTION_CHANGED, |
315 tabs::OnSelectionChanged::kEventName, args->CreateDeepCopy(), | 319 tabs::OnSelectionChanged::kEventName, args->CreateDeepCopy(), |
316 gesture); | 320 gesture); |
317 DispatchEvent(profile, events::TABS_ON_ACTIVE_CHANGED, | 321 DispatchEvent(profile, events::TABS_ON_ACTIVE_CHANGED, |
318 tabs::OnActiveChanged::kEventName, std::move(args), gesture); | 322 tabs::OnActiveChanged::kEventName, std::move(args), gesture); |
319 | 323 |
320 // The onActivated event takes one argument: {windowId, tabId}. | 324 // The onActivated event takes one argument: {windowId, tabId}. |
321 auto on_activated_args = base::MakeUnique<base::ListValue>(); | 325 auto on_activated_args = base::MakeUnique<base::ListValue>(); |
322 object_args->Set(tabs_constants::kTabIdKey, new Value(tab_id)); | 326 object_args->Set(tabs_constants::kTabIdKey, base::MakeUnique<Value>(tab_id)); |
323 on_activated_args->Append(std::move(object_args)); | 327 on_activated_args->Append(std::move(object_args)); |
324 DispatchEvent(profile, events::TABS_ON_ACTIVATED, | 328 DispatchEvent(profile, events::TABS_ON_ACTIVATED, |
325 tabs::OnActivated::kEventName, std::move(on_activated_args), | 329 tabs::OnActivated::kEventName, std::move(on_activated_args), |
326 gesture); | 330 gesture); |
327 } | 331 } |
328 | 332 |
329 void TabsEventRouter::TabSelectionChanged( | 333 void TabsEventRouter::TabSelectionChanged( |
330 TabStripModel* tab_strip_model, | 334 TabStripModel* tab_strip_model, |
331 const ui::ListSelectionModel& old_model) { | 335 const ui::ListSelectionModel& old_model) { |
332 ui::ListSelectionModel::SelectedIndices new_selection = | 336 ui::ListSelectionModel::SelectedIndices new_selection = |
333 tab_strip_model->selection_model().selected_indices(); | 337 tab_strip_model->selection_model().selected_indices(); |
334 std::unique_ptr<base::ListValue> all_tabs(new base::ListValue); | 338 std::unique_ptr<base::ListValue> all_tabs(new base::ListValue); |
335 | 339 |
336 for (size_t i = 0; i < new_selection.size(); ++i) { | 340 for (size_t i = 0; i < new_selection.size(); ++i) { |
337 int index = new_selection[i]; | 341 int index = new_selection[i]; |
338 WebContents* contents = tab_strip_model->GetWebContentsAt(index); | 342 WebContents* contents = tab_strip_model->GetWebContentsAt(index); |
339 if (!contents) | 343 if (!contents) |
340 break; | 344 break; |
341 int tab_id = ExtensionTabUtil::GetTabId(contents); | 345 int tab_id = ExtensionTabUtil::GetTabId(contents); |
342 all_tabs->AppendInteger(tab_id); | 346 all_tabs->AppendInteger(tab_id); |
343 } | 347 } |
344 | 348 |
345 std::unique_ptr<base::ListValue> args(new base::ListValue); | 349 std::unique_ptr<base::ListValue> args(new base::ListValue); |
346 std::unique_ptr<base::DictionaryValue> select_info(new base::DictionaryValue); | 350 std::unique_ptr<base::DictionaryValue> select_info(new base::DictionaryValue); |
347 | 351 |
348 select_info->Set( | 352 select_info->Set( |
349 tabs_constants::kWindowIdKey, | 353 tabs_constants::kWindowIdKey, |
350 new Value(ExtensionTabUtil::GetWindowIdOfTabStripModel(tab_strip_model))); | 354 base::MakeUnique<Value>( |
| 355 ExtensionTabUtil::GetWindowIdOfTabStripModel(tab_strip_model))); |
351 | 356 |
352 select_info->Set(tabs_constants::kTabIdsKey, all_tabs.release()); | 357 select_info->Set(tabs_constants::kTabIdsKey, all_tabs.release()); |
353 args->Append(std::move(select_info)); | 358 args->Append(std::move(select_info)); |
354 | 359 |
355 // The onHighlighted event replaced onHighlightChanged. | 360 // The onHighlighted event replaced onHighlightChanged. |
356 Profile* profile = tab_strip_model->profile(); | 361 Profile* profile = tab_strip_model->profile(); |
357 DispatchEvent(profile, events::TABS_ON_HIGHLIGHT_CHANGED, | 362 DispatchEvent(profile, events::TABS_ON_HIGHLIGHT_CHANGED, |
358 tabs::OnHighlightChanged::kEventName, | 363 tabs::OnHighlightChanged::kEventName, |
359 std::unique_ptr<base::ListValue>(args->DeepCopy()), | 364 std::unique_ptr<base::ListValue>(args->DeepCopy()), |
360 EventRouter::USER_GESTURE_UNKNOWN); | 365 EventRouter::USER_GESTURE_UNKNOWN); |
361 DispatchEvent(profile, events::TABS_ON_HIGHLIGHTED, | 366 DispatchEvent(profile, events::TABS_ON_HIGHLIGHTED, |
362 tabs::OnHighlighted::kEventName, std::move(args), | 367 tabs::OnHighlighted::kEventName, std::move(args), |
363 EventRouter::USER_GESTURE_UNKNOWN); | 368 EventRouter::USER_GESTURE_UNKNOWN); |
364 } | 369 } |
365 | 370 |
366 void TabsEventRouter::TabMoved(WebContents* contents, | 371 void TabsEventRouter::TabMoved(WebContents* contents, |
367 int from_index, | 372 int from_index, |
368 int to_index) { | 373 int to_index) { |
369 std::unique_ptr<base::ListValue> args(new base::ListValue); | 374 std::unique_ptr<base::ListValue> args(new base::ListValue); |
370 args->AppendInteger(ExtensionTabUtil::GetTabId(contents)); | 375 args->AppendInteger(ExtensionTabUtil::GetTabId(contents)); |
371 | 376 |
372 std::unique_ptr<base::DictionaryValue> object_args( | 377 std::unique_ptr<base::DictionaryValue> object_args( |
373 new base::DictionaryValue()); | 378 new base::DictionaryValue()); |
374 object_args->Set(tabs_constants::kWindowIdKey, | 379 object_args->Set( |
375 new Value(ExtensionTabUtil::GetWindowIdOfTab(contents))); | 380 tabs_constants::kWindowIdKey, |
376 object_args->Set(tabs_constants::kFromIndexKey, new Value(from_index)); | 381 base::MakeUnique<Value>(ExtensionTabUtil::GetWindowIdOfTab(contents))); |
377 object_args->Set(tabs_constants::kToIndexKey, new Value(to_index)); | 382 object_args->Set(tabs_constants::kFromIndexKey, |
| 383 base::MakeUnique<Value>(from_index)); |
| 384 object_args->Set(tabs_constants::kToIndexKey, |
| 385 base::MakeUnique<Value>(to_index)); |
378 args->Append(std::move(object_args)); | 386 args->Append(std::move(object_args)); |
379 | 387 |
380 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 388 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
381 DispatchEvent(profile, events::TABS_ON_MOVED, tabs::OnMoved::kEventName, | 389 DispatchEvent(profile, events::TABS_ON_MOVED, tabs::OnMoved::kEventName, |
382 std::move(args), EventRouter::USER_GESTURE_UNKNOWN); | 390 std::move(args), EventRouter::USER_GESTURE_UNKNOWN); |
383 } | 391 } |
384 | 392 |
385 void TabsEventRouter::TabUpdated(TabEntry* entry, | 393 void TabsEventRouter::TabUpdated(TabEntry* entry, |
386 std::set<std::string> changed_property_names) { | 394 std::set<std::string> changed_property_names) { |
387 bool audible = entry->web_contents()->WasRecentlyAudible(); | 395 bool audible = entry->web_contents()->WasRecentlyAudible(); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 } | 559 } |
552 | 560 |
553 void TabsEventRouter::OnAutoDiscardableStateChange(WebContents* contents, | 561 void TabsEventRouter::OnAutoDiscardableStateChange(WebContents* contents, |
554 bool is_auto_discardable) { | 562 bool is_auto_discardable) { |
555 std::set<std::string> changed_property_names; | 563 std::set<std::string> changed_property_names; |
556 changed_property_names.insert(tabs_constants::kAutoDiscardableKey); | 564 changed_property_names.insert(tabs_constants::kAutoDiscardableKey); |
557 DispatchTabUpdatedEvent(contents, std::move(changed_property_names)); | 565 DispatchTabUpdatedEvent(contents, std::move(changed_property_names)); |
558 } | 566 } |
559 | 567 |
560 } // namespace extensions | 568 } // namespace extensions |
OLD | NEW |