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 <iostream> |
6 | |
7 #include "chrome/browser/extensions/api/sessions/sessions_api.h" | |
6 | 8 |
7 #include <vector> | 9 #include <vector> |
8 | 10 |
9 #include "base/i18n/rtl.h" | 11 #include "base/i18n/rtl.h" |
10 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
11 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.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/foreign_session_id.h" | |
17 #include "chrome/browser/extensions/api/tabs/windows_helper.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" |
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" |
27 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
36 #include "extensions/common/error_utils.h" | |
28 #include "ui/base/layout.h" | 37 #include "ui/base/layout.h" |
29 | 38 |
30 | |
31 namespace { | 39 namespace { |
32 | 40 |
33 const unsigned int kMaxRecentlyClosedSessionResults = 25; | 41 const int kMaxRecentlyClosedSessionResults = 25; |
34 const char kRecentlyClosedListEmpty[] = | 42 const int kMaxSyncedSessionResults = 10; |
43 const char kRecentlyClosedListEmptyError[] = | |
not at google - send to devlin
2013/08/12 23:51:34
this name is bugging me. kNoRecentlyClosedSessions
Kristen Dwan
2013/08/15 07:11:56
Done.
| |
35 "There are no recently closed sessions."; | 44 "There are no recently closed sessions."; |
36 const char kInvalidSessionId[] = "Invalid session id."; | 45 const char kInvalidSessionIdError[] = "Invalid session id."; |
not at google - send to devlin
2013/08/12 23:51:34
would be nicer if it included the session ID in he
Kristen Dwan
2013/08/15 07:11:56
Done.
| |
37 const char kNoBrowserToRestoreSession[] = | 46 const char kNoBrowserToRestoreSession[] = |
38 "There are no browser windows to restore the session."; | 47 "There are no browser windows to restore the session."; |
48 const char kSessionSyncNotEnabledError[] = "Syncing sessions is not enabled."; | |
49 const char kSyncedSessionsListEmptyError[] = "There are no foreign sessions."; | |
not at google - send to devlin
2013/08/12 23:51:34
not used
Kristen Dwan
2013/08/15 07:11:56
Done.
| |
50 const char kSessionNotFoundError[] = "Session is not found."; | |
not at google - send to devlin
2013/08/12 23:51:34
would be nicer if it included the session ID in he
Kristen Dwan
2013/08/15 07:11:56
no reason to have session not found and invalid se
| |
51 const char kSessionSyncError[] = "Session sync error."; | |
52 | |
53 // Comparator function for use with std::sort that will sort sessions by | |
54 // descending modified_time (i.e., most recent first). | |
55 bool SortSessionsByRecency(const browser_sync::SyncedSession* s1, | |
56 const browser_sync::SyncedSession* s2) { | |
57 return s1->modified_time > s2->modified_time; | |
58 } | |
59 | |
60 scoped_ptr<extensions::api::tabs::Tab> CreateTabModelHelper( | |
not at google - send to devlin
2013/08/12 23:51:34
Put this whole file in the extensions namespace. T
Kristen Dwan
2013/08/15 07:11:56
Done.
| |
61 const sessions::SerializedNavigationEntry& current_navigation, | |
62 const std::string& session_id, | |
63 int index, | |
64 bool pinned, | |
65 int selected_index, | |
66 const extensions::Extension* extension) { | |
67 scoped_ptr<extensions::api::tabs::Tab> tab_struct( | |
68 new extensions::api::tabs::Tab); | |
69 | |
70 GURL gurl = current_navigation.virtual_url(); | |
71 std::string title = UTF16ToUTF8(current_navigation.title()); | |
72 | |
73 tab_struct->session_id.reset(new std::string(session_id)); | |
74 tab_struct->url.reset(new std::string(gurl.spec())); | |
75 tab_struct->title.reset(new std::string(title.empty() ? gurl.spec() : title)); | |
not at google - send to devlin
2013/08/12 23:51:34
This line looked suspicious so I did some digging.
Kristen Dwan
2013/08/15 07:11:56
Done.
| |
76 tab_struct->index = index; | |
77 tab_struct->pinned = pinned; | |
78 tab_struct->selected = index == selected_index; | |
79 tab_struct->active = false; | |
80 tab_struct->highlighted = false; | |
81 tab_struct->incognito = false; | |
82 ExtensionTabUtil::ScrubTabForExtension(extension, tab_struct.get()); | |
83 return tab_struct.Pass(); | |
84 } | |
85 | |
86 scoped_ptr<extensions::api::windows::Window> CreateWindowModelHelper( | |
87 std::vector<linked_ptr<extensions::api::tabs::Tab> >* tabs, | |
not at google - send to devlin
2013/08/12 23:51:34
since you're passing ownership to this method, tab
Kristen Dwan
2013/08/15 07:11:56
Done.
| |
88 const std::string& session_id, | |
89 const extensions::api::windows::Window::Type& type, | |
90 const extensions::api::windows::Window::State& state) { | |
91 scoped_ptr<extensions::api::windows::Window> window_struct( | |
92 new extensions::api::windows::Window); | |
93 window_struct->tabs.reset(tabs); | |
94 window_struct->session_id.reset(new std::string(session_id)); | |
95 window_struct->incognito = false; | |
96 window_struct->always_on_top = false; | |
97 window_struct->focused = false; | |
98 window_struct->type = type; | |
99 window_struct->state = state; | |
100 return window_struct.Pass(); | |
101 } | |
102 | |
103 scoped_ptr<extensions::api::sessions::Session> CreateSessionModelHelper( | |
104 int last_modified, | |
105 extensions::api::tabs::Tab* tab, | |
106 extensions::api::windows::Window* window) { | |
not at google - send to devlin
2013/08/12 23:51:34
pass scoped_ptrs in here.
Kristen Dwan
2013/08/15 07:11:56
Done.
| |
107 scoped_ptr<extensions::api::sessions::Session> session_struct( | |
108 new extensions::api::sessions::Session); | |
109 session_struct->last_modified = last_modified; | |
110 if (tab) | |
111 session_struct->tab.reset(tab); | |
112 else if (window) | |
113 session_struct->window.reset(window); | |
not at google - send to devlin
2013/08/12 23:51:34
else
NOTREACHED()
?
Kristen Dwan
2013/08/15 07:11:56
Done.
| |
114 return session_struct.Pass(); | |
115 } | |
116 | |
117 inline bool is_type_window(const TabRestoreService::Entry* entry) { | |
not at google - send to devlin
2013/08/12 23:51:34
inline doesn't mean anything in this context, comp
Kristen Dwan
2013/08/15 07:11:56
Done.
| |
118 return entry->type == TabRestoreService::WINDOW; | |
119 } | |
39 | 120 |
40 } // namespace | 121 } // namespace |
41 | 122 |
42 namespace extensions { | 123 namespace extensions { |
43 | 124 |
44 namespace GetRecentlyClosed = api::session_restore::GetRecentlyClosed; | 125 namespace GetRecentlyClosed = api::sessions::GetRecentlyClosed; |
45 namespace Restore = api::session_restore::Restore; | 126 namespace GetDevices = api::sessions::GetDevices; |
127 namespace Restore = api::sessions::Restore; | |
46 namespace tabs = api::tabs; | 128 namespace tabs = api::tabs; |
47 namespace windows = api::windows; | 129 namespace windows = api::windows; |
48 namespace session_restore = api::session_restore; | |
49 | 130 |
50 scoped_ptr<tabs::Tab> SessionRestoreGetRecentlyClosedFunction::CreateTabModel( | 131 scoped_ptr<tabs::Tab> SessionsGetRecentlyClosedFunction::CreateTabModel( |
51 const TabRestoreService::Tab& tab, int selected_index) { | 132 const TabRestoreService::Tab& tab, int session_id, int selected_index) { |
52 scoped_ptr<tabs::Tab> tab_struct(new tabs::Tab); | 133 return CreateTabModelHelper(tab.navigations[tab.current_navigation_index], |
53 const sessions::SerializedNavigationEntry& current_navigation = | 134 base::IntToString(session_id), |
54 tab.navigations[tab.current_navigation_index]; | 135 tab.tabstrip_index, |
55 GURL gurl = current_navigation.virtual_url(); | 136 tab.pinned, |
56 std::string title = UTF16ToUTF8(current_navigation.title()); | 137 selected_index, |
57 | 138 GetExtension()).Pass(); |
58 tab_struct->url.reset(new std::string(gurl.spec())); | |
59 tab_struct->title.reset(new std::string(title.empty() ? gurl.spec() : title)); | |
60 tab_struct->index = tab.tabstrip_index; | |
61 tab_struct->pinned = tab.pinned; | |
62 tab_struct->id = tab.id; | |
63 tab_struct->window_id = tab.browser_id; | |
64 tab_struct->index = tab.tabstrip_index; | |
65 tab_struct->pinned = tab.pinned; | |
66 tab_struct->selected = tab.tabstrip_index == selected_index; | |
67 tab_struct->active = false; | |
68 tab_struct->highlighted = false; | |
69 tab_struct->incognito = false; | |
70 ExtensionTabUtil::ScrubTabForExtension(GetExtension(), | |
71 tab_struct.get()); | |
72 return tab_struct.Pass(); | |
73 } | 139 } |
74 | 140 |
75 scoped_ptr<windows::Window> | 141 scoped_ptr<windows::Window> |
76 SessionRestoreGetRecentlyClosedFunction::CreateWindowModel( | 142 SessionsGetRecentlyClosedFunction::CreateWindowModel( |
77 const TabRestoreService::Window& window) { | 143 const TabRestoreService::Window& window, |
78 scoped_ptr<windows::Window> window_struct(new windows::Window); | 144 int session_id) { |
79 DCHECK(!window.tabs.empty()); | 145 DCHECK(!window.tabs.empty()); |
80 | 146 |
81 scoped_ptr<std::vector<linked_ptr<tabs::Tab> > > tabs( | 147 scoped_ptr<std::vector<linked_ptr<tabs::Tab> > > tabs( |
82 new std::vector<linked_ptr<tabs::Tab> >); | 148 new std::vector<linked_ptr<tabs::Tab> >); |
83 for (size_t i = 0; i < window.tabs.size(); ++i) { | 149 for (size_t i = 0; i < window.tabs.size(); ++i) { |
84 tabs->push_back(make_linked_ptr(CreateTabModel(window.tabs[i], | 150 tabs->push_back(make_linked_ptr( |
85 window.selected_tab_index).release())); | 151 CreateTabModel(window.tabs[i], window.tabs[i].id, |
152 window.selected_tab_index).release())); | |
86 } | 153 } |
87 window_struct->tabs.reset(tabs.release()); | 154 |
88 window_struct->incognito = false; | 155 return CreateWindowModelHelper(tabs.release(), |
89 window_struct->always_on_top = false; | 156 base::IntToString(session_id), |
90 window_struct->focused = false; | 157 windows::Window::TYPE_NORMAL, |
91 window_struct->type = windows::Window::TYPE_NORMAL; | 158 windows::Window::STATE_NORMAL).Pass(); |
92 window_struct->state = windows::Window::STATE_NORMAL; | |
93 return window_struct.Pass(); | |
94 } | 159 } |
95 | 160 |
96 scoped_ptr<session_restore::ClosedEntry> | 161 scoped_ptr<api::sessions::Session> |
97 SessionRestoreGetRecentlyClosedFunction::CreateEntryModel( | 162 SessionsGetRecentlyClosedFunction::CreateSessionModel( |
98 const TabRestoreService::Entry* entry) { | 163 const TabRestoreService::Entry* entry) { |
99 scoped_ptr<session_restore::ClosedEntry> entry_struct( | 164 if (entry->type == TabRestoreService::TAB) { |
not at google - send to devlin
2013/08/12 23:51:34
I preferred the switch...
Kristen Dwan
2013/08/15 07:11:56
Done.
| |
100 new session_restore::ClosedEntry); | 165 scoped_ptr<tabs::Tab> tab(CreateTabModel( |
101 switch (entry->type) { | 166 *static_cast<const TabRestoreService::Tab*>(entry), entry->id, -1) |
102 case TabRestoreService::TAB: | 167 .release()); |
not at google - send to devlin
2013/08/12 23:51:34
CreateTabModel already returns a scoped_ptr so you
Kristen Dwan
2013/08/15 07:11:56
Done.
| |
103 entry_struct->tab.reset(CreateTabModel( | 168 return CreateSessionModelHelper(entry->timestamp.ToTimeT(), tab.release(), |
104 *static_cast<const TabRestoreService::Tab*>(entry), -1).release()); | 169 NULL).Pass(); |
105 break; | |
106 case TabRestoreService::WINDOW: | |
107 entry_struct->window.reset(CreateWindowModel( | |
108 *static_cast<const TabRestoreService::Window*>(entry)).release()); | |
109 break; | |
110 default: | |
111 NOTREACHED(); | |
112 } | 170 } |
113 entry_struct->timestamp = entry->timestamp.ToTimeT(); | 171 |
114 entry_struct->id = entry->id; | 172 DCHECK(entry->type == TabRestoreService::WINDOW); |
115 return entry_struct.Pass(); | 173 scoped_ptr<windows::Window> window(CreateWindowModel( |
174 *static_cast<const TabRestoreService::Window*>(entry), entry->id) | |
175 .release()); | |
176 return CreateSessionModelHelper(entry->timestamp.ToTimeT(), NULL, | |
177 window.release()).Pass(); | |
116 } | 178 } |
117 | 179 |
118 bool SessionRestoreGetRecentlyClosedFunction::RunImpl() { | 180 bool SessionsGetRecentlyClosedFunction::RunImpl() { |
119 scoped_ptr<GetRecentlyClosed::Params> params( | 181 scoped_ptr<GetRecentlyClosed::Params> params( |
120 GetRecentlyClosed::Params::Create(*args_)); | 182 GetRecentlyClosed::Params::Create(*args_)); |
121 EXTENSION_FUNCTION_VALIDATE(params.get()); | 183 EXTENSION_FUNCTION_VALIDATE(params.get()); |
122 unsigned int max_results = kMaxRecentlyClosedSessionResults; | 184 int max_results = kMaxRecentlyClosedSessionResults; |
123 if (params->options && params->options->max_results) | 185 if (params->options && params->options->max_results) |
124 max_results = *params->options->max_results; | 186 max_results = *params->options->max_results; |
125 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && | 187 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && |
126 max_results <= kMaxRecentlyClosedSessionResults); | 188 max_results <= kMaxRecentlyClosedSessionResults); |
127 | 189 |
128 std::vector<linked_ptr<session_restore::ClosedEntry> > result; | 190 std::vector<linked_ptr<api::sessions::Session> > result; |
129 TabRestoreService* tab_restore_service = | 191 TabRestoreService* tab_restore_service = |
130 TabRestoreServiceFactory::GetForProfile(profile()); | 192 TabRestoreServiceFactory::GetForProfile(profile()); |
131 DCHECK(tab_restore_service); | 193 DCHECK(tab_restore_service); |
132 | 194 |
133 // List of entries. They are ordered from most to least recent. | 195 // 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 | 196 // We prune the list to contain max 25 entries at any time and removes |
135 // uninteresting entries. | 197 // uninteresting entries. |
136 TabRestoreService::Entries entries = tab_restore_service->entries(); | 198 TabRestoreService::Entries entries = tab_restore_service->entries(); |
137 for (TabRestoreService::Entries::const_iterator it = entries.begin(); | 199 for (TabRestoreService::Entries::const_iterator it = entries.begin(); |
138 it != entries.end() && result.size() < max_results; ++it) { | 200 it != entries.end() && static_cast<int>(result.size()) < max_results; |
201 ++it) { | |
139 TabRestoreService::Entry* entry = *it; | 202 TabRestoreService::Entry* entry = *it; |
140 if (!params->options || params->options->entry_type == | 203 if (!params->options || params->options->entry_type == |
141 GetRecentlyClosed::Params::Options::ENTRY_TYPE_NONE) { | 204 GetRecentlyClosed::Params::Options::ENTRY_TYPE_NONE) { |
142 // Include both tabs and windows if type is not defined. | 205 // Include both tabs and windows if type is not defined. |
143 result.push_back(make_linked_ptr(CreateEntryModel(entry).release())); | 206 result.push_back(make_linked_ptr(CreateSessionModel(entry).release())); |
144 } else if ( | 207 } else if ( |
145 (params->options->entry_type == | 208 (params->options->entry_type == |
146 GetRecentlyClosed::Params::Options::ENTRY_TYPE_TAB && | 209 GetRecentlyClosed::Params::Options::ENTRY_TYPE_TAB && |
147 entry->type == TabRestoreService::TAB) || | 210 entry->type == TabRestoreService::TAB) || |
148 (params->options->entry_type == | 211 (params->options->entry_type == |
149 GetRecentlyClosed::Params::Options::ENTRY_TYPE_WINDOW && | 212 GetRecentlyClosed::Params::Options::ENTRY_TYPE_WINDOW && |
150 entry->type == TabRestoreService::WINDOW)) { | 213 entry->type == TabRestoreService::WINDOW)) { |
151 result.push_back(make_linked_ptr(CreateEntryModel(entry).release())); | 214 result.push_back(make_linked_ptr(CreateSessionModel(entry).release())); |
152 } | 215 } |
153 } | 216 } |
154 | 217 |
155 results_ = GetRecentlyClosed::Results::Create(result); | 218 results_ = GetRecentlyClosed::Results::Create(result); |
156 return true; | 219 return true; |
157 } | 220 } |
158 | 221 |
159 bool SessionRestoreRestoreFunction::RunImpl() { | 222 scoped_ptr<tabs::Tab> SessionsGetDevicesFunction::CreateTabModel( |
160 scoped_ptr<Restore::Params> params(Restore::Params::Create(*args_)); | 223 const std::string& session_tag, |
224 const SessionTab& tab, | |
225 int tab_index, | |
226 int selected_index) { | |
227 std::string session_id = | |
228 ForeignSessionId(session_tag, tab.tab_id.id()).ToString(); | |
not at google - send to devlin
2013/08/12 23:51:34
palindromic expression, nice.
| |
229 return CreateTabModelHelper(tab.navigations[tab.current_navigation_index], | |
230 session_id, | |
231 tab_index, | |
232 tab.pinned, | |
233 selected_index, | |
234 GetExtension()).Pass(); | |
not at google - send to devlin
2013/08/12 23:51:34
I don't think you need to Pass() if the scoped_ptr
Kristen Dwan
2013/08/15 07:11:56
Done.
| |
235 } | |
236 | |
237 scoped_ptr<windows::Window> SessionsGetDevicesFunction::CreateWindowModel( | |
238 const SessionWindow& window, const std::string& session_tag) { | |
239 DCHECK(!window.tabs.empty()); | |
240 | |
241 scoped_ptr<std::vector<linked_ptr<tabs::Tab> > > tabs( | |
242 new std::vector<linked_ptr<tabs::Tab> >); | |
243 for (size_t i = 0; i < window.tabs.size(); ++i) { | |
244 tabs->push_back(make_linked_ptr( | |
245 CreateTabModel(session_tag, *window.tabs[i], i, | |
246 window.selected_tab_index).release())); | |
247 } | |
248 | |
249 std::string session_id = | |
250 ForeignSessionId(session_tag, window.window_id.id()).ToString(); | |
251 | |
252 windows::Window::Type type; | |
253 switch (window.type) { | |
254 case Browser::TYPE_TABBED: | |
255 type = windows::Window::TYPE_NORMAL; | |
256 break; | |
257 case Browser::TYPE_POPUP: | |
258 type = windows::Window::TYPE_POPUP; | |
259 break; | |
260 default: | |
not at google - send to devlin
2013/08/12 23:51:34
default is unnecessary. Just initialize type to TY
Kristen Dwan
2013/08/15 07:11:56
Done.
| |
261 type = windows::Window::TYPE_NONE; | |
262 } | |
263 | |
264 windows::Window::State state; | |
265 switch (window.show_state) { | |
266 case ui::SHOW_STATE_NORMAL: | |
267 state = windows::Window::STATE_NORMAL; | |
268 break; | |
269 case ui::SHOW_STATE_MINIMIZED: | |
270 state = windows::Window::STATE_MINIMIZED; | |
271 break; | |
272 case ui::SHOW_STATE_MAXIMIZED: | |
273 state = windows::Window::STATE_MAXIMIZED; | |
274 break; | |
275 case ui::SHOW_STATE_FULLSCREEN: | |
276 state = windows::Window::STATE_FULLSCREEN; | |
277 break; | |
278 default: | |
279 state = windows::Window::STATE_NONE; | |
not at google - send to devlin
2013/08/12 23:51:34
ditto
Kristen Dwan
2013/08/15 07:11:56
Done.
| |
280 } | |
281 | |
282 scoped_ptr<windows::Window> window_struct( | |
283 CreateWindowModelHelper(tabs.release(), session_id, type, state) | |
284 .release()); | |
not at google - send to devlin
2013/08/12 23:51:34
release also not needed here
Kristen Dwan
2013/08/15 07:11:56
Done.
| |
285 // TODO(dwankri): Dig deeper to resolve bounds not being optional, so closed | |
286 // windows in GetRecentlyClosed can have set values in Window helper. | |
287 window_struct->left.reset(new int(window.bounds.x())); | |
288 window_struct->top.reset(new int(window.bounds.y())); | |
289 window_struct->width.reset(new int(window.bounds.width())); | |
290 window_struct->height.reset(new int(window.bounds.height())); | |
291 | |
292 return window_struct.Pass(); | |
293 } | |
294 | |
295 scoped_ptr<api::sessions::Session> | |
296 SessionsGetDevicesFunction::CreateSessionModel( | |
297 const SessionWindow& window, const std::string& session_tag) { | |
298 return CreateSessionModelHelper(window.timestamp.ToTimeT(), NULL, | |
299 CreateWindowModel(window, session_tag) | |
300 .release()).Pass(); | |
301 } | |
302 | |
303 scoped_ptr<api::sessions::Device> SessionsGetDevicesFunction::CreateDeviceModel( | |
304 const browser_sync::SyncedSession* session) { | |
305 scoped_ptr<api::sessions::Device> device_struct(new api::sessions::Device); | |
306 device_struct->info = session->session_name; | |
307 | |
308 for (browser_sync::SyncedSession::SyncedWindowMap::const_iterator it = | |
309 session->windows.begin(); it != session->windows.end(); ++it) { | |
310 device_struct->sessions.push_back(make_linked_ptr(CreateSessionModel( | |
311 *it->second, session->session_tag).release())); | |
312 } | |
313 return device_struct.Pass(); | |
314 } | |
315 | |
316 bool SessionsGetDevicesFunction::RunImpl() { | |
317 ProfileSyncService* service = | |
318 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile()); | |
319 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { | |
320 // Sync not enabled. | |
321 results_ = GetDevices::Results::Create( | |
322 std::vector<linked_ptr<api::sessions::Device> >()); | |
323 return true; | |
324 } | |
325 | |
326 browser_sync::SessionModelAssociator* associator = | |
327 service->GetSessionModelAssociator(); | |
328 std::vector<const browser_sync::SyncedSession*> sessions; | |
329 if (!(associator && associator->GetAllForeignSessions(&sessions))) { | |
330 results_ = GetDevices::Results::Create( | |
331 std::vector<linked_ptr<api::sessions::Device> >()); | |
332 return true; | |
333 } | |
334 | |
335 int max_results = kMaxSyncedSessionResults; | |
336 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); | |
161 EXTENSION_FUNCTION_VALIDATE(params.get()); | 337 EXTENSION_FUNCTION_VALIDATE(params.get()); |
162 | 338 if (params->options && params->options->max_results) |
163 Browser* browser = | 339 max_results = *params->options->max_results.get(); |
164 chrome::FindBrowserWithProfile(profile(), | 340 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && |
165 chrome::HOST_DESKTOP_TYPE_NATIVE); | 341 max_results <= kMaxSyncedSessionResults); |
166 if (!browser) { | 342 |
167 error_ = kNoBrowserToRestoreSession; | 343 std::vector<linked_ptr<api::sessions::Device> > result; |
344 // Sort sessions from most recent to least recent. | |
345 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency); | |
346 for (size_t i = 0; i < sessions.size() && | |
347 static_cast<int>(result.size()) < max_results; ++i) { | |
348 result.push_back(make_linked_ptr(CreateDeviceModel(sessions[i]).release())); | |
349 } | |
350 | |
351 results_ = GetDevices::Results::Create(result); | |
352 return true; | |
353 } | |
354 | |
355 void SessionsRestoreFunction::SetResultRestoredTab( | |
356 const content::WebContents* contents) { | |
357 scoped_ptr<tabs::Tab> tab(tabs::Tab::FromValue( | |
358 *ExtensionTabUtil::CreateTabValue(contents, GetExtension()))); | |
359 results_ = Restore::Results::Create(*CreateSessionModelHelper( | |
360 base::Time::Now().ToTimeT(), | |
361 tab.release(), | |
362 NULL).get()); | |
363 } | |
364 | |
365 bool SessionsRestoreFunction::SetResultRestoredWindow(int window_id) { | |
366 WindowController* controller = NULL; | |
367 if (!GetWindowFromWindowID(this, window_id, &controller)) { | |
368 // error_ is set by GetWindowFromWindowId function call. | |
168 return false; | 369 return false; |
169 } | 370 } |
170 | 371 scoped_ptr<windows::Window> window(windows::Window::FromValue( |
372 *controller->CreateWindowValueWithTabs(GetExtension()))); | |
373 results_ = Restore::Results::Create(*CreateSessionModelHelper( | |
374 base::Time::Now().ToTimeT(), | |
375 NULL, | |
376 window.release()).get()); | |
377 return true; | |
378 } | |
379 | |
380 bool SessionsRestoreFunction::RestoreMostRecentlyClosed(Browser* browser) { | |
171 TabRestoreService* tab_restore_service = | 381 TabRestoreService* tab_restore_service = |
172 TabRestoreServiceFactory::GetForProfile(profile()); | 382 TabRestoreServiceFactory::GetForProfile(profile()); |
383 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); | |
384 TabRestoreService::Entries entries = tab_restore_service->entries(); | |
385 | |
386 if (entries.empty()) { | |
387 SetError(kRecentlyClosedListEmptyError); | |
388 return false; | |
389 } | |
390 | |
391 bool is_window = is_type_window(entries.front()); | |
173 TabRestoreServiceDelegate* delegate = | 392 TabRestoreServiceDelegate* delegate = |
174 TabRestoreServiceDelegate::FindDelegateForWebContents( | 393 TabRestoreServiceDelegate::FindDelegateForWebContents( |
175 browser->tab_strip_model()->GetActiveWebContents()); | 394 browser->tab_strip_model()->GetActiveWebContents()); |
176 DCHECK(delegate); | 395 std::vector<content::WebContents*> contents = |
396 tab_restore_service->RestoreMostRecentEntry(delegate, host_desktop_type); | |
397 DCHECK(contents.size()); | |
398 | |
399 if (is_window) { | |
400 return SetResultRestoredWindow( | |
401 ExtensionTabUtil::GetWindowIdOfTab(contents[0])); | |
402 } | |
403 | |
404 SetResultRestoredTab(contents[0]); | |
405 return true; | |
406 } | |
407 | |
408 bool SessionsRestoreFunction::RestoreLocalSession(int id, Browser* browser) { | |
409 TabRestoreService* tab_restore_service = | |
410 TabRestoreServiceFactory::GetForProfile(profile()); | |
177 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); | 411 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); |
178 TabRestoreService::Entries entries = tab_restore_service->entries(); | 412 TabRestoreService::Entries entries = tab_restore_service->entries(); |
179 | 413 |
180 if (entries.empty()) { | 414 if (entries.empty()) { |
181 error_ = kRecentlyClosedListEmpty; | 415 SetError(kInvalidSessionIdError); |
not at google - send to devlin
2013/08/12 23:51:34
Doesn't seem like the right error string.
Kristen Dwan
2013/08/15 07:11:56
i figured if you passed in an id and sessions are
not at google - send to devlin
2013/08/15 20:16:24
Makes sense.
| |
182 return false; | 416 return false; |
183 } | 417 } |
184 | 418 |
185 if (!params->id) { | |
186 tab_restore_service->RestoreMostRecentEntry(delegate, host_desktop_type); | |
187 return true; | |
188 } | |
189 | |
190 // Check if the recently closed list contains an entry with the provided id. | 419 // Check if the recently closed list contains an entry with the provided id. |
191 bool is_valid_id = false; | 420 bool is_valid_id = false; |
421 bool is_window = false; | |
192 for (TabRestoreService::Entries::iterator it = entries.begin(); | 422 for (TabRestoreService::Entries::iterator it = entries.begin(); |
193 it != entries.end(); ++it) { | 423 it != entries.end(); ++it) { |
194 if ((*it)->id == *params->id) { | 424 if ((*it)->id == id) { |
195 is_valid_id = true; | 425 is_valid_id = true; |
426 // The only time a full window is being restored is if the entry ID | |
427 // matches the provided ID and the entry type is Window. | |
428 is_window = is_type_window(*it); | |
196 break; | 429 break; |
197 } | 430 } |
198 | |
199 // For Window entries, see if the ID matches a tab. If so, report true for | 431 // For Window entries, see if the ID matches a tab. If so, report true for |
200 // the window as the Entry. | 432 // the window as the Entry. |
201 if ((*it)->type == TabRestoreService::WINDOW) { | 433 if (is_type_window(*it)) { |
202 std::vector<TabRestoreService::Tab>& tabs = | 434 std::vector<TabRestoreService::Tab>& tabs = |
203 static_cast<TabRestoreService::Window*>(*it)->tabs; | 435 static_cast<TabRestoreService::Window*>(*it)->tabs; |
204 for (std::vector<TabRestoreService::Tab>::iterator tab_it = tabs.begin(); | 436 for (std::vector<TabRestoreService::Tab>::iterator tab_it = tabs.begin(); |
205 tab_it != tabs.end(); ++tab_it) { | 437 tab_it != tabs.end(); ++tab_it) { |
206 if ((*tab_it).id == *params->id) { | 438 if ((*tab_it).id == id) { |
207 is_valid_id = true; | 439 is_valid_id = true; |
208 break; | 440 break; |
209 } | 441 } |
210 } | 442 } |
211 } | 443 } |
212 } | 444 } |
213 | 445 |
214 if (!is_valid_id) { | 446 if (!is_valid_id) { |
215 error_ = kInvalidSessionId; | 447 SetError(kInvalidSessionIdError); |
not at google - send to devlin
2013/08/12 23:51:34
doesn't seem like the right error string either.
Kristen Dwan
2013/08/15 07:11:56
if you didn't find the specific id, it would be in
not at google - send to devlin
2013/08/15 20:16:24
Makes sense, sorry.
| |
216 return false; | 448 return false; |
217 } | 449 } |
218 | 450 |
219 tab_restore_service->RestoreEntryById(delegate, *params->id, | 451 TabRestoreServiceDelegate* delegate = |
220 host_desktop_type, UNKNOWN); | 452 TabRestoreServiceDelegate::FindDelegateForWebContents( |
453 browser->tab_strip_model()->GetActiveWebContents()); | |
454 std::vector<content::WebContents*> contents = | |
455 tab_restore_service->RestoreEntryById(delegate, id, host_desktop_type, | |
456 UNKNOWN); | |
457 // We know the entry is valid. It should always be returned by contents. | |
458 DCHECK(contents.size()); | |
not at google - send to devlin
2013/08/12 23:51:34
I see. Yeah maybe we should just rely on this chec
Kristen Dwan
2013/08/15 07:11:56
good point. it does the exact thing. but before th
| |
459 | |
460 // The restored entry is a full window if ID matches and type is Window. | |
461 // Retrieve the window through any of the tabs in contents. | |
462 if (is_window) { | |
463 return SetResultRestoredWindow( | |
464 ExtensionTabUtil::GetWindowIdOfTab(contents[0])); | |
465 } | |
466 | |
467 SetResultRestoredTab(contents[0]); | |
221 return true; | 468 return true; |
222 } | 469 } |
223 | 470 |
224 SessionRestoreAPI::SessionRestoreAPI(Profile* profile) { | 471 bool SessionsRestoreFunction::RestoreForeignSession( |
472 const ForeignSessionId* fs_id, | |
473 Browser* browser) { | |
474 ProfileSyncService* service = | |
475 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile()); | |
476 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { | |
477 SetError(kSessionSyncNotEnabledError); | |
not at google - send to devlin
2013/08/12 23:51:34
let's just make all of the not-found errors the sa
Kristen Dwan
2013/08/15 07:11:56
we can't include the session id in the error messa
not at google - send to devlin
2013/08/15 20:16:24
Ah makes sense.
| |
478 return false; | |
479 } | |
480 browser_sync::SessionModelAssociator* associator = | |
481 service->GetSessionModelAssociator(); | |
482 if (!associator) { | |
483 SetError(kSessionSyncError); | |
not at google - send to devlin
2013/08/12 23:51:34
what does it actually mean for this to be null?
Kristen Dwan
2013/08/15 07:11:56
"Returns the session model associator associated w
| |
484 return false; | |
485 } | |
486 | |
487 const SessionTab* tab = NULL; | |
488 if (associator->GetForeignTab(fs_id->session_tag(), fs_id->id(), &tab)) { | |
489 TabStripModel* tab_strip = browser->tab_strip_model(); | |
490 content::WebContents* contents = tab_strip->GetActiveWebContents(); | |
491 | |
492 content::WebContents* tab_contents = | |
493 SessionRestore::RestoreForeignSessionTab(contents, *tab, | |
494 NEW_FOREGROUND_TAB); | |
495 SetResultRestoredTab(tab_contents); | |
496 return true; | |
497 } | |
498 | |
499 // Restoring a full window. | |
500 std::vector<const SessionWindow*> windows; | |
501 if (!associator->GetForeignSession(fs_id->session_tag(), &windows)) { | |
502 SetError(kSessionNotFoundError); | |
503 return false; | |
504 } | |
505 | |
506 std::vector<const SessionWindow*>::const_iterator window = windows.begin(); | |
507 while (window != windows.end() && (*window)->window_id.id() != fs_id->id()) { | |
508 ++window; | |
509 } | |
510 if (window == windows.end()) { | |
511 SetError(kSessionNotFoundError); | |
512 return false; | |
513 } | |
514 | |
515 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); | |
516 // Only restore one window at a time. | |
517 std::vector<Browser*> browsers = | |
518 SessionRestore::RestoreForeignSessionWindows(profile(), host_desktop_type, | |
519 window, window + 1); | |
520 // Will always create one browser because we only restore one window per call. | |
521 DCHECK_EQ(1u, browsers.size()); | |
522 return SetResultRestoredWindow(ExtensionTabUtil::GetWindowId(browsers[0])); | |
225 } | 523 } |
226 | 524 |
227 SessionRestoreAPI::~SessionRestoreAPI() { | 525 bool SessionsRestoreFunction::RunImpl() { |
526 scoped_ptr<Restore::Params> params(Restore::Params::Create(*args_)); | |
527 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
528 | |
529 Browser* browser = | |
530 chrome::FindBrowserWithProfile(profile(), | |
531 chrome::HOST_DESKTOP_TYPE_NATIVE); | |
532 if (!browser) { | |
533 SetError(kNoBrowserToRestoreSession); | |
534 return false; | |
535 } | |
536 | |
537 if (!params->session_id) | |
538 return RestoreMostRecentlyClosed(browser); | |
539 | |
540 if (ForeignSessionId::IsForeignSessionId(*params->session_id)) { | |
not at google - send to devlin
2013/08/12 23:51:34
it would be ideal if this foreign vs local stuff w
Kristen Dwan
2013/08/15 07:11:56
way nicer now :)
| |
541 scoped_ptr<ForeignSessionId> fs_id( | |
542 new ForeignSessionId(*params->session_id)); | |
543 return RestoreForeignSession(fs_id.get(), browser); | |
544 } | |
545 | |
546 int id; | |
547 if (!base::StringToInt(*params->session_id, &id)) { | |
548 SetError(kInvalidSessionIdError); | |
549 return false; | |
550 } | |
551 return RestoreLocalSession(id, browser); | |
228 } | 552 } |
229 | 553 |
230 static base::LazyInstance<ProfileKeyedAPIFactory<SessionRestoreAPI> > | 554 SessionsAPI::SessionsAPI(Profile* profile) { |
555 } | |
556 | |
557 SessionsAPI::~SessionsAPI() { | |
558 } | |
559 | |
560 static base::LazyInstance<ProfileKeyedAPIFactory<SessionsAPI> > | |
231 g_factory = LAZY_INSTANCE_INITIALIZER; | 561 g_factory = LAZY_INSTANCE_INITIALIZER; |
232 | 562 |
233 // static | 563 // static |
234 ProfileKeyedAPIFactory<SessionRestoreAPI>* | 564 ProfileKeyedAPIFactory<SessionsAPI>* |
235 SessionRestoreAPI::GetFactoryInstance() { | 565 SessionsAPI::GetFactoryInstance() { |
236 return &g_factory.Get(); | 566 return &g_factory.Get(); |
237 } | 567 } |
238 | 568 |
239 } // namespace extensions | 569 } // namespace extensions |
OLD | NEW |