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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sync_sessions/test_synced_window_delegates_getter.h"
#include <vector>
#include "base/functional/bind.h"
#include "components/sessions/core/serialized_navigation_entry_test_helper.h"
#include "components/sync_sessions/synced_session.h"
#include "components/sync_sessions/tab_node_pool.h"
namespace sync_sessions {
namespace {
const char kTitle[] = "title";
} // namespace
TestSyncedTabDelegate::TestSyncedTabDelegate(
SessionID window_id,
SessionID tab_id,
const base::RepeatingCallback<void(SyncedTabDelegate*)>& notify_cb)
: window_id_(window_id), tab_id_(tab_id), notify_cb_(notify_cb) {}
TestSyncedTabDelegate::~TestSyncedTabDelegate() = default;
void TestSyncedTabDelegate::Navigate(const std::string& url,
base::Time time,
ui::PageTransition transition) {
sync_pb::TabNavigation tab_navigation;
tab_navigation.set_virtual_url(url);
tab_navigation.set_title(kTitle);
tab_navigation.set_global_id(time.ToInternalValue());
tab_navigation.set_unique_id(time.ToInternalValue());
tab_navigation.set_http_status_code(200);
auto entry = std::make_unique<sessions::SerializedNavigationEntry>(
SessionNavigationFromSyncData(0, tab_navigation));
sessions::SerializedNavigationEntryTestHelper::SetTimestamp(time,
entry.get());
sessions::SerializedNavigationEntryTestHelper::SetTransitionType(transition,
entry.get());
entries_.push_back(std::move(entry));
set_current_entry_index(GetCurrentEntryIndex() + 1);
notify_cb_.Run(this);
}
void TestSyncedTabDelegate::set_current_entry_index(int i) {
current_entry_index_ = i;
}
void TestSyncedTabDelegate::set_blocked_navigations(
const std::vector<std::unique_ptr<sessions::SerializedNavigationEntry>>&
navs) {
for (auto& entry : navs) {
blocked_navigations_.push_back(
std::make_unique<sessions::SerializedNavigationEntry>(*entry));
}
}
bool TestSyncedTabDelegate::IsInitialBlankNavigation() const {
// This differs from NavigationControllerImpl, which has an initial blank
// NavigationEntry.
return GetEntryCount() == 0;
}
int TestSyncedTabDelegate::GetCurrentEntryIndex() const {
return current_entry_index_;
}
GURL TestSyncedTabDelegate::GetVirtualURLAtIndex(int i) const {
if (static_cast<size_t>(i) >= entries_.size()) {
return GURL();
}
return entries_[i]->virtual_url();
}
void TestSyncedTabDelegate::GetSerializedNavigationAtIndex(
int i,
sessions::SerializedNavigationEntry* serialized_entry) const {
if (static_cast<size_t>(i) >= entries_.size()) {
return;
}
*serialized_entry = *entries_[i];
}
int TestSyncedTabDelegate::GetEntryCount() const {
return entries_.size();
}
SessionID TestSyncedTabDelegate::GetWindowId() const {
return window_id_;
}
SessionID TestSyncedTabDelegate::GetSessionId() const {
return tab_id_;
}
bool TestSyncedTabDelegate::IsBeingDestroyed() const {
return false;
}
base::Time TestSyncedTabDelegate::GetLastActiveTime() {
return base::Time::UnixEpoch();
}
std::string TestSyncedTabDelegate::GetExtensionAppId() const {
return std::string();
}
bool TestSyncedTabDelegate::ProfileHasChildAccount() const {
return has_child_account_;
}
void TestSyncedTabDelegate::set_has_child_account(bool has_child_account) {
has_child_account_ = has_child_account;
}
const std::vector<std::unique_ptr<const sessions::SerializedNavigationEntry>>*
TestSyncedTabDelegate::GetBlockedNavigations() const {
return &blocked_navigations_;
}
bool TestSyncedTabDelegate::IsPlaceholderTab() const {
return false;
}
bool TestSyncedTabDelegate::ShouldSync(SyncSessionsClient* sessions_client) {
// This is just a simple filter that isn't meant to fully reproduce
// the TabContentsTabDelegate's ShouldSync logic.
// Verify all URL's are valid (which will ignore an initial blank page) and
// that there is at least one http:// url.
int http_count = 0;
for (auto& entry : entries_) {
if (!entry->virtual_url().is_valid()) {
return false;
}
if (entry->virtual_url().SchemeIsHTTPOrHTTPS()) {
http_count++;
}
}
return http_count > 0;
}
int64_t TestSyncedTabDelegate::GetTaskIdForNavigationId(int nav_id) const {
// Task IDs are currently not used in the tests. -1 signals an unknown Task
// ID.
return -1;
}
int64_t TestSyncedTabDelegate::GetParentTaskIdForNavigationId(
int nav_id) const {
// Task IDs are currently not used in the tests. -1 signals an unknown Task
// ID.
return -1;
}
int64_t TestSyncedTabDelegate::GetRootTaskIdForNavigationId(int nav_id) const {
// Task IDs are currently not used in the tests. -1 signals an unknown Task
// ID.
return -1;
}
std::unique_ptr<SyncedTabDelegate>
TestSyncedTabDelegate::ReadPlaceholderTabSnapshotIfItShouldSync(
SyncSessionsClient* sessions_client) {
NOTREACHED();
}
PlaceholderTabDelegate::PlaceholderTabDelegate(SessionID tab_id)
: tab_id_(tab_id) {}
PlaceholderTabDelegate::~PlaceholderTabDelegate() = default;
SessionID PlaceholderTabDelegate::GetSessionId() const {
return tab_id_;
}
bool PlaceholderTabDelegate::IsPlaceholderTab() const {
return true;
}
void PlaceholderTabDelegate::SetPlaceholderTabSyncedTabDelegate(
std::unique_ptr<SyncedTabDelegate> delegate) {
placeholder_tab_synced_tab_delegate_ = std::move(delegate);
}
std::unique_ptr<SyncedTabDelegate>
PlaceholderTabDelegate::ReadPlaceholderTabSnapshotIfItShouldSync(
SyncSessionsClient* sessions_client) {
CHECK(placeholder_tab_synced_tab_delegate_);
return std::move(placeholder_tab_synced_tab_delegate_);
}
SessionID PlaceholderTabDelegate::GetWindowId() const {
NOTREACHED();
}
bool PlaceholderTabDelegate::IsBeingDestroyed() const {
NOTREACHED();
}
base::Time PlaceholderTabDelegate::GetLastActiveTime() {
NOTREACHED();
}
std::string PlaceholderTabDelegate::GetExtensionAppId() const {
NOTREACHED();
}
bool PlaceholderTabDelegate::IsInitialBlankNavigation() const {
NOTREACHED();
}
int PlaceholderTabDelegate::GetCurrentEntryIndex() const {
NOTREACHED();
}
int PlaceholderTabDelegate::GetEntryCount() const {
NOTREACHED();
}
GURL PlaceholderTabDelegate::GetVirtualURLAtIndex(int i) const {
NOTREACHED();
}
void PlaceholderTabDelegate::GetSerializedNavigationAtIndex(
int i,
sessions::SerializedNavigationEntry* serialized_entry) const {
NOTREACHED();
}
bool PlaceholderTabDelegate::ProfileHasChildAccount() const {
NOTREACHED();
}
const std::vector<std::unique_ptr<const sessions::SerializedNavigationEntry>>*
PlaceholderTabDelegate::GetBlockedNavigations() const {
NOTREACHED();
}
bool PlaceholderTabDelegate::ShouldSync(SyncSessionsClient* sessions_client) {
NOTREACHED();
}
int64_t PlaceholderTabDelegate::GetTaskIdForNavigationId(int nav_id) const {
// Task IDs are currently not used in the tests. -1 signals an unknown Task
// ID.
NOTREACHED() << "Task IDs are not used for Placeholder Tabs";
}
int64_t PlaceholderTabDelegate::GetParentTaskIdForNavigationId(
int nav_id) const {
// Task IDs are currently not used in the tests. -1 signals an unknown Task
// ID.
NOTREACHED() << "Task IDs are not used for Placeholder Tabs";
}
int64_t PlaceholderTabDelegate::GetRootTaskIdForNavigationId(int nav_id) const {
// Task IDs are currently not used in the tests. -1 signals an unknown Task
// ID.
NOTREACHED() << "Task IDs are not used for Placeholder Tabs";
}
TestSyncedWindowDelegate::TestSyncedWindowDelegate(
SessionID window_id,
sync_pb::SyncEnums_BrowserType type)
: window_id_(window_id),
window_type_(type),
is_session_restore_in_progress_(false) {}
TestSyncedWindowDelegate::~TestSyncedWindowDelegate() = default;
void TestSyncedWindowDelegate::OverrideTabAt(int index,
SyncedTabDelegate* delegate) {
if (index >= static_cast<int>(tab_delegates_.size())) {
tab_delegates_.resize(index + 1, nullptr);
}
tab_delegates_[index] = delegate;
}
void TestSyncedWindowDelegate::CloseTab(SessionID tab_id) {
std::erase_if(tab_delegates_, [tab_id](SyncedTabDelegate* tab) {
return tab->GetSessionId() == tab_id;
});
}
void TestSyncedWindowDelegate::SetIsSessionRestoreInProgress(bool value) {
is_session_restore_in_progress_ = value;
}
bool TestSyncedWindowDelegate::HasWindow() const {
return true;
}
SessionID TestSyncedWindowDelegate::GetSessionId() const {
return window_id_;
}
int TestSyncedWindowDelegate::GetTabCount() const {
return tab_delegates_.size();
}
bool TestSyncedWindowDelegate::IsTypeNormal() const {
return window_type_ == sync_pb::SyncEnums_BrowserType_TYPE_TABBED;
}
bool TestSyncedWindowDelegate::IsTypePopup() const {
return window_type_ == sync_pb::SyncEnums_BrowserType_TYPE_POPUP;
}
bool TestSyncedWindowDelegate::IsTabPinned(const SyncedTabDelegate* tab) const {
return false;
}
SyncedTabDelegate* TestSyncedWindowDelegate::GetTabAt(int index) const {
if (index >= static_cast<int>(tab_delegates_.size())) {
return nullptr;
}
return tab_delegates_[index];
}
SessionID TestSyncedWindowDelegate::GetTabIdAt(int index) const {
SyncedTabDelegate* delegate = GetTabAt(index);
if (!delegate) {
return SessionID::InvalidValue();
}
return delegate->GetSessionId();
}
bool TestSyncedWindowDelegate::IsSessionRestoreInProgress() const {
return is_session_restore_in_progress_;
}
bool TestSyncedWindowDelegate::ShouldSync() const {
return true;
}
TestSyncedWindowDelegatesGetter::TestSyncedWindowDelegatesGetter() = default;
TestSyncedWindowDelegatesGetter::~TestSyncedWindowDelegatesGetter() = default;
void TestSyncedWindowDelegatesGetter::ResetWindows() {
delegates_.clear();
windows_.clear();
}
TestSyncedWindowDelegate* TestSyncedWindowDelegatesGetter::AddWindow(
sync_pb::SyncEnums_BrowserType type,
SessionID window_id) {
windows_.push_back(
std::make_unique<TestSyncedWindowDelegate>(window_id, type));
CHECK_EQ(window_id, windows_.back()->GetSessionId());
delegates_[window_id] = windows_.back().get();
return windows_.back().get();
}
TestSyncedTabDelegate* TestSyncedWindowDelegatesGetter::AddTab(
SessionID window_id,
SessionID tab_id) {
tabs_.push_back(std::make_unique<TestSyncedTabDelegate>(
window_id, tab_id,
base::BindRepeating(&TestRouter::NotifyNav, base::Unretained(&router_))));
for (auto& window : windows_) {
if (window->GetSessionId() == window_id) {
int tab_index = window->GetTabCount();
window->OverrideTabAt(tab_index, tabs_.back().get());
}
}
// Simulate the browser firing a tab parented notification, ahead of actual
// navigations.
router_.NotifyNav(tabs_.back().get());
return tabs_.back().get();
}
void TestSyncedWindowDelegatesGetter::CloseTab(SessionID tab_id) {
for (auto& window : windows_) {
// CloseTab() will only take effect with the belonging window, the rest will
// simply ignore the call.
window->CloseTab(tab_id);
}
}
void TestSyncedWindowDelegatesGetter::SessionRestoreComplete() {
for (auto& window : windows_) {
window->SetIsSessionRestoreInProgress(false);
}
router_.NotifySessionRestoreComplete();
}
LocalSessionEventRouter* TestSyncedWindowDelegatesGetter::router() {
return &router_;
}
SyncedWindowDelegatesGetter::SyncedWindowDelegateMap
TestSyncedWindowDelegatesGetter::GetSyncedWindowDelegates() {
return delegates_;
}
const SyncedWindowDelegate* TestSyncedWindowDelegatesGetter::FindById(
SessionID session_id) {
for (const auto& [window_id, delegate] : delegates_) {
if (delegate->GetSessionId() == session_id) {
return delegate;
}
}
return nullptr;
}
TestSyncedWindowDelegatesGetter::TestRouter::TestRouter() = default;
TestSyncedWindowDelegatesGetter::TestRouter::~TestRouter() = default;
void TestSyncedWindowDelegatesGetter::TestRouter::StartRoutingTo(
LocalSessionEventHandler* handler) {
handler_ = handler;
}
void TestSyncedWindowDelegatesGetter::TestRouter::Stop() {
handler_ = nullptr;
}
void TestSyncedWindowDelegatesGetter::TestRouter::NotifyNav(
SyncedTabDelegate* tab) {
if (handler_) {
handler_->OnLocalTabModified(tab);
}
}
void TestSyncedWindowDelegatesGetter::TestRouter::
NotifySessionRestoreComplete() {
if (handler_) {
handler_->OnSessionRestoreComplete();
}
}
} // namespace sync_sessions
|