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

Side by Side Diff: chrome/browser/extensions/api/sessions/sessions_api.cc

Issue 1871713002: Convert //chrome/browser/extensions from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and fix header Created 4 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/sessions/sessions_api.h" 5 #include "chrome/browser/extensions/api/sessions/sessions_api.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8
8 #include <utility> 9 #include <utility>
9 #include <vector> 10 #include <vector>
10 11
11 #include "base/i18n/rtl.h" 12 #include "base/i18n/rtl.h"
12 #include "base/lazy_instance.h" 13 #include "base/lazy_instance.h"
14 #include "base/memory/ptr_util.h"
13 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
16 #include "base/time/time.h" 18 #include "base/time/time.h"
17 #include "chrome/browser/extensions/api/sessions/session_id.h" 19 #include "chrome/browser/extensions/api/sessions/session_id.h"
18 #include "chrome/browser/extensions/api/tabs/windows_util.h" 20 #include "chrome/browser/extensions/api/tabs/windows_util.h"
19 #include "chrome/browser/extensions/extension_tab_util.h" 21 #include "chrome/browser/extensions/extension_tab_util.h"
20 #include "chrome/browser/extensions/window_controller.h" 22 #include "chrome/browser/extensions/window_controller.h"
21 #include "chrome/browser/extensions/window_controller_list.h" 23 #include "chrome/browser/extensions/window_controller_list.h"
22 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/profiles/profile.h"
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 // Note: |selected_index| from the sync sessions model is what we call 101 // Note: |selected_index| from the sync sessions model is what we call
100 // "active" in extensions terminology. "selected" is deprecated because it's 102 // "active" in extensions terminology. "selected" is deprecated because it's
101 // not clear whether it means "active" (user can see) or "highlighted" (user 103 // not clear whether it means "active" (user can see) or "highlighted" (user
102 // has highlighted, since you can select tabs without bringing them into the 104 // has highlighted, since you can select tabs without bringing them into the
103 // foreground). 105 // foreground).
104 tab_struct.active = index == selected_index; 106 tab_struct.active = index == selected_index;
105 ExtensionTabUtil::ScrubTabForExtension(extension, nullptr, &tab_struct); 107 ExtensionTabUtil::ScrubTabForExtension(extension, nullptr, &tab_struct);
106 return tab_struct; 108 return tab_struct;
107 } 109 }
108 110
109 scoped_ptr<windows::Window> CreateWindowModelHelper( 111 std::unique_ptr<windows::Window> CreateWindowModelHelper(
110 scoped_ptr<std::vector<tabs::Tab>> tabs, 112 std::unique_ptr<std::vector<tabs::Tab>> tabs,
111 const std::string& session_id, 113 const std::string& session_id,
112 const windows::WindowType& type, 114 const windows::WindowType& type,
113 const windows::WindowState& state) { 115 const windows::WindowState& state) {
114 scoped_ptr<windows::Window> window_struct(new windows::Window); 116 std::unique_ptr<windows::Window> window_struct(new windows::Window);
115 window_struct->tabs = std::move(tabs); 117 window_struct->tabs = std::move(tabs);
116 window_struct->session_id.reset(new std::string(session_id)); 118 window_struct->session_id.reset(new std::string(session_id));
117 window_struct->incognito = false; 119 window_struct->incognito = false;
118 window_struct->always_on_top = false; 120 window_struct->always_on_top = false;
119 window_struct->focused = false; 121 window_struct->focused = false;
120 window_struct->type = type; 122 window_struct->type = type;
121 window_struct->state = state; 123 window_struct->state = state;
122 return window_struct; 124 return window_struct;
123 } 125 }
124 126
125 scoped_ptr<api::sessions::Session> CreateSessionModelHelper( 127 std::unique_ptr<api::sessions::Session> CreateSessionModelHelper(
126 int last_modified, 128 int last_modified,
127 scoped_ptr<tabs::Tab> tab, 129 std::unique_ptr<tabs::Tab> tab,
128 scoped_ptr<windows::Window> window) { 130 std::unique_ptr<windows::Window> window) {
129 scoped_ptr<api::sessions::Session> session_struct( 131 std::unique_ptr<api::sessions::Session> session_struct(
130 new api::sessions::Session()); 132 new api::sessions::Session());
131 session_struct->last_modified = last_modified; 133 session_struct->last_modified = last_modified;
132 if (tab) 134 if (tab)
133 session_struct->tab = std::move(tab); 135 session_struct->tab = std::move(tab);
134 else if (window) 136 else if (window)
135 session_struct->window = std::move(window); 137 session_struct->window = std::move(window);
136 else 138 else
137 NOTREACHED(); 139 NOTREACHED();
138 return session_struct; 140 return session_struct;
139 } 141 }
(...skipping 12 matching lines...) Expand all
152 int selected_index) { 154 int selected_index) {
153 return CreateTabModelHelper(GetProfile(), 155 return CreateTabModelHelper(GetProfile(),
154 tab.navigations[tab.current_navigation_index], 156 tab.navigations[tab.current_navigation_index],
155 base::IntToString(session_id), 157 base::IntToString(session_id),
156 tab.tabstrip_index, 158 tab.tabstrip_index,
157 tab.pinned, 159 tab.pinned,
158 selected_index, 160 selected_index,
159 extension()); 161 extension());
160 } 162 }
161 163
162 scoped_ptr<windows::Window> 164 std::unique_ptr<windows::Window>
163 SessionsGetRecentlyClosedFunction::CreateWindowModel( 165 SessionsGetRecentlyClosedFunction::CreateWindowModel(
164 const sessions::TabRestoreService::Window& window, 166 const sessions::TabRestoreService::Window& window,
165 int session_id) { 167 int session_id) {
166 DCHECK(!window.tabs.empty()); 168 DCHECK(!window.tabs.empty());
167 169
168 scoped_ptr<std::vector<tabs::Tab>> tabs(new std::vector<tabs::Tab>()); 170 std::unique_ptr<std::vector<tabs::Tab>> tabs(new std::vector<tabs::Tab>());
169 for (size_t i = 0; i < window.tabs.size(); ++i) { 171 for (size_t i = 0; i < window.tabs.size(); ++i) {
170 tabs->push_back(CreateTabModel(window.tabs[i], window.tabs[i].id, 172 tabs->push_back(CreateTabModel(window.tabs[i], window.tabs[i].id,
171 window.selected_tab_index)); 173 window.selected_tab_index));
172 } 174 }
173 175
174 return CreateWindowModelHelper(std::move(tabs), base::IntToString(session_id), 176 return CreateWindowModelHelper(std::move(tabs), base::IntToString(session_id),
175 windows::WINDOW_TYPE_NORMAL, 177 windows::WINDOW_TYPE_NORMAL,
176 windows::WINDOW_STATE_NORMAL); 178 windows::WINDOW_STATE_NORMAL);
177 } 179 }
178 180
179 scoped_ptr<api::sessions::Session> 181 std::unique_ptr<api::sessions::Session>
180 SessionsGetRecentlyClosedFunction::CreateSessionModel( 182 SessionsGetRecentlyClosedFunction::CreateSessionModel(
181 const sessions::TabRestoreService::Entry* entry) { 183 const sessions::TabRestoreService::Entry* entry) {
182 scoped_ptr<tabs::Tab> tab; 184 std::unique_ptr<tabs::Tab> tab;
183 scoped_ptr<windows::Window> window; 185 std::unique_ptr<windows::Window> window;
184 switch (entry->type) { 186 switch (entry->type) {
185 case sessions::TabRestoreService::TAB: 187 case sessions::TabRestoreService::TAB:
186 tab.reset(new tabs::Tab(CreateTabModel( 188 tab.reset(new tabs::Tab(CreateTabModel(
187 *static_cast<const sessions::TabRestoreService::Tab*>(entry), 189 *static_cast<const sessions::TabRestoreService::Tab*>(entry),
188 entry->id, -1))); 190 entry->id, -1)));
189 break; 191 break;
190 case sessions::TabRestoreService::WINDOW: 192 case sessions::TabRestoreService::WINDOW:
191 window = CreateWindowModel( 193 window = CreateWindowModel(
192 *static_cast<const sessions::TabRestoreService::Window*>(entry), 194 *static_cast<const sessions::TabRestoreService::Window*>(entry),
193 entry->id); 195 entry->id);
194 break; 196 break;
195 default: 197 default:
196 NOTREACHED(); 198 NOTREACHED();
197 } 199 }
198 return CreateSessionModelHelper(entry->timestamp.ToTimeT(), std::move(tab), 200 return CreateSessionModelHelper(entry->timestamp.ToTimeT(), std::move(tab),
199 std::move(window)); 201 std::move(window));
200 } 202 }
201 203
202 bool SessionsGetRecentlyClosedFunction::RunSync() { 204 bool SessionsGetRecentlyClosedFunction::RunSync() {
203 scoped_ptr<GetRecentlyClosed::Params> params( 205 std::unique_ptr<GetRecentlyClosed::Params> params(
204 GetRecentlyClosed::Params::Create(*args_)); 206 GetRecentlyClosed::Params::Create(*args_));
205 EXTENSION_FUNCTION_VALIDATE(params); 207 EXTENSION_FUNCTION_VALIDATE(params);
206 int max_results = api::sessions::MAX_SESSION_RESULTS; 208 int max_results = api::sessions::MAX_SESSION_RESULTS;
207 if (params->filter && params->filter->max_results) 209 if (params->filter && params->filter->max_results)
208 max_results = *params->filter->max_results; 210 max_results = *params->filter->max_results;
209 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && 211 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 &&
210 max_results <= api::sessions::MAX_SESSION_RESULTS); 212 max_results <= api::sessions::MAX_SESSION_RESULTS);
211 213
212 std::vector<api::sessions::Session> result; 214 std::vector<api::sessions::Session> result;
213 sessions::TabRestoreService* tab_restore_service = 215 sessions::TabRestoreService* tab_restore_service =
(...skipping 29 matching lines...) Expand all
243 return CreateTabModelHelper( 245 return CreateTabModelHelper(
244 GetProfile(), 246 GetProfile(),
245 tab.navigations[tab.normalized_navigation_index()], 247 tab.navigations[tab.normalized_navigation_index()],
246 session_id, 248 session_id,
247 tab_index, 249 tab_index,
248 tab.pinned, 250 tab.pinned,
249 selected_index, 251 selected_index,
250 extension()); 252 extension());
251 } 253 }
252 254
253 scoped_ptr<windows::Window> SessionsGetDevicesFunction::CreateWindowModel( 255 std::unique_ptr<windows::Window> SessionsGetDevicesFunction::CreateWindowModel(
254 const sessions::SessionWindow& window, const std::string& session_tag) { 256 const sessions::SessionWindow& window,
257 const std::string& session_tag) {
255 DCHECK(!window.tabs.empty()); 258 DCHECK(!window.tabs.empty());
256 259
257 // Prune tabs that are not syncable or are NewTabPage. Then, sort the tabs 260 // Prune tabs that are not syncable or are NewTabPage. Then, sort the tabs
258 // from most recent to least recent. 261 // from most recent to least recent.
259 std::vector<const sessions::SessionTab*> tabs_in_window; 262 std::vector<const sessions::SessionTab*> tabs_in_window;
260 for (size_t i = 0; i < window.tabs.size(); ++i) { 263 for (size_t i = 0; i < window.tabs.size(); ++i) {
261 const sessions::SessionTab* tab = window.tabs[i]; 264 const sessions::SessionTab* tab = window.tabs[i];
262 if (tab->navigations.empty()) 265 if (tab->navigations.empty())
263 continue; 266 continue;
264 const sessions::SerializedNavigationEntry& current_navigation = 267 const sessions::SerializedNavigationEntry& current_navigation =
265 tab->navigations.at(tab->normalized_navigation_index()); 268 tab->navigations.at(tab->normalized_navigation_index());
266 if (search::IsNTPURL(current_navigation.virtual_url(), GetProfile())) { 269 if (search::IsNTPURL(current_navigation.virtual_url(), GetProfile())) {
267 continue; 270 continue;
268 } 271 }
269 tabs_in_window.push_back(tab); 272 tabs_in_window.push_back(tab);
270 } 273 }
271 if (tabs_in_window.empty()) 274 if (tabs_in_window.empty())
272 return scoped_ptr<windows::Window>(); 275 return std::unique_ptr<windows::Window>();
273 std::sort(tabs_in_window.begin(), tabs_in_window.end(), SortTabsByRecency); 276 std::sort(tabs_in_window.begin(), tabs_in_window.end(), SortTabsByRecency);
274 277
275 scoped_ptr<std::vector<tabs::Tab>> tabs(new std::vector<tabs::Tab>()); 278 std::unique_ptr<std::vector<tabs::Tab>> tabs(new std::vector<tabs::Tab>());
276 for (size_t i = 0; i < tabs_in_window.size(); ++i) { 279 for (size_t i = 0; i < tabs_in_window.size(); ++i) {
277 tabs->push_back(CreateTabModel(session_tag, *tabs_in_window[i], i, 280 tabs->push_back(CreateTabModel(session_tag, *tabs_in_window[i], i,
278 window.selected_tab_index)); 281 window.selected_tab_index));
279 } 282 }
280 283
281 std::string session_id = 284 std::string session_id =
282 SessionId(session_tag, window.window_id.id()).ToString(); 285 SessionId(session_tag, window.window_id.id()).ToString();
283 286
284 windows::WindowType type = windows::WINDOW_TYPE_NONE; 287 windows::WindowType type = windows::WINDOW_TYPE_NONE;
285 switch (window.type) { 288 switch (window.type) {
(...skipping 19 matching lines...) Expand all
305 break; 308 break;
306 case ui::SHOW_STATE_FULLSCREEN: 309 case ui::SHOW_STATE_FULLSCREEN:
307 state = windows::WINDOW_STATE_FULLSCREEN; 310 state = windows::WINDOW_STATE_FULLSCREEN;
308 break; 311 break;
309 case ui::SHOW_STATE_DEFAULT: 312 case ui::SHOW_STATE_DEFAULT:
310 case ui::SHOW_STATE_INACTIVE: 313 case ui::SHOW_STATE_INACTIVE:
311 case ui::SHOW_STATE_END: 314 case ui::SHOW_STATE_END:
312 break; 315 break;
313 } 316 }
314 317
315 scoped_ptr<windows::Window> window_struct( 318 std::unique_ptr<windows::Window> window_struct(
316 CreateWindowModelHelper(std::move(tabs), session_id, type, state)); 319 CreateWindowModelHelper(std::move(tabs), session_id, type, state));
317 // TODO(dwankri): Dig deeper to resolve bounds not being optional, so closed 320 // TODO(dwankri): Dig deeper to resolve bounds not being optional, so closed
318 // windows in GetRecentlyClosed can have set values in Window helper. 321 // windows in GetRecentlyClosed can have set values in Window helper.
319 window_struct->left.reset(new int(window.bounds.x())); 322 window_struct->left.reset(new int(window.bounds.x()));
320 window_struct->top.reset(new int(window.bounds.y())); 323 window_struct->top.reset(new int(window.bounds.y()));
321 window_struct->width.reset(new int(window.bounds.width())); 324 window_struct->width.reset(new int(window.bounds.width()));
322 window_struct->height.reset(new int(window.bounds.height())); 325 window_struct->height.reset(new int(window.bounds.height()));
323 326
324 return window_struct; 327 return window_struct;
325 } 328 }
326 329
327 scoped_ptr<api::sessions::Session> 330 std::unique_ptr<api::sessions::Session>
328 SessionsGetDevicesFunction::CreateSessionModel( 331 SessionsGetDevicesFunction::CreateSessionModel(
329 const sessions::SessionWindow& window, const std::string& session_tag) { 332 const sessions::SessionWindow& window,
330 scoped_ptr<windows::Window> window_model( 333 const std::string& session_tag) {
334 std::unique_ptr<windows::Window> window_model(
331 CreateWindowModel(window, session_tag)); 335 CreateWindowModel(window, session_tag));
332 // There is a chance that after pruning uninteresting tabs the window will be 336 // There is a chance that after pruning uninteresting tabs the window will be
333 // empty. 337 // empty.
334 return !window_model ? scoped_ptr<api::sessions::Session>() 338 return !window_model ? std::unique_ptr<api::sessions::Session>()
335 : CreateSessionModelHelper(window.timestamp.ToTimeT(), 339 : CreateSessionModelHelper(window.timestamp.ToTimeT(),
336 scoped_ptr<tabs::Tab>(), 340 std::unique_ptr<tabs::Tab>(),
337 std::move(window_model)); 341 std::move(window_model));
338 } 342 }
339 343
340 api::sessions::Device SessionsGetDevicesFunction::CreateDeviceModel( 344 api::sessions::Device SessionsGetDevicesFunction::CreateDeviceModel(
341 const sync_driver::SyncedSession* session) { 345 const sync_driver::SyncedSession* session) {
342 int max_results = api::sessions::MAX_SESSION_RESULTS; 346 int max_results = api::sessions::MAX_SESSION_RESULTS;
343 // Already validated in RunAsync(). 347 // Already validated in RunAsync().
344 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); 348 std::unique_ptr<GetDevices::Params> params(
349 GetDevices::Params::Create(*args_));
345 if (params->filter && params->filter->max_results) 350 if (params->filter && params->filter->max_results)
346 max_results = *params->filter->max_results; 351 max_results = *params->filter->max_results;
347 352
348 api::sessions::Device device_struct; 353 api::sessions::Device device_struct;
349 device_struct.info = session->session_name; 354 device_struct.info = session->session_name;
350 device_struct.device_name = session->session_name; 355 device_struct.device_name = session->session_name;
351 356
352 for (sync_driver::SyncedSession::SyncedWindowMap::const_iterator it = 357 for (sync_driver::SyncedSession::SyncedWindowMap::const_iterator it =
353 session->windows.begin(); 358 session->windows.begin();
354 it != session->windows.end() && 359 it != session->windows.end() &&
355 static_cast<int>(device_struct.sessions.size()) < max_results; 360 static_cast<int>(device_struct.sessions.size()) < max_results;
356 ++it) { 361 ++it) {
357 scoped_ptr<api::sessions::Session> session_model(CreateSessionModel( 362 std::unique_ptr<api::sessions::Session> session_model(
358 *it->second, session->session_tag)); 363 CreateSessionModel(*it->second, session->session_tag));
359 if (session_model) 364 if (session_model)
360 device_struct.sessions.push_back(std::move(*session_model)); 365 device_struct.sessions.push_back(std::move(*session_model));
361 } 366 }
362 return device_struct; 367 return device_struct;
363 } 368 }
364 369
365 bool SessionsGetDevicesFunction::RunSync() { 370 bool SessionsGetDevicesFunction::RunSync() {
366 ProfileSyncService* service = 371 ProfileSyncService* service =
367 ProfileSyncServiceFactory::GetInstance()->GetForProfile(GetProfile()); 372 ProfileSyncServiceFactory::GetInstance()->GetForProfile(GetProfile());
368 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { 373 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) {
369 // Sync not enabled. 374 // Sync not enabled.
370 results_ = 375 results_ =
371 GetDevices::Results::Create(std::vector<api::sessions::Device>()); 376 GetDevices::Results::Create(std::vector<api::sessions::Device>());
372 return true; 377 return true;
373 } 378 }
374 379
375 sync_driver::OpenTabsUIDelegate* open_tabs = service->GetOpenTabsUIDelegate(); 380 sync_driver::OpenTabsUIDelegate* open_tabs = service->GetOpenTabsUIDelegate();
376 std::vector<const sync_driver::SyncedSession*> sessions; 381 std::vector<const sync_driver::SyncedSession*> sessions;
377 if (!(open_tabs && open_tabs->GetAllForeignSessions(&sessions))) { 382 if (!(open_tabs && open_tabs->GetAllForeignSessions(&sessions))) {
378 results_ = 383 results_ =
379 GetDevices::Results::Create(std::vector<api::sessions::Device>()); 384 GetDevices::Results::Create(std::vector<api::sessions::Device>());
380 return true; 385 return true;
381 } 386 }
382 387
383 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); 388 std::unique_ptr<GetDevices::Params> params(
389 GetDevices::Params::Create(*args_));
384 EXTENSION_FUNCTION_VALIDATE(params); 390 EXTENSION_FUNCTION_VALIDATE(params);
385 if (params->filter && params->filter->max_results) { 391 if (params->filter && params->filter->max_results) {
386 EXTENSION_FUNCTION_VALIDATE(*params->filter->max_results >= 0 && 392 EXTENSION_FUNCTION_VALIDATE(*params->filter->max_results >= 0 &&
387 *params->filter->max_results <= api::sessions::MAX_SESSION_RESULTS); 393 *params->filter->max_results <= api::sessions::MAX_SESSION_RESULTS);
388 } 394 }
389 395
390 std::vector<api::sessions::Device> result; 396 std::vector<api::sessions::Device> result;
391 // Sort sessions from most recent to least recent. 397 // Sort sessions from most recent to least recent.
392 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency); 398 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency);
393 for (size_t i = 0; i < sessions.size(); ++i) 399 for (size_t i = 0; i < sessions.size(); ++i)
394 result.push_back(CreateDeviceModel(sessions[i])); 400 result.push_back(CreateDeviceModel(sessions[i]));
395 401
396 results_ = GetDevices::Results::Create(result); 402 results_ = GetDevices::Results::Create(result);
397 return true; 403 return true;
398 } 404 }
399 405
400 void SessionsRestoreFunction::SetInvalidIdError(const std::string& invalid_id) { 406 void SessionsRestoreFunction::SetInvalidIdError(const std::string& invalid_id) {
401 SetError(ErrorUtils::FormatErrorMessage(kInvalidSessionIdError, invalid_id)); 407 SetError(ErrorUtils::FormatErrorMessage(kInvalidSessionIdError, invalid_id));
402 } 408 }
403 409
404 410
405 void SessionsRestoreFunction::SetResultRestoredTab( 411 void SessionsRestoreFunction::SetResultRestoredTab(
406 content::WebContents* contents) { 412 content::WebContents* contents) {
407 scoped_ptr<tabs::Tab> tab( 413 std::unique_ptr<tabs::Tab> tab(
408 ExtensionTabUtil::CreateTabObject(contents, extension())); 414 ExtensionTabUtil::CreateTabObject(contents, extension()));
409 scoped_ptr<api::sessions::Session> restored_session( 415 std::unique_ptr<api::sessions::Session> restored_session(
410 CreateSessionModelHelper(base::Time::Now().ToTimeT(), std::move(tab), 416 CreateSessionModelHelper(base::Time::Now().ToTimeT(), std::move(tab),
411 scoped_ptr<windows::Window>())); 417 std::unique_ptr<windows::Window>()));
412 results_ = Restore::Results::Create(*restored_session); 418 results_ = Restore::Results::Create(*restored_session);
413 } 419 }
414 420
415 bool SessionsRestoreFunction::SetResultRestoredWindow(int window_id) { 421 bool SessionsRestoreFunction::SetResultRestoredWindow(int window_id) {
416 WindowController* controller = NULL; 422 WindowController* controller = NULL;
417 if (!windows_util::GetWindowFromWindowID(this, window_id, 0, &controller)) { 423 if (!windows_util::GetWindowFromWindowID(this, window_id, 0, &controller)) {
418 // error_ is set by GetWindowFromWindowId function call. 424 // error_ is set by GetWindowFromWindowId function call.
419 return false; 425 return false;
420 } 426 }
421 scoped_ptr<base::DictionaryValue> window_value( 427 std::unique_ptr<base::DictionaryValue> window_value(
422 controller->CreateWindowValueWithTabs(extension())); 428 controller->CreateWindowValueWithTabs(extension()));
423 scoped_ptr<windows::Window> window(windows::Window::FromValue( 429 std::unique_ptr<windows::Window> window(
424 *window_value)); 430 windows::Window::FromValue(*window_value));
425 results_ = Restore::Results::Create(*CreateSessionModelHelper( 431 results_ = Restore::Results::Create(*CreateSessionModelHelper(
426 base::Time::Now().ToTimeT(), scoped_ptr<tabs::Tab>(), std::move(window))); 432 base::Time::Now().ToTimeT(), std::unique_ptr<tabs::Tab>(),
433 std::move(window)));
427 return true; 434 return true;
428 } 435 }
429 436
430 bool SessionsRestoreFunction::RestoreMostRecentlyClosed(Browser* browser) { 437 bool SessionsRestoreFunction::RestoreMostRecentlyClosed(Browser* browser) {
431 sessions::TabRestoreService* tab_restore_service = 438 sessions::TabRestoreService* tab_restore_service =
432 TabRestoreServiceFactory::GetForProfile(GetProfile()); 439 TabRestoreServiceFactory::GetForProfile(GetProfile());
433 sessions::TabRestoreService::Entries entries = tab_restore_service->entries(); 440 sessions::TabRestoreService::Entries entries = tab_restore_service->entries();
434 441
435 if (entries.empty()) { 442 if (entries.empty()) {
436 SetError(kNoRecentlyClosedSessionsError); 443 SetError(kNoRecentlyClosedSessionsError);
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 558
552 // Only restore one window at a time. 559 // Only restore one window at a time.
553 std::vector<Browser*> browsers = SessionRestore::RestoreForeignSessionWindows( 560 std::vector<Browser*> browsers = SessionRestore::RestoreForeignSessionWindows(
554 GetProfile(), window, window + 1); 561 GetProfile(), window, window + 1);
555 // Will always create one browser because we only restore one window per call. 562 // Will always create one browser because we only restore one window per call.
556 DCHECK_EQ(1u, browsers.size()); 563 DCHECK_EQ(1u, browsers.size());
557 return SetResultRestoredWindow(ExtensionTabUtil::GetWindowId(browsers[0])); 564 return SetResultRestoredWindow(ExtensionTabUtil::GetWindowId(browsers[0]));
558 } 565 }
559 566
560 bool SessionsRestoreFunction::RunSync() { 567 bool SessionsRestoreFunction::RunSync() {
561 scoped_ptr<Restore::Params> params(Restore::Params::Create(*args_)); 568 std::unique_ptr<Restore::Params> params(Restore::Params::Create(*args_));
562 EXTENSION_FUNCTION_VALIDATE(params); 569 EXTENSION_FUNCTION_VALIDATE(params);
563 570
564 Browser* browser = chrome::FindBrowserWithProfile(GetProfile()); 571 Browser* browser = chrome::FindBrowserWithProfile(GetProfile());
565 if (!browser) { 572 if (!browser) {
566 SetError(kNoBrowserToRestoreSession); 573 SetError(kNoBrowserToRestoreSession);
567 return false; 574 return false;
568 } 575 }
569 576
570 if (GetProfile() != GetProfile()->GetOriginalProfile()) { 577 if (GetProfile() != GetProfile()->GetOriginalProfile()) {
571 SetError(kRestoreInIncognitoError); 578 SetError(kRestoreInIncognitoError);
572 return false; 579 return false;
573 } 580 }
574 581
575 if (!params->session_id) 582 if (!params->session_id)
576 return RestoreMostRecentlyClosed(browser); 583 return RestoreMostRecentlyClosed(browser);
577 584
578 scoped_ptr<SessionId> session_id(SessionId::Parse(*params->session_id)); 585 std::unique_ptr<SessionId> session_id(SessionId::Parse(*params->session_id));
579 if (!session_id) { 586 if (!session_id) {
580 SetInvalidIdError(*params->session_id); 587 SetInvalidIdError(*params->session_id);
581 return false; 588 return false;
582 } 589 }
583 590
584 return session_id->IsForeign() ? 591 return session_id->IsForeign() ?
585 RestoreForeignSession(*session_id, browser) 592 RestoreForeignSession(*session_id, browser)
586 : RestoreLocalSession(*session_id, browser); 593 : RestoreLocalSession(*session_id, browser);
587 } 594 }
588 595
589 SessionsEventRouter::SessionsEventRouter(Profile* profile) 596 SessionsEventRouter::SessionsEventRouter(Profile* profile)
590 : profile_(profile), 597 : profile_(profile),
591 tab_restore_service_(TabRestoreServiceFactory::GetForProfile(profile)) { 598 tab_restore_service_(TabRestoreServiceFactory::GetForProfile(profile)) {
592 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in 599 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in
593 // incognito mode) 600 // incognito mode)
594 if (tab_restore_service_) { 601 if (tab_restore_service_) {
595 tab_restore_service_->LoadTabsFromLastSession(); 602 tab_restore_service_->LoadTabsFromLastSession();
596 tab_restore_service_->AddObserver(this); 603 tab_restore_service_->AddObserver(this);
597 } 604 }
598 } 605 }
599 606
600 SessionsEventRouter::~SessionsEventRouter() { 607 SessionsEventRouter::~SessionsEventRouter() {
601 if (tab_restore_service_) 608 if (tab_restore_service_)
602 tab_restore_service_->RemoveObserver(this); 609 tab_restore_service_->RemoveObserver(this);
603 } 610 }
604 611
605 void SessionsEventRouter::TabRestoreServiceChanged( 612 void SessionsEventRouter::TabRestoreServiceChanged(
606 sessions::TabRestoreService* service) { 613 sessions::TabRestoreService* service) {
607 scoped_ptr<base::ListValue> args(new base::ListValue()); 614 std::unique_ptr<base::ListValue> args(new base::ListValue());
608 EventRouter::Get(profile_)->BroadcastEvent(make_scoped_ptr( 615 EventRouter::Get(profile_)->BroadcastEvent(base::WrapUnique(
609 new Event(events::SESSIONS_ON_CHANGED, 616 new Event(events::SESSIONS_ON_CHANGED,
610 api::sessions::OnChanged::kEventName, std::move(args)))); 617 api::sessions::OnChanged::kEventName, std::move(args))));
611 } 618 }
612 619
613 void SessionsEventRouter::TabRestoreServiceDestroyed( 620 void SessionsEventRouter::TabRestoreServiceDestroyed(
614 sessions::TabRestoreService* service) { 621 sessions::TabRestoreService* service) {
615 tab_restore_service_ = NULL; 622 tab_restore_service_ = NULL;
616 } 623 }
617 624
618 SessionsAPI::SessionsAPI(content::BrowserContext* context) 625 SessionsAPI::SessionsAPI(content::BrowserContext* context)
(...skipping 17 matching lines...) Expand all
636 return g_factory.Pointer(); 643 return g_factory.Pointer();
637 } 644 }
638 645
639 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) { 646 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) {
640 sessions_event_router_.reset( 647 sessions_event_router_.reset(
641 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_))); 648 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_)));
642 EventRouter::Get(browser_context_)->UnregisterObserver(this); 649 EventRouter::Get(browser_context_)->UnregisterObserver(this);
643 } 650 }
644 651
645 } // namespace extensions 652 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/sessions/sessions_api.h ('k') | chrome/browser/extensions/api/sessions/sessions_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698