Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: chrome/browser/extensions/api/tabs/tabs_event_router.cc

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

Powered by Google App Engine
This is Rietveld 408576698