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

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

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

Powered by Google App Engine
This is Rietveld 408576698