OLD | NEW |
---|---|
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/tabs/windows_util.h" | |
17 #include "chrome/browser/extensions/extension_function_dispatcher.h" | |
13 #include "chrome/browser/extensions/extension_function_registry.h" | 18 #include "chrome/browser/extensions/extension_function_registry.h" |
14 #include "chrome/browser/extensions/extension_tab_util.h" | 19 #include "chrome/browser/extensions/extension_tab_util.h" |
20 #include "chrome/browser/extensions/window_controller.h" | |
21 #include "chrome/browser/extensions/window_controller_list.h" | |
15 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/browser/search/search.h" | |
16 #include "chrome/browser/sessions/session_restore.h" | 24 #include "chrome/browser/sessions/session_restore.h" |
17 #include "chrome/browser/sessions/tab_restore_service_delegate.h" | 25 #include "chrome/browser/sessions/tab_restore_service_delegate.h" |
18 #include "chrome/browser/sessions/tab_restore_service_factory.h" | 26 #include "chrome/browser/sessions/tab_restore_service_factory.h" |
19 #include "chrome/browser/sync/glue/session_model_associator.h" | 27 #include "chrome/browser/sync/glue/session_model_associator.h" |
20 #include "chrome/browser/sync/glue/synced_session.h" | 28 #include "chrome/browser/sync/glue/synced_session.h" |
21 #include "chrome/browser/sync/profile_sync_service.h" | 29 #include "chrome/browser/sync/profile_sync_service.h" |
22 #include "chrome/browser/sync/profile_sync_service_factory.h" | 30 #include "chrome/browser/sync/profile_sync_service_factory.h" |
23 #include "chrome/browser/ui/browser.h" | 31 #include "chrome/browser/ui/browser.h" |
24 #include "chrome/browser/ui/browser_finder.h" | 32 #include "chrome/browser/ui/browser_finder.h" |
25 #include "chrome/browser/ui/host_desktop.h" | 33 #include "chrome/browser/ui/host_desktop.h" |
26 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 34 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
35 #include "chrome/common/pref_names.cc" | |
27 #include "content/public/browser/web_contents.h" | 36 #include "content/public/browser/web_contents.h" |
37 #include "extensions/common/error_utils.h" | |
38 #include "net/base/net_util.h" | |
28 #include "ui/base/layout.h" | 39 #include "ui/base/layout.h" |
29 | 40 |
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 { | 41 namespace extensions { |
43 | 42 |
44 namespace GetRecentlyClosed = api::session_restore::GetRecentlyClosed; | 43 namespace GetRecentlyClosed = api::sessions::GetRecentlyClosed; |
45 namespace Restore = api::session_restore::Restore; | 44 namespace GetDevices = api::sessions::GetDevices; |
45 namespace Restore = api::sessions::Restore; | |
46 namespace tabs = api::tabs; | 46 namespace tabs = api::tabs; |
47 namespace windows = api::windows; | 47 namespace windows = api::windows; |
48 namespace session_restore = api::session_restore; | |
49 | 48 |
50 scoped_ptr<tabs::Tab> SessionRestoreGetRecentlyClosedFunction::CreateTabModel( | 49 const int kMaxSessionResults = 25; |
51 const TabRestoreService::Tab& tab, int selected_index) { | 50 const char kNoRecentlyClosedSessionsError[] = |
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.get()); |
122 unsigned int max_results = kMaxRecentlyClosedSessionResults; | 199 int max_results = kMaxSessionResults; |
123 if (params->options && params->options->max_results) | 200 if (params->options && params->options->max_results) |
124 max_results = *params->options->max_results; | 201 max_results = *params->options->max_results; |
125 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && | 202 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && |
126 max_results <= kMaxRecentlyClosedSessionResults); | 203 max_results <= kMaxSessionResults); |
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 if (!params->options || params->options->entry_type == |
141 GetRecentlyClosed::Params::Options::ENTRY_TYPE_NONE) { | 219 api::sessions::Filter::ENTRY_TYPE_NONE) { |
142 // Include both tabs and windows if type is not defined. | 220 // Include both tabs and windows if type is not defined. |
143 result.push_back(make_linked_ptr(CreateEntryModel(entry).release())); | 221 result.push_back(make_linked_ptr(CreateSessionModel(entry).release())); |
144 } else if ( | 222 } else if ( |
145 (params->options->entry_type == | 223 (params->options->entry_type == |
146 GetRecentlyClosed::Params::Options::ENTRY_TYPE_TAB && | 224 api::sessions::Filter::ENTRY_TYPE_TAB && |
147 entry->type == TabRestoreService::TAB) || | 225 is_tab_entry(entry)) || |
not at google - send to devlin
2013/08/19 19:34:40
these should be able to fit on 1 line?
Kristen Dwan
2013/08/19 21:35:05
.. the " {" hangs off the second if i limit them e
| |
148 (params->options->entry_type == | 226 (params->options->entry_type == |
149 GetRecentlyClosed::Params::Options::ENTRY_TYPE_WINDOW && | 227 api::sessions::Filter::ENTRY_TYPE_WINDOW && |
150 entry->type == TabRestoreService::WINDOW)) { | 228 is_window_entry(entry))) { |
151 result.push_back(make_linked_ptr(CreateEntryModel(entry).release())); | 229 result.push_back(make_linked_ptr(CreateSessionModel(entry).release())); |
152 } | 230 } |
153 } | 231 } |
154 | 232 |
155 results_ = GetRecentlyClosed::Results::Create(result); | 233 results_ = GetRecentlyClosed::Results::Create(result); |
156 return true; | 234 return true; |
157 } | 235 } |
158 | 236 |
159 bool SessionRestoreRestoreFunction::RunImpl() { | 237 scoped_ptr<tabs::Tab> SessionsGetDevicesFunction::CreateTabModel( |
238 const std::string& session_tag, | |
239 const SessionTab& tab, | |
240 int tab_index, | |
241 int selected_index) { | |
242 std::string session_id = SessionId(session_tag, tab.tab_id.id()).ToString(); | |
243 return CreateTabModelHelper(profile(), | |
244 tab.navigations[tab.current_navigation_index], | |
245 session_id, | |
246 tab_index, | |
247 tab.pinned, | |
248 selected_index, | |
249 GetExtension()); | |
250 } | |
251 | |
252 scoped_ptr<windows::Window> SessionsGetDevicesFunction::CreateWindowModel( | |
253 const SessionWindow& window, const std::string& session_tag) { | |
254 DCHECK(!window.tabs.empty()); | |
255 | |
256 // Prune tabs that are not syncable or are NewTabPage. Then, sort the tabs | |
257 // from most recent to least recent. | |
258 std::vector<const SessionTab*> tabs_in_window; | |
259 for (size_t i = 0; i < window.tabs.size(); ++i) { | |
260 const SessionTab* tab = window.tabs[i]; | |
261 if (tab->navigations.empty()) | |
262 continue; | |
263 const sessions::SerializedNavigationEntry& current_navigation = | |
264 tab->navigations.at(tab->normalized_navigation_index()); | |
265 if (chrome::IsNTPURL(current_navigation.virtual_url(), | |
266 profile())) { | |
not at google - send to devlin
2013/08/19 19:34:40
looks like this will fit on 1 line
Kristen Dwan
2013/08/19 21:35:05
Done.
| |
267 continue; | |
268 } | |
269 tabs_in_window.push_back(tab); | |
270 } | |
271 if (tabs_in_window.empty()) | |
272 return scoped_ptr<windows::Window>(); | |
273 std::sort(tabs_in_window.begin(), tabs_in_window.end(), | |
274 SortTabsByRecency); | |
not at google - send to devlin
2013/08/19 19:34:40
this too
Kristen Dwan
2013/08/19 21:35:05
wow don't know what was wrong with me .. sorry. mu
| |
275 | |
276 scoped_ptr<std::vector<linked_ptr<tabs::Tab> > > tabs( | |
277 new std::vector<linked_ptr<tabs::Tab> >); | |
278 for (size_t i = 0; i < window.tabs.size(); ++i) { | |
279 tabs->push_back(make_linked_ptr( | |
280 CreateTabModel(session_tag, *window.tabs[i], i, | |
281 window.selected_tab_index).release())); | |
282 } | |
283 | |
284 std::string session_id = | |
285 SessionId(session_tag, window.window_id.id()).ToString(); | |
286 | |
287 windows::Window::Type type = windows::Window::TYPE_NONE; | |
288 switch (window.type) { | |
289 case Browser::TYPE_TABBED: | |
290 type = windows::Window::TYPE_NORMAL; | |
291 break; | |
292 case Browser::TYPE_POPUP: | |
293 type = windows::Window::TYPE_POPUP; | |
294 break; | |
295 } | |
296 | |
297 windows::Window::State state = windows::Window::STATE_NONE; | |
298 switch (window.show_state) { | |
299 case ui::SHOW_STATE_NORMAL: | |
300 state = windows::Window::STATE_NORMAL; | |
301 break; | |
302 case ui::SHOW_STATE_MINIMIZED: | |
303 state = windows::Window::STATE_MINIMIZED; | |
304 break; | |
305 case ui::SHOW_STATE_MAXIMIZED: | |
306 state = windows::Window::STATE_MAXIMIZED; | |
307 break; | |
308 case ui::SHOW_STATE_FULLSCREEN: | |
309 state = windows::Window::STATE_FULLSCREEN; | |
310 break; | |
311 default: | |
312 break; | |
313 } | |
314 | |
315 scoped_ptr<windows::Window> window_struct( | |
316 CreateWindowModelHelper(tabs.Pass(), session_id, type, state)); | |
317 // TODO(dwankri): Dig deeper to resolve bounds not being optional, so closed | |
318 // windows in GetRecentlyClosed can have set values in Window helper. | |
319 window_struct->left.reset(new int(window.bounds.x())); | |
320 window_struct->top.reset(new int(window.bounds.y())); | |
321 window_struct->width.reset(new int(window.bounds.width())); | |
322 window_struct->height.reset(new int(window.bounds.height())); | |
323 | |
324 return window_struct.Pass(); | |
325 } | |
326 | |
327 scoped_ptr<api::sessions::Session> | |
328 SessionsGetDevicesFunction::CreateSessionModel( | |
329 const SessionWindow& window, const std::string& session_tag) { | |
330 scoped_ptr<windows::Window> window_model( | |
331 CreateWindowModel(window, session_tag)); | |
332 // There is a chance that after pruning uninteresting tabs the window will be | |
333 // empty. | |
334 return !window_model ? scoped_ptr<api::sessions::Session>() | |
335 : CreateSessionModelHelper(window.timestamp.ToTimeT(), | |
336 scoped_ptr<tabs::Tab>(), | |
337 window_model.Pass()); | |
338 } | |
339 | |
340 scoped_ptr<api::sessions::Device> SessionsGetDevicesFunction::CreateDeviceModel( | |
341 const browser_sync::SyncedSession* session) { | |
342 int max_results = kMaxSessionResults; | |
343 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); | |
344 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
345 if (params->options && params->options->max_results) | |
346 max_results = *params->options->max_results.get(); | |
not at google - send to devlin
2013/08/19 19:34:40
no need for the .get() here. same for elsewhere wi
Kristen Dwan
2013/08/19 21:35:05
Done.
| |
347 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && | |
348 max_results <= kMaxSessionResults); | |
349 | |
350 scoped_ptr<api::sessions::Device> device_struct(new api::sessions::Device); | |
351 device_struct->info = session->session_name; | |
352 | |
353 for (browser_sync::SyncedSession::SyncedWindowMap::const_iterator it = | |
354 session->windows.begin(); it != session->windows.end() && | |
355 static_cast<int>(device_struct->sessions.size()) < max_results; ++it) { | |
356 scoped_ptr<api::sessions::Session> session_model(CreateSessionModel( | |
357 *it->second, session->session_tag)); | |
358 if (session_model) | |
359 device_struct->sessions.push_back(make_linked_ptr( | |
360 session_model.release())); | |
361 } | |
362 return device_struct.Pass(); | |
363 } | |
364 | |
365 bool SessionsGetDevicesFunction::RunImpl() { | |
366 ProfileSyncService* service = | |
367 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile()); | |
368 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { | |
369 // Sync not enabled. | |
370 results_ = GetDevices::Results::Create( | |
371 std::vector<linked_ptr<api::sessions::Device> >()); | |
372 return true; | |
373 } | |
374 | |
375 browser_sync::SessionModelAssociator* associator = | |
376 service->GetSessionModelAssociator(); | |
377 std::vector<const browser_sync::SyncedSession*> sessions; | |
378 if (!(associator && associator->GetAllForeignSessions(&sessions))) { | |
379 results_ = GetDevices::Results::Create( | |
380 std::vector<linked_ptr<api::sessions::Device> >()); | |
381 return true; | |
382 } | |
383 | |
384 std::vector<linked_ptr<api::sessions::Device> > result; | |
385 // Sort sessions from most recent to least recent. | |
386 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency); | |
387 for (size_t i = 0; i < sessions.size(); ++i) { | |
388 result.push_back(make_linked_ptr(CreateDeviceModel(sessions[i]).release())); | |
389 } | |
390 | |
391 results_ = GetDevices::Results::Create(result); | |
392 return true; | |
393 } | |
394 | |
395 void SessionsRestoreFunction::SetInvalidIdError(const std::string& invalid_id) { | |
396 SetError(ErrorUtils::FormatErrorMessage(kInvalidSessionIdError, invalid_id)); | |
397 } | |
398 | |
399 | |
400 void SessionsRestoreFunction::SetResultRestoredTab( | |
401 const content::WebContents* contents) { | |
402 scoped_ptr<tabs::Tab> tab(tabs::Tab::FromValue( | |
403 *ExtensionTabUtil::CreateTabValue(contents, GetExtension()))); | |
404 scoped_ptr<api::sessions::Session> restored_session(CreateSessionModelHelper( | |
405 base::Time::Now().ToTimeT(), | |
406 tab.Pass(), | |
407 scoped_ptr<windows::Window>())); | |
408 results_ = Restore::Results::Create(*restored_session.get()); | |
409 } | |
410 | |
411 bool SessionsRestoreFunction::SetResultRestoredWindow(int window_id) { | |
412 WindowController* controller = NULL; | |
413 if (!windows_util::GetWindowFromWindowID(this, window_id, &controller)) { | |
414 // error_ is set by GetWindowFromWindowId function call. | |
415 return false; | |
416 } | |
417 scoped_ptr<windows::Window> window(windows::Window::FromValue( | |
418 *controller->CreateWindowValueWithTabs(GetExtension()))); | |
419 results_ = Restore::Results::Create(*CreateSessionModelHelper( | |
420 base::Time::Now().ToTimeT(), | |
421 scoped_ptr<tabs::Tab>(), | |
422 window.Pass()).get()); | |
423 return true; | |
424 } | |
425 | |
426 bool SessionsRestoreFunction::RestoreMostRecentlyClosed(Browser* browser) { | |
427 TabRestoreService* tab_restore_service = | |
428 TabRestoreServiceFactory::GetForProfile(profile()); | |
429 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); | |
430 TabRestoreService::Entries entries = tab_restore_service->entries(); | |
431 | |
432 if (entries.empty()) { | |
433 SetError(kNoRecentlyClosedSessionsError); | |
434 return false; | |
435 } | |
436 | |
437 bool is_window = is_window_entry(entries.front()); | |
438 TabRestoreServiceDelegate* delegate = | |
439 TabRestoreServiceDelegate::FindDelegateForWebContents( | |
440 browser->tab_strip_model()->GetActiveWebContents()); | |
441 std::vector<content::WebContents*> contents = | |
442 tab_restore_service->RestoreMostRecentEntry(delegate, host_desktop_type); | |
443 DCHECK(contents.size()); | |
444 | |
445 if (is_window) { | |
446 return SetResultRestoredWindow( | |
447 ExtensionTabUtil::GetWindowIdOfTab(contents[0])); | |
448 } | |
449 | |
450 SetResultRestoredTab(contents[0]); | |
451 return true; | |
452 } | |
453 | |
454 bool SessionsRestoreFunction::RestoreLocalSession(const SessionId& session_id, | |
455 Browser* browser) { | |
456 TabRestoreService* tab_restore_service = | |
457 TabRestoreServiceFactory::GetForProfile(profile()); | |
458 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); | |
459 TabRestoreService::Entries entries = tab_restore_service->entries(); | |
460 | |
461 if (entries.empty()) { | |
462 SetInvalidIdError(session_id.ToString()); | |
463 return false; | |
464 } | |
465 | |
466 // Check if the recently closed list contains an entry with the provided id. | |
467 bool is_window = false; | |
468 for (TabRestoreService::Entries::iterator it = entries.begin(); | |
469 it != entries.end(); ++it) { | |
470 if ((*it)->id == session_id.id()) { | |
471 // The only time a full window is being restored is if the entry ID | |
472 // matches the provided ID and the entry type is Window. | |
473 is_window = is_window_entry(*it); | |
474 break; | |
475 } | |
476 } | |
477 | |
478 TabRestoreServiceDelegate* delegate = | |
479 TabRestoreServiceDelegate::FindDelegateForWebContents( | |
480 browser->tab_strip_model()->GetActiveWebContents()); | |
481 std::vector<content::WebContents*> contents = | |
482 tab_restore_service->RestoreEntryById(delegate, | |
483 session_id.id(), | |
484 host_desktop_type, | |
485 UNKNOWN); | |
486 // If the ID is invalid, contents will be empty. | |
487 if (!contents.size()) { | |
488 SetInvalidIdError(session_id.ToString()); | |
489 return false; | |
490 } | |
491 | |
492 // Retrieve the window through any of the tabs in contents. | |
493 if (is_window) { | |
494 return SetResultRestoredWindow( | |
495 ExtensionTabUtil::GetWindowIdOfTab(contents[0])); | |
496 } | |
497 | |
498 SetResultRestoredTab(contents[0]); | |
499 return true; | |
500 } | |
501 | |
502 bool SessionsRestoreFunction::RestoreForeignSession(const SessionId& session_id, | |
503 Browser* browser) { | |
504 ProfileSyncService* service = | |
505 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile()); | |
506 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { | |
507 SetError(kSessionSyncError); | |
508 return false; | |
509 } | |
510 browser_sync::SessionModelAssociator* associator = | |
511 service->GetSessionModelAssociator(); | |
512 if (!associator) { | |
513 SetError(kSessionSyncError); | |
514 return false; | |
515 } | |
516 | |
517 const SessionTab* tab = NULL; | |
518 if (associator->GetForeignTab(session_id.session_tag(), | |
519 session_id.id(), | |
520 &tab)) { | |
521 TabStripModel* tab_strip = browser->tab_strip_model(); | |
522 content::WebContents* contents = tab_strip->GetActiveWebContents(); | |
523 | |
524 content::WebContents* tab_contents = | |
525 SessionRestore::RestoreForeignSessionTab(contents, *tab, | |
526 NEW_FOREGROUND_TAB); | |
527 SetResultRestoredTab(tab_contents); | |
528 return true; | |
529 } | |
530 | |
531 // Restoring a full window. | |
532 std::vector<const SessionWindow*> windows; | |
533 if (!associator->GetForeignSession(session_id.session_tag(), &windows)) { | |
534 SetInvalidIdError(session_id.ToString()); | |
535 return false; | |
536 } | |
537 | |
538 std::vector<const SessionWindow*>::const_iterator window = windows.begin(); | |
539 while (window != windows.end() | |
540 && (*window)->window_id.id() != session_id.id()) { | |
541 ++window; | |
542 } | |
543 if (window == windows.end()) { | |
544 SetInvalidIdError(session_id.ToString()); | |
545 return false; | |
546 } | |
547 | |
548 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); | |
549 // Only restore one window at a time. | |
550 std::vector<Browser*> browsers = | |
551 SessionRestore::RestoreForeignSessionWindows(profile(), host_desktop_type, | |
552 window, window + 1); | |
553 // Will always create one browser because we only restore one window per call. | |
554 DCHECK_EQ(1u, browsers.size()); | |
555 return SetResultRestoredWindow(ExtensionTabUtil::GetWindowId(browsers[0])); | |
556 } | |
557 | |
558 bool SessionsRestoreFunction::RunImpl() { | |
160 scoped_ptr<Restore::Params> params(Restore::Params::Create(*args_)); | 559 scoped_ptr<Restore::Params> params(Restore::Params::Create(*args_)); |
161 EXTENSION_FUNCTION_VALIDATE(params.get()); | 560 EXTENSION_FUNCTION_VALIDATE(params.get()); |
162 | 561 |
163 Browser* browser = | 562 Browser* browser = |
164 chrome::FindBrowserWithProfile(profile(), | 563 chrome::FindBrowserWithProfile(profile(), |
165 chrome::HOST_DESKTOP_TYPE_NATIVE); | 564 chrome::HOST_DESKTOP_TYPE_NATIVE); |
166 if (!browser) { | 565 if (!browser) { |
167 error_ = kNoBrowserToRestoreSession; | 566 SetError(kNoBrowserToRestoreSession); |
168 return false; | 567 return false; |
169 } | 568 } |
170 | 569 |
171 TabRestoreService* tab_restore_service = | 570 if (!params->session_id) |
172 TabRestoreServiceFactory::GetForProfile(profile()); | 571 return RestoreMostRecentlyClosed(browser); |
173 TabRestoreServiceDelegate* delegate = | 572 |
174 TabRestoreServiceDelegate::FindDelegateForWebContents( | 573 scoped_ptr<SessionId> session_id(SessionId::Parse(*params->session_id)); |
175 browser->tab_strip_model()->GetActiveWebContents()); | 574 if (!session_id) { |
176 DCHECK(delegate); | 575 SetInvalidIdError(*params->session_id); |
177 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); | 576 return false; |
178 TabRestoreService::Entries entries = tab_restore_service->entries(); | 577 } |
179 | 578 |
180 if (entries.empty()) { | 579 return session_id->IsForeign() ? |
181 error_ = kRecentlyClosedListEmpty; | 580 RestoreForeignSession(*session_id.get(), browser) |
182 return false; | 581 : RestoreLocalSession(*session_id.get(), browser); |
183 } | 582 } |
184 | 583 |
185 if (!params->id) { | 584 SessionsAPI::SessionsAPI(Profile* profile) { |
186 tab_restore_service->RestoreMostRecentEntry(delegate, host_desktop_type); | 585 } |
187 return true; | 586 |
188 } | 587 SessionsAPI::~SessionsAPI() { |
189 | 588 } |
190 // Check if the recently closed list contains an entry with the provided id. | 589 |
191 bool is_valid_id = false; | 590 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; | 591 g_factory = LAZY_INSTANCE_INITIALIZER; |
232 | 592 |
233 // static | 593 // static |
234 ProfileKeyedAPIFactory<SessionRestoreAPI>* | 594 ProfileKeyedAPIFactory<SessionsAPI>* |
235 SessionRestoreAPI::GetFactoryInstance() { | 595 SessionsAPI::GetFactoryInstance() { |
236 return &g_factory.Get(); | 596 return &g_factory.Get(); |
237 } | 597 } |
238 | 598 |
239 } // namespace extensions | 599 } // namespace extensions |
OLD | NEW |