1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/history/foreign_session_handler.h"
#include <stddef.h>
#include <algorithm>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/i18n/time_formatting.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/session_restore.h"
#include "chrome/browser/sync/session_sync_service_factory.h"
#include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/strings/grit/components_strings.h"
#include "components/sync_sessions/session_sync_service.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "ui/base/l10n/time_format.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/base/window_open_disposition_utils.h"
namespace browser_sync {
namespace {
// Maximum number of sessions we're going to display on the NTP
const size_t kMaxSessionsToShow = 10;
// Helper method to create JSON compatible objects from Session objects.
std::optional<base::Value::Dict> SessionTabToValue(
const ::sessions::SessionTab& tab) {
if (tab.navigations.empty()) {
return std::nullopt;
}
int selected_index = std::min(tab.current_navigation_index,
static_cast<int>(tab.navigations.size() - 1));
const ::sessions::SerializedNavigationEntry& current_navigation =
tab.navigations.at(selected_index);
GURL tab_url = current_navigation.virtual_url();
if (!tab_url.is_valid() || tab_url.spec() == chrome::kChromeUINewTabURL) {
return std::nullopt;
}
base::Value::Dict dictionary;
NewTabUI::SetUrlTitleAndDirection(&dictionary, current_navigation.title(),
tab_url);
dictionary.Set("remoteIconUrlForUma",
current_navigation.favicon_url().spec());
dictionary.Set("type", "tab");
dictionary.Set("timestamp",
static_cast<double>(tab.timestamp.ToInternalValue()));
// TODO(jeremycho): This should probably be renamed to tabId to avoid
// confusion with the ID corresponding to a session. Investigate all the
// places (C++ and JS) where this is being used. (http://crbug.com/154865).
dictionary.Set("sessionId", tab.tab_id.id());
return dictionary;
}
// Helper for initializing a boilerplate SessionWindow JSON compatible object.
base::Value::Dict BuildWindowData(base::Time modification_time,
SessionID window_id) {
base::Value::Dict dictionary;
dictionary.Set("type", "window");
dictionary.Set("timestamp",
static_cast<double>(modification_time.ToInternalValue()));
dictionary.Set("sessionId", window_id.id());
return dictionary;
}
// Helper method to create JSON compatible objects from SessionWindow objects.
std::optional<base::Value::Dict> SessionWindowToValue(
const ::sessions::SessionWindow& window) {
if (window.tabs.empty()) {
return std::nullopt;
}
base::Value::List tab_values;
// Calculate the last |modification_time| for all entries within a window.
base::Time modification_time = window.timestamp;
for (const std::unique_ptr<sessions::SessionTab>& tab : window.tabs) {
auto tab_value = SessionTabToValue(*tab.get());
if (tab_value.has_value()) {
modification_time = std::max(modification_time, tab->timestamp);
tab_values.Append(std::move(*tab_value));
}
}
if (tab_values.empty()) {
return std::nullopt;
}
base::Value::Dict dictionary =
BuildWindowData(window.timestamp, window.window_id);
dictionary.Set("tabs", std::move(tab_values));
return dictionary;
}
} // namespace
ForeignSessionHandler::ForeignSessionHandler() = default;
ForeignSessionHandler::~ForeignSessionHandler() = default;
// static
void ForeignSessionHandler::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterDictionaryPref(prefs::kNtpCollapsedForeignSessions);
}
// static
void ForeignSessionHandler::OpenForeignSessionTab(
content::WebUI* web_ui,
const std::string& session_string_value,
SessionID tab_id,
const WindowOpenDisposition& disposition) {
sync_sessions::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(web_ui);
if (!open_tabs) {
return;
}
const ::sessions::SessionTab* tab;
if (!open_tabs->GetForeignTab(session_string_value, tab_id, &tab)) {
LOG(ERROR) << "Failed to load foreign tab.";
return;
}
if (tab->navigations.empty()) {
LOG(ERROR) << "Foreign tab no longer has valid navigations.";
return;
}
SessionRestore::RestoreForeignSessionTab(web_ui->GetWebContents(), *tab,
disposition);
}
// static
void ForeignSessionHandler::OpenForeignSessionWindows(
content::WebUI* web_ui,
const std::string& session_string_value) {
sync_sessions::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(web_ui);
if (!open_tabs) {
return;
}
// Note: we don't own the ForeignSessions themselves.
std::vector<const ::sessions::SessionWindow*> windows =
open_tabs->GetForeignSession(session_string_value);
if (windows.empty()) {
LOG(ERROR) << "ForeignSessionHandler failed to get session data from"
"OpenTabsUIDelegate.";
return;
}
SessionRestore::RestoreForeignSessionWindows(Profile::FromWebUI(web_ui),
windows.begin(), windows.end());
}
// static
sync_sessions::OpenTabsUIDelegate* ForeignSessionHandler::GetOpenTabsUIDelegate(
content::WebUI* web_ui) {
Profile* profile = Profile::FromWebUI(web_ui);
sync_sessions::SessionSyncService* service =
SessionSyncServiceFactory::GetInstance()->GetForProfile(profile);
return service ? service->GetOpenTabsUIDelegate() : nullptr;
}
void ForeignSessionHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"deleteForeignSession",
base::BindRepeating(&ForeignSessionHandler::HandleDeleteForeignSession,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getForeignSessions",
base::BindRepeating(&ForeignSessionHandler::HandleGetForeignSessions,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"openForeignSessionAllTabs",
base::BindRepeating(
&ForeignSessionHandler::HandleOpenForeignSessionAllTabs,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"openForeignSessionTab",
base::BindRepeating(&ForeignSessionHandler::HandleOpenForeignSessionTab,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"setForeignSessionCollapsed",
base::BindRepeating(
&ForeignSessionHandler::HandleSetForeignSessionCollapsed,
base::Unretained(this)));
}
void ForeignSessionHandler::OnJavascriptAllowed() {
// This can happen if the page is refreshed.
if (!initial_session_list_) {
initial_session_list_ = GetForeignSessions();
}
Profile* profile = Profile::FromWebUI(web_ui());
sync_sessions::SessionSyncService* service =
SessionSyncServiceFactory::GetInstance()->GetForProfile(profile);
// NOTE: The SessionSyncService can be null in tests.
if (service) {
// base::Unretained() is safe below because the subscription itself is a
// class member field and handles destruction well.
foreign_session_updated_subscription_ =
service->SubscribeToForeignSessionsChanged(
base::BindRepeating(&ForeignSessionHandler::OnForeignSessionUpdated,
base::Unretained(this)));
}
}
void ForeignSessionHandler::OnJavascriptDisallowed() {
// Avoid notifying Javascript listeners due to foreign session changes, which
// is now disallowed and would otherwise run into CHECK failures in
// OnForeignSessionUpdated().
foreign_session_updated_subscription_ = base::CallbackListSubscription();
}
void ForeignSessionHandler::OnForeignSessionUpdated() {
FireWebUIListener("foreign-sessions-changed",
std::move(GetForeignSessions()));
}
void ForeignSessionHandler::InitializeForeignSessions() {
initial_session_list_ = GetForeignSessions();
}
std::u16string ForeignSessionHandler::FormatSessionTime(
const base::Time& time) {
// Return a time like "1 hour ago", "2 days ago", etc.
base::Time now = base::Time::Now();
// TimeFormat does not support negative TimeDelta values, so then we use 0.
return ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_ELAPSED,
ui::TimeFormat::LENGTH_SHORT,
now < time ? base::TimeDelta() : now - time);
}
void ForeignSessionHandler::HandleGetForeignSessions(
const base::Value::List& args) {
AllowJavascript();
CHECK(initial_session_list_);
const base::Value& callback_id = args[0];
ResolveJavascriptCallback(callback_id, *initial_session_list_);
// Clear the initial list so that it will be reset in AllowJavascript if the
// page is refreshed.
initial_session_list_ = std::nullopt;
}
base::Value::List ForeignSessionHandler::GetForeignSessions() {
sync_sessions::OpenTabsUIDelegate* open_tabs =
GetOpenTabsUIDelegate(web_ui());
std::vector<raw_ptr<const sync_sessions::SyncedSession, VectorExperimental>>
sessions;
base::Value::List session_list;
if (open_tabs && open_tabs->GetAllForeignSessions(&sessions)) {
// Use a pref to keep track of sessions that were collapsed by the user.
// To prevent the pref from accumulating stale sessions, clear it each time
// and only add back sessions that are still current.
ScopedDictPrefUpdate pref_update(Profile::FromWebUI(web_ui())->GetPrefs(),
prefs::kNtpCollapsedForeignSessions);
base::Value::Dict& current_collapsed_sessions = pref_update.Get();
base::Value::Dict collapsed_sessions = current_collapsed_sessions.Clone();
current_collapsed_sessions.clear();
// Note: we don't own the SyncedSessions themselves.
for (size_t i = 0; i < sessions.size() && i < kMaxSessionsToShow; ++i) {
const sync_sessions::SyncedSession* session = sessions[i];
const std::string& session_tag = session->GetSessionTag();
base::Value::Dict session_data;
// The items which are to be written into |session_data| are also
// described in chrome/browser/resources/history/externs.js
// @typedef for ForeignSession. Please update it whenever you add or
// remove any keys here.
session_data.Set("tag", session_tag);
session_data.Set("name", session->GetSessionName());
session_data.Set("modifiedTime",
FormatSessionTime(session->GetModifiedTime()));
session_data.Set(
"timestamp",
session->GetModifiedTime().InMillisecondsFSinceUnixEpoch());
bool is_collapsed = collapsed_sessions.Find(session_tag);
session_data.Set("collapsed", is_collapsed);
if (is_collapsed) {
current_collapsed_sessions.Set(session_tag, true);
}
base::Value::List window_list;
// Order tabs by visual order within window.
for (const auto& window_pair : session->windows) {
auto window_data =
SessionWindowToValue(window_pair.second->wrapped_window);
if (window_data.has_value()) {
window_list.Append(std::move(*window_data));
}
}
session_data.Set("windows", std::move(window_list));
session_list.Append(std::move(session_data));
}
}
return session_list;
}
void ForeignSessionHandler::HandleOpenForeignSessionAllTabs(
const base::Value::List& args) {
CHECK_EQ(args.size(), 1U);
// Extract the session tag (always provided).
if (!args[0].is_string()) {
LOG(ERROR) << "Failed to extract session tag.";
return;
}
const std::string& session_string_value = args[0].GetString();
OpenForeignSessionWindows(web_ui(), session_string_value);
}
void ForeignSessionHandler::HandleOpenForeignSessionTab(
const base::Value::List& args) {
CHECK_EQ(args.size(), 7U);
// Extract the session tag (always provided).
if (!args[0].is_string()) {
LOG(ERROR) << "Failed to extract session tag.";
return;
}
const std::string& session_string_value = args[0].GetString();
// Extract tab id.
SessionID::id_type tab_id_value = 0;
if (!args[1].is_string() ||
!base::StringToInt(args[1].GetString(), &tab_id_value)) {
LOG(ERROR) << "Failed to extract tab SessionID.";
return;
}
SessionID tab_id = SessionID::FromSerializedValue(tab_id_value);
if (!tab_id.is_valid()) {
LOG(ERROR) << "Failed to deserialize tab ID.";
return;
}
WindowOpenDisposition disposition = webui::GetDispositionFromClick(args, 2);
OpenForeignSessionTab(web_ui(), session_string_value, tab_id, disposition);
}
void ForeignSessionHandler::HandleDeleteForeignSession(
const base::Value::List& args) {
if (args.size() != 1U) {
LOG(ERROR) << "Wrong number of args to deleteForeignSession";
return;
}
// Get the session tag argument (required).
if (!args[0].is_string()) {
LOG(ERROR) << "Unable to extract session tag";
return;
}
const std::string& session_tag = args[0].GetString();
sync_sessions::OpenTabsUIDelegate* open_tabs =
GetOpenTabsUIDelegate(web_ui());
if (open_tabs) {
open_tabs->DeleteForeignSession(session_tag);
}
}
void ForeignSessionHandler::HandleSetForeignSessionCollapsed(
const base::Value::List& args) {
if (args.size() != 2U) {
LOG(ERROR) << "Wrong number of args to setForeignSessionCollapsed";
return;
}
// Get the session tag argument (required).
if (!args[0].is_string()) {
LOG(ERROR) << "Unable to extract session tag";
return;
}
const std::string& session_tag = args[0].GetString();
if (!args[1].is_bool()) {
LOG(ERROR) << "Unable to extract boolean argument";
return;
}
const bool is_collapsed = args[1].GetBool();
// Store session tags for collapsed sessions in a preference so that the
// collapsed state persists.
PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
ScopedDictPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions);
if (is_collapsed) {
update->Set(session_tag, true);
} else {
update->Remove(session_tag);
}
}
} // namespace browser_sync
|