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 <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" |
(...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 = tab.Pass(); | 135 session_struct->tab = tab.Pass(); |
136 else if (window) | 136 else if (window) |
137 session_struct->window = window.Pass(); | 137 session_struct->window = window.Pass(); |
138 else | 138 else |
139 NOTREACHED(); | 139 NOTREACHED(); |
140 return session_struct.Pass(); | 140 return session_struct.Pass(); |
141 } | 141 } |
142 | 142 |
143 bool is_tab_entry(const TabRestoreService::Entry* entry) { | 143 bool is_tab_entry(const sessions::TabRestoreService::Entry* entry) { |
144 return entry->type == TabRestoreService::TAB; | 144 return entry->type == sessions::TabRestoreService::TAB; |
145 } | 145 } |
146 | 146 |
147 bool is_window_entry(const TabRestoreService::Entry* entry) { | 147 bool is_window_entry(const sessions::TabRestoreService::Entry* entry) { |
148 return entry->type == TabRestoreService::WINDOW; | 148 return entry->type == sessions::TabRestoreService::WINDOW; |
149 } | 149 } |
150 | 150 |
151 scoped_ptr<tabs::Tab> SessionsGetRecentlyClosedFunction::CreateTabModel( | 151 scoped_ptr<tabs::Tab> SessionsGetRecentlyClosedFunction::CreateTabModel( |
152 const TabRestoreService::Tab& tab, int session_id, int selected_index) { | 152 const sessions::TabRestoreService::Tab& tab, |
| 153 int session_id, |
| 154 int selected_index) { |
153 return CreateTabModelHelper(GetProfile(), | 155 return CreateTabModelHelper(GetProfile(), |
154 tab.navigations[tab.current_navigation_index], | 156 tab.navigations[tab.current_navigation_index], |
155 base::IntToString(session_id), | 157 base::IntToString(session_id), |
156 tab.tabstrip_index, | 158 tab.tabstrip_index, |
157 tab.pinned, | 159 tab.pinned, |
158 selected_index, | 160 selected_index, |
159 extension()); | 161 extension()); |
160 } | 162 } |
161 | 163 |
162 scoped_ptr<windows::Window> | 164 scoped_ptr<windows::Window> |
163 SessionsGetRecentlyClosedFunction::CreateWindowModel( | 165 SessionsGetRecentlyClosedFunction::CreateWindowModel( |
164 const TabRestoreService::Window& window, | 166 const sessions::TabRestoreService::Window& window, |
165 int session_id) { | 167 int session_id) { |
166 DCHECK(!window.tabs.empty()); | 168 DCHECK(!window.tabs.empty()); |
167 | 169 |
168 scoped_ptr<std::vector<linked_ptr<tabs::Tab> > > tabs( | 170 scoped_ptr<std::vector<linked_ptr<tabs::Tab> > > tabs( |
169 new std::vector<linked_ptr<tabs::Tab> >); | 171 new std::vector<linked_ptr<tabs::Tab> >); |
170 for (size_t i = 0; i < window.tabs.size(); ++i) { | 172 for (size_t i = 0; i < window.tabs.size(); ++i) { |
171 tabs->push_back(make_linked_ptr( | 173 tabs->push_back(make_linked_ptr( |
172 CreateTabModel(window.tabs[i], window.tabs[i].id, | 174 CreateTabModel(window.tabs[i], window.tabs[i].id, |
173 window.selected_tab_index).release())); | 175 window.selected_tab_index).release())); |
174 } | 176 } |
175 | 177 |
176 return CreateWindowModelHelper(tabs.Pass(), base::IntToString(session_id), | 178 return CreateWindowModelHelper(tabs.Pass(), base::IntToString(session_id), |
177 windows::WINDOW_TYPE_NORMAL, | 179 windows::WINDOW_TYPE_NORMAL, |
178 windows::WINDOW_STATE_NORMAL); | 180 windows::WINDOW_STATE_NORMAL); |
179 } | 181 } |
180 | 182 |
181 scoped_ptr<api::sessions::Session> | 183 scoped_ptr<api::sessions::Session> |
182 SessionsGetRecentlyClosedFunction::CreateSessionModel( | 184 SessionsGetRecentlyClosedFunction::CreateSessionModel( |
183 const TabRestoreService::Entry* entry) { | 185 const sessions::TabRestoreService::Entry* entry) { |
184 scoped_ptr<tabs::Tab> tab; | 186 scoped_ptr<tabs::Tab> tab; |
185 scoped_ptr<windows::Window> window; | 187 scoped_ptr<windows::Window> window; |
186 switch (entry->type) { | 188 switch (entry->type) { |
187 case TabRestoreService::TAB: | 189 case sessions::TabRestoreService::TAB: |
188 tab = CreateTabModel( | 190 tab = CreateTabModel( |
189 *static_cast<const TabRestoreService::Tab*>(entry), entry->id, -1); | 191 *static_cast<const sessions::TabRestoreService::Tab*>(entry), |
| 192 entry->id, -1); |
190 break; | 193 break; |
191 case TabRestoreService::WINDOW: | 194 case sessions::TabRestoreService::WINDOW: |
192 window = CreateWindowModel( | 195 window = CreateWindowModel( |
193 *static_cast<const TabRestoreService::Window*>(entry), entry->id); | 196 *static_cast<const sessions::TabRestoreService::Window*>(entry), |
| 197 entry->id); |
194 break; | 198 break; |
195 default: | 199 default: |
196 NOTREACHED(); | 200 NOTREACHED(); |
197 } | 201 } |
198 return CreateSessionModelHelper(entry->timestamp.ToTimeT(), | 202 return CreateSessionModelHelper(entry->timestamp.ToTimeT(), |
199 tab.Pass(), | 203 tab.Pass(), |
200 window.Pass()); | 204 window.Pass()); |
201 } | 205 } |
202 | 206 |
203 bool SessionsGetRecentlyClosedFunction::RunSync() { | 207 bool SessionsGetRecentlyClosedFunction::RunSync() { |
204 scoped_ptr<GetRecentlyClosed::Params> params( | 208 scoped_ptr<GetRecentlyClosed::Params> params( |
205 GetRecentlyClosed::Params::Create(*args_)); | 209 GetRecentlyClosed::Params::Create(*args_)); |
206 EXTENSION_FUNCTION_VALIDATE(params); | 210 EXTENSION_FUNCTION_VALIDATE(params); |
207 int max_results = api::sessions::MAX_SESSION_RESULTS; | 211 int max_results = api::sessions::MAX_SESSION_RESULTS; |
208 if (params->filter && params->filter->max_results) | 212 if (params->filter && params->filter->max_results) |
209 max_results = *params->filter->max_results; | 213 max_results = *params->filter->max_results; |
210 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && | 214 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && |
211 max_results <= api::sessions::MAX_SESSION_RESULTS); | 215 max_results <= api::sessions::MAX_SESSION_RESULTS); |
212 | 216 |
213 std::vector<linked_ptr<api::sessions::Session> > result; | 217 std::vector<linked_ptr<api::sessions::Session> > result; |
214 TabRestoreService* tab_restore_service = | 218 sessions::TabRestoreService* tab_restore_service = |
215 TabRestoreServiceFactory::GetForProfile(GetProfile()); | 219 TabRestoreServiceFactory::GetForProfile(GetProfile()); |
216 | 220 |
217 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in | 221 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in |
218 // incognito mode) | 222 // incognito mode) |
219 if (!tab_restore_service) { | 223 if (!tab_restore_service) { |
220 DCHECK_NE(GetProfile(), GetProfile()->GetOriginalProfile()) | 224 DCHECK_NE(GetProfile(), GetProfile()->GetOriginalProfile()) |
221 << "TabRestoreService expected for normal profiles"; | 225 << "sessions::TabRestoreService expected for normal profiles"; |
222 results_ = GetRecentlyClosed::Results::Create( | 226 results_ = GetRecentlyClosed::Results::Create( |
223 std::vector<linked_ptr<api::sessions::Session> >()); | 227 std::vector<linked_ptr<api::sessions::Session> >()); |
224 return true; | 228 return true; |
225 } | 229 } |
226 | 230 |
227 // List of entries. They are ordered from most to least recent. | 231 // 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 | 232 // We prune the list to contain max 25 entries at any time and removes |
229 // uninteresting entries. | 233 // uninteresting entries. |
230 TabRestoreService::Entries entries = tab_restore_service->entries(); | 234 sessions::TabRestoreService::Entries entries = tab_restore_service->entries(); |
231 for (TabRestoreService::Entries::const_iterator it = entries.begin(); | 235 for (sessions::TabRestoreService::Entries::const_iterator it = |
| 236 entries.begin(); |
232 it != entries.end() && static_cast<int>(result.size()) < max_results; | 237 it != entries.end() && static_cast<int>(result.size()) < max_results; |
233 ++it) { | 238 ++it) { |
234 TabRestoreService::Entry* entry = *it; | 239 sessions::TabRestoreService::Entry* entry = *it; |
235 result.push_back(make_linked_ptr(CreateSessionModel(entry).release())); | 240 result.push_back(make_linked_ptr(CreateSessionModel(entry).release())); |
236 } | 241 } |
237 | 242 |
238 results_ = GetRecentlyClosed::Results::Create(result); | 243 results_ = GetRecentlyClosed::Results::Create(result); |
239 return true; | 244 return true; |
240 } | 245 } |
241 | 246 |
242 scoped_ptr<tabs::Tab> SessionsGetDevicesFunction::CreateTabModel( | 247 scoped_ptr<tabs::Tab> SessionsGetDevicesFunction::CreateTabModel( |
243 const std::string& session_tag, | 248 const std::string& session_tag, |
244 const sessions::SessionTab& tab, | 249 const sessions::SessionTab& tab, |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 scoped_ptr<windows::Window> window(windows::Window::FromValue( | 439 scoped_ptr<windows::Window> window(windows::Window::FromValue( |
435 *window_value)); | 440 *window_value)); |
436 results_ = Restore::Results::Create(*CreateSessionModelHelper( | 441 results_ = Restore::Results::Create(*CreateSessionModelHelper( |
437 base::Time::Now().ToTimeT(), | 442 base::Time::Now().ToTimeT(), |
438 scoped_ptr<tabs::Tab>(), | 443 scoped_ptr<tabs::Tab>(), |
439 window.Pass())); | 444 window.Pass())); |
440 return true; | 445 return true; |
441 } | 446 } |
442 | 447 |
443 bool SessionsRestoreFunction::RestoreMostRecentlyClosed(Browser* browser) { | 448 bool SessionsRestoreFunction::RestoreMostRecentlyClosed(Browser* browser) { |
444 TabRestoreService* tab_restore_service = | 449 sessions::TabRestoreService* tab_restore_service = |
445 TabRestoreServiceFactory::GetForProfile(GetProfile()); | 450 TabRestoreServiceFactory::GetForProfile(GetProfile()); |
446 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); | 451 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); |
447 TabRestoreService::Entries entries = tab_restore_service->entries(); | 452 sessions::TabRestoreService::Entries entries = tab_restore_service->entries(); |
448 | 453 |
449 if (entries.empty()) { | 454 if (entries.empty()) { |
450 SetError(kNoRecentlyClosedSessionsError); | 455 SetError(kNoRecentlyClosedSessionsError); |
451 return false; | 456 return false; |
452 } | 457 } |
453 | 458 |
454 bool is_window = is_window_entry(entries.front()); | 459 bool is_window = is_window_entry(entries.front()); |
455 TabRestoreServiceDelegate* delegate = | 460 sessions::TabRestoreServiceDelegate* delegate = |
456 BrowserTabRestoreServiceDelegate::FindDelegateForWebContents( | 461 BrowserTabRestoreServiceDelegate::FindDelegateForWebContents( |
457 browser->tab_strip_model()->GetActiveWebContents()); | 462 browser->tab_strip_model()->GetActiveWebContents()); |
458 std::vector<sessions::LiveTab*> restored_tabs = | 463 std::vector<sessions::LiveTab*> restored_tabs = |
459 tab_restore_service->RestoreMostRecentEntry(delegate, host_desktop_type); | 464 tab_restore_service->RestoreMostRecentEntry(delegate, host_desktop_type); |
460 DCHECK(restored_tabs.size()); | 465 DCHECK(restored_tabs.size()); |
461 | 466 |
462 sessions::ContentLiveTab* first_tab = | 467 sessions::ContentLiveTab* first_tab = |
463 static_cast<sessions::ContentLiveTab*>(restored_tabs[0]); | 468 static_cast<sessions::ContentLiveTab*>(restored_tabs[0]); |
464 if (is_window) { | 469 if (is_window) { |
465 return SetResultRestoredWindow( | 470 return SetResultRestoredWindow( |
466 ExtensionTabUtil::GetWindowIdOfTab(first_tab->web_contents())); | 471 ExtensionTabUtil::GetWindowIdOfTab(first_tab->web_contents())); |
467 } | 472 } |
468 | 473 |
469 SetResultRestoredTab(first_tab->web_contents()); | 474 SetResultRestoredTab(first_tab->web_contents()); |
470 return true; | 475 return true; |
471 } | 476 } |
472 | 477 |
473 bool SessionsRestoreFunction::RestoreLocalSession(const SessionId& session_id, | 478 bool SessionsRestoreFunction::RestoreLocalSession(const SessionId& session_id, |
474 Browser* browser) { | 479 Browser* browser) { |
475 TabRestoreService* tab_restore_service = | 480 sessions::TabRestoreService* tab_restore_service = |
476 TabRestoreServiceFactory::GetForProfile(GetProfile()); | 481 TabRestoreServiceFactory::GetForProfile(GetProfile()); |
477 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); | 482 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); |
478 TabRestoreService::Entries entries = tab_restore_service->entries(); | 483 sessions::TabRestoreService::Entries entries = tab_restore_service->entries(); |
479 | 484 |
480 if (entries.empty()) { | 485 if (entries.empty()) { |
481 SetInvalidIdError(session_id.ToString()); | 486 SetInvalidIdError(session_id.ToString()); |
482 return false; | 487 return false; |
483 } | 488 } |
484 | 489 |
485 // Check if the recently closed list contains an entry with the provided id. | 490 // Check if the recently closed list contains an entry with the provided id. |
486 bool is_window = false; | 491 bool is_window = false; |
487 for (TabRestoreService::Entries::iterator it = entries.begin(); | 492 for (sessions::TabRestoreService::Entries::iterator it = entries.begin(); |
488 it != entries.end(); ++it) { | 493 it != entries.end(); ++it) { |
489 if ((*it)->id == session_id.id()) { | 494 if ((*it)->id == session_id.id()) { |
490 // The only time a full window is being restored is if the entry ID | 495 // The only time a full window is being restored is if the entry ID |
491 // matches the provided ID and the entry type is Window. | 496 // matches the provided ID and the entry type is Window. |
492 is_window = is_window_entry(*it); | 497 is_window = is_window_entry(*it); |
493 break; | 498 break; |
494 } | 499 } |
495 } | 500 } |
496 | 501 |
497 TabRestoreServiceDelegate* delegate = | 502 sessions::TabRestoreServiceDelegate* delegate = |
498 BrowserTabRestoreServiceDelegate::FindDelegateForWebContents( | 503 BrowserTabRestoreServiceDelegate::FindDelegateForWebContents( |
499 browser->tab_strip_model()->GetActiveWebContents()); | 504 browser->tab_strip_model()->GetActiveWebContents()); |
500 std::vector<sessions::LiveTab*> restored_tabs = | 505 std::vector<sessions::LiveTab*> restored_tabs = |
501 tab_restore_service->RestoreEntryById(delegate, session_id.id(), | 506 tab_restore_service->RestoreEntryById(delegate, session_id.id(), |
502 host_desktop_type, UNKNOWN); | 507 host_desktop_type, UNKNOWN); |
503 // If the ID is invalid, restored_tabs will be empty. | 508 // If the ID is invalid, restored_tabs will be empty. |
504 if (!restored_tabs.size()) { | 509 if (!restored_tabs.size()) { |
505 SetInvalidIdError(session_id.ToString()); | 510 SetInvalidIdError(session_id.ToString()); |
506 return false; | 511 return false; |
507 } | 512 } |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 tab_restore_service_->AddObserver(this); | 619 tab_restore_service_->AddObserver(this); |
615 } | 620 } |
616 } | 621 } |
617 | 622 |
618 SessionsEventRouter::~SessionsEventRouter() { | 623 SessionsEventRouter::~SessionsEventRouter() { |
619 if (tab_restore_service_) | 624 if (tab_restore_service_) |
620 tab_restore_service_->RemoveObserver(this); | 625 tab_restore_service_->RemoveObserver(this); |
621 } | 626 } |
622 | 627 |
623 void SessionsEventRouter::TabRestoreServiceChanged( | 628 void SessionsEventRouter::TabRestoreServiceChanged( |
624 TabRestoreService* service) { | 629 sessions::TabRestoreService* service) { |
625 scoped_ptr<base::ListValue> args(new base::ListValue()); | 630 scoped_ptr<base::ListValue> args(new base::ListValue()); |
626 EventRouter::Get(profile_)->BroadcastEvent(make_scoped_ptr( | 631 EventRouter::Get(profile_)->BroadcastEvent(make_scoped_ptr( |
627 new Event(events::SESSIONS_ON_CHANGED, | 632 new Event(events::SESSIONS_ON_CHANGED, |
628 api::sessions::OnChanged::kEventName, args.Pass()))); | 633 api::sessions::OnChanged::kEventName, args.Pass()))); |
629 } | 634 } |
630 | 635 |
631 void SessionsEventRouter::TabRestoreServiceDestroyed( | 636 void SessionsEventRouter::TabRestoreServiceDestroyed( |
632 TabRestoreService* service) { | 637 sessions::TabRestoreService* service) { |
633 tab_restore_service_ = NULL; | 638 tab_restore_service_ = NULL; |
634 } | 639 } |
635 | 640 |
636 SessionsAPI::SessionsAPI(content::BrowserContext* context) | 641 SessionsAPI::SessionsAPI(content::BrowserContext* context) |
637 : browser_context_(context) { | 642 : browser_context_(context) { |
638 EventRouter::Get(browser_context_)->RegisterObserver(this, | 643 EventRouter::Get(browser_context_)->RegisterObserver(this, |
639 api::sessions::OnChanged::kEventName); | 644 api::sessions::OnChanged::kEventName); |
640 } | 645 } |
641 | 646 |
642 SessionsAPI::~SessionsAPI() { | 647 SessionsAPI::~SessionsAPI() { |
(...skipping 11 matching lines...) Expand all Loading... |
654 return g_factory.Pointer(); | 659 return g_factory.Pointer(); |
655 } | 660 } |
656 | 661 |
657 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) { | 662 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) { |
658 sessions_event_router_.reset( | 663 sessions_event_router_.reset( |
659 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_))); | 664 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_))); |
660 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 665 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
661 } | 666 } |
662 | 667 |
663 } // namespace extensions | 668 } // namespace extensions |
OLD | NEW |