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

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

Issue 2408763002: [Extensions] Convert some ChromeSyncExtensionFunctions (Closed)
Patch Set: foo4 Created 4 years, 2 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
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 } 69 }
70 70
71 // Comparator function for use with std::sort that will sort tabs in a window 71 // Comparator function for use with std::sort that will sort tabs in a window
72 // by descending timestamp (i.e., most recent first). 72 // by descending timestamp (i.e., most recent first).
73 bool SortTabsByRecency(const sessions::SessionTab* t1, 73 bool SortTabsByRecency(const sessions::SessionTab* t1,
74 const sessions::SessionTab* t2) { 74 const sessions::SessionTab* t2) {
75 return t1->timestamp > t2->timestamp; 75 return t1->timestamp > t2->timestamp;
76 } 76 }
77 77
78 tabs::Tab CreateTabModelHelper( 78 tabs::Tab CreateTabModelHelper(
79 Profile* profile,
80 const sessions::SerializedNavigationEntry& current_navigation, 79 const sessions::SerializedNavigationEntry& current_navigation,
81 const std::string& session_id, 80 const std::string& session_id,
82 int index, 81 int index,
83 bool pinned, 82 bool pinned,
84 bool active, 83 bool active,
85 const Extension* extension) { 84 const Extension* extension) {
86 tabs::Tab tab_struct; 85 tabs::Tab tab_struct;
87 86
88 const GURL& url = current_navigation.virtual_url(); 87 const GURL& url = current_navigation.virtual_url();
89 std::string title = base::UTF16ToUTF8(current_navigation.title()); 88 std::string title = base::UTF16ToUTF8(current_navigation.title());
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 138
140 bool is_window_entry(const sessions::TabRestoreService::Entry& entry) { 139 bool is_window_entry(const sessions::TabRestoreService::Entry& entry) {
141 return entry.type == sessions::TabRestoreService::WINDOW; 140 return entry.type == sessions::TabRestoreService::WINDOW;
142 } 141 }
143 142
144 } // namespace 143 } // namespace
145 144
146 tabs::Tab SessionsGetRecentlyClosedFunction::CreateTabModel( 145 tabs::Tab SessionsGetRecentlyClosedFunction::CreateTabModel(
147 const sessions::TabRestoreService::Tab& tab, 146 const sessions::TabRestoreService::Tab& tab,
148 bool active) { 147 bool active) {
149 return CreateTabModelHelper(GetProfile(), 148 return CreateTabModelHelper(tab.navigations[tab.current_navigation_index],
150 tab.navigations[tab.current_navigation_index],
151 base::IntToString(tab.id), tab.tabstrip_index, 149 base::IntToString(tab.id), tab.tabstrip_index,
152 tab.pinned, active, extension()); 150 tab.pinned, active, extension());
153 } 151 }
154 152
155 std::unique_ptr<windows::Window> 153 std::unique_ptr<windows::Window>
156 SessionsGetRecentlyClosedFunction::CreateWindowModel( 154 SessionsGetRecentlyClosedFunction::CreateWindowModel(
157 const sessions::TabRestoreService::Window& window) { 155 const sessions::TabRestoreService::Window& window) {
158 DCHECK(!window.tabs.empty()); 156 DCHECK(!window.tabs.empty());
159 157
160 auto tabs = base::MakeUnique<std::vector<tabs::Tab>>(); 158 auto tabs = base::MakeUnique<std::vector<tabs::Tab>>();
(...skipping 20 matching lines...) Expand all
181 window = CreateWindowModel( 179 window = CreateWindowModel(
182 static_cast<const sessions::TabRestoreService::Window&>(entry)); 180 static_cast<const sessions::TabRestoreService::Window&>(entry));
183 break; 181 break;
184 default: 182 default:
185 NOTREACHED(); 183 NOTREACHED();
186 } 184 }
187 return CreateSessionModelHelper(entry.timestamp.ToTimeT(), std::move(tab), 185 return CreateSessionModelHelper(entry.timestamp.ToTimeT(), std::move(tab),
188 std::move(window)); 186 std::move(window));
189 } 187 }
190 188
191 bool SessionsGetRecentlyClosedFunction::RunSync() { 189 ExtensionFunction::ResponseAction SessionsGetRecentlyClosedFunction::Run() {
192 std::unique_ptr<GetRecentlyClosed::Params> params( 190 std::unique_ptr<GetRecentlyClosed::Params> params(
193 GetRecentlyClosed::Params::Create(*args_)); 191 GetRecentlyClosed::Params::Create(*args_));
194 EXTENSION_FUNCTION_VALIDATE(params); 192 EXTENSION_FUNCTION_VALIDATE(params);
195 int max_results = api::sessions::MAX_SESSION_RESULTS; 193 int max_results = api::sessions::MAX_SESSION_RESULTS;
196 if (params->filter && params->filter->max_results) 194 if (params->filter && params->filter->max_results)
197 max_results = *params->filter->max_results; 195 max_results = *params->filter->max_results;
198 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && 196 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 &&
199 max_results <= api::sessions::MAX_SESSION_RESULTS); 197 max_results <= api::sessions::MAX_SESSION_RESULTS);
200 198
201 std::vector<api::sessions::Session> result; 199 std::vector<api::sessions::Session> result;
202 sessions::TabRestoreService* tab_restore_service = 200 sessions::TabRestoreService* tab_restore_service =
203 TabRestoreServiceFactory::GetForProfile(GetProfile()); 201 TabRestoreServiceFactory::GetForProfile(
202 Profile::FromBrowserContext(browser_context()));
204 203
205 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in 204 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in
206 // incognito mode) 205 // incognito mode)
207 if (!tab_restore_service) { 206 if (!tab_restore_service) {
208 DCHECK_NE(GetProfile(), GetProfile()->GetOriginalProfile()) 207 DCHECK(browser_context()->IsOffTheRecord())
209 << "sessions::TabRestoreService expected for normal profiles"; 208 << "sessions::TabRestoreService expected for normal profiles";
210 results_ = GetRecentlyClosed::Results::Create(result); 209 return RespondNow(ArgumentList(GetRecentlyClosed::Results::Create(result)));
211 return true;
212 } 210 }
213 211
214 // List of entries. They are ordered from most to least recent. 212 // List of entries. They are ordered from most to least recent.
215 // We prune the list to contain max 25 entries at any time and removes 213 // We prune the list to contain max 25 entries at any time and removes
216 // uninteresting entries. 214 // uninteresting entries.
217 for (const auto& entry : tab_restore_service->entries()) { 215 for (const auto& entry : tab_restore_service->entries()) {
218 result.push_back(std::move(*CreateSessionModel(*entry))); 216 result.push_back(std::move(*CreateSessionModel(*entry)));
219 } 217 }
220 218
221 results_ = GetRecentlyClosed::Results::Create(result); 219 return RespondNow(ArgumentList(GetRecentlyClosed::Results::Create(result)));
222 return true;
223 } 220 }
224 221
225 tabs::Tab SessionsGetDevicesFunction::CreateTabModel( 222 tabs::Tab SessionsGetDevicesFunction::CreateTabModel(
226 const std::string& session_tag, 223 const std::string& session_tag,
227 const sessions::SessionTab& tab, 224 const sessions::SessionTab& tab,
228 int tab_index, 225 int tab_index,
229 bool active) { 226 bool active) {
230 std::string session_id = SessionId(session_tag, tab.tab_id.id()).ToString(); 227 std::string session_id = SessionId(session_tag, tab.tab_id.id()).ToString();
231 return CreateTabModelHelper( 228 return CreateTabModelHelper(
232 GetProfile(), tab.navigations[tab.normalized_navigation_index()], 229 tab.navigations[tab.normalized_navigation_index()], session_id, tab_index,
233 session_id, tab_index, tab.pinned, active, extension()); 230 tab.pinned, active, extension());
234 } 231 }
235 232
236 std::unique_ptr<windows::Window> SessionsGetDevicesFunction::CreateWindowModel( 233 std::unique_ptr<windows::Window> SessionsGetDevicesFunction::CreateWindowModel(
237 const sessions::SessionWindow& window, 234 const sessions::SessionWindow& window,
238 const std::string& session_tag) { 235 const std::string& session_tag) {
239 DCHECK(!window.tabs.empty()); 236 DCHECK(!window.tabs.empty());
240 237
241 // Prune tabs that are not syncable or are NewTabPage. Then, sort the tabs 238 // Prune tabs that are not syncable or are NewTabPage. Then, sort the tabs
242 // from most recent to least recent. 239 // from most recent to least recent.
243 std::vector<const sessions::SessionTab*> tabs_in_window; 240 std::vector<const sessions::SessionTab*> tabs_in_window;
244 for (size_t i = 0; i < window.tabs.size(); ++i) { 241 for (size_t i = 0; i < window.tabs.size(); ++i) {
245 const sessions::SessionTab* tab = window.tabs[i].get(); 242 const sessions::SessionTab* tab = window.tabs[i].get();
246 if (tab->navigations.empty()) 243 if (tab->navigations.empty())
247 continue; 244 continue;
248 const sessions::SerializedNavigationEntry& current_navigation = 245 const sessions::SerializedNavigationEntry& current_navigation =
249 tab->navigations.at(tab->normalized_navigation_index()); 246 tab->navigations.at(tab->normalized_navigation_index());
250 if (search::IsNTPURL(current_navigation.virtual_url(), GetProfile())) { 247 if (search::IsNTPURL(current_navigation.virtual_url(),
248 Profile::FromBrowserContext(browser_context()))) {
251 continue; 249 continue;
252 } 250 }
253 tabs_in_window.push_back(tab); 251 tabs_in_window.push_back(tab);
254 } 252 }
255 if (tabs_in_window.empty()) 253 if (tabs_in_window.empty())
256 return std::unique_ptr<windows::Window>(); 254 return std::unique_ptr<windows::Window>();
257 std::sort(tabs_in_window.begin(), tabs_in_window.end(), SortTabsByRecency); 255 std::sort(tabs_in_window.begin(), tabs_in_window.end(), SortTabsByRecency);
258 256
259 std::unique_ptr<std::vector<tabs::Tab>> tabs(new std::vector<tabs::Tab>()); 257 std::unique_ptr<std::vector<tabs::Tab>> tabs(new std::vector<tabs::Tab>());
260 for (size_t i = 0; i < tabs_in_window.size(); ++i) { 258 for (size_t i = 0; i < tabs_in_window.size(); ++i) {
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 static_cast<int>(device_struct.sessions.size()) < max_results; 338 static_cast<int>(device_struct.sessions.size()) < max_results;
341 ++it) { 339 ++it) {
342 std::unique_ptr<api::sessions::Session> session_model( 340 std::unique_ptr<api::sessions::Session> session_model(
343 CreateSessionModel(*it->second, session->session_tag)); 341 CreateSessionModel(*it->second, session->session_tag));
344 if (session_model) 342 if (session_model)
345 device_struct.sessions.push_back(std::move(*session_model)); 343 device_struct.sessions.push_back(std::move(*session_model));
346 } 344 }
347 return device_struct; 345 return device_struct;
348 } 346 }
349 347
350 bool SessionsGetDevicesFunction::RunSync() { 348 ExtensionFunction::ResponseAction SessionsGetDevicesFunction::Run() {
351 browser_sync::ProfileSyncService* service = 349 browser_sync::ProfileSyncService* service =
352 ProfileSyncServiceFactory::GetInstance()->GetForProfile(GetProfile()); 350 ProfileSyncServiceFactory::GetInstance()->GetForProfile(
351 Profile::FromBrowserContext(browser_context()));
353 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { 352 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) {
354 // Sync not enabled. 353 // Sync not enabled.
355 results_ = 354 return RespondNow(ArgumentList(
356 GetDevices::Results::Create(std::vector<api::sessions::Device>()); 355 GetDevices::Results::Create(std::vector<api::sessions::Device>())));
357 return true;
358 } 356 }
359 357
360 sync_sessions::OpenTabsUIDelegate* open_tabs = 358 sync_sessions::OpenTabsUIDelegate* open_tabs =
361 service->GetOpenTabsUIDelegate(); 359 service->GetOpenTabsUIDelegate();
362 std::vector<const sync_sessions::SyncedSession*> sessions; 360 std::vector<const sync_sessions::SyncedSession*> sessions;
363 if (!(open_tabs && open_tabs->GetAllForeignSessions(&sessions))) { 361 if (!(open_tabs && open_tabs->GetAllForeignSessions(&sessions))) {
364 results_ = 362 return RespondNow(ArgumentList(
365 GetDevices::Results::Create(std::vector<api::sessions::Device>()); 363 GetDevices::Results::Create(std::vector<api::sessions::Device>())));
366 return true;
367 } 364 }
368 365
369 std::unique_ptr<GetDevices::Params> params( 366 std::unique_ptr<GetDevices::Params> params(
370 GetDevices::Params::Create(*args_)); 367 GetDevices::Params::Create(*args_));
371 EXTENSION_FUNCTION_VALIDATE(params); 368 EXTENSION_FUNCTION_VALIDATE(params);
372 if (params->filter && params->filter->max_results) { 369 if (params->filter && params->filter->max_results) {
373 EXTENSION_FUNCTION_VALIDATE(*params->filter->max_results >= 0 && 370 EXTENSION_FUNCTION_VALIDATE(*params->filter->max_results >= 0 &&
374 *params->filter->max_results <= api::sessions::MAX_SESSION_RESULTS); 371 *params->filter->max_results <= api::sessions::MAX_SESSION_RESULTS);
375 } 372 }
376 373
377 std::vector<api::sessions::Device> result; 374 std::vector<api::sessions::Device> result;
378 // Sort sessions from most recent to least recent. 375 // Sort sessions from most recent to least recent.
379 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency); 376 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency);
380 for (size_t i = 0; i < sessions.size(); ++i) 377 for (size_t i = 0; i < sessions.size(); ++i)
381 result.push_back(CreateDeviceModel(sessions[i])); 378 result.push_back(CreateDeviceModel(sessions[i]));
382 379
383 results_ = GetDevices::Results::Create(result); 380 return RespondNow(ArgumentList(GetDevices::Results::Create(result)));
384 return true;
385 } 381 }
386 382
387 void SessionsRestoreFunction::SetInvalidIdError(const std::string& invalid_id) { 383 ExtensionFunction::ResponseValue SessionsRestoreFunction::GetRestoredTabResult(
388 SetError(ErrorUtils::FormatErrorMessage(kInvalidSessionIdError, invalid_id));
389 }
390
391
392 void SessionsRestoreFunction::SetResultRestoredTab(
393 content::WebContents* contents) { 384 content::WebContents* contents) {
394 std::unique_ptr<tabs::Tab> tab( 385 std::unique_ptr<tabs::Tab> tab(
395 ExtensionTabUtil::CreateTabObject(contents, extension())); 386 ExtensionTabUtil::CreateTabObject(contents, extension()));
396 std::unique_ptr<api::sessions::Session> restored_session( 387 std::unique_ptr<api::sessions::Session> restored_session(
397 CreateSessionModelHelper(base::Time::Now().ToTimeT(), std::move(tab), 388 CreateSessionModelHelper(base::Time::Now().ToTimeT(), std::move(tab),
398 std::unique_ptr<windows::Window>())); 389 std::unique_ptr<windows::Window>()));
399 results_ = Restore::Results::Create(*restored_session); 390 return ArgumentList(Restore::Results::Create(*restored_session));
400 } 391 }
401 392
402 bool SessionsRestoreFunction::SetResultRestoredWindow(int window_id) { 393 ExtensionFunction::ResponseValue
394 SessionsRestoreFunction::GetRestoredWindowResult(int window_id) {
403 WindowController* controller = NULL; 395 WindowController* controller = NULL;
396 std::string error;
404 if (!windows_util::GetWindowFromWindowID(this, window_id, 0, &controller, 397 if (!windows_util::GetWindowFromWindowID(this, window_id, 0, &controller,
405 &error_)) { 398 &error)) {
406 return false; 399 return Error(error);
407 } 400 }
408 std::unique_ptr<base::DictionaryValue> window_value( 401 std::unique_ptr<base::DictionaryValue> window_value(
409 controller->CreateWindowValueWithTabs(extension())); 402 controller->CreateWindowValueWithTabs(extension()));
410 std::unique_ptr<windows::Window> window( 403 std::unique_ptr<windows::Window> window(
411 windows::Window::FromValue(*window_value)); 404 windows::Window::FromValue(*window_value));
412 results_ = Restore::Results::Create(*CreateSessionModelHelper( 405 return ArgumentList(Restore::Results::Create(*CreateSessionModelHelper(
413 base::Time::Now().ToTimeT(), std::unique_ptr<tabs::Tab>(), 406 base::Time::Now().ToTimeT(), std::unique_ptr<tabs::Tab>(),
414 std::move(window))); 407 std::move(window))));
415 return true;
416 } 408 }
417 409
418 bool SessionsRestoreFunction::RestoreMostRecentlyClosed(Browser* browser) { 410 ExtensionFunction::ResponseValue
411 SessionsRestoreFunction::RestoreMostRecentlyClosed(Browser* browser) {
419 sessions::TabRestoreService* tab_restore_service = 412 sessions::TabRestoreService* tab_restore_service =
420 TabRestoreServiceFactory::GetForProfile(GetProfile()); 413 TabRestoreServiceFactory::GetForProfile(
414 Profile::FromBrowserContext(browser_context()));
421 const sessions::TabRestoreService::Entries& entries = 415 const sessions::TabRestoreService::Entries& entries =
422 tab_restore_service->entries(); 416 tab_restore_service->entries();
423 417
424 if (entries.empty()) { 418 if (entries.empty())
425 SetError(kNoRecentlyClosedSessionsError); 419 return Error(kNoRecentlyClosedSessionsError);
426 return false;
427 }
428 420
429 bool is_window = is_window_entry(*entries.front()); 421 bool is_window = is_window_entry(*entries.front());
430 sessions::LiveTabContext* context = 422 sessions::LiveTabContext* context =
431 BrowserLiveTabContext::FindContextForWebContents( 423 BrowserLiveTabContext::FindContextForWebContents(
432 browser->tab_strip_model()->GetActiveWebContents()); 424 browser->tab_strip_model()->GetActiveWebContents());
433 std::vector<sessions::LiveTab*> restored_tabs = 425 std::vector<sessions::LiveTab*> restored_tabs =
434 tab_restore_service->RestoreMostRecentEntry(context); 426 tab_restore_service->RestoreMostRecentEntry(context);
435 DCHECK(restored_tabs.size()); 427 DCHECK(restored_tabs.size());
436 428
437 sessions::ContentLiveTab* first_tab = 429 sessions::ContentLiveTab* first_tab =
438 static_cast<sessions::ContentLiveTab*>(restored_tabs[0]); 430 static_cast<sessions::ContentLiveTab*>(restored_tabs[0]);
439 if (is_window) { 431 if (is_window) {
440 return SetResultRestoredWindow( 432 return GetRestoredWindowResult(
441 ExtensionTabUtil::GetWindowIdOfTab(first_tab->web_contents())); 433 ExtensionTabUtil::GetWindowIdOfTab(first_tab->web_contents()));
442 } 434 }
443 435
444 SetResultRestoredTab(first_tab->web_contents()); 436 return GetRestoredTabResult(first_tab->web_contents());
445 return true;
446 } 437 }
447 438
448 bool SessionsRestoreFunction::RestoreLocalSession(const SessionId& session_id, 439 ExtensionFunction::ResponseValue SessionsRestoreFunction::RestoreLocalSession(
449 Browser* browser) { 440 const SessionId& session_id,
441 Browser* browser) {
450 sessions::TabRestoreService* tab_restore_service = 442 sessions::TabRestoreService* tab_restore_service =
451 TabRestoreServiceFactory::GetForProfile(GetProfile()); 443 TabRestoreServiceFactory::GetForProfile(
444 Profile::FromBrowserContext(browser_context()));
452 const sessions::TabRestoreService::Entries& entries = 445 const sessions::TabRestoreService::Entries& entries =
453 tab_restore_service->entries(); 446 tab_restore_service->entries();
454 447
455 if (entries.empty()) { 448 if (entries.empty())
456 SetInvalidIdError(session_id.ToString()); 449 return Error(kInvalidSessionIdError, session_id.ToString());
457 return false;
458 }
459 450
460 // Check if the recently closed list contains an entry with the provided id. 451 // Check if the recently closed list contains an entry with the provided id.
461 bool is_window = false; 452 bool is_window = false;
462 for (const auto& entry : entries) { 453 for (const auto& entry : entries) {
463 if (entry->id == session_id.id()) { 454 if (entry->id == session_id.id()) {
464 // A full window is being restored only if the entry ID 455 // A full window is being restored only if the entry ID
465 // matches the provided ID and the entry type is Window. 456 // matches the provided ID and the entry type is Window.
466 is_window = is_window_entry(*entry); 457 is_window = is_window_entry(*entry);
467 break; 458 break;
468 } 459 }
469 } 460 }
470 461
471 sessions::LiveTabContext* context = 462 sessions::LiveTabContext* context =
472 BrowserLiveTabContext::FindContextForWebContents( 463 BrowserLiveTabContext::FindContextForWebContents(
473 browser->tab_strip_model()->GetActiveWebContents()); 464 browser->tab_strip_model()->GetActiveWebContents());
474 std::vector<sessions::LiveTab*> restored_tabs = 465 std::vector<sessions::LiveTab*> restored_tabs =
475 tab_restore_service->RestoreEntryById(context, session_id.id(), 466 tab_restore_service->RestoreEntryById(context, session_id.id(),
476 WindowOpenDisposition::UNKNOWN); 467 WindowOpenDisposition::UNKNOWN);
477 // If the ID is invalid, restored_tabs will be empty. 468 // If the ID is invalid, restored_tabs will be empty.
478 if (restored_tabs.empty()) { 469 if (restored_tabs.empty())
479 SetInvalidIdError(session_id.ToString()); 470 return Error(kInvalidSessionIdError, session_id.ToString());
480 return false;
481 }
482 471
483 sessions::ContentLiveTab* first_tab = 472 sessions::ContentLiveTab* first_tab =
484 static_cast<sessions::ContentLiveTab*>(restored_tabs[0]); 473 static_cast<sessions::ContentLiveTab*>(restored_tabs[0]);
485 474
486 // Retrieve the window through any of the tabs in restored_tabs. 475 // Retrieve the window through any of the tabs in restored_tabs.
487 if (is_window) { 476 if (is_window) {
488 return SetResultRestoredWindow( 477 return GetRestoredWindowResult(
489 ExtensionTabUtil::GetWindowIdOfTab(first_tab->web_contents())); 478 ExtensionTabUtil::GetWindowIdOfTab(first_tab->web_contents()));
490 } 479 }
491 480
492 SetResultRestoredTab(first_tab->web_contents()); 481 return GetRestoredTabResult(first_tab->web_contents());
493 return true;
494 } 482 }
495 483
496 bool SessionsRestoreFunction::RestoreForeignSession(const SessionId& session_id, 484 ExtensionFunction::ResponseValue SessionsRestoreFunction::RestoreForeignSession(
497 Browser* browser) { 485 const SessionId& session_id,
486 Browser* browser) {
487 Profile* profile = Profile::FromBrowserContext(browser_context());
498 browser_sync::ProfileSyncService* service = 488 browser_sync::ProfileSyncService* service =
499 ProfileSyncServiceFactory::GetInstance()->GetForProfile(GetProfile()); 489 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
500 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { 490 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS)))
501 SetError(kSessionSyncError); 491 return Error(kSessionSyncError);
502 return false;
503 }
504 sync_sessions::OpenTabsUIDelegate* open_tabs = 492 sync_sessions::OpenTabsUIDelegate* open_tabs =
505 service->GetOpenTabsUIDelegate(); 493 service->GetOpenTabsUIDelegate();
506 if (!open_tabs) { 494 if (!open_tabs)
507 SetError(kSessionSyncError); 495 return Error(kSessionSyncError);
508 return false;
509 }
510 496
511 const sessions::SessionTab* tab = NULL; 497 const sessions::SessionTab* tab = NULL;
512 if (open_tabs->GetForeignTab(session_id.session_tag(), 498 if (open_tabs->GetForeignTab(session_id.session_tag(),
513 session_id.id(), 499 session_id.id(),
514 &tab)) { 500 &tab)) {
515 TabStripModel* tab_strip = browser->tab_strip_model(); 501 TabStripModel* tab_strip = browser->tab_strip_model();
516 content::WebContents* contents = tab_strip->GetActiveWebContents(); 502 content::WebContents* contents = tab_strip->GetActiveWebContents();
517 503
518 content::WebContents* tab_contents = 504 content::WebContents* tab_contents =
519 SessionRestore::RestoreForeignSessionTab( 505 SessionRestore::RestoreForeignSessionTab(
520 contents, *tab, WindowOpenDisposition::NEW_FOREGROUND_TAB); 506 contents, *tab, WindowOpenDisposition::NEW_FOREGROUND_TAB);
521 SetResultRestoredTab(tab_contents); 507 return GetRestoredTabResult(tab_contents);
522 return true;
523 } 508 }
524 509
525 // Restoring a full window. 510 // Restoring a full window.
526 std::vector<const sessions::SessionWindow*> windows; 511 std::vector<const sessions::SessionWindow*> windows;
527 if (!open_tabs->GetForeignSession(session_id.session_tag(), &windows)) { 512 if (!open_tabs->GetForeignSession(session_id.session_tag(), &windows))
528 SetInvalidIdError(session_id.ToString()); 513 return Error(kInvalidSessionIdError, session_id.ToString());
529 return false;
530 }
531 514
532 std::vector<const sessions::SessionWindow*>::const_iterator window = 515 std::vector<const sessions::SessionWindow*>::const_iterator window =
533 windows.begin(); 516 windows.begin();
534 while (window != windows.end() 517 while (window != windows.end()
535 && (*window)->window_id.id() != session_id.id()) { 518 && (*window)->window_id.id() != session_id.id()) {
536 ++window; 519 ++window;
537 } 520 }
538 if (window == windows.end()) { 521 if (window == windows.end())
539 SetInvalidIdError(session_id.ToString()); 522 return Error(kInvalidSessionIdError, session_id.ToString());
540 return false;
541 }
542 523
543 // Only restore one window at a time. 524 // Only restore one window at a time.
544 std::vector<Browser*> browsers = SessionRestore::RestoreForeignSessionWindows( 525 std::vector<Browser*> browsers =
545 GetProfile(), window, window + 1); 526 SessionRestore::RestoreForeignSessionWindows(profile, window, window + 1);
546 // Will always create one browser because we only restore one window per call. 527 // Will always create one browser because we only restore one window per call.
547 DCHECK_EQ(1u, browsers.size()); 528 DCHECK_EQ(1u, browsers.size());
548 return SetResultRestoredWindow(ExtensionTabUtil::GetWindowId(browsers[0])); 529 return GetRestoredWindowResult(ExtensionTabUtil::GetWindowId(browsers[0]));
549 } 530 }
550 531
551 bool SessionsRestoreFunction::RunSync() { 532 ExtensionFunction::ResponseAction SessionsRestoreFunction::Run() {
552 std::unique_ptr<Restore::Params> params(Restore::Params::Create(*args_)); 533 std::unique_ptr<Restore::Params> params(Restore::Params::Create(*args_));
553 EXTENSION_FUNCTION_VALIDATE(params); 534 EXTENSION_FUNCTION_VALIDATE(params);
554 535
555 Browser* browser = chrome::FindBrowserWithProfile(GetProfile()); 536 Profile* profile = Profile::FromBrowserContext(browser_context());
556 if (!browser) { 537 Browser* browser = chrome::FindBrowserWithProfile(profile);
557 SetError(kNoBrowserToRestoreSession); 538 if (!browser)
558 return false; 539 return RespondNow(Error(kNoBrowserToRestoreSession));
559 }
560 540
561 if (GetProfile() != GetProfile()->GetOriginalProfile()) { 541 if (profile != profile->GetOriginalProfile())
562 SetError(kRestoreInIncognitoError); 542 return RespondNow(Error(kRestoreInIncognitoError));
563 return false;
564 }
565 543
566 if (!params->session_id) 544 if (!params->session_id)
567 return RestoreMostRecentlyClosed(browser); 545 return RespondNow(RestoreMostRecentlyClosed(browser));
568 546
569 std::unique_ptr<SessionId> session_id(SessionId::Parse(*params->session_id)); 547 std::unique_ptr<SessionId> session_id(SessionId::Parse(*params->session_id));
570 if (!session_id) { 548 if (!session_id)
571 SetInvalidIdError(*params->session_id); 549 return RespondNow(Error(kInvalidSessionIdError, *params->session_id));
572 return false;
573 }
574 550
575 return session_id->IsForeign() ? 551 return RespondNow(session_id->IsForeign()
576 RestoreForeignSession(*session_id, browser) 552 ? RestoreForeignSession(*session_id, browser)
577 : RestoreLocalSession(*session_id, browser); 553 : RestoreLocalSession(*session_id, browser));
578 } 554 }
579 555
580 SessionsEventRouter::SessionsEventRouter(Profile* profile) 556 SessionsEventRouter::SessionsEventRouter(Profile* profile)
581 : profile_(profile), 557 : profile_(profile),
582 tab_restore_service_(TabRestoreServiceFactory::GetForProfile(profile)) { 558 tab_restore_service_(TabRestoreServiceFactory::GetForProfile(profile)) {
583 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in 559 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in
584 // incognito mode) 560 // incognito mode)
585 if (tab_restore_service_) { 561 if (tab_restore_service_) {
586 tab_restore_service_->LoadTabsFromLastSession(); 562 tab_restore_service_->LoadTabsFromLastSession();
587 tab_restore_service_->AddObserver(this); 563 tab_restore_service_->AddObserver(this);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 return g_factory.Pointer(); 603 return g_factory.Pointer();
628 } 604 }
629 605
630 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) { 606 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) {
631 sessions_event_router_.reset( 607 sessions_event_router_.reset(
632 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_))); 608 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_)));
633 EventRouter::Get(browser_context_)->UnregisterObserver(this); 609 EventRouter::Get(browser_context_)->UnregisterObserver(this);
634 } 610 }
635 611
636 } // namespace extensions 612 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698