Chromium Code Reviews| Index: chrome/browser/extensions/api/sessions/sessions_api.cc |
| diff --git a/chrome/browser/extensions/api/session_restore/session_restore_api.cc b/chrome/browser/extensions/api/sessions/sessions_api.cc |
| similarity index 29% |
| rename from chrome/browser/extensions/api/session_restore/session_restore_api.cc |
| rename to chrome/browser/extensions/api/sessions/sessions_api.cc |
| index ed0042cb6efc7676a160159f8c400df72ba3630c..f849734379ba9c5e0f266886d2d2dce7b4539759 100644 |
| --- a/chrome/browser/extensions/api/session_restore/session_restore_api.cc |
| +++ b/chrome/browser/extensions/api/sessions/sessions_api.cc |
| @@ -1,8 +1,8 @@ |
| -// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "chrome/browser/extensions/api/session_restore/session_restore_api.h" |
| +#include "chrome/browser/extensions/api/sessions/sessions_api.h" |
| #include <vector> |
| @@ -10,8 +10,11 @@ |
| #include "base/lazy_instance.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/extensions/extension_function_dispatcher.h" |
| #include "chrome/browser/extensions/extension_function_registry.h" |
| #include "chrome/browser/extensions/extension_tab_util.h" |
| +#include "chrome/browser/extensions/window_controller.h" |
| +#include "chrome/browser/extensions/window_controller_list.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/sessions/session_restore.h" |
| #include "chrome/browser/sessions/tab_restore_service_delegate.h" |
| @@ -25,42 +28,112 @@ |
| #include "chrome/browser/ui/host_desktop.h" |
| #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| #include "content/public/browser/web_contents.h" |
| +#include "extensions/common/error_utils.h" |
| #include "ui/base/layout.h" |
| - |
| namespace { |
| const unsigned int kMaxRecentlyClosedSessionResults = 25; |
| -const char kRecentlyClosedListEmpty[] = |
| +const unsigned int kMaxSyncedSessionResults = 10; |
| +const char kRecentlyClosedListEmptyError[] = |
| "There are no recently closed sessions."; |
| -const char kInvalidSessionId[] = "Invalid session id."; |
| +const char kInvalidSessionIdError[] = "Invalid session id."; |
| const char kNoBrowserToRestoreSession[] = |
| "There are no browser windows to restore the session."; |
| +const char kSessionsSyncNotEnabledError[] = "Syncing sessions is not enabled."; |
| +const char kSyncedSessionsListEmptyError[] = "There are no foreign sessions."; |
| +const char kSessionNotFoundError[] = "Session is not found."; |
| +const char kSessionSyncError[] = "Session sync error."; |
| +const char kForeignIdSeparator = '.'; |
| + |
| +const char kReturnObjectError[] = "Error returning window or tab."; |
| +const char kWindowNotFoundError[] = "No window with id: *."; |
| +const char kNoCurrentWindowError[] = "No current window."; |
| +const char kWindowRestoreError[] = "Error restoring foreign window."; |
| + |
| +std::string CreateForeignId(const std::string& session_tag, int id) { |
| + return (session_tag + kForeignIdSeparator + base::IntToString(id)); |
| +} |
|
not at google - send to devlin
2013/08/06 19:04:09
Rather than sharing a magic variable (kForeignIdSe
Kristen Dwan
2013/08/12 15:11:30
Done.
|
| + |
| +// Comparator function for use with std::sort that will sort sessions by |
| +// descending modified_time (i.e., most recent first). |
| +bool SortSessionsByRecency(const browser_sync::SyncedSession* s1, |
| + const browser_sync::SyncedSession* s2) { |
| + return s1->modified_time > s2->modified_time; |
| +} |
| + |
| +bool GetWindowFromWindowID(UIThreadExtensionFunction* function, |
| + int window_id, |
| + extensions::WindowController** controller) { |
|
not at google - send to devlin
2013/08/06 19:04:09
Please pull this into a common place and share it
Kristen Dwan
2013/08/12 15:11:30
Done.
|
| + if (window_id == extension_misc::kCurrentWindowId) { |
| + extensions::WindowController* extension_window_controller = |
| + function->dispatcher()->delegate()->GetExtensionWindowController(); |
| + // If there is a window controller associated with this extension, use that. |
| + if (extension_window_controller) { |
| + *controller = extension_window_controller; |
| + } else { |
| + // Otherwise get the focused or most recently added window. |
| + *controller = extensions::WindowControllerList::GetInstance()-> |
| + CurrentWindowForFunction(function); |
| + } |
| + if (!(*controller)) { |
| + function->SetError(kNoCurrentWindowError); |
| + return false; |
| + } |
| + } else { |
| + *controller = extensions::WindowControllerList::GetInstance()-> |
| + FindWindowForFunctionById(function, window_id); |
| + if (!(*controller)) { |
| + function->SetError(extensions::ErrorUtils::FormatErrorMessage( |
| + kWindowNotFoundError, base::IntToString(window_id))); |
| + return false; |
| + } |
| + } |
| + return true; |
| +} |
| + |
| +bool IsLocalSession(const std::string& id) { |
| + return (id.find(kForeignIdSeparator) == std::string::npos); |
| +} |
| + |
| +bool SplitId(const std::string& sid, std::string* session_tag, int* id) { |
| + std::size_t separator_index = sid.find(kForeignIdSeparator); |
| + *session_tag = sid.substr(0, separator_index); |
| + if (!base::StringToInt(sid.substr(separator_index + 1), id)) |
| + return false; |
| + return true; |
| +} |
| } // namespace |
| namespace extensions { |
| -namespace GetRecentlyClosed = api::session_restore::GetRecentlyClosed; |
| -namespace Restore = api::session_restore::Restore; |
| +namespace GetRecentlyClosed = api::sessions::GetRecentlyClosed; |
| +namespace GetDevices = api::sessions::GetDevices; |
| +namespace Restore = api::sessions::Restore; |
| namespace tabs = api::tabs; |
| namespace windows = api::windows; |
| -namespace session_restore = api::session_restore; |
| -scoped_ptr<tabs::Tab> SessionRestoreGetRecentlyClosedFunction::CreateTabModel( |
| - const TabRestoreService::Tab& tab, int selected_index) { |
| +scoped_ptr<tabs::Tab> SessionsGetRecentlyClosedFunction::CreateTabModel( |
| + const TabRestoreService::Tab& tab, const int* const session_id, |
|
not at google - send to devlin
2013/08/06 19:04:09
The second const isn't necessary.
That said I thi
Kristen Dwan
2013/08/12 15:11:30
Done.
|
| + int selected_index) { |
| scoped_ptr<tabs::Tab> tab_struct(new tabs::Tab); |
| const sessions::SerializedNavigationEntry& current_navigation = |
| tab.navigations[tab.current_navigation_index]; |
| GURL gurl = current_navigation.virtual_url(); |
| std::string title = UTF16ToUTF8(current_navigation.title()); |
| + if (session_id) { |
| + tab_struct->session_id.reset( |
| + new std::string(base::IntToString(*session_id))); |
| + } else { |
| + tab_struct->session_id.reset( |
| + new std::string(base::IntToString(tab.id))); |
| + } |
| tab_struct->url.reset(new std::string(gurl.spec())); |
| tab_struct->title.reset(new std::string(title.empty() ? gurl.spec() : title)); |
| tab_struct->index = tab.tabstrip_index; |
| tab_struct->pinned = tab.pinned; |
| - tab_struct->id = tab.id; |
| - tab_struct->window_id = tab.browser_id; |
| tab_struct->index = tab.tabstrip_index; |
| tab_struct->pinned = tab.pinned; |
| tab_struct->selected = tab.tabstrip_index == selected_index; |
| @@ -73,17 +146,19 @@ scoped_ptr<tabs::Tab> SessionRestoreGetRecentlyClosedFunction::CreateTabModel( |
| } |
| scoped_ptr<windows::Window> |
| - SessionRestoreGetRecentlyClosedFunction::CreateWindowModel( |
| - const TabRestoreService::Window& window) { |
| + SessionsGetRecentlyClosedFunction::CreateWindowModel( |
| + const TabRestoreService::Window& window, int session_id) { |
| scoped_ptr<windows::Window> window_struct(new windows::Window); |
| DCHECK(!window.tabs.empty()); |
| scoped_ptr<std::vector<linked_ptr<tabs::Tab> > > tabs( |
| new std::vector<linked_ptr<tabs::Tab> >); |
| for (size_t i = 0; i < window.tabs.size(); ++i) { |
| - tabs->push_back(make_linked_ptr(CreateTabModel(window.tabs[i], |
| + tabs->push_back(make_linked_ptr(CreateTabModel(window.tabs[i], NULL, |
| window.selected_tab_index).release())); |
| } |
| + window_struct->session_id.reset( |
| + new std::string(base::IntToString(session_id))); |
| window_struct->tabs.reset(tabs.release()); |
| window_struct->incognito = false; |
| window_struct->always_on_top = false; |
| @@ -93,29 +168,29 @@ scoped_ptr<windows::Window> |
| return window_struct.Pass(); |
| } |
| -scoped_ptr<session_restore::ClosedEntry> |
| - SessionRestoreGetRecentlyClosedFunction::CreateEntryModel( |
| +scoped_ptr<api::sessions::Session> |
| + SessionsGetRecentlyClosedFunction::CreateSessionModel( |
| const TabRestoreService::Entry* entry) { |
| - scoped_ptr<session_restore::ClosedEntry> entry_struct( |
| - new session_restore::ClosedEntry); |
| + scoped_ptr<api::sessions::Session> session_struct(new api::sessions::Session); |
| switch (entry->type) { |
| case TabRestoreService::TAB: |
| - entry_struct->tab.reset(CreateTabModel( |
| - *static_cast<const TabRestoreService::Tab*>(entry), -1).release()); |
| + session_struct->tab.reset(CreateTabModel( |
| + *static_cast<const TabRestoreService::Tab*>(entry), &entry->id, -1) |
| + .release()); |
| break; |
| case TabRestoreService::WINDOW: |
| - entry_struct->window.reset(CreateWindowModel( |
| - *static_cast<const TabRestoreService::Window*>(entry)).release()); |
| + session_struct->window.reset(CreateWindowModel( |
| + *static_cast<const TabRestoreService::Window*>(entry), entry->id) |
| + .release()); |
| break; |
| default: |
| NOTREACHED(); |
| } |
| - entry_struct->timestamp = entry->timestamp.ToTimeT(); |
| - entry_struct->id = entry->id; |
| - return entry_struct.Pass(); |
| + session_struct->last_modified = entry->timestamp.ToTimeT(); |
| + return session_struct.Pass(); |
| } |
| -bool SessionRestoreGetRecentlyClosedFunction::RunImpl() { |
| +bool SessionsGetRecentlyClosedFunction::RunImpl() { |
| scoped_ptr<GetRecentlyClosed::Params> params( |
| GetRecentlyClosed::Params::Create(*args_)); |
| EXTENSION_FUNCTION_VALIDATE(params.get()); |
| @@ -125,7 +200,7 @@ bool SessionRestoreGetRecentlyClosedFunction::RunImpl() { |
| EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && |
| max_results <= kMaxRecentlyClosedSessionResults); |
| - std::vector<linked_ptr<session_restore::ClosedEntry> > result; |
| + std::vector<linked_ptr<api::sessions::Session> > result; |
| TabRestoreService* tab_restore_service = |
| TabRestoreServiceFactory::GetForProfile(profile()); |
| DCHECK(tab_restore_service); |
| @@ -140,7 +215,7 @@ bool SessionRestoreGetRecentlyClosedFunction::RunImpl() { |
| if (!params->options || params->options->entry_type == |
| GetRecentlyClosed::Params::Options::ENTRY_TYPE_NONE) { |
| // Include both tabs and windows if type is not defined. |
| - result.push_back(make_linked_ptr(CreateEntryModel(entry).release())); |
| + result.push_back(make_linked_ptr(CreateSessionModel(entry).release())); |
| } else if ( |
| (params->options->entry_type == |
| GetRecentlyClosed::Params::Options::ENTRY_TYPE_TAB && |
| @@ -148,7 +223,7 @@ bool SessionRestoreGetRecentlyClosedFunction::RunImpl() { |
| (params->options->entry_type == |
| GetRecentlyClosed::Params::Options::ENTRY_TYPE_WINDOW && |
| entry->type == TabRestoreService::WINDOW)) { |
| - result.push_back(make_linked_ptr(CreateEntryModel(entry).release())); |
| + result.push_back(make_linked_ptr(CreateSessionModel(entry).release())); |
| } |
| } |
| @@ -156,18 +231,147 @@ bool SessionRestoreGetRecentlyClosedFunction::RunImpl() { |
| return true; |
| } |
| -bool SessionRestoreRestoreFunction::RunImpl() { |
| - scoped_ptr<Restore::Params> params(Restore::Params::Create(*args_)); |
| +scoped_ptr<tabs::Tab> SessionsGetDevicesFunction::CreateTabModel( |
|
not at google - send to devlin
2013/08/06 19:04:09
what's the difference between this and the other C
Kristen Dwan
2013/08/12 15:11:30
This takes a SessionTab and the other takes a TabR
|
| + const SessionTab& tab, int selected_index, int tab_index, |
| + const std::string& session_tag) { |
| + scoped_ptr<tabs::Tab> tab_struct(new tabs::Tab); |
| + const sessions::SerializedNavigationEntry& current_navigation = |
| + tab.navigations[tab.current_navigation_index]; |
| + const GURL& gurl = current_navigation.virtual_url(); |
| + std::string title = UTF16ToUTF8(current_navigation.title()); |
| + |
| + tab_struct->session_id.reset( |
| + new std::string(CreateForeignId(session_tag, tab.tab_id.id()))); |
| + tab_struct->url.reset(new std::string(gurl.spec())); |
| + tab_struct->title.reset(new std::string(title.empty() ? gurl.spec() : title)); |
| + tab_struct->pinned = tab.pinned; |
| + tab_struct->index = tab_index; |
| + tab_struct->pinned = tab.pinned; |
| + tab_struct->selected = tab_index == selected_index; |
| + tab_struct->active = false; |
| + tab_struct->highlighted = false; |
| + tab_struct->incognito = false; |
| + ExtensionTabUtil::ScrubTabForExtension(GetExtension(), tab_struct.get()); |
| + return tab_struct.Pass(); |
| +} |
| + |
| +scoped_ptr<windows::Window> SessionsGetDevicesFunction::CreateWindowModel( |
|
not at google - send to devlin
2013/08/06 19:04:09
ditto?
Kristen Dwan
2013/08/12 15:11:30
SessionWindow VS TabRestoreService::Window.
Chang
|
| + const SessionWindow& window, const std::string& session_tag) { |
| + scoped_ptr<windows::Window> window_struct(new windows::Window); |
| + DCHECK(!window.tabs.empty()); |
| + |
| + scoped_ptr<std::vector<linked_ptr<tabs::Tab> > > tabs( |
| + new std::vector<linked_ptr<tabs::Tab> >); |
| + for (size_t i = 0; i < window.tabs.size(); ++i) { |
| + tabs->push_back(make_linked_ptr( |
| + CreateTabModel(*window.tabs[i], window.selected_tab_index, i, |
| + session_tag).release())); |
| + } |
| + window_struct->tabs = tabs.Pass(); |
| + |
| + window_struct->session_id.reset( |
| + new std::string(CreateForeignId(session_tag, window.window_id.id()))); |
| + window_struct->left.reset(new int(window.bounds.x())); |
| + window_struct->top.reset(new int(window.bounds.y())); |
| + window_struct->width.reset(new int(window.bounds.width())); |
| + window_struct->height.reset(new int(window.bounds.height())); |
| + window_struct->incognito = false; |
| + window_struct->always_on_top = false; |
| + window_struct->focused = false; |
| + |
| + switch (window.type) { |
| + case Browser::TYPE_TABBED: |
| + window_struct->type = windows::Window::TYPE_NORMAL; |
| + break; |
| + case Browser::TYPE_POPUP: |
| + window_struct->type = windows::Window::TYPE_POPUP; |
| + break; |
| + default: |
| + window_struct->type = windows::Window::TYPE_NONE; |
| + } |
| + |
| + switch (window.show_state) { |
| + case ui::SHOW_STATE_NORMAL: |
| + window_struct->state = windows::Window::STATE_NORMAL; |
| + break; |
| + case ui::SHOW_STATE_MINIMIZED: |
| + window_struct->state = windows::Window::STATE_MINIMIZED; |
| + break; |
| + case ui::SHOW_STATE_MAXIMIZED: |
| + window_struct->state = windows::Window::STATE_MAXIMIZED; |
| + break; |
| + case ui::SHOW_STATE_FULLSCREEN: |
| + window_struct->state = windows::Window::STATE_FULLSCREEN; |
| + break; |
| + default: |
| + window_struct->state = windows::Window::STATE_NONE; |
| + } |
| + return window_struct.Pass(); |
| +} |
| + |
| +scoped_ptr<api::sessions::Session> |
| + SessionsGetDevicesFunction::CreateSessionModel( |
|
not at google - send to devlin
2013/08/06 19:04:09
ditto?
Kristen Dwan
2013/08/12 15:11:30
SessionWindow vs. TabRestoreService::Entry*. put i
|
| + const SessionWindow& window, const std::string& session_tag) { |
| + scoped_ptr<api::sessions::Session> session_struct(new api::sessions::Session); |
| + session_struct->last_modified = window.timestamp.ToTimeT(); |
| + session_struct->window.reset( |
| + CreateWindowModel(window, session_tag).release()); |
| + return session_struct.Pass(); |
| +} |
| + |
| +scoped_ptr<api::sessions::Device> SessionsGetDevicesFunction::CreateDeviceModel( |
| + const browser_sync::SyncedSession* session) { |
| + scoped_ptr<api::sessions::Device> device_struct(new api::sessions::Device); |
| + device_struct->info = session->session_name; |
| + |
| + for (browser_sync::SyncedSession::SyncedWindowMap::const_iterator it = |
| + session->windows.begin(); it != session->windows.end(); ++it) { |
| + device_struct->sessions.push_back(make_linked_ptr(CreateSessionModel( |
| + *it->second, session->session_tag).release())); |
| + } |
| + return device_struct.Pass(); |
| +} |
| + |
| +bool SessionsGetDevicesFunction::RunImpl() { |
| + ProfileSyncService* service = |
| + ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile()); |
| + if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { |
| + SetError(kSessionsSyncNotEnabledError); |
|
not at google - send to devlin
2013/08/06 19:04:09
Not sure about this one. There should be a way for
Kristen Dwan
2013/08/12 15:11:30
I can't think of anything that would be clean and
not at google - send to devlin
2013/08/12 23:51:34
This sounds good to me.
|
| + return false; |
| + } |
| + unsigned int max_results = kMaxSyncedSessionResults; |
|
not at google - send to devlin
2013/08/06 19:04:09
just "int".
Kristen Dwan
2013/08/12 15:11:30
Done.
|
| + scoped_ptr<GetDevices::Params> params( |
| + GetDevices::Params::Create(*args_)); |
|
not at google - send to devlin
2013/08/06 19:04:09
1 line?
Kristen Dwan
2013/08/12 15:11:30
Done.
|
| EXTENSION_FUNCTION_VALIDATE(params.get()); |
| + if (params->max_results.get()) |
| + max_results = *params->max_results.get(); |
| + EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && |
| + max_results <= kMaxSyncedSessionResults); |
| - Browser* browser = |
| - chrome::FindBrowserWithProfile(profile(), |
| - chrome::HOST_DESKTOP_TYPE_NATIVE); |
| - if (!browser) { |
| - error_ = kNoBrowserToRestoreSession; |
| + browser_sync::SessionModelAssociator* associator = |
| + service->GetSessionModelAssociator(); |
| + std::vector<const browser_sync::SyncedSession*> sessions; |
| + if (!associator) { |
| + SetError(kSessionSyncError); |
| return false; |
| } |
| + if (!associator->GetAllForeignSessions(&sessions)) { |
| + SetError(kSyncedSessionsListEmptyError); |
|
not at google - send to devlin
2013/08/06 19:04:09
This isn't really an *error* case. Similar as befo
Kristen Dwan
2013/08/12 15:11:30
would you recommend just returning true here then
not at google - send to devlin
2013/08/12 23:51:34
empty results seems consistent.
|
| + return false; |
| + } |
| + |
| + std::vector<linked_ptr<api::sessions::Device> > result; |
| + // Sort sessions from most recent to least recent. |
| + std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency); |
| + for (size_t i = 0; i < sessions.size() && result.size() < max_results; ++i) { |
| + result.push_back(make_linked_ptr(CreateDeviceModel(sessions[i]).release())); |
| + } |
| + |
| + results_ = GetDevices::Results::Create(result); |
| + return true; |
| +} |
| +bool SessionsRestoreFunction::RestoreMostRecentlyClosed(Browser* browser) { |
| TabRestoreService* tab_restore_service = |
| TabRestoreServiceFactory::GetForProfile(profile()); |
| TabRestoreServiceDelegate* delegate = |
| @@ -178,24 +382,58 @@ bool SessionRestoreRestoreFunction::RunImpl() { |
| TabRestoreService::Entries entries = tab_restore_service->entries(); |
| if (entries.empty()) { |
| - error_ = kRecentlyClosedListEmpty; |
| + SetError(kRecentlyClosedListEmptyError); |
|
not at google - send to devlin
2013/08/06 19:04:09
also not an error - and this function shouldn't be
Kristen Dwan
2013/08/12 15:11:30
this is how the current sessionRestore api impleme
not at google - send to devlin
2013/08/12 23:51:34
Yeah ok I might have gotten carried away with maki
|
| return false; |
| } |
| - if (!params->id) { |
| - tab_restore_service->RestoreMostRecentEntry(delegate, host_desktop_type); |
| - return true; |
| + bool is_window = entries.front()->type == TabRestoreService::WINDOW; |
|
not at google - send to devlin
2013/08/06 19:04:09
inline this?
Kristen Dwan
2013/08/12 15:11:30
Done.
|
| + std::vector<content::WebContents*> contents = |
| + tab_restore_service->RestoreMostRecentEntry(delegate, host_desktop_type); |
| + if (!contents.size()) { |
| + SetError(kReturnObjectError); |
|
not at google - send to devlin
2013/08/06 19:04:09
I think we can do a better job of an error message
Kristen Dwan
2013/08/12 15:11:30
got rid of this. backend calls are always returnin
|
| + return false; |
| + } |
| + |
| + if (is_window) { |
| + WindowController* controller; |
|
not at google - send to devlin
2013/08/06 19:04:09
always initialise pointers to NULL in situations l
Kristen Dwan
2013/08/12 15:11:30
Done.
|
| + if (!GetWindowFromWindowID(this, |
| + ExtensionTabUtil::GetWindowIdOfTab(contents[0]), |
| + &controller)) |
| + return false; |
|
not at google - send to devlin
2013/08/06 19:04:09
error message
Kristen Dwan
2013/08/12 15:11:30
Done.
|
| + SetResult(controller->CreateWindowValueWithTabs(GetExtension())); |
| + } else { |
| + SetResult(ExtensionTabUtil::CreateTabValue(contents[0], GetExtension())); |
| + } |
| + return true; |
| +} |
| + |
| +bool SessionsRestoreFunction::RestoreLocalSession(int id, Browser* browser) { |
| + TabRestoreService* tab_restore_service = |
| + TabRestoreServiceFactory::GetForProfile(profile()); |
| + TabRestoreServiceDelegate* delegate = |
| + TabRestoreServiceDelegate::FindDelegateForWebContents( |
| + browser->tab_strip_model()->GetActiveWebContents()); |
|
not at google - send to devlin
2013/08/06 19:04:09
delegate isn't used until way later, grab it then.
Kristen Dwan
2013/08/12 15:11:30
"|delegate| will be NULL in cases where one isn't
|
| + DCHECK(delegate); |
| + chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); |
| + TabRestoreService::Entries entries = tab_restore_service->entries(); |
| + |
| + if (entries.empty()) { |
| + SetError(kRecentlyClosedListEmptyError); |
| + return false; |
| } |
| // Check if the recently closed list contains an entry with the provided id. |
| bool is_valid_id = false; |
| + bool is_window = false; |
|
not at google - send to devlin
2013/08/06 19:04:09
rather than hold onto these two values, perhaps fi
Kristen Dwan
2013/08/12 15:11:30
https://code.google.com/p/chromium/codesearch#chro
|
| for (TabRestoreService::Entries::iterator it = entries.begin(); |
| it != entries.end(); ++it) { |
| - if ((*it)->id == *params->id) { |
| + if ((*it)->id == id) { |
| is_valid_id = true; |
| + // The only time a full window is being restored is if the entry ID |
| + // matches the provided ID and the entry type is Window. |
| + is_window = (*it)->type == TabRestoreService::WINDOW; |
| break; |
| } |
| - |
| // For Window entries, see if the ID matches a tab. If so, report true for |
| // the window as the Entry. |
| if ((*it)->type == TabRestoreService::WINDOW) { |
| @@ -203,7 +441,7 @@ bool SessionRestoreRestoreFunction::RunImpl() { |
| static_cast<TabRestoreService::Window*>(*it)->tabs; |
| for (std::vector<TabRestoreService::Tab>::iterator tab_it = tabs.begin(); |
| tab_it != tabs.end(); ++tab_it) { |
| - if ((*tab_it).id == *params->id) { |
| + if ((*tab_it).id == id) { |
| is_valid_id = true; |
| break; |
| } |
| @@ -212,27 +450,142 @@ bool SessionRestoreRestoreFunction::RunImpl() { |
| } |
| if (!is_valid_id) { |
| - error_ = kInvalidSessionId; |
| + SetError(kInvalidSessionIdError); |
| + return false; |
| + } |
| + |
| + std::vector<content::WebContents*> contents = |
| + tab_restore_service->RestoreEntryById(delegate, id, host_desktop_type, |
| + UNKNOWN); |
| + if (!contents.size()) { |
| + SetError(kReturnObjectError); |
|
not at google - send to devlin
2013/08/06 19:04:09
yeah so kReturnObject error - not a great error in
Kristen Dwan
2013/08/12 15:11:30
got rid of it.
|
| + return false; |
| + } |
| + |
| + if (is_window) { |
| + WindowController* controller; |
| + if (!GetWindowFromWindowID(this, |
| + ExtensionTabUtil::GetWindowIdOfTab(contents[0]), |
| + &controller)) |
| + return false; |
| + SetResult(controller->CreateWindowValueWithTabs(GetExtension())); |
| + } else { |
| + SetResult(ExtensionTabUtil::CreateTabValue(contents[0], GetExtension())); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +bool SessionsRestoreFunction::RestoreForeignSession( |
| + const std::string& session_tag, int id, Browser* browser) { |
|
not at google - send to devlin
2013/08/06 19:04:09
similar comments throughout here and the next func
Kristen Dwan
2013/08/12 15:11:30
Done.
|
| + ProfileSyncService* service = |
| + ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile()); |
| + if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { |
| + SetError(kSessionsSyncNotEnabledError); |
|
Kristen Dwan
2013/08/12 15:11:30
Would you agree that these sync errors in restore
|
| return false; |
| } |
| + browser_sync::SessionModelAssociator* associator = |
| + service->GetSessionModelAssociator(); |
| + if (!associator) { |
| + SetError(kSessionSyncError); |
| + return false; |
| + } |
| + |
| + const SessionTab* tab = NULL; |
| + if (associator->GetForeignTab(session_tag, id, &tab)) { |
| + TabStripModel* tab_strip = browser->tab_strip_model(); |
| + content::WebContents* contents = tab_strip->GetActiveWebContents(); |
| + |
| + content::WebContents* tab_content = |
| + SessionRestore::RestoreForeignSessionTab(contents, *tab, |
| + NEW_FOREGROUND_TAB); |
| + SetResult(ExtensionTabUtil::CreateTabValue(tab_content, GetExtension())); |
| + return true; |
| + } else { |
|
not at google - send to devlin
2013/08/06 19:04:09
no else after return
Kristen Dwan
2013/08/12 15:11:30
Done.
|
| + std::vector<const SessionWindow*> windows; |
| + if (!associator->GetForeignSession(session_tag, &windows)) { |
| + SetError(kSessionNotFoundError); |
| + return false; |
| + } |
| + |
| + std::vector<const SessionWindow*>::const_iterator window = windows.begin(); |
| + while (window != windows.end() && (*window)->window_id.id() != id) { |
| + ++window; |
| + } |
| + if (window == windows.end()) { |
| + SetError(kSessionNotFoundError); |
| + return false; |
| + } |
| + |
| + chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); |
| + // Only restore one window at a time. |
| + std::vector<Browser*> browser = |
| + SessionRestore::RestoreForeignSessionWindows(profile(), |
| + host_desktop_type, window, (window + 1)); |
| + if (browser.size() != 1) { |
| + SetError(kWindowRestoreError); |
| + return false; |
| + } |
| + |
| + WindowController* controller; |
| + if (!GetWindowFromWindowID(this, |
| + ExtensionTabUtil::GetWindowId(browser[0]), |
| + &controller)) |
| + return false; |
| + SetResult(controller->CreateWindowValueWithTabs(GetExtension())); |
| + } |
| - tab_restore_service->RestoreEntryById(delegate, *params->id, |
| - host_desktop_type, UNKNOWN); |
| return true; |
| } |
| -SessionRestoreAPI::SessionRestoreAPI(Profile* profile) { |
| +bool SessionsRestoreFunction::RunImpl() { |
| + scoped_ptr<Restore::Params> params(Restore::Params::Create(*args_)); |
| + EXTENSION_FUNCTION_VALIDATE(params.get()); |
| + |
| + Browser* browser = |
| + chrome::FindBrowserWithProfile(profile(), |
| + chrome::HOST_DESKTOP_TYPE_NATIVE); |
| + if (!browser) { |
| + SetError(kNoBrowserToRestoreSession); |
| + return false; |
| + } |
| + |
| + if (!params->session_id) |
| + return RestoreMostRecentlyClosed(browser); |
| + |
| + if (IsLocalSession(*params->session_id)) { |
| + int id; |
| + if (!base::StringToInt(*params->session_id, &id)) { |
| + SetError(kInvalidSessionIdError); |
| + return false; |
| + } |
| + return RestoreLocalSession(id, browser); |
| + } else { |
| + std::string session_tag; |
| + int id; |
| + if (!SplitId(*params->session_id, &session_tag, &id)) { |
| + SetError(kInvalidSessionIdError); |
| + return false; |
| + } |
| + return RestoreForeignSession(session_tag, id, browser); |
| + } |
| + |
| + // Should never reach here. |
| + return false; |
| +} |
| + |
| +SessionsAPI::SessionsAPI(Profile* profile) { |
| } |
| -SessionRestoreAPI::~SessionRestoreAPI() { |
| +SessionsAPI::~SessionsAPI() { |
| } |
| -static base::LazyInstance<ProfileKeyedAPIFactory<SessionRestoreAPI> > |
| +static base::LazyInstance<ProfileKeyedAPIFactory<SessionsAPI> > |
| g_factory = LAZY_INSTANCE_INITIALIZER; |
| // static |
| -ProfileKeyedAPIFactory<SessionRestoreAPI>* |
| - SessionRestoreAPI::GetFactoryInstance() { |
| +ProfileKeyedAPIFactory<SessionsAPI>* |
| + SessionsAPI::GetFactoryInstance() { |
| return &g_factory.Get(); |
| } |