OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/sessions/sessions_api.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/i18n/rtl.h" |
| 10 #include "base/lazy_instance.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
| 14 #include "chrome/browser/extensions/extension_function_registry.h" |
| 15 #include "chrome/browser/extensions/extension_tab_util.h" |
| 16 #include "chrome/browser/extensions/window_controller.h" |
| 17 #include "chrome/browser/extensions/window_controller_list.h" |
| 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/sessions/session_restore.h" |
| 20 #include "chrome/browser/sessions/tab_restore_service_delegate.h" |
| 21 #include "chrome/browser/sessions/tab_restore_service_factory.h" |
| 22 #include "chrome/browser/sync/glue/session_model_associator.h" |
| 23 #include "chrome/browser/sync/glue/synced_session.h" |
| 24 #include "chrome/browser/sync/profile_sync_service.h" |
| 25 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 26 #include "chrome/browser/ui/browser.h" |
| 27 #include "chrome/browser/ui/browser_finder.h" |
| 28 #include "chrome/browser/ui/host_desktop.h" |
| 29 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 30 #include "content/public/browser/web_contents.h" |
| 31 #include "extensions/common/error_utils.h" |
| 32 #include "ui/base/layout.h" |
| 33 |
| 34 namespace { |
| 35 |
| 36 const unsigned int kMaxRecentlyClosedSessionResults = 25; |
| 37 const unsigned int kMaxSyncedSessionResults = 10; |
| 38 const char kRecentlyClosedListEmptyError[] = |
| 39 "There are no recently closed sessions."; |
| 40 const char kInvalidSessionIdError[] = "Invalid session id."; |
| 41 const char kNoBrowserToRestoreSession[] = |
| 42 "There are no browser windows to restore the session."; |
| 43 const char kSessionsSyncNotEnabledError[] = "Syncing sessions is not enabled."; |
| 44 const char kSyncedSessionsListEmptyError[] = "There are no foreign sessions."; |
| 45 const char kSessionNotFoundError[] = "Session is not found."; |
| 46 const char kSessionSyncError[] = "Session sync error."; |
| 47 const char kForeignIdSeparator = '.'; |
| 48 |
| 49 const char kWindowNotFoundError[] = "No window with id: *."; |
| 50 const char kNoCurrentWindowError[] = "No current window."; |
| 51 const char kWindowRestoreError[] = "Error restoring foreign window."; |
| 52 |
| 53 std::string CreateForeignId(const std::string& session_tag, int id) { |
| 54 return (session_tag + kForeignIdSeparator + base::IntToString(id)); |
| 55 } |
| 56 |
| 57 // Comparator function for use with std::sort that will sort sessions by |
| 58 // descending modified_time (i.e., most recent first). |
| 59 bool SortSessionsByRecency(const browser_sync::SyncedSession* s1, |
| 60 const browser_sync::SyncedSession* s2) { |
| 61 return s1->modified_time > s2->modified_time; |
| 62 } |
| 63 |
| 64 bool GetWindowFromWindowID(UIThreadExtensionFunction* function, |
| 65 int window_id, |
| 66 extensions::WindowController** controller) { |
| 67 if (window_id == extension_misc::kCurrentWindowId) { |
| 68 extensions::WindowController* extension_window_controller = |
| 69 function->dispatcher()->delegate()->GetExtensionWindowController(); |
| 70 // If there is a window controller associated with this extension, use that. |
| 71 if (extension_window_controller) { |
| 72 *controller = extension_window_controller; |
| 73 } else { |
| 74 // Otherwise get the focused or most recently added window. |
| 75 *controller = extensions::WindowControllerList::GetInstance()-> |
| 76 CurrentWindowForFunction(function); |
| 77 } |
| 78 if (!(*controller)) { |
| 79 function->SetError(kNoCurrentWindowError); |
| 80 return false; |
| 81 } |
| 82 } else { |
| 83 *controller = extensions::WindowControllerList::GetInstance()-> |
| 84 FindWindowForFunctionById(function, window_id); |
| 85 if (!(*controller)) { |
| 86 function->SetError(extensions::ErrorUtils::FormatErrorMessage( |
| 87 kWindowNotFoundError, base::IntToString(window_id))); |
| 88 return false; |
| 89 } |
| 90 } |
| 91 return true; |
| 92 } |
| 93 |
| 94 bool IsLocalSession(const std::string& id) { |
| 95 return (id.find(kForeignIdSeparator) == std::string::npos); |
| 96 } |
| 97 |
| 98 bool SplitId(const std::string& sid, std::string* session_tag, int* id) { |
| 99 std::size_t separator_index = sid.find(kForeignIdSeparator); |
| 100 *session_tag = sid.substr(0, separator_index); |
| 101 if (!base::StringToInt(sid.substr(separator_index + 1), id)) |
| 102 return false; |
| 103 return true; |
| 104 } |
| 105 |
| 106 } // namespace |
| 107 |
| 108 namespace extensions { |
| 109 |
| 110 namespace GetRecentlyClosed = api::sessions::GetRecentlyClosed; |
| 111 namespace GetDevices = api::sessions::GetDevices; |
| 112 namespace Restore = api::sessions::Restore; |
| 113 namespace tabs = api::tabs; |
| 114 namespace windows = api::windows; |
| 115 |
| 116 scoped_ptr<tabs::Tab> SessionsGetRecentlyClosedFunction::CreateTabModel( |
| 117 const TabRestoreService::Tab& tab, const int* const session_id, |
| 118 int selected_index) { |
| 119 scoped_ptr<tabs::Tab> tab_struct(new tabs::Tab); |
| 120 const sessions::SerializedNavigationEntry& current_navigation = |
| 121 tab.navigations[tab.current_navigation_index]; |
| 122 GURL gurl = current_navigation.virtual_url(); |
| 123 std::string title = UTF16ToUTF8(current_navigation.title()); |
| 124 |
| 125 if (session_id) { |
| 126 tab_struct->session_id.reset( |
| 127 new std::string(base::IntToString(*session_id))); |
| 128 } else { |
| 129 tab_struct->session_id.reset( |
| 130 new std::string(base::IntToString(tab.id))); |
| 131 } |
| 132 tab_struct->url.reset(new std::string(gurl.spec())); |
| 133 tab_struct->title.reset(new std::string(title.empty() ? gurl.spec() : title)); |
| 134 tab_struct->index = tab.tabstrip_index; |
| 135 tab_struct->pinned = tab.pinned; |
| 136 tab_struct->index = tab.tabstrip_index; |
| 137 tab_struct->pinned = tab.pinned; |
| 138 tab_struct->selected = tab.tabstrip_index == selected_index; |
| 139 tab_struct->active = false; |
| 140 tab_struct->highlighted = false; |
| 141 tab_struct->incognito = false; |
| 142 ExtensionTabUtil::ScrubTabForExtension(GetExtension(), |
| 143 tab_struct.get()); |
| 144 return tab_struct.Pass(); |
| 145 } |
| 146 |
| 147 scoped_ptr<windows::Window> |
| 148 SessionsGetRecentlyClosedFunction::CreateWindowModel( |
| 149 const TabRestoreService::Window& window, int session_id) { |
| 150 scoped_ptr<windows::Window> window_struct(new windows::Window); |
| 151 DCHECK(!window.tabs.empty()); |
| 152 |
| 153 scoped_ptr<std::vector<linked_ptr<tabs::Tab> > > tabs( |
| 154 new std::vector<linked_ptr<tabs::Tab> >); |
| 155 for (size_t i = 0; i < window.tabs.size(); ++i) { |
| 156 tabs->push_back(make_linked_ptr(CreateTabModel(window.tabs[i], NULL, |
| 157 window.selected_tab_index).release())); |
| 158 } |
| 159 window_struct->session_id.reset( |
| 160 new std::string(base::IntToString(session_id))); |
| 161 window_struct->tabs.reset(tabs.release()); |
| 162 window_struct->incognito = false; |
| 163 window_struct->always_on_top = false; |
| 164 window_struct->focused = false; |
| 165 window_struct->type = windows::Window::TYPE_NORMAL; |
| 166 window_struct->state = windows::Window::STATE_NORMAL; |
| 167 return window_struct.Pass(); |
| 168 } |
| 169 |
| 170 scoped_ptr<api::sessions::Session> |
| 171 SessionsGetRecentlyClosedFunction::CreateSessionModel( |
| 172 const TabRestoreService::Entry* entry) { |
| 173 scoped_ptr<api::sessions::Session> session_struct(new api::sessions::Session); |
| 174 switch (entry->type) { |
| 175 case TabRestoreService::TAB: |
| 176 session_struct->tab.reset(CreateTabModel( |
| 177 *static_cast<const TabRestoreService::Tab*>(entry), &entry->id, -1) |
| 178 .release()); |
| 179 break; |
| 180 case TabRestoreService::WINDOW: |
| 181 session_struct->window.reset(CreateWindowModel( |
| 182 *static_cast<const TabRestoreService::Window*>(entry), entry->id) |
| 183 .release()); |
| 184 break; |
| 185 default: |
| 186 NOTREACHED(); |
| 187 } |
| 188 session_struct->last_modified = entry->timestamp.ToTimeT(); |
| 189 return session_struct.Pass(); |
| 190 } |
| 191 |
| 192 bool SessionsGetRecentlyClosedFunction::RunImpl() { |
| 193 scoped_ptr<GetRecentlyClosed::Params> params( |
| 194 GetRecentlyClosed::Params::Create(*args_)); |
| 195 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 196 unsigned int max_results = kMaxRecentlyClosedSessionResults; |
| 197 if (params->options && params->options->max_results) |
| 198 max_results = *params->options->max_results; |
| 199 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && |
| 200 max_results <= kMaxRecentlyClosedSessionResults); |
| 201 |
| 202 std::vector<linked_ptr<api::sessions::Session> > result; |
| 203 TabRestoreService* tab_restore_service = |
| 204 TabRestoreServiceFactory::GetForProfile(profile()); |
| 205 DCHECK(tab_restore_service); |
| 206 |
| 207 // List of entries. They are ordered from most to least recent. |
| 208 // We prune the list to contain max 25 entries at any time and removes |
| 209 // uninteresting entries. |
| 210 TabRestoreService::Entries entries = tab_restore_service->entries(); |
| 211 for (TabRestoreService::Entries::const_iterator it = entries.begin(); |
| 212 it != entries.end() && result.size() < max_results; ++it) { |
| 213 TabRestoreService::Entry* entry = *it; |
| 214 if (!params->options || params->options->entry_type == |
| 215 GetRecentlyClosed::Params::Options::ENTRY_TYPE_NONE) { |
| 216 // Include both tabs and windows if type is not defined. |
| 217 result.push_back(make_linked_ptr(CreateSessionModel(entry).release())); |
| 218 } else if ( |
| 219 (params->options->entry_type == |
| 220 GetRecentlyClosed::Params::Options::ENTRY_TYPE_TAB && |
| 221 entry->type == TabRestoreService::TAB) || |
| 222 (params->options->entry_type == |
| 223 GetRecentlyClosed::Params::Options::ENTRY_TYPE_WINDOW && |
| 224 entry->type == TabRestoreService::WINDOW)) { |
| 225 result.push_back(make_linked_ptr(CreateSessionModel(entry).release())); |
| 226 } |
| 227 } |
| 228 |
| 229 results_ = GetRecentlyClosed::Results::Create(result); |
| 230 return true; |
| 231 } |
| 232 |
| 233 scoped_ptr<tabs::Tab> SessionsGetDevicesFunction::CreateTabModel( |
| 234 const SessionTab& tab, int selected_index, int tab_index, |
| 235 const std::string& session_tag) { |
| 236 scoped_ptr<tabs::Tab> tab_struct(new tabs::Tab); |
| 237 const sessions::SerializedNavigationEntry& current_navigation = |
| 238 tab.navigations[tab.current_navigation_index]; |
| 239 const GURL& gurl = current_navigation.virtual_url(); |
| 240 std::string title = UTF16ToUTF8(current_navigation.title()); |
| 241 |
| 242 tab_struct->session_id.reset( |
| 243 new std::string(CreateForeignId(session_tag, tab.tab_id.id()))); |
| 244 tab_struct->url.reset(new std::string(gurl.spec())); |
| 245 tab_struct->title.reset(new std::string(title.empty() ? gurl.spec() : title)); |
| 246 tab_struct->pinned = tab.pinned; |
| 247 tab_struct->session_id.reset( |
| 248 new std::string(CreateForeignId(session_tag, tab.tab_id.id()))); |
| 249 tab_struct->index = tab_index; |
| 250 tab_struct->pinned = tab.pinned; |
| 251 tab_struct->selected = tab_index == selected_index; |
| 252 tab_struct->active = false; |
| 253 tab_struct->highlighted = false; |
| 254 tab_struct->incognito = false; |
| 255 ExtensionTabUtil::ScrubTabForExtension(GetExtension(), tab_struct.get()); |
| 256 return tab_struct.Pass(); |
| 257 } |
| 258 |
| 259 scoped_ptr<windows::Window> SessionsGetDevicesFunction::CreateWindowModel( |
| 260 const SessionWindow& window, const std::string& session_tag) { |
| 261 scoped_ptr<windows::Window> window_struct(new windows::Window); |
| 262 DCHECK(!window.tabs.empty()); |
| 263 |
| 264 scoped_ptr<std::vector<linked_ptr<tabs::Tab> > > tabs( |
| 265 new std::vector<linked_ptr<tabs::Tab> >); |
| 266 for (size_t i = 0; i < window.tabs.size(); ++i) { |
| 267 tabs->push_back(make_linked_ptr( |
| 268 CreateTabModel(*window.tabs[i], window.selected_tab_index, i, |
| 269 session_tag).release())); |
| 270 } |
| 271 window_struct->tabs = tabs.Pass(); |
| 272 |
| 273 window_struct->session_id.reset( |
| 274 new std::string(CreateForeignId(session_tag, window.window_id.id()))); |
| 275 window_struct->left.reset(new int(window.bounds.x())); |
| 276 window_struct->top.reset(new int(window.bounds.y())); |
| 277 window_struct->width.reset(new int(window.bounds.width())); |
| 278 window_struct->height.reset(new int(window.bounds.height())); |
| 279 window_struct->incognito = false; |
| 280 window_struct->always_on_top = false; |
| 281 window_struct->focused = false; |
| 282 |
| 283 switch (window.type) { |
| 284 case Browser::TYPE_TABBED: |
| 285 window_struct->type = windows::Window::TYPE_NORMAL; |
| 286 break; |
| 287 case Browser::TYPE_POPUP: |
| 288 window_struct->type = windows::Window::TYPE_POPUP; |
| 289 break; |
| 290 default: |
| 291 window_struct->type = windows::Window::TYPE_NONE; |
| 292 } |
| 293 |
| 294 switch (window.show_state) { |
| 295 case ui::SHOW_STATE_NORMAL: |
| 296 window_struct->state = windows::Window::STATE_NORMAL; |
| 297 break; |
| 298 case ui::SHOW_STATE_MINIMIZED: |
| 299 window_struct->state = windows::Window::STATE_MINIMIZED; |
| 300 break; |
| 301 case ui::SHOW_STATE_MAXIMIZED: |
| 302 window_struct->state = windows::Window::STATE_MAXIMIZED; |
| 303 break; |
| 304 case ui::SHOW_STATE_FULLSCREEN: |
| 305 window_struct->state = windows::Window::STATE_FULLSCREEN; |
| 306 break; |
| 307 default: |
| 308 window_struct->state = windows::Window::STATE_NONE; |
| 309 } |
| 310 return window_struct.Pass(); |
| 311 } |
| 312 |
| 313 scoped_ptr<api::sessions::Session> |
| 314 SessionsGetDevicesFunction::CreateSessionModel( |
| 315 const SessionWindow& window, const std::string& session_tag) { |
| 316 scoped_ptr<api::sessions::Session> session_struct(new api::sessions::Session); |
| 317 session_struct->last_modified = window.timestamp.ToTimeT(); |
| 318 session_struct->window.reset( |
| 319 CreateWindowModel(window, session_tag).release()); |
| 320 return session_struct.Pass(); |
| 321 } |
| 322 |
| 323 scoped_ptr<api::sessions::Device> SessionsGetDevicesFunction::CreateDeviceModel( |
| 324 const browser_sync::SyncedSession* session) { |
| 325 scoped_ptr<api::sessions::Device> device_struct(new api::sessions::Device); |
| 326 device_struct->info = session->session_name; |
| 327 |
| 328 for (browser_sync::SyncedSession::SyncedWindowMap::const_iterator it = |
| 329 session->windows.begin(); it != session->windows.end(); ++it) { |
| 330 device_struct->sessions.push_back(make_linked_ptr(CreateSessionModel( |
| 331 *it->second, session->session_tag).release())); |
| 332 } |
| 333 return device_struct.Pass(); |
| 334 } |
| 335 |
| 336 bool SessionsGetDevicesFunction::RunImpl() { |
| 337 ProfileSyncService* service = |
| 338 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile()); |
| 339 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { |
| 340 SetError(kSessionsSyncNotEnabledError); |
| 341 return false; |
| 342 } |
| 343 unsigned int max_results = kMaxSyncedSessionResults; |
| 344 scoped_ptr<GetDevices::Params> params( |
| 345 GetDevices::Params::Create(*args_)); |
| 346 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 347 if (params->max_results.get()) |
| 348 max_results = *params->max_results.get(); |
| 349 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && |
| 350 max_results <= kMaxSyncedSessionResults); |
| 351 |
| 352 browser_sync::SessionModelAssociator* associator = |
| 353 service->GetSessionModelAssociator(); |
| 354 std::vector<const browser_sync::SyncedSession*> sessions; |
| 355 if (!associator) { |
| 356 SetError(kSessionSyncError); |
| 357 return false; |
| 358 } |
| 359 if (!associator->GetAllForeignSessions(&sessions)) { |
| 360 SetError(kSyncedSessionsListEmptyError); |
| 361 return false; |
| 362 } |
| 363 |
| 364 std::vector<linked_ptr<api::sessions::Device> > result; |
| 365 // Sort sessions from most recent to least recent. |
| 366 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency); |
| 367 for (size_t i = 0; i < sessions.size() && result.size() < max_results; ++i) { |
| 368 result.push_back(make_linked_ptr(CreateDeviceModel(sessions[i]).release())); |
| 369 } |
| 370 |
| 371 results_ = GetDevices::Results::Create(result); |
| 372 return true; |
| 373 } |
| 374 |
| 375 bool SessionsRestoreFunction::RestoreMostRecentlyClosed(Browser* browser) { |
| 376 TabRestoreService* tab_restore_service = |
| 377 TabRestoreServiceFactory::GetForProfile(profile()); |
| 378 TabRestoreServiceDelegate* delegate = |
| 379 TabRestoreServiceDelegate::FindDelegateForWebContents( |
| 380 browser->tab_strip_model()->GetActiveWebContents()); |
| 381 DCHECK(delegate); |
| 382 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); |
| 383 TabRestoreService::Entries entries = tab_restore_service->entries(); |
| 384 |
| 385 if (entries.empty()) { |
| 386 SetError(kRecentlyClosedListEmptyError); |
| 387 return false; |
| 388 } |
| 389 |
| 390 bool is_window = entries.front()->type == TabRestoreService::WINDOW; |
| 391 // If the entry being restored is of type Window, the WebContents returned are |
| 392 // the contents of the last successfully restored tab in the window. |
| 393 content::WebContents* contents = tab_restore_service->RestoreMostRecentEntry( |
| 394 delegate, host_desktop_type); |
| 395 |
| 396 if (is_window) { |
| 397 WindowController* controller; |
| 398 if (!GetWindowFromWindowID(this, |
| 399 ExtensionTabUtil::GetWindowIdOfTab(contents), |
| 400 &controller)) |
| 401 return false; |
| 402 SetResult(controller->CreateWindowValueWithTabs(GetExtension())); |
| 403 } else { |
| 404 SetResult(ExtensionTabUtil::CreateTabValue(contents, GetExtension())); |
| 405 } |
| 406 return true; |
| 407 } |
| 408 |
| 409 bool SessionsRestoreFunction::RestoreLocalSession(int id, Browser* browser) { |
| 410 TabRestoreService* tab_restore_service = |
| 411 TabRestoreServiceFactory::GetForProfile(profile()); |
| 412 |
| 413 TabRestoreServiceDelegate* delegate = |
| 414 TabRestoreServiceDelegate::FindDelegateForWebContents( |
| 415 browser->tab_strip_model()->GetActiveWebContents()); |
| 416 DCHECK(delegate); |
| 417 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); |
| 418 TabRestoreService::Entries entries = tab_restore_service->entries(); |
| 419 |
| 420 if (entries.empty()) { |
| 421 SetError(kRecentlyClosedListEmptyError); |
| 422 return false; |
| 423 } |
| 424 |
| 425 // Check if the recently closed list contains an entry with the provided id. |
| 426 bool is_valid_id = false; |
| 427 bool is_window = false; |
| 428 for (TabRestoreService::Entries::iterator it = entries.begin(); |
| 429 it != entries.end(); ++it) { |
| 430 if ((*it)->id == id) { |
| 431 is_valid_id = true; |
| 432 // The only time a full window is being restored is if the entry ID |
| 433 // matches the provided ID and the entry type is Window. |
| 434 is_window = (*it)->type == TabRestoreService::WINDOW; |
| 435 break; |
| 436 } |
| 437 // For Window entries, see if the ID matches a tab. If so, report true for |
| 438 // the window as the Entry. |
| 439 if ((*it)->type == TabRestoreService::WINDOW) { |
| 440 std::vector<TabRestoreService::Tab>& tabs = |
| 441 static_cast<TabRestoreService::Window*>(*it)->tabs; |
| 442 for (std::vector<TabRestoreService::Tab>::iterator tab_it = tabs.begin(); |
| 443 tab_it != tabs.end(); ++tab_it) { |
| 444 if ((*tab_it).id == id) { |
| 445 is_valid_id = true; |
| 446 break; |
| 447 } |
| 448 } |
| 449 } |
| 450 } |
| 451 |
| 452 if (!is_valid_id) { |
| 453 SetError(kInvalidSessionIdError); |
| 454 return false; |
| 455 } |
| 456 |
| 457 content::WebContents* contents = tab_restore_service->RestoreEntryById( |
| 458 delegate, id, host_desktop_type, UNKNOWN); |
| 459 if (is_window) { |
| 460 WindowController* controller; |
| 461 if (!GetWindowFromWindowID(this, |
| 462 ExtensionTabUtil::GetWindowIdOfTab(contents), |
| 463 &controller)) |
| 464 return false; |
| 465 SetResult(controller->CreateWindowValueWithTabs(GetExtension())); |
| 466 } else { |
| 467 SetResult(ExtensionTabUtil::CreateTabValue(contents, GetExtension())); |
| 468 } |
| 469 |
| 470 return true; |
| 471 } |
| 472 |
| 473 bool SessionsRestoreFunction::RestoreForeignSession( |
| 474 const std::string& session_tag, int id, Browser* browser) { |
| 475 ProfileSyncService* service = |
| 476 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile()); |
| 477 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { |
| 478 SetError(kSessionsSyncNotEnabledError); |
| 479 return false; |
| 480 } |
| 481 browser_sync::SessionModelAssociator* associator = |
| 482 service->GetSessionModelAssociator(); |
| 483 if (!associator) { |
| 484 SetError(kSessionSyncError); |
| 485 return false; |
| 486 } |
| 487 |
| 488 const SessionTab* tab = NULL; |
| 489 if (associator->GetForeignTab(session_tag, id, &tab)) { |
| 490 TabStripModel* tab_strip = browser->tab_strip_model(); |
| 491 content::WebContents* contents = tab_strip->GetActiveWebContents(); |
| 492 |
| 493 content::WebContents* tab_content = |
| 494 SessionRestore::RestoreForeignSessionTab(contents, *tab, |
| 495 NEW_FOREGROUND_TAB); |
| 496 SetResult(ExtensionTabUtil::CreateTabValue(tab_content, GetExtension())); |
| 497 return true; |
| 498 } else { |
| 499 std::vector<const SessionWindow*> windows; |
| 500 if (!associator->GetForeignSession(session_tag, &windows)) { |
| 501 SetError(kSessionNotFoundError); |
| 502 return false; |
| 503 } |
| 504 |
| 505 std::vector<const SessionWindow*>::const_iterator window = windows.begin(); |
| 506 while (window != windows.end() && (*window)->window_id.id() != id) { |
| 507 ++window; |
| 508 } |
| 509 if (window == windows.end()) { |
| 510 SetError(kSessionNotFoundError); |
| 511 return false; |
| 512 } |
| 513 |
| 514 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); |
| 515 // Only restore one window at a time. |
| 516 std::vector<Browser*> browser = |
| 517 SessionRestore::RestoreForeignSessionWindows(profile(), |
| 518 host_desktop_type, window, (window + 1)); |
| 519 if (browser.size() != 1) { |
| 520 SetError(kWindowRestoreError); |
| 521 return false; |
| 522 } |
| 523 |
| 524 WindowController* controller; |
| 525 if (!GetWindowFromWindowID(this, |
| 526 ExtensionTabUtil::GetWindowId(browser[0]), |
| 527 &controller)) |
| 528 return false; |
| 529 SetResult(controller->CreateWindowValueWithTabs(GetExtension())); |
| 530 } |
| 531 |
| 532 return true; |
| 533 } |
| 534 |
| 535 bool SessionsRestoreFunction::RunImpl() { |
| 536 scoped_ptr<Restore::Params> params(Restore::Params::Create(*args_)); |
| 537 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 538 |
| 539 Browser* browser = |
| 540 chrome::FindBrowserWithProfile(profile(), |
| 541 chrome::HOST_DESKTOP_TYPE_NATIVE); |
| 542 if (!browser) { |
| 543 SetError(kNoBrowserToRestoreSession); |
| 544 return false; |
| 545 } |
| 546 |
| 547 if (!params->session_id) |
| 548 return RestoreMostRecentlyClosed(browser); |
| 549 |
| 550 if (IsLocalSession(*params->session_id)) { |
| 551 int id; |
| 552 if (!base::StringToInt(*params->session_id, &id)) { |
| 553 SetError(kInvalidSessionIdError); |
| 554 return false; |
| 555 } |
| 556 return RestoreLocalSession(id, browser); |
| 557 } else { |
| 558 std::string session_tag; |
| 559 int id; |
| 560 if (!SplitId(*params->session_id, &session_tag, &id)) { |
| 561 SetError(kInvalidSessionIdError); |
| 562 return false; |
| 563 } |
| 564 return RestoreForeignSession(session_tag, id, browser); |
| 565 } |
| 566 |
| 567 // Should never reach here. |
| 568 return false; |
| 569 } |
| 570 |
| 571 SessionsAPI::SessionsAPI(Profile* profile) { |
| 572 } |
| 573 |
| 574 SessionsAPI::~SessionsAPI() { |
| 575 } |
| 576 |
| 577 static base::LazyInstance<ProfileKeyedAPIFactory<SessionsAPI> > |
| 578 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 579 |
| 580 // static |
| 581 ProfileKeyedAPIFactory<SessionsAPI>* |
| 582 SessionsAPI::GetFactoryInstance() { |
| 583 return &g_factory.Get(); |
| 584 } |
| 585 |
| 586 } // namespace extensions |
OLD | NEW |