OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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/sessions/sessions_api.h" | 5 #include "chrome/browser/extensions/api/sessions/sessions_api.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 session_struct->last_modified = last_modified; | 133 session_struct->last_modified = last_modified; |
134 if (tab) | 134 if (tab) |
135 session_struct->tab = std::move(tab); | 135 session_struct->tab = std::move(tab); |
136 else if (window) | 136 else if (window) |
137 session_struct->window = std::move(window); | 137 session_struct->window = std::move(window); |
138 else | 138 else |
139 NOTREACHED(); | 139 NOTREACHED(); |
140 return session_struct; | 140 return session_struct; |
141 } | 141 } |
142 | 142 |
143 bool is_tab_entry(const sessions::TabRestoreService::Entry* entry) { | 143 namespace { |
144 return entry->type == sessions::TabRestoreService::TAB; | 144 |
145 bool is_window_entry(const sessions::TabRestoreService::Entry& entry) { | |
146 return entry.type == sessions::TabRestoreService::WINDOW; | |
145 } | 147 } |
146 | 148 |
147 bool is_window_entry(const sessions::TabRestoreService::Entry* entry) { | 149 } // namespace |
148 return entry->type == sessions::TabRestoreService::WINDOW; | |
149 } | |
150 | 150 |
151 tabs::Tab SessionsGetRecentlyClosedFunction::CreateTabModel( | 151 tabs::Tab SessionsGetRecentlyClosedFunction::CreateTabModel( |
152 const sessions::TabRestoreService::Tab& tab, | 152 const sessions::TabRestoreService::Tab& tab, |
153 int session_id, | 153 int session_id, |
sky
2016/08/04 16:34:38
While you're cleaning up it seems like session_id
Sidney San Martín
2016/08/09 04:10:52
Done! I also made a couple of other small changes
| |
154 int selected_index) { | 154 int selected_index) { |
155 return CreateTabModelHelper(GetProfile(), | 155 return CreateTabModelHelper(GetProfile(), |
156 tab.navigations[tab.current_navigation_index], | 156 tab.navigations[tab.current_navigation_index], |
157 base::IntToString(session_id), | 157 base::IntToString(session_id), |
158 tab.tabstrip_index, | 158 tab.tabstrip_index, |
159 tab.pinned, | 159 tab.pinned, |
160 selected_index, | 160 selected_index, |
161 extension()); | 161 extension()); |
162 } | 162 } |
163 | 163 |
164 std::unique_ptr<windows::Window> | 164 std::unique_ptr<windows::Window> |
165 SessionsGetRecentlyClosedFunction::CreateWindowModel( | 165 SessionsGetRecentlyClosedFunction::CreateWindowModel( |
166 const sessions::TabRestoreService::Window& window, | 166 const sessions::TabRestoreService::Window& window, |
167 int session_id) { | 167 int session_id) { |
168 DCHECK(!window.tabs.empty()); | 168 DCHECK(!window.tabs.empty()); |
169 | 169 |
170 std::unique_ptr<std::vector<tabs::Tab>> tabs(new std::vector<tabs::Tab>()); | 170 auto tabs = base::MakeUnique<std::vector<tabs::Tab>>(); |
171 for (size_t i = 0; i < window.tabs.size(); ++i) { | 171 for (const auto& tab : window.tabs) |
172 tabs->push_back(CreateTabModel(window.tabs[i], window.tabs[i].id, | 172 tabs->push_back(CreateTabModel(*tab, tab->id, window.selected_tab_index)); |
173 window.selected_tab_index)); | |
174 } | |
175 | 173 |
176 return CreateWindowModelHelper(std::move(tabs), base::IntToString(session_id), | 174 return CreateWindowModelHelper(std::move(tabs), base::IntToString(session_id), |
177 windows::WINDOW_TYPE_NORMAL, | 175 windows::WINDOW_TYPE_NORMAL, |
178 windows::WINDOW_STATE_NORMAL); | 176 windows::WINDOW_STATE_NORMAL); |
179 } | 177 } |
180 | 178 |
181 std::unique_ptr<api::sessions::Session> | 179 std::unique_ptr<api::sessions::Session> |
182 SessionsGetRecentlyClosedFunction::CreateSessionModel( | 180 SessionsGetRecentlyClosedFunction::CreateSessionModel( |
183 const sessions::TabRestoreService::Entry* entry) { | 181 const sessions::TabRestoreService::Entry& entry) { |
184 std::unique_ptr<tabs::Tab> tab; | 182 std::unique_ptr<tabs::Tab> tab; |
185 std::unique_ptr<windows::Window> window; | 183 std::unique_ptr<windows::Window> window; |
186 switch (entry->type) { | 184 switch (entry.type) { |
187 case sessions::TabRestoreService::TAB: | 185 case sessions::TabRestoreService::TAB: |
188 tab.reset(new tabs::Tab(CreateTabModel( | 186 tab.reset(new tabs::Tab(CreateTabModel( |
189 *static_cast<const sessions::TabRestoreService::Tab*>(entry), | 187 static_cast<const sessions::TabRestoreService::Tab&>(entry), entry.id, |
190 entry->id, -1))); | 188 -1))); |
191 break; | 189 break; |
192 case sessions::TabRestoreService::WINDOW: | 190 case sessions::TabRestoreService::WINDOW: |
193 window = CreateWindowModel( | 191 window = CreateWindowModel( |
194 *static_cast<const sessions::TabRestoreService::Window*>(entry), | 192 static_cast<const sessions::TabRestoreService::Window&>(entry), |
195 entry->id); | 193 entry.id); |
196 break; | 194 break; |
197 default: | 195 default: |
198 NOTREACHED(); | 196 NOTREACHED(); |
199 } | 197 } |
200 return CreateSessionModelHelper(entry->timestamp.ToTimeT(), std::move(tab), | 198 return CreateSessionModelHelper(entry.timestamp.ToTimeT(), std::move(tab), |
201 std::move(window)); | 199 std::move(window)); |
202 } | 200 } |
203 | 201 |
204 bool SessionsGetRecentlyClosedFunction::RunSync() { | 202 bool SessionsGetRecentlyClosedFunction::RunSync() { |
205 std::unique_ptr<GetRecentlyClosed::Params> params( | 203 std::unique_ptr<GetRecentlyClosed::Params> params( |
206 GetRecentlyClosed::Params::Create(*args_)); | 204 GetRecentlyClosed::Params::Create(*args_)); |
207 EXTENSION_FUNCTION_VALIDATE(params); | 205 EXTENSION_FUNCTION_VALIDATE(params); |
208 int max_results = api::sessions::MAX_SESSION_RESULTS; | 206 int max_results = api::sessions::MAX_SESSION_RESULTS; |
209 if (params->filter && params->filter->max_results) | 207 if (params->filter && params->filter->max_results) |
210 max_results = *params->filter->max_results; | 208 max_results = *params->filter->max_results; |
211 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && | 209 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && |
212 max_results <= api::sessions::MAX_SESSION_RESULTS); | 210 max_results <= api::sessions::MAX_SESSION_RESULTS); |
213 | 211 |
214 std::vector<api::sessions::Session> result; | 212 std::vector<api::sessions::Session> result; |
215 sessions::TabRestoreService* tab_restore_service = | 213 sessions::TabRestoreService* tab_restore_service = |
216 TabRestoreServiceFactory::GetForProfile(GetProfile()); | 214 TabRestoreServiceFactory::GetForProfile(GetProfile()); |
217 | 215 |
218 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in | 216 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in |
219 // incognito mode) | 217 // incognito mode) |
220 if (!tab_restore_service) { | 218 if (!tab_restore_service) { |
221 DCHECK_NE(GetProfile(), GetProfile()->GetOriginalProfile()) | 219 DCHECK_NE(GetProfile(), GetProfile()->GetOriginalProfile()) |
222 << "sessions::TabRestoreService expected for normal profiles"; | 220 << "sessions::TabRestoreService expected for normal profiles"; |
223 results_ = GetRecentlyClosed::Results::Create(result); | 221 results_ = GetRecentlyClosed::Results::Create(result); |
224 return true; | 222 return true; |
225 } | 223 } |
226 | 224 |
227 // List of entries. They are ordered from most to least recent. | 225 // List of entries. They are ordered from most to least recent. |
228 // We prune the list to contain max 25 entries at any time and removes | 226 // We prune the list to contain max 25 entries at any time and removes |
229 // uninteresting entries. | 227 // uninteresting entries. |
230 for (const sessions::TabRestoreService::Entry* entry : | 228 for (const auto& entry : tab_restore_service->entries()) { |
231 tab_restore_service->entries()) { | 229 result.push_back(std::move(*CreateSessionModel(*entry))); |
232 result.push_back(std::move(*CreateSessionModel(entry))); | |
233 } | 230 } |
234 | 231 |
235 results_ = GetRecentlyClosed::Results::Create(result); | 232 results_ = GetRecentlyClosed::Results::Create(result); |
236 return true; | 233 return true; |
237 } | 234 } |
238 | 235 |
239 tabs::Tab SessionsGetDevicesFunction::CreateTabModel( | 236 tabs::Tab SessionsGetDevicesFunction::CreateTabModel( |
240 const std::string& session_tag, | 237 const std::string& session_tag, |
241 const sessions::SessionTab& tab, | 238 const sessions::SessionTab& tab, |
242 int tab_index, | 239 int tab_index, |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
430 windows::Window::FromValue(*window_value)); | 427 windows::Window::FromValue(*window_value)); |
431 results_ = Restore::Results::Create(*CreateSessionModelHelper( | 428 results_ = Restore::Results::Create(*CreateSessionModelHelper( |
432 base::Time::Now().ToTimeT(), std::unique_ptr<tabs::Tab>(), | 429 base::Time::Now().ToTimeT(), std::unique_ptr<tabs::Tab>(), |
433 std::move(window))); | 430 std::move(window))); |
434 return true; | 431 return true; |
435 } | 432 } |
436 | 433 |
437 bool SessionsRestoreFunction::RestoreMostRecentlyClosed(Browser* browser) { | 434 bool SessionsRestoreFunction::RestoreMostRecentlyClosed(Browser* browser) { |
438 sessions::TabRestoreService* tab_restore_service = | 435 sessions::TabRestoreService* tab_restore_service = |
439 TabRestoreServiceFactory::GetForProfile(GetProfile()); | 436 TabRestoreServiceFactory::GetForProfile(GetProfile()); |
440 sessions::TabRestoreService::Entries entries = tab_restore_service->entries(); | 437 const sessions::TabRestoreService::Entries& entries = |
438 tab_restore_service->entries(); | |
441 | 439 |
442 if (entries.empty()) { | 440 if (entries.empty()) { |
443 SetError(kNoRecentlyClosedSessionsError); | 441 SetError(kNoRecentlyClosedSessionsError); |
444 return false; | 442 return false; |
445 } | 443 } |
446 | 444 |
447 bool is_window = is_window_entry(entries.front()); | 445 bool is_window = is_window_entry(*entries.front()); |
448 sessions::LiveTabContext* context = | 446 sessions::LiveTabContext* context = |
449 BrowserLiveTabContext::FindContextForWebContents( | 447 BrowserLiveTabContext::FindContextForWebContents( |
450 browser->tab_strip_model()->GetActiveWebContents()); | 448 browser->tab_strip_model()->GetActiveWebContents()); |
451 std::vector<sessions::LiveTab*> restored_tabs = | 449 std::vector<sessions::LiveTab*> restored_tabs = |
452 tab_restore_service->RestoreMostRecentEntry(context); | 450 tab_restore_service->RestoreMostRecentEntry(context); |
453 DCHECK(restored_tabs.size()); | 451 DCHECK(restored_tabs.size()); |
454 | 452 |
455 sessions::ContentLiveTab* first_tab = | 453 sessions::ContentLiveTab* first_tab = |
456 static_cast<sessions::ContentLiveTab*>(restored_tabs[0]); | 454 static_cast<sessions::ContentLiveTab*>(restored_tabs[0]); |
457 if (is_window) { | 455 if (is_window) { |
458 return SetResultRestoredWindow( | 456 return SetResultRestoredWindow( |
459 ExtensionTabUtil::GetWindowIdOfTab(first_tab->web_contents())); | 457 ExtensionTabUtil::GetWindowIdOfTab(first_tab->web_contents())); |
460 } | 458 } |
461 | 459 |
462 SetResultRestoredTab(first_tab->web_contents()); | 460 SetResultRestoredTab(first_tab->web_contents()); |
463 return true; | 461 return true; |
464 } | 462 } |
465 | 463 |
466 bool SessionsRestoreFunction::RestoreLocalSession(const SessionId& session_id, | 464 bool SessionsRestoreFunction::RestoreLocalSession(const SessionId& session_id, |
467 Browser* browser) { | 465 Browser* browser) { |
468 sessions::TabRestoreService* tab_restore_service = | 466 sessions::TabRestoreService* tab_restore_service = |
469 TabRestoreServiceFactory::GetForProfile(GetProfile()); | 467 TabRestoreServiceFactory::GetForProfile(GetProfile()); |
470 sessions::TabRestoreService::Entries entries = tab_restore_service->entries(); | 468 const sessions::TabRestoreService::Entries& entries = |
469 tab_restore_service->entries(); | |
471 | 470 |
472 if (entries.empty()) { | 471 if (entries.empty()) { |
473 SetInvalidIdError(session_id.ToString()); | 472 SetInvalidIdError(session_id.ToString()); |
474 return false; | 473 return false; |
475 } | 474 } |
476 | 475 |
477 // Check if the recently closed list contains an entry with the provided id. | 476 // Check if the recently closed list contains an entry with the provided id. |
478 bool is_window = false; | 477 bool is_window = false; |
479 for (sessions::TabRestoreService::Entries::iterator it = entries.begin(); | 478 for (const auto& entry : entries) { |
480 it != entries.end(); ++it) { | 479 if (entry->id == session_id.id()) { |
481 if ((*it)->id == session_id.id()) { | 480 // A full window is being restored only if the entry ID |
482 // The only time a full window is being restored is if the entry ID | |
483 // matches the provided ID and the entry type is Window. | 481 // matches the provided ID and the entry type is Window. |
484 is_window = is_window_entry(*it); | 482 is_window = is_window_entry(*entry); |
485 break; | 483 break; |
486 } | 484 } |
487 } | 485 } |
488 | 486 |
489 sessions::LiveTabContext* context = | 487 sessions::LiveTabContext* context = |
490 BrowserLiveTabContext::FindContextForWebContents( | 488 BrowserLiveTabContext::FindContextForWebContents( |
491 browser->tab_strip_model()->GetActiveWebContents()); | 489 browser->tab_strip_model()->GetActiveWebContents()); |
492 std::vector<sessions::LiveTab*> restored_tabs = | 490 std::vector<sessions::LiveTab*> restored_tabs = |
493 tab_restore_service->RestoreEntryById(context, session_id.id(), UNKNOWN); | 491 tab_restore_service->RestoreEntryById(context, session_id.id(), UNKNOWN); |
494 // If the ID is invalid, restored_tabs will be empty. | 492 // If the ID is invalid, restored_tabs will be empty. |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
643 return g_factory.Pointer(); | 641 return g_factory.Pointer(); |
644 } | 642 } |
645 | 643 |
646 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) { | 644 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) { |
647 sessions_event_router_.reset( | 645 sessions_event_router_.reset( |
648 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_))); | 646 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_))); |
649 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 647 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
650 } | 648 } |
651 | 649 |
652 } // namespace extensions | 650 } // namespace extensions |
OLD | NEW |