blob: 56945394236f0851786143f04c9bb545d78775db [file] [log] [blame]
[email protected]d791ad12013-08-23 02:12:591// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/extensions/api/sessions/sessions_api.h"
6
avia2f4804a2015-12-24 23:11:137#include <stddef.h>
dchengc963c7142016-04-08 03:55:228
dcheng1fc00f12015-12-26 22:18:039#include <utility>
[email protected]d791ad12013-08-23 02:12:5910#include <vector>
11
12#include "base/i18n/rtl.h"
13#include "base/lazy_instance.h"
dchengc963c7142016-04-08 03:55:2214#include "base/memory/ptr_util.h"
[email protected]d791ad12013-08-23 02:12:5915#include "base/strings/string_number_conversions.h"
[email protected]d791ad12013-08-23 02:12:5916#include "base/strings/utf_string_conversions.h"
17#include "base/time/time.h"
18#include "chrome/browser/extensions/api/sessions/session_id.h"
19#include "chrome/browser/extensions/api/tabs/windows_util.h"
[email protected]d791ad12013-08-23 02:12:5920#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"
23#include "chrome/browser/profiles/profile.h"
24#include "chrome/browser/search/search.h"
25#include "chrome/browser/sessions/session_restore.h"
[email protected]d791ad12013-08-23 02:12:5926#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]d791ad12013-08-23 02:12:5927#include "chrome/browser/sync/profile_sync_service_factory.h"
28#include "chrome/browser/ui/browser.h"
29#include "chrome/browser/ui/browser_finder.h"
blundellbde024d2015-09-30 13:47:0730#include "chrome/browser/ui/browser_live_tab_context.h"
[email protected]d791ad12013-08-23 02:12:5931#include "chrome/browser/ui/tabs/tab_strip_model.h"
maxbogue26f40222016-09-16 20:22:1832#include "components/browser_sync/profile_sync_service.h"
blundell80c5b2a02015-09-17 18:22:1233#include "components/sessions/content/content_live_tab.h"
maxbogue8ef25082015-11-16 19:09:5834#include "components/sync_sessions/open_tabs_ui_delegate.h"
35#include "components/sync_sessions/synced_session.h"
rsleevi24f64dc22015-08-07 21:39:2136#include "components/url_formatter/url_formatter.h"
[email protected]d791ad12013-08-23 02:12:5937#include "content/public/browser/web_contents.h"
[email protected]0b9de032014-03-15 05:47:0138#include "extensions/browser/extension_function_dispatcher.h"
[email protected]21c6c432014-03-05 18:47:3139#include "extensions/browser/extension_function_registry.h"
[email protected]38f92582014-03-30 20:48:5540#include "extensions/browser/extension_system.h"
[email protected]d791ad12013-08-23 02:12:5941#include "extensions/common/error_utils.h"
[email protected]d791ad12013-08-23 02:12:5942#include "ui/base/layout.h"
43
44namespace extensions {
45
sdy7710d22c2016-08-09 14:04:3446namespace {
47
[email protected]d791ad12013-08-23 02:12:5948namespace GetRecentlyClosed = api::sessions::GetRecentlyClosed;
49namespace GetDevices = api::sessions::GetDevices;
50namespace Restore = api::sessions::Restore;
51namespace tabs = api::tabs;
52namespace windows = api::windows;
53
54const char kNoRecentlyClosedSessionsError[] =
55 "There are no recently closed sessions.";
56const char kInvalidSessionIdError[] = "Invalid session id: \"*\".";
57const char kNoBrowserToRestoreSession[] =
58 "There are no browser windows to restore the session.";
59const char kSessionSyncError[] = "Synced sessions are not available.";
[email protected]ecf519882014-03-21 15:49:5260const char kRestoreInIncognitoError[] =
61 "Can not restore sessions in incognito mode.";
[email protected]d791ad12013-08-23 02:12:5962
63// Comparator function for use with std::sort that will sort sessions by
64// descending modified_time (i.e., most recent first).
maxboguea79d99b72016-09-15 15:59:1665bool SortSessionsByRecency(const sync_sessions::SyncedSession* s1,
66 const sync_sessions::SyncedSession* s2) {
[email protected]d791ad12013-08-23 02:12:5967 return s1->modified_time > s2->modified_time;
68}
69
70// Comparator function for use with std::sort that will sort tabs in a window
71// by descending timestamp (i.e., most recent first).
skuhneb7409dcf2014-11-14 04:06:5572bool SortTabsByRecency(const sessions::SessionTab* t1,
73 const sessions::SessionTab* t2) {
[email protected]d791ad12013-08-23 02:12:5974 return t1->timestamp > t2->timestamp;
75}
76
rdevlin.cronin372cf1d52016-03-30 22:46:1777tabs::Tab CreateTabModelHelper(
[email protected]d791ad12013-08-23 02:12:5978 const sessions::SerializedNavigationEntry& current_navigation,
79 const std::string& session_id,
80 int index,
81 bool pinned,
sdy7710d22c2016-08-09 14:04:3482 bool active,
[email protected]d791ad12013-08-23 02:12:5983 const Extension* extension) {
rdevlin.cronin372cf1d52016-03-30 22:46:1784 tabs::Tab tab_struct;
[email protected]d791ad12013-08-23 02:12:5985
kalman4399163f2014-10-23 20:34:5886 const GURL& url = current_navigation.virtual_url();
[email protected]04338722013-12-24 23:18:0587 std::string title = base::UTF16ToUTF8(current_navigation.title());
[email protected]d791ad12013-08-23 02:12:5988
rdevlin.cronin372cf1d52016-03-30 22:46:1789 tab_struct.session_id.reset(new std::string(session_id));
90 tab_struct.url.reset(new std::string(url.spec()));
91 tab_struct.fav_icon_url.reset(
kalman4399163f2014-10-23 20:34:5892 new std::string(current_navigation.favicon_url().spec()));
[email protected]d791ad12013-08-23 02:12:5993 if (!title.empty()) {
rdevlin.cronin372cf1d52016-03-30 22:46:1794 tab_struct.title.reset(new std::string(title));
[email protected]d791ad12013-08-23 02:12:5995 } else {
rdevlin.cronin372cf1d52016-03-30 22:46:1796 tab_struct.title.reset(new std::string(
jshin1fb76462016-04-05 22:13:0397 base::UTF16ToUTF8(url_formatter::FormatUrl(url))));
[email protected]d791ad12013-08-23 02:12:5998 }
rdevlin.cronin372cf1d52016-03-30 22:46:1799 tab_struct.index = index;
100 tab_struct.pinned = pinned;
sdy7710d22c2016-08-09 14:04:34101 tab_struct.active = active;
rdevlin.cronin372cf1d52016-03-30 22:46:17102 ExtensionTabUtil::ScrubTabForExtension(extension, nullptr, &tab_struct);
dcheng1fc00f12015-12-26 22:18:03103 return tab_struct;
[email protected]d791ad12013-08-23 02:12:59104}
105
dchengc963c7142016-04-08 03:55:22106std::unique_ptr<windows::Window> CreateWindowModelHelper(
107 std::unique_ptr<std::vector<tabs::Tab>> tabs,
[email protected]d791ad12013-08-23 02:12:59108 const std::string& session_id,
rdevlin.cronin00f1fc22015-04-06 17:19:18109 const windows::WindowType& type,
110 const windows::WindowState& state) {
dchengc963c7142016-04-08 03:55:22111 std::unique_ptr<windows::Window> window_struct(new windows::Window);
dcheng1fc00f12015-12-26 22:18:03112 window_struct->tabs = std::move(tabs);
[email protected]d791ad12013-08-23 02:12:59113 window_struct->session_id.reset(new std::string(session_id));
114 window_struct->incognito = false;
115 window_struct->always_on_top = false;
116 window_struct->focused = false;
117 window_struct->type = type;
118 window_struct->state = state;
dcheng1fc00f12015-12-26 22:18:03119 return window_struct;
[email protected]d791ad12013-08-23 02:12:59120}
121
dchengc963c7142016-04-08 03:55:22122std::unique_ptr<api::sessions::Session> CreateSessionModelHelper(
[email protected]d791ad12013-08-23 02:12:59123 int last_modified,
dchengc963c7142016-04-08 03:55:22124 std::unique_ptr<tabs::Tab> tab,
125 std::unique_ptr<windows::Window> window) {
126 std::unique_ptr<api::sessions::Session> session_struct(
rdevlin.cronin2e624f82016-03-29 00:15:03127 new api::sessions::Session());
[email protected]d791ad12013-08-23 02:12:59128 session_struct->last_modified = last_modified;
129 if (tab)
dcheng1fc00f12015-12-26 22:18:03130 session_struct->tab = std::move(tab);
[email protected]d791ad12013-08-23 02:12:59131 else if (window)
dcheng1fc00f12015-12-26 22:18:03132 session_struct->window = std::move(window);
[email protected]d791ad12013-08-23 02:12:59133 else
134 NOTREACHED();
dcheng1fc00f12015-12-26 22:18:03135 return session_struct;
[email protected]d791ad12013-08-23 02:12:59136}
137
sdy7710d22c2016-08-09 14:04:34138bool is_window_entry(const sessions::TabRestoreService::Entry& entry) {
139 return entry.type == sessions::TabRestoreService::WINDOW;
[email protected]d791ad12013-08-23 02:12:59140}
141
sdy7710d22c2016-08-09 14:04:34142} // namespace
[email protected]d791ad12013-08-23 02:12:59143
rdevlin.cronin372cf1d52016-03-30 22:46:17144tabs::Tab SessionsGetRecentlyClosedFunction::CreateTabModel(
blundell74001adc2015-09-18 11:04:25145 const sessions::TabRestoreService::Tab& tab,
sdy7710d22c2016-08-09 14:04:34146 bool active) {
rdevlin.cronin456b2832016-10-10 20:20:21147 return CreateTabModelHelper(tab.navigations[tab.current_navigation_index],
sdy7710d22c2016-08-09 14:04:34148 base::IntToString(tab.id), tab.tabstrip_index,
149 tab.pinned, active, extension());
[email protected]d791ad12013-08-23 02:12:59150}
151
dchengc963c7142016-04-08 03:55:22152std::unique_ptr<windows::Window>
blundell74001adc2015-09-18 11:04:25153SessionsGetRecentlyClosedFunction::CreateWindowModel(
sdy7710d22c2016-08-09 14:04:34154 const sessions::TabRestoreService::Window& window) {
[email protected]d791ad12013-08-23 02:12:59155 DCHECK(!window.tabs.empty());
156
sdy7710d22c2016-08-09 14:04:34157 auto tabs = base::MakeUnique<std::vector<tabs::Tab>>();
158 for (const auto& tab : window.tabs)
159 tabs->push_back(
160 CreateTabModel(*tab, tab->tabstrip_index == window.selected_tab_index));
[email protected]d791ad12013-08-23 02:12:59161
sdy7710d22c2016-08-09 14:04:34162 return CreateWindowModelHelper(std::move(tabs), base::IntToString(window.id),
rdevlin.cronin00f1fc22015-04-06 17:19:18163 windows::WINDOW_TYPE_NORMAL,
164 windows::WINDOW_STATE_NORMAL);
[email protected]d791ad12013-08-23 02:12:59165}
166
dchengc963c7142016-04-08 03:55:22167std::unique_ptr<api::sessions::Session>
blundell74001adc2015-09-18 11:04:25168SessionsGetRecentlyClosedFunction::CreateSessionModel(
sdy7710d22c2016-08-09 14:04:34169 const sessions::TabRestoreService::Entry& entry) {
dchengc963c7142016-04-08 03:55:22170 std::unique_ptr<tabs::Tab> tab;
171 std::unique_ptr<windows::Window> window;
sdy7710d22c2016-08-09 14:04:34172 switch (entry.type) {
blundell74001adc2015-09-18 11:04:25173 case sessions::TabRestoreService::TAB:
rdevlin.cronin372cf1d52016-03-30 22:46:17174 tab.reset(new tabs::Tab(CreateTabModel(
sdy7710d22c2016-08-09 14:04:34175 static_cast<const sessions::TabRestoreService::Tab&>(entry), false)));
[email protected]d791ad12013-08-23 02:12:59176 break;
blundell74001adc2015-09-18 11:04:25177 case sessions::TabRestoreService::WINDOW:
[email protected]d791ad12013-08-23 02:12:59178 window = CreateWindowModel(
sdy7710d22c2016-08-09 14:04:34179 static_cast<const sessions::TabRestoreService::Window&>(entry));
[email protected]d791ad12013-08-23 02:12:59180 break;
181 default:
182 NOTREACHED();
183 }
sdy7710d22c2016-08-09 14:04:34184 return CreateSessionModelHelper(entry.timestamp.ToTimeT(), std::move(tab),
dcheng1fc00f12015-12-26 22:18:03185 std::move(window));
[email protected]d791ad12013-08-23 02:12:59186}
187
rdevlin.cronin456b2832016-10-10 20:20:21188ExtensionFunction::ResponseAction SessionsGetRecentlyClosedFunction::Run() {
dchengc963c7142016-04-08 03:55:22189 std::unique_ptr<GetRecentlyClosed::Params> params(
[email protected]d791ad12013-08-23 02:12:59190 GetRecentlyClosed::Params::Create(*args_));
191 EXTENSION_FUNCTION_VALIDATE(params);
192 int max_results = api::sessions::MAX_SESSION_RESULTS;
193 if (params->filter && params->filter->max_results)
194 max_results = *params->filter->max_results;
195 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 &&
196 max_results <= api::sessions::MAX_SESSION_RESULTS);
197
rdevlin.cronin2e624f82016-03-29 00:15:03198 std::vector<api::sessions::Session> result;
blundell74001adc2015-09-18 11:04:25199 sessions::TabRestoreService* tab_restore_service =
rdevlin.cronin456b2832016-10-10 20:20:21200 TabRestoreServiceFactory::GetForProfile(
201 Profile::FromBrowserContext(browser_context()));
[email protected]b3e0f85c2014-03-22 04:58:00202
203 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in
204 // incognito mode)
205 if (!tab_restore_service) {
rdevlin.cronin456b2832016-10-10 20:20:21206 DCHECK(browser_context()->IsOffTheRecord())
blundell74001adc2015-09-18 11:04:25207 << "sessions::TabRestoreService expected for normal profiles";
rdevlin.cronin456b2832016-10-10 20:20:21208 return RespondNow(ArgumentList(GetRecentlyClosed::Results::Create(result)));
[email protected]b3e0f85c2014-03-22 04:58:00209 }
[email protected]d791ad12013-08-23 02:12:59210
211 // List of entries. They are ordered from most to least recent.
212 // We prune the list to contain max 25 entries at any time and removes
213 // uninteresting entries.
sdy7710d22c2016-08-09 14:04:34214 for (const auto& entry : tab_restore_service->entries()) {
215 result.push_back(std::move(*CreateSessionModel(*entry)));
[email protected]d791ad12013-08-23 02:12:59216 }
217
rdevlin.cronin456b2832016-10-10 20:20:21218 return RespondNow(ArgumentList(GetRecentlyClosed::Results::Create(result)));
[email protected]d791ad12013-08-23 02:12:59219}
220
rdevlin.cronin372cf1d52016-03-30 22:46:17221tabs::Tab SessionsGetDevicesFunction::CreateTabModel(
[email protected]d791ad12013-08-23 02:12:59222 const std::string& session_tag,
skuhneb7409dcf2014-11-14 04:06:55223 const sessions::SessionTab& tab,
[email protected]d791ad12013-08-23 02:12:59224 int tab_index,
sdy7710d22c2016-08-09 14:04:34225 bool active) {
[email protected]d791ad12013-08-23 02:12:59226 std::string session_id = SessionId(session_tag, tab.tab_id.id()).ToString();
[email protected]eba8f7d2014-07-28 22:09:23227 return CreateTabModelHelper(
rdevlin.cronin456b2832016-10-10 20:20:21228 tab.navigations[tab.normalized_navigation_index()], session_id, tab_index,
229 tab.pinned, active, extension());
[email protected]d791ad12013-08-23 02:12:59230}
231
dchengc963c7142016-04-08 03:55:22232std::unique_ptr<windows::Window> SessionsGetDevicesFunction::CreateWindowModel(
233 const sessions::SessionWindow& window,
234 const std::string& session_tag) {
[email protected]d791ad12013-08-23 02:12:59235 DCHECK(!window.tabs.empty());
236
237 // Prune tabs that are not syncable or are NewTabPage. Then, sort the tabs
238 // from most recent to least recent.
skuhneb7409dcf2014-11-14 04:06:55239 std::vector<const sessions::SessionTab*> tabs_in_window;
[email protected]d791ad12013-08-23 02:12:59240 for (size_t i = 0; i < window.tabs.size(); ++i) {
avi498cabca2016-09-21 19:03:50241 const sessions::SessionTab* tab = window.tabs[i].get();
[email protected]d791ad12013-08-23 02:12:59242 if (tab->navigations.empty())
243 continue;
244 const sessions::SerializedNavigationEntry& current_navigation =
245 tab->navigations.at(tab->normalized_navigation_index());
rdevlin.cronin456b2832016-10-10 20:20:21246 if (search::IsNTPURL(current_navigation.virtual_url(),
247 Profile::FromBrowserContext(browser_context()))) {
[email protected]d791ad12013-08-23 02:12:59248 continue;
249 }
250 tabs_in_window.push_back(tab);
251 }
252 if (tabs_in_window.empty())
dchengc963c7142016-04-08 03:55:22253 return std::unique_ptr<windows::Window>();
[email protected]d791ad12013-08-23 02:12:59254 std::sort(tabs_in_window.begin(), tabs_in_window.end(), SortTabsByRecency);
255
dchengc963c7142016-04-08 03:55:22256 std::unique_ptr<std::vector<tabs::Tab>> tabs(new std::vector<tabs::Tab>());
[email protected]ddd54352014-04-05 07:43:30257 for (size_t i = 0; i < tabs_in_window.size(); ++i) {
rdevlin.cronin372cf1d52016-03-30 22:46:17258 tabs->push_back(CreateTabModel(session_tag, *tabs_in_window[i], i,
sdy7710d22c2016-08-09 14:04:34259 window.selected_tab_index == (int)i));
[email protected]d791ad12013-08-23 02:12:59260 }
261
262 std::string session_id =
263 SessionId(session_tag, window.window_id.id()).ToString();
264
rdevlin.cronin00f1fc22015-04-06 17:19:18265 windows::WindowType type = windows::WINDOW_TYPE_NONE;
[email protected]d791ad12013-08-23 02:12:59266 switch (window.type) {
skuhneb7409dcf2014-11-14 04:06:55267 case sessions::SessionWindow::TYPE_TABBED:
rdevlin.cronin00f1fc22015-04-06 17:19:18268 type = windows::WINDOW_TYPE_NORMAL;
[email protected]d791ad12013-08-23 02:12:59269 break;
skuhneb7409dcf2014-11-14 04:06:55270 case sessions::SessionWindow::TYPE_POPUP:
rdevlin.cronin00f1fc22015-04-06 17:19:18271 type = windows::WINDOW_TYPE_POPUP;
[email protected]d791ad12013-08-23 02:12:59272 break;
273 }
274
rdevlin.cronin00f1fc22015-04-06 17:19:18275 windows::WindowState state = windows::WINDOW_STATE_NONE;
[email protected]d791ad12013-08-23 02:12:59276 switch (window.show_state) {
277 case ui::SHOW_STATE_NORMAL:
afakhry25a6b952017-01-27 20:36:50278
279 // TODO(afakhry): Remove Docked Windows in M58.
varkha74014e0e2015-05-05 18:12:31280 case ui::SHOW_STATE_DOCKED:
rdevlin.cronin00f1fc22015-04-06 17:19:18281 state = windows::WINDOW_STATE_NORMAL;
[email protected]d791ad12013-08-23 02:12:59282 break;
283 case ui::SHOW_STATE_MINIMIZED:
rdevlin.cronin00f1fc22015-04-06 17:19:18284 state = windows::WINDOW_STATE_MINIMIZED;
[email protected]d791ad12013-08-23 02:12:59285 break;
286 case ui::SHOW_STATE_MAXIMIZED:
rdevlin.cronin00f1fc22015-04-06 17:19:18287 state = windows::WINDOW_STATE_MAXIMIZED;
[email protected]d791ad12013-08-23 02:12:59288 break;
289 case ui::SHOW_STATE_FULLSCREEN:
rdevlin.cronin00f1fc22015-04-06 17:19:18290 state = windows::WINDOW_STATE_FULLSCREEN;
[email protected]d791ad12013-08-23 02:12:59291 break;
292 case ui::SHOW_STATE_DEFAULT:
293 case ui::SHOW_STATE_INACTIVE:
[email protected]d791ad12013-08-23 02:12:59294 case ui::SHOW_STATE_END:
295 break;
296 }
297
dchengc963c7142016-04-08 03:55:22298 std::unique_ptr<windows::Window> window_struct(
dcheng1fc00f12015-12-26 22:18:03299 CreateWindowModelHelper(std::move(tabs), session_id, type, state));
[email protected]d791ad12013-08-23 02:12:59300 // TODO(dwankri): Dig deeper to resolve bounds not being optional, so closed
301 // windows in GetRecentlyClosed can have set values in Window helper.
302 window_struct->left.reset(new int(window.bounds.x()));
303 window_struct->top.reset(new int(window.bounds.y()));
304 window_struct->width.reset(new int(window.bounds.width()));
305 window_struct->height.reset(new int(window.bounds.height()));
306
dcheng1fc00f12015-12-26 22:18:03307 return window_struct;
[email protected]d791ad12013-08-23 02:12:59308}
309
dchengc963c7142016-04-08 03:55:22310std::unique_ptr<api::sessions::Session>
[email protected]d791ad12013-08-23 02:12:59311SessionsGetDevicesFunction::CreateSessionModel(
dchengc963c7142016-04-08 03:55:22312 const sessions::SessionWindow& window,
313 const std::string& session_tag) {
314 std::unique_ptr<windows::Window> window_model(
[email protected]d791ad12013-08-23 02:12:59315 CreateWindowModel(window, session_tag));
316 // There is a chance that after pruning uninteresting tabs the window will be
317 // empty.
dchengc963c7142016-04-08 03:55:22318 return !window_model ? std::unique_ptr<api::sessions::Session>()
dcheng1fc00f12015-12-26 22:18:03319 : CreateSessionModelHelper(window.timestamp.ToTimeT(),
dchengc963c7142016-04-08 03:55:22320 std::unique_ptr<tabs::Tab>(),
dcheng1fc00f12015-12-26 22:18:03321 std::move(window_model));
[email protected]d791ad12013-08-23 02:12:59322}
323
rdevlin.cronin2e624f82016-03-29 00:15:03324api::sessions::Device SessionsGetDevicesFunction::CreateDeviceModel(
maxboguea79d99b72016-09-15 15:59:16325 const sync_sessions::SyncedSession* session) {
[email protected]d791ad12013-08-23 02:12:59326 int max_results = api::sessions::MAX_SESSION_RESULTS;
[email protected]a0c91a9f2014-05-03 03:41:43327 // Already validated in RunAsync().
dchengc963c7142016-04-08 03:55:22328 std::unique_ptr<GetDevices::Params> params(
329 GetDevices::Params::Create(*args_));
[email protected]d791ad12013-08-23 02:12:59330 if (params->filter && params->filter->max_results)
331 max_results = *params->filter->max_results;
332
rdevlin.cronin2e624f82016-03-29 00:15:03333 api::sessions::Device device_struct;
334 device_struct.info = session->session_name;
335 device_struct.device_name = session->session_name;
[email protected]d791ad12013-08-23 02:12:59336
avi498cabca2016-09-21 19:03:50337 for (auto it = session->windows.begin();
droger6a118632015-06-23 17:59:45338 it != session->windows.end() &&
rdevlin.cronin2e624f82016-03-29 00:15:03339 static_cast<int>(device_struct.sessions.size()) < max_results;
droger6a118632015-06-23 17:59:45340 ++it) {
dchengc963c7142016-04-08 03:55:22341 std::unique_ptr<api::sessions::Session> session_model(
342 CreateSessionModel(*it->second, session->session_tag));
[email protected]d791ad12013-08-23 02:12:59343 if (session_model)
rdevlin.cronin2e624f82016-03-29 00:15:03344 device_struct.sessions.push_back(std::move(*session_model));
[email protected]d791ad12013-08-23 02:12:59345 }
dcheng1fc00f12015-12-26 22:18:03346 return device_struct;
[email protected]d791ad12013-08-23 02:12:59347}
348
rdevlin.cronin456b2832016-10-10 20:20:21349ExtensionFunction::ResponseAction SessionsGetDevicesFunction::Run() {
maxbogue0a379452016-09-22 21:35:05350 browser_sync::ProfileSyncService* service =
rdevlin.cronin456b2832016-10-10 20:20:21351 ProfileSyncServiceFactory::GetInstance()->GetForProfile(
352 Profile::FromBrowserContext(browser_context()));
[email protected]d791ad12013-08-23 02:12:59353 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) {
354 // Sync not enabled.
rdevlin.cronin456b2832016-10-10 20:20:21355 return RespondNow(ArgumentList(
356 GetDevices::Results::Create(std::vector<api::sessions::Device>())));
[email protected]d791ad12013-08-23 02:12:59357 }
358
maxboguea79d99b72016-09-15 15:59:16359 sync_sessions::OpenTabsUIDelegate* open_tabs =
360 service->GetOpenTabsUIDelegate();
361 std::vector<const sync_sessions::SyncedSession*> sessions;
[email protected]a9f56622013-11-21 19:37:06362 if (!(open_tabs && open_tabs->GetAllForeignSessions(&sessions))) {
rdevlin.cronin456b2832016-10-10 20:20:21363 return RespondNow(ArgumentList(
364 GetDevices::Results::Create(std::vector<api::sessions::Device>())));
[email protected]d791ad12013-08-23 02:12:59365 }
366
dchengc963c7142016-04-08 03:55:22367 std::unique_ptr<GetDevices::Params> params(
368 GetDevices::Params::Create(*args_));
[email protected]d791ad12013-08-23 02:12:59369 EXTENSION_FUNCTION_VALIDATE(params);
370 if (params->filter && params->filter->max_results) {
371 EXTENSION_FUNCTION_VALIDATE(*params->filter->max_results >= 0 &&
372 *params->filter->max_results <= api::sessions::MAX_SESSION_RESULTS);
373 }
374
rdevlin.cronin2e624f82016-03-29 00:15:03375 std::vector<api::sessions::Device> result;
[email protected]d791ad12013-08-23 02:12:59376 // Sort sessions from most recent to least recent.
377 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency);
rdevlin.cronin2e624f82016-03-29 00:15:03378 for (size_t i = 0; i < sessions.size(); ++i)
379 result.push_back(CreateDeviceModel(sessions[i]));
[email protected]d791ad12013-08-23 02:12:59380
rdevlin.cronin456b2832016-10-10 20:20:21381 return RespondNow(ArgumentList(GetDevices::Results::Create(result)));
[email protected]d791ad12013-08-23 02:12:59382}
383
rdevlin.cronin456b2832016-10-10 20:20:21384ExtensionFunction::ResponseValue SessionsRestoreFunction::GetRestoredTabResult(
[email protected]fc2b46b2014-05-03 16:33:45385 content::WebContents* contents) {
dchengc963c7142016-04-08 03:55:22386 std::unique_ptr<tabs::Tab> tab(
limasdf6dcdc442016-02-26 04:58:26387 ExtensionTabUtil::CreateTabObject(contents, extension()));
dchengc963c7142016-04-08 03:55:22388 std::unique_ptr<api::sessions::Session> restored_session(
dcheng1fc00f12015-12-26 22:18:03389 CreateSessionModelHelper(base::Time::Now().ToTimeT(), std::move(tab),
dchengc963c7142016-04-08 03:55:22390 std::unique_ptr<windows::Window>()));
rdevlin.cronin456b2832016-10-10 20:20:21391 return ArgumentList(Restore::Results::Create(*restored_session));
[email protected]d791ad12013-08-23 02:12:59392}
393
rdevlin.cronin456b2832016-10-10 20:20:21394ExtensionFunction::ResponseValue
395SessionsRestoreFunction::GetRestoredWindowResult(int window_id) {
[email protected]d6da1132013-11-07 17:56:21396 WindowController* controller = NULL;
rdevlin.cronin456b2832016-10-10 20:20:21397 std::string error;
rdevlin.croninfd7bb622016-10-03 23:14:46398 if (!windows_util::GetWindowFromWindowID(this, window_id, 0, &controller,
rdevlin.cronin456b2832016-10-10 20:20:21399 &error)) {
400 return Error(error);
[email protected]d791ad12013-08-23 02:12:59401 }
dchengc963c7142016-04-08 03:55:22402 std::unique_ptr<base::DictionaryValue> window_value(
[email protected]eba8f7d2014-07-28 22:09:23403 controller->CreateWindowValueWithTabs(extension()));
dchengc963c7142016-04-08 03:55:22404 std::unique_ptr<windows::Window> window(
405 windows::Window::FromValue(*window_value));
rdevlin.cronin456b2832016-10-10 20:20:21406 return ArgumentList(Restore::Results::Create(*CreateSessionModelHelper(
dchengc963c7142016-04-08 03:55:22407 base::Time::Now().ToTimeT(), std::unique_ptr<tabs::Tab>(),
rdevlin.cronin456b2832016-10-10 20:20:21408 std::move(window))));
[email protected]d791ad12013-08-23 02:12:59409}
410
rdevlin.cronin456b2832016-10-10 20:20:21411ExtensionFunction::ResponseValue
412SessionsRestoreFunction::RestoreMostRecentlyClosed(Browser* browser) {
blundell74001adc2015-09-18 11:04:25413 sessions::TabRestoreService* tab_restore_service =
rdevlin.cronin456b2832016-10-10 20:20:21414 TabRestoreServiceFactory::GetForProfile(
415 Profile::FromBrowserContext(browser_context()));
sdy7710d22c2016-08-09 14:04:34416 const sessions::TabRestoreService::Entries& entries =
417 tab_restore_service->entries();
[email protected]d791ad12013-08-23 02:12:59418
rdevlin.cronin456b2832016-10-10 20:20:21419 if (entries.empty())
420 return Error(kNoRecentlyClosedSessionsError);
[email protected]d791ad12013-08-23 02:12:59421
sdy7710d22c2016-08-09 14:04:34422 bool is_window = is_window_entry(*entries.front());
blundellbde024d2015-09-30 13:47:07423 sessions::LiveTabContext* context =
424 BrowserLiveTabContext::FindContextForWebContents(
[email protected]d791ad12013-08-23 02:12:59425 browser->tab_strip_model()->GetActiveWebContents());
blundell80c5b2a02015-09-17 18:22:12426 std::vector<sessions::LiveTab*> restored_tabs =
scottmgd161e6c2016-02-17 02:08:01427 tab_restore_service->RestoreMostRecentEntry(context);
blundell80c5b2a02015-09-17 18:22:12428 DCHECK(restored_tabs.size());
[email protected]d791ad12013-08-23 02:12:59429
blundell80c5b2a02015-09-17 18:22:12430 sessions::ContentLiveTab* first_tab =
431 static_cast<sessions::ContentLiveTab*>(restored_tabs[0]);
[email protected]d791ad12013-08-23 02:12:59432 if (is_window) {
rdevlin.cronin456b2832016-10-10 20:20:21433 return GetRestoredWindowResult(
blundell80c5b2a02015-09-17 18:22:12434 ExtensionTabUtil::GetWindowIdOfTab(first_tab->web_contents()));
[email protected]d791ad12013-08-23 02:12:59435 }
436
rdevlin.cronin456b2832016-10-10 20:20:21437 return GetRestoredTabResult(first_tab->web_contents());
[email protected]d791ad12013-08-23 02:12:59438}
439
rdevlin.cronin456b2832016-10-10 20:20:21440ExtensionFunction::ResponseValue SessionsRestoreFunction::RestoreLocalSession(
441 const SessionId& session_id,
442 Browser* browser) {
blundell74001adc2015-09-18 11:04:25443 sessions::TabRestoreService* tab_restore_service =
rdevlin.cronin456b2832016-10-10 20:20:21444 TabRestoreServiceFactory::GetForProfile(
445 Profile::FromBrowserContext(browser_context()));
sdy7710d22c2016-08-09 14:04:34446 const sessions::TabRestoreService::Entries& entries =
447 tab_restore_service->entries();
[email protected]d791ad12013-08-23 02:12:59448
rdevlin.cronin456b2832016-10-10 20:20:21449 if (entries.empty())
450 return Error(kInvalidSessionIdError, session_id.ToString());
[email protected]d791ad12013-08-23 02:12:59451
452 // Check if the recently closed list contains an entry with the provided id.
453 bool is_window = false;
sdy7710d22c2016-08-09 14:04:34454 for (const auto& entry : entries) {
455 if (entry->id == session_id.id()) {
456 // A full window is being restored only if the entry ID
[email protected]d791ad12013-08-23 02:12:59457 // matches the provided ID and the entry type is Window.
sdy7710d22c2016-08-09 14:04:34458 is_window = is_window_entry(*entry);
[email protected]d791ad12013-08-23 02:12:59459 break;
460 }
461 }
462
blundellbde024d2015-09-30 13:47:07463 sessions::LiveTabContext* context =
464 BrowserLiveTabContext::FindContextForWebContents(
[email protected]d791ad12013-08-23 02:12:59465 browser->tab_strip_model()->GetActiveWebContents());
blundell80c5b2a02015-09-17 18:22:12466 std::vector<sessions::LiveTab*> restored_tabs =
nick3b04f322016-08-31 19:29:19467 tab_restore_service->RestoreEntryById(context, session_id.id(),
468 WindowOpenDisposition::UNKNOWN);
blundell80c5b2a02015-09-17 18:22:12469 // If the ID is invalid, restored_tabs will be empty.
rdevlin.cronin456b2832016-10-10 20:20:21470 if (restored_tabs.empty())
471 return Error(kInvalidSessionIdError, session_id.ToString());
[email protected]d791ad12013-08-23 02:12:59472
blundell80c5b2a02015-09-17 18:22:12473 sessions::ContentLiveTab* first_tab =
474 static_cast<sessions::ContentLiveTab*>(restored_tabs[0]);
475
476 // Retrieve the window through any of the tabs in restored_tabs.
[email protected]d791ad12013-08-23 02:12:59477 if (is_window) {
rdevlin.cronin456b2832016-10-10 20:20:21478 return GetRestoredWindowResult(
blundell80c5b2a02015-09-17 18:22:12479 ExtensionTabUtil::GetWindowIdOfTab(first_tab->web_contents()));
[email protected]d791ad12013-08-23 02:12:59480 }
481
rdevlin.cronin456b2832016-10-10 20:20:21482 return GetRestoredTabResult(first_tab->web_contents());
[email protected]d791ad12013-08-23 02:12:59483}
484
rdevlin.cronin456b2832016-10-10 20:20:21485ExtensionFunction::ResponseValue SessionsRestoreFunction::RestoreForeignSession(
486 const SessionId& session_id,
487 Browser* browser) {
488 Profile* profile = Profile::FromBrowserContext(browser_context());
maxbogue0a379452016-09-22 21:35:05489 browser_sync::ProfileSyncService* service =
rdevlin.cronin456b2832016-10-10 20:20:21490 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
491 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS)))
492 return Error(kSessionSyncError);
maxboguea79d99b72016-09-15 15:59:16493 sync_sessions::OpenTabsUIDelegate* open_tabs =
494 service->GetOpenTabsUIDelegate();
rdevlin.cronin456b2832016-10-10 20:20:21495 if (!open_tabs)
496 return Error(kSessionSyncError);
[email protected]d791ad12013-08-23 02:12:59497
skuhneb7409dcf2014-11-14 04:06:55498 const sessions::SessionTab* tab = NULL;
[email protected]a9f56622013-11-21 19:37:06499 if (open_tabs->GetForeignTab(session_id.session_tag(),
500 session_id.id(),
501 &tab)) {
[email protected]d791ad12013-08-23 02:12:59502 TabStripModel* tab_strip = browser->tab_strip_model();
503 content::WebContents* contents = tab_strip->GetActiveWebContents();
504
505 content::WebContents* tab_contents =
nick3b04f322016-08-31 19:29:19506 SessionRestore::RestoreForeignSessionTab(
507 contents, *tab, WindowOpenDisposition::NEW_FOREGROUND_TAB);
rdevlin.cronin456b2832016-10-10 20:20:21508 return GetRestoredTabResult(tab_contents);
[email protected]d791ad12013-08-23 02:12:59509 }
510
511 // Restoring a full window.
skuhneb7409dcf2014-11-14 04:06:55512 std::vector<const sessions::SessionWindow*> windows;
rdevlin.cronin456b2832016-10-10 20:20:21513 if (!open_tabs->GetForeignSession(session_id.session_tag(), &windows))
514 return Error(kInvalidSessionIdError, session_id.ToString());
[email protected]d791ad12013-08-23 02:12:59515
skuhneb7409dcf2014-11-14 04:06:55516 std::vector<const sessions::SessionWindow*>::const_iterator window =
517 windows.begin();
[email protected]d791ad12013-08-23 02:12:59518 while (window != windows.end()
519 && (*window)->window_id.id() != session_id.id()) {
520 ++window;
521 }
rdevlin.cronin456b2832016-10-10 20:20:21522 if (window == windows.end())
523 return Error(kInvalidSessionIdError, session_id.ToString());
[email protected]d791ad12013-08-23 02:12:59524
[email protected]d791ad12013-08-23 02:12:59525 // Only restore one window at a time.
rdevlin.cronin456b2832016-10-10 20:20:21526 std::vector<Browser*> browsers =
527 SessionRestore::RestoreForeignSessionWindows(profile, window, window + 1);
[email protected]d791ad12013-08-23 02:12:59528 // Will always create one browser because we only restore one window per call.
529 DCHECK_EQ(1u, browsers.size());
rdevlin.cronin456b2832016-10-10 20:20:21530 return GetRestoredWindowResult(ExtensionTabUtil::GetWindowId(browsers[0]));
[email protected]d791ad12013-08-23 02:12:59531}
532
rdevlin.cronin456b2832016-10-10 20:20:21533ExtensionFunction::ResponseAction SessionsRestoreFunction::Run() {
dchengc963c7142016-04-08 03:55:22534 std::unique_ptr<Restore::Params> params(Restore::Params::Create(*args_));
[email protected]d791ad12013-08-23 02:12:59535 EXTENSION_FUNCTION_VALIDATE(params);
536
rdevlin.cronin456b2832016-10-10 20:20:21537 Profile* profile = Profile::FromBrowserContext(browser_context());
538 Browser* browser = chrome::FindBrowserWithProfile(profile);
539 if (!browser)
540 return RespondNow(Error(kNoBrowserToRestoreSession));
[email protected]d791ad12013-08-23 02:12:59541
rdevlin.cronin456b2832016-10-10 20:20:21542 if (profile != profile->GetOriginalProfile())
543 return RespondNow(Error(kRestoreInIncognitoError));
[email protected]ecf519882014-03-21 15:49:52544
[email protected]d791ad12013-08-23 02:12:59545 if (!params->session_id)
rdevlin.cronin456b2832016-10-10 20:20:21546 return RespondNow(RestoreMostRecentlyClosed(browser));
[email protected]d791ad12013-08-23 02:12:59547
dchengc963c7142016-04-08 03:55:22548 std::unique_ptr<SessionId> session_id(SessionId::Parse(*params->session_id));
rdevlin.cronin456b2832016-10-10 20:20:21549 if (!session_id)
550 return RespondNow(Error(kInvalidSessionIdError, *params->session_id));
[email protected]d791ad12013-08-23 02:12:59551
rdevlin.cronin456b2832016-10-10 20:20:21552 return RespondNow(session_id->IsForeign()
553 ? RestoreForeignSession(*session_id, browser)
554 : RestoreLocalSession(*session_id, browser));
[email protected]d791ad12013-08-23 02:12:59555}
556
[email protected]38f92582014-03-30 20:48:55557SessionsEventRouter::SessionsEventRouter(Profile* profile)
558 : profile_(profile),
559 tab_restore_service_(TabRestoreServiceFactory::GetForProfile(profile)) {
560 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in
561 // incognito mode)
562 if (tab_restore_service_) {
563 tab_restore_service_->LoadTabsFromLastSession();
564 tab_restore_service_->AddObserver(this);
565 }
566}
567
568SessionsEventRouter::~SessionsEventRouter() {
569 if (tab_restore_service_)
570 tab_restore_service_->RemoveObserver(this);
571}
572
573void SessionsEventRouter::TabRestoreServiceChanged(
blundell74001adc2015-09-18 11:04:25574 sessions::TabRestoreService* service) {
dchengc963c7142016-04-08 03:55:22575 std::unique_ptr<base::ListValue> args(new base::ListValue());
ricea91d6fc122016-08-30 08:47:14576 EventRouter::Get(profile_)->BroadcastEvent(base::MakeUnique<Event>(
577 events::SESSIONS_ON_CHANGED, api::sessions::OnChanged::kEventName,
578 std::move(args)));
[email protected]38f92582014-03-30 20:48:55579}
580
581void SessionsEventRouter::TabRestoreServiceDestroyed(
blundell74001adc2015-09-18 11:04:25582 sessions::TabRestoreService* service) {
[email protected]38f92582014-03-30 20:48:55583 tab_restore_service_ = NULL;
584}
585
586SessionsAPI::SessionsAPI(content::BrowserContext* context)
587 : browser_context_(context) {
588 EventRouter::Get(browser_context_)->RegisterObserver(this,
589 api::sessions::OnChanged::kEventName);
590}
591
592SessionsAPI::~SessionsAPI() {
593}
594
595void SessionsAPI::Shutdown() {
596 EventRouter::Get(browser_context_)->UnregisterObserver(this);
597}
598
scottmg5e65e3a2017-03-08 08:48:46599static base::LazyInstance<
600 BrowserContextKeyedAPIFactory<SessionsAPI>>::DestructorAtExit g_factory =
601 LAZY_INSTANCE_INITIALIZER;
[email protected]38f92582014-03-30 20:48:55602
603BrowserContextKeyedAPIFactory<SessionsAPI>*
604SessionsAPI::GetFactoryInstance() {
605 return g_factory.Pointer();
606}
607
608void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) {
609 sessions_event_router_.reset(
610 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_)));
611 EventRouter::Get(browser_context_)->UnregisterObserver(this);
612}
613
[email protected]d791ad12013-08-23 02:12:59614} // namespace extensions