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 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/i18n/rtl.h" | 11 #include "base/i18n/rtl.h" |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 #include "chrome/browser/extensions/api/sessions/session_id.h" | 18 #include "chrome/browser/extensions/api/sessions/session_id.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 } | 101 } |
102 tab_struct->index = index; | 102 tab_struct->index = index; |
103 tab_struct->pinned = pinned; | 103 tab_struct->pinned = pinned; |
104 // Note: |selected_index| from the sync sessions model is what we call | 104 // Note: |selected_index| from the sync sessions model is what we call |
105 // "active" in extensions terminology. "selected" is deprecated because it's | 105 // "active" in extensions terminology. "selected" is deprecated because it's |
106 // not clear whether it means "active" (user can see) or "highlighted" (user | 106 // not clear whether it means "active" (user can see) or "highlighted" (user |
107 // has highlighted, since you can select tabs without bringing them into the | 107 // has highlighted, since you can select tabs without bringing them into the |
108 // foreground). | 108 // foreground). |
109 tab_struct->active = index == selected_index; | 109 tab_struct->active = index == selected_index; |
110 ExtensionTabUtil::ScrubTabForExtension(extension, tab_struct.get()); | 110 ExtensionTabUtil::ScrubTabForExtension(extension, tab_struct.get()); |
111 return tab_struct.Pass(); | 111 return tab_struct; |
112 } | 112 } |
113 | 113 |
114 scoped_ptr<windows::Window> CreateWindowModelHelper( | 114 scoped_ptr<windows::Window> CreateWindowModelHelper( |
115 scoped_ptr<std::vector<linked_ptr<tabs::Tab>>> tabs, | 115 scoped_ptr<std::vector<linked_ptr<tabs::Tab>>> tabs, |
116 const std::string& session_id, | 116 const std::string& session_id, |
117 const windows::WindowType& type, | 117 const windows::WindowType& type, |
118 const windows::WindowState& state) { | 118 const windows::WindowState& state) { |
119 scoped_ptr<windows::Window> window_struct(new windows::Window); | 119 scoped_ptr<windows::Window> window_struct(new windows::Window); |
120 window_struct->tabs = tabs.Pass(); | 120 window_struct->tabs = std::move(tabs); |
121 window_struct->session_id.reset(new std::string(session_id)); | 121 window_struct->session_id.reset(new std::string(session_id)); |
122 window_struct->incognito = false; | 122 window_struct->incognito = false; |
123 window_struct->always_on_top = false; | 123 window_struct->always_on_top = false; |
124 window_struct->focused = false; | 124 window_struct->focused = false; |
125 window_struct->type = type; | 125 window_struct->type = type; |
126 window_struct->state = state; | 126 window_struct->state = state; |
127 return window_struct.Pass(); | 127 return window_struct; |
128 } | 128 } |
129 | 129 |
130 scoped_ptr<api::sessions::Session> CreateSessionModelHelper( | 130 scoped_ptr<api::sessions::Session> CreateSessionModelHelper( |
131 int last_modified, | 131 int last_modified, |
132 scoped_ptr<tabs::Tab> tab, | 132 scoped_ptr<tabs::Tab> tab, |
133 scoped_ptr<windows::Window> window) { | 133 scoped_ptr<windows::Window> window) { |
134 scoped_ptr<api::sessions::Session> session_struct(new api::sessions::Session); | 134 scoped_ptr<api::sessions::Session> session_struct(new api::sessions::Session); |
135 session_struct->last_modified = last_modified; | 135 session_struct->last_modified = last_modified; |
136 if (tab) | 136 if (tab) |
137 session_struct->tab = tab.Pass(); | 137 session_struct->tab = std::move(tab); |
138 else if (window) | 138 else if (window) |
139 session_struct->window = window.Pass(); | 139 session_struct->window = std::move(window); |
140 else | 140 else |
141 NOTREACHED(); | 141 NOTREACHED(); |
142 return session_struct.Pass(); | 142 return session_struct; |
143 } | 143 } |
144 | 144 |
145 bool is_tab_entry(const sessions::TabRestoreService::Entry* entry) { | 145 bool is_tab_entry(const sessions::TabRestoreService::Entry* entry) { |
146 return entry->type == sessions::TabRestoreService::TAB; | 146 return entry->type == sessions::TabRestoreService::TAB; |
147 } | 147 } |
148 | 148 |
149 bool is_window_entry(const sessions::TabRestoreService::Entry* entry) { | 149 bool is_window_entry(const sessions::TabRestoreService::Entry* entry) { |
150 return entry->type == sessions::TabRestoreService::WINDOW; | 150 return entry->type == sessions::TabRestoreService::WINDOW; |
151 } | 151 } |
152 | 152 |
(...skipping 17 matching lines...) Expand all Loading... |
170 DCHECK(!window.tabs.empty()); | 170 DCHECK(!window.tabs.empty()); |
171 | 171 |
172 scoped_ptr<std::vector<linked_ptr<tabs::Tab> > > tabs( | 172 scoped_ptr<std::vector<linked_ptr<tabs::Tab> > > tabs( |
173 new std::vector<linked_ptr<tabs::Tab> >); | 173 new std::vector<linked_ptr<tabs::Tab> >); |
174 for (size_t i = 0; i < window.tabs.size(); ++i) { | 174 for (size_t i = 0; i < window.tabs.size(); ++i) { |
175 tabs->push_back(make_linked_ptr( | 175 tabs->push_back(make_linked_ptr( |
176 CreateTabModel(window.tabs[i], window.tabs[i].id, | 176 CreateTabModel(window.tabs[i], window.tabs[i].id, |
177 window.selected_tab_index).release())); | 177 window.selected_tab_index).release())); |
178 } | 178 } |
179 | 179 |
180 return CreateWindowModelHelper(tabs.Pass(), base::IntToString(session_id), | 180 return CreateWindowModelHelper(std::move(tabs), base::IntToString(session_id), |
181 windows::WINDOW_TYPE_NORMAL, | 181 windows::WINDOW_TYPE_NORMAL, |
182 windows::WINDOW_STATE_NORMAL); | 182 windows::WINDOW_STATE_NORMAL); |
183 } | 183 } |
184 | 184 |
185 scoped_ptr<api::sessions::Session> | 185 scoped_ptr<api::sessions::Session> |
186 SessionsGetRecentlyClosedFunction::CreateSessionModel( | 186 SessionsGetRecentlyClosedFunction::CreateSessionModel( |
187 const sessions::TabRestoreService::Entry* entry) { | 187 const sessions::TabRestoreService::Entry* entry) { |
188 scoped_ptr<tabs::Tab> tab; | 188 scoped_ptr<tabs::Tab> tab; |
189 scoped_ptr<windows::Window> window; | 189 scoped_ptr<windows::Window> window; |
190 switch (entry->type) { | 190 switch (entry->type) { |
191 case sessions::TabRestoreService::TAB: | 191 case sessions::TabRestoreService::TAB: |
192 tab = CreateTabModel( | 192 tab = CreateTabModel( |
193 *static_cast<const sessions::TabRestoreService::Tab*>(entry), | 193 *static_cast<const sessions::TabRestoreService::Tab*>(entry), |
194 entry->id, -1); | 194 entry->id, -1); |
195 break; | 195 break; |
196 case sessions::TabRestoreService::WINDOW: | 196 case sessions::TabRestoreService::WINDOW: |
197 window = CreateWindowModel( | 197 window = CreateWindowModel( |
198 *static_cast<const sessions::TabRestoreService::Window*>(entry), | 198 *static_cast<const sessions::TabRestoreService::Window*>(entry), |
199 entry->id); | 199 entry->id); |
200 break; | 200 break; |
201 default: | 201 default: |
202 NOTREACHED(); | 202 NOTREACHED(); |
203 } | 203 } |
204 return CreateSessionModelHelper(entry->timestamp.ToTimeT(), | 204 return CreateSessionModelHelper(entry->timestamp.ToTimeT(), std::move(tab), |
205 tab.Pass(), | 205 std::move(window)); |
206 window.Pass()); | |
207 } | 206 } |
208 | 207 |
209 bool SessionsGetRecentlyClosedFunction::RunSync() { | 208 bool SessionsGetRecentlyClosedFunction::RunSync() { |
210 scoped_ptr<GetRecentlyClosed::Params> params( | 209 scoped_ptr<GetRecentlyClosed::Params> params( |
211 GetRecentlyClosed::Params::Create(*args_)); | 210 GetRecentlyClosed::Params::Create(*args_)); |
212 EXTENSION_FUNCTION_VALIDATE(params); | 211 EXTENSION_FUNCTION_VALIDATE(params); |
213 int max_results = api::sessions::MAX_SESSION_RESULTS; | 212 int max_results = api::sessions::MAX_SESSION_RESULTS; |
214 if (params->filter && params->filter->max_results) | 213 if (params->filter && params->filter->max_results) |
215 max_results = *params->filter->max_results; | 214 max_results = *params->filter->max_results; |
216 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && | 215 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 case ui::SHOW_STATE_FULLSCREEN: | 319 case ui::SHOW_STATE_FULLSCREEN: |
321 state = windows::WINDOW_STATE_FULLSCREEN; | 320 state = windows::WINDOW_STATE_FULLSCREEN; |
322 break; | 321 break; |
323 case ui::SHOW_STATE_DEFAULT: | 322 case ui::SHOW_STATE_DEFAULT: |
324 case ui::SHOW_STATE_INACTIVE: | 323 case ui::SHOW_STATE_INACTIVE: |
325 case ui::SHOW_STATE_END: | 324 case ui::SHOW_STATE_END: |
326 break; | 325 break; |
327 } | 326 } |
328 | 327 |
329 scoped_ptr<windows::Window> window_struct( | 328 scoped_ptr<windows::Window> window_struct( |
330 CreateWindowModelHelper(tabs.Pass(), session_id, type, state)); | 329 CreateWindowModelHelper(std::move(tabs), session_id, type, state)); |
331 // TODO(dwankri): Dig deeper to resolve bounds not being optional, so closed | 330 // TODO(dwankri): Dig deeper to resolve bounds not being optional, so closed |
332 // windows in GetRecentlyClosed can have set values in Window helper. | 331 // windows in GetRecentlyClosed can have set values in Window helper. |
333 window_struct->left.reset(new int(window.bounds.x())); | 332 window_struct->left.reset(new int(window.bounds.x())); |
334 window_struct->top.reset(new int(window.bounds.y())); | 333 window_struct->top.reset(new int(window.bounds.y())); |
335 window_struct->width.reset(new int(window.bounds.width())); | 334 window_struct->width.reset(new int(window.bounds.width())); |
336 window_struct->height.reset(new int(window.bounds.height())); | 335 window_struct->height.reset(new int(window.bounds.height())); |
337 | 336 |
338 return window_struct.Pass(); | 337 return window_struct; |
339 } | 338 } |
340 | 339 |
341 scoped_ptr<api::sessions::Session> | 340 scoped_ptr<api::sessions::Session> |
342 SessionsGetDevicesFunction::CreateSessionModel( | 341 SessionsGetDevicesFunction::CreateSessionModel( |
343 const sessions::SessionWindow& window, const std::string& session_tag) { | 342 const sessions::SessionWindow& window, const std::string& session_tag) { |
344 scoped_ptr<windows::Window> window_model( | 343 scoped_ptr<windows::Window> window_model( |
345 CreateWindowModel(window, session_tag)); | 344 CreateWindowModel(window, session_tag)); |
346 // There is a chance that after pruning uninteresting tabs the window will be | 345 // There is a chance that after pruning uninteresting tabs the window will be |
347 // empty. | 346 // empty. |
348 return !window_model ? scoped_ptr<api::sessions::Session>() | 347 return !window_model ? scoped_ptr<api::sessions::Session>() |
349 : CreateSessionModelHelper(window.timestamp.ToTimeT(), | 348 : CreateSessionModelHelper(window.timestamp.ToTimeT(), |
350 scoped_ptr<tabs::Tab>(), | 349 scoped_ptr<tabs::Tab>(), |
351 window_model.Pass()); | 350 std::move(window_model)); |
352 } | 351 } |
353 | 352 |
354 scoped_ptr<api::sessions::Device> SessionsGetDevicesFunction::CreateDeviceModel( | 353 scoped_ptr<api::sessions::Device> SessionsGetDevicesFunction::CreateDeviceModel( |
355 const sync_driver::SyncedSession* session) { | 354 const sync_driver::SyncedSession* session) { |
356 int max_results = api::sessions::MAX_SESSION_RESULTS; | 355 int max_results = api::sessions::MAX_SESSION_RESULTS; |
357 // Already validated in RunAsync(). | 356 // Already validated in RunAsync(). |
358 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); | 357 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); |
359 if (params->filter && params->filter->max_results) | 358 if (params->filter && params->filter->max_results) |
360 max_results = *params->filter->max_results; | 359 max_results = *params->filter->max_results; |
361 | 360 |
362 scoped_ptr<api::sessions::Device> device_struct(new api::sessions::Device); | 361 scoped_ptr<api::sessions::Device> device_struct(new api::sessions::Device); |
363 device_struct->info = session->session_name; | 362 device_struct->info = session->session_name; |
364 device_struct->device_name = session->session_name; | 363 device_struct->device_name = session->session_name; |
365 | 364 |
366 for (sync_driver::SyncedSession::SyncedWindowMap::const_iterator it = | 365 for (sync_driver::SyncedSession::SyncedWindowMap::const_iterator it = |
367 session->windows.begin(); | 366 session->windows.begin(); |
368 it != session->windows.end() && | 367 it != session->windows.end() && |
369 static_cast<int>(device_struct->sessions.size()) < max_results; | 368 static_cast<int>(device_struct->sessions.size()) < max_results; |
370 ++it) { | 369 ++it) { |
371 scoped_ptr<api::sessions::Session> session_model(CreateSessionModel( | 370 scoped_ptr<api::sessions::Session> session_model(CreateSessionModel( |
372 *it->second, session->session_tag)); | 371 *it->second, session->session_tag)); |
373 if (session_model) | 372 if (session_model) |
374 device_struct->sessions.push_back(make_linked_ptr( | 373 device_struct->sessions.push_back(make_linked_ptr( |
375 session_model.release())); | 374 session_model.release())); |
376 } | 375 } |
377 return device_struct.Pass(); | 376 return device_struct; |
378 } | 377 } |
379 | 378 |
380 bool SessionsGetDevicesFunction::RunSync() { | 379 bool SessionsGetDevicesFunction::RunSync() { |
381 ProfileSyncService* service = | 380 ProfileSyncService* service = |
382 ProfileSyncServiceFactory::GetInstance()->GetForProfile(GetProfile()); | 381 ProfileSyncServiceFactory::GetInstance()->GetForProfile(GetProfile()); |
383 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { | 382 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { |
384 // Sync not enabled. | 383 // Sync not enabled. |
385 results_ = GetDevices::Results::Create( | 384 results_ = GetDevices::Results::Create( |
386 std::vector<linked_ptr<api::sessions::Device> >()); | 385 std::vector<linked_ptr<api::sessions::Device> >()); |
387 return true; | 386 return true; |
(...skipping 28 matching lines...) Expand all Loading... |
416 void SessionsRestoreFunction::SetInvalidIdError(const std::string& invalid_id) { | 415 void SessionsRestoreFunction::SetInvalidIdError(const std::string& invalid_id) { |
417 SetError(ErrorUtils::FormatErrorMessage(kInvalidSessionIdError, invalid_id)); | 416 SetError(ErrorUtils::FormatErrorMessage(kInvalidSessionIdError, invalid_id)); |
418 } | 417 } |
419 | 418 |
420 | 419 |
421 void SessionsRestoreFunction::SetResultRestoredTab( | 420 void SessionsRestoreFunction::SetResultRestoredTab( |
422 content::WebContents* contents) { | 421 content::WebContents* contents) { |
423 scoped_ptr<base::DictionaryValue> tab_value( | 422 scoped_ptr<base::DictionaryValue> tab_value( |
424 ExtensionTabUtil::CreateTabValue(contents, extension())); | 423 ExtensionTabUtil::CreateTabValue(contents, extension())); |
425 scoped_ptr<tabs::Tab> tab(tabs::Tab::FromValue(*tab_value)); | 424 scoped_ptr<tabs::Tab> tab(tabs::Tab::FromValue(*tab_value)); |
426 scoped_ptr<api::sessions::Session> restored_session(CreateSessionModelHelper( | 425 scoped_ptr<api::sessions::Session> restored_session( |
427 base::Time::Now().ToTimeT(), | 426 CreateSessionModelHelper(base::Time::Now().ToTimeT(), std::move(tab), |
428 tab.Pass(), | 427 scoped_ptr<windows::Window>())); |
429 scoped_ptr<windows::Window>())); | |
430 results_ = Restore::Results::Create(*restored_session); | 428 results_ = Restore::Results::Create(*restored_session); |
431 } | 429 } |
432 | 430 |
433 bool SessionsRestoreFunction::SetResultRestoredWindow(int window_id) { | 431 bool SessionsRestoreFunction::SetResultRestoredWindow(int window_id) { |
434 WindowController* controller = NULL; | 432 WindowController* controller = NULL; |
435 if (!windows_util::GetWindowFromWindowID(this, window_id, 0, &controller)) { | 433 if (!windows_util::GetWindowFromWindowID(this, window_id, 0, &controller)) { |
436 // error_ is set by GetWindowFromWindowId function call. | 434 // error_ is set by GetWindowFromWindowId function call. |
437 return false; | 435 return false; |
438 } | 436 } |
439 scoped_ptr<base::DictionaryValue> window_value( | 437 scoped_ptr<base::DictionaryValue> window_value( |
440 controller->CreateWindowValueWithTabs(extension())); | 438 controller->CreateWindowValueWithTabs(extension())); |
441 scoped_ptr<windows::Window> window(windows::Window::FromValue( | 439 scoped_ptr<windows::Window> window(windows::Window::FromValue( |
442 *window_value)); | 440 *window_value)); |
443 results_ = Restore::Results::Create(*CreateSessionModelHelper( | 441 results_ = Restore::Results::Create(*CreateSessionModelHelper( |
444 base::Time::Now().ToTimeT(), | 442 base::Time::Now().ToTimeT(), scoped_ptr<tabs::Tab>(), std::move(window))); |
445 scoped_ptr<tabs::Tab>(), | |
446 window.Pass())); | |
447 return true; | 443 return true; |
448 } | 444 } |
449 | 445 |
450 bool SessionsRestoreFunction::RestoreMostRecentlyClosed(Browser* browser) { | 446 bool SessionsRestoreFunction::RestoreMostRecentlyClosed(Browser* browser) { |
451 sessions::TabRestoreService* tab_restore_service = | 447 sessions::TabRestoreService* tab_restore_service = |
452 TabRestoreServiceFactory::GetForProfile(GetProfile()); | 448 TabRestoreServiceFactory::GetForProfile(GetProfile()); |
453 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); | 449 chrome::HostDesktopType host_desktop_type = browser->host_desktop_type(); |
454 sessions::TabRestoreService::Entries entries = tab_restore_service->entries(); | 450 sessions::TabRestoreService::Entries entries = tab_restore_service->entries(); |
455 | 451 |
456 if (entries.empty()) { | 452 if (entries.empty()) { |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
625 SessionsEventRouter::~SessionsEventRouter() { | 621 SessionsEventRouter::~SessionsEventRouter() { |
626 if (tab_restore_service_) | 622 if (tab_restore_service_) |
627 tab_restore_service_->RemoveObserver(this); | 623 tab_restore_service_->RemoveObserver(this); |
628 } | 624 } |
629 | 625 |
630 void SessionsEventRouter::TabRestoreServiceChanged( | 626 void SessionsEventRouter::TabRestoreServiceChanged( |
631 sessions::TabRestoreService* service) { | 627 sessions::TabRestoreService* service) { |
632 scoped_ptr<base::ListValue> args(new base::ListValue()); | 628 scoped_ptr<base::ListValue> args(new base::ListValue()); |
633 EventRouter::Get(profile_)->BroadcastEvent(make_scoped_ptr( | 629 EventRouter::Get(profile_)->BroadcastEvent(make_scoped_ptr( |
634 new Event(events::SESSIONS_ON_CHANGED, | 630 new Event(events::SESSIONS_ON_CHANGED, |
635 api::sessions::OnChanged::kEventName, args.Pass()))); | 631 api::sessions::OnChanged::kEventName, std::move(args)))); |
636 } | 632 } |
637 | 633 |
638 void SessionsEventRouter::TabRestoreServiceDestroyed( | 634 void SessionsEventRouter::TabRestoreServiceDestroyed( |
639 sessions::TabRestoreService* service) { | 635 sessions::TabRestoreService* service) { |
640 tab_restore_service_ = NULL; | 636 tab_restore_service_ = NULL; |
641 } | 637 } |
642 | 638 |
643 SessionsAPI::SessionsAPI(content::BrowserContext* context) | 639 SessionsAPI::SessionsAPI(content::BrowserContext* context) |
644 : browser_context_(context) { | 640 : browser_context_(context) { |
645 EventRouter::Get(browser_context_)->RegisterObserver(this, | 641 EventRouter::Get(browser_context_)->RegisterObserver(this, |
(...skipping 15 matching lines...) Expand all Loading... |
661 return g_factory.Pointer(); | 657 return g_factory.Pointer(); |
662 } | 658 } |
663 | 659 |
664 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) { | 660 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) { |
665 sessions_event_router_.reset( | 661 sessions_event_router_.reset( |
666 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_))); | 662 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_))); |
667 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 663 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
668 } | 664 } |
669 | 665 |
670 } // namespace extensions | 666 } // namespace extensions |
OLD | NEW |