Skip to content

Commit

Permalink
[ios] Move synced_sessions to ios/c/b/synced_sessions and refactor
Browse files Browse the repository at this point in the history
synced_sessions structs and classes in ios/c/b/ui/recent_tabs will be
useful to subsequent features and they need to access these data types
from the model layer. Moving it to ios/c/b.

Aside from `OpenDistantTabsInBackground`, a new method that opens a
specific tab is added for the benefit of callers that do not own the
distant tabs vector.

Also all synced_sessions data types used to be exposed in one header.
Splitting them into three headers.

Bug: 1418121
Change-Id: I1307068333ab46917218b1f3b7f5cfc1fa626518
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4355950
Auto-Submit: Ginny Huang <ginnyhuang@chromium.org>
Reviewed-by: Mark Cogan <marq@chromium.org>
Commit-Queue: Mark Cogan <marq@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1121153}
  • Loading branch information
ginnnnnnny authored and Chromium LUCI CQ committed Mar 23, 2023
1 parent 600c74e commit a834e82
Show file tree
Hide file tree
Showing 31 changed files with 525 additions and 399 deletions.
31 changes: 31 additions & 0 deletions ios/chrome/browser/synced_sessions/BUILD.gn
@@ -0,0 +1,31 @@
# Copyright 2023 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

source_set("synced_sessions") {
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
"distant_session.h",
"distant_session.mm",
"distant_tab.h",
"distant_tab.mm",
"synced_sessions.h",
"synced_sessions.mm",
"synced_sessions_bridge.h",
"synced_sessions_bridge.mm",
"synced_sessions_util.h",
"synced_sessions_util.mm",
]
deps = [
"//components/sessions",
"//components/signin/public/identity_manager",
"//components/sync",
"//components/sync_device_info",
"//ios/chrome/browser/signin",
"//ios/chrome/browser/sync",
"//ios/chrome/browser/url_loading",
"//ios/chrome/browser/url_loading:url_loading_params_header",
"//third_party/abseil-cpp:absl",
]
public_deps = [ "//components/sync_sessions" ]
}
59 changes: 59 additions & 0 deletions ios/chrome/browser/synced_sessions/distant_session.h
@@ -0,0 +1,59 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef IOS_CHROME_BROWSER_SYNCED_SESSIONS_DISTANT_SESSION_H_
#define IOS_CHROME_BROWSER_SYNCED_SESSIONS_DISTANT_SESSION_H_

#import <string>

#import "components/sync_device_info/device_info.h"
#import "ios/chrome/browser/synced_sessions/distant_tab.h"

namespace base {
class Time;
} // namespace base

namespace sync_sessions {
class OpenTabsUIDelegate;
class SessionSyncService;
struct SyncedSession;
} // namespace sync_sessions

namespace synced_sessions {

// Data holder that contains the data of the distant sessions and their tabs to
// show in the UI.
struct DistantSession {
DistantSession();
// Initializes with the session tagged with `tag` and obtained with
// `sync_service`. `sync_service` must not be null.
DistantSession(sync_sessions::SessionSyncService* sync_service,
const std::string& tag);

DistantSession(const DistantSession&) = delete;
DistantSession& operator=(const DistantSession&) = delete;

~DistantSession();

// Loads information from `synced_session` to a distant session using
// `open_tabs_delegate`.
void InitWithSyncedSession(
const sync_sessions::SyncedSession* synced_session,
sync_sessions::OpenTabsUIDelegate* open_tabs_delegate);

// Unique identifier of a session.
std::string tag;
// Session name.
std::string name;
// Time the session is last modified.
base::Time modified_time;
// A list of tabs opened in this session.
DistantTabVector tabs;
// The form factor of the device in which the session is created.
syncer::DeviceInfo::FormFactor form_factor;
};

} // namespace synced_sessions

#endif // IOS_CHROME_BROWSER_SYNCED_SESSIONS_DISTANT_SESSION_H_
87 changes: 87 additions & 0 deletions ios/chrome/browser/synced_sessions/distant_session.mm
@@ -0,0 +1,87 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ios/chrome/browser/synced_sessions/distant_session.h"

#import "base/strings/utf_string_conversions.h"
#import "components/sync_sessions/open_tabs_ui_delegate.h"
#import "components/sync_sessions/session_sync_service.h"
#import "components/sync_sessions/synced_session.h"
#import "ios/chrome/browser/synced_sessions/synced_sessions.h"

#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif

namespace {

// Helper to extract the relevant content from a SessionTab and add it to a
// DistantSession.
void AddTabToDistantSession(const sessions::SessionTab& session_tab,
const std::string& session_tag,
synced_sessions::DistantSession* distant_session) {
if (session_tab.navigations.size() > 0) {
distant_session->tabs.push_back(
std::make_unique<synced_sessions::DistantTab>());
synced_sessions::DistantTab& distant_tab = *distant_session->tabs.back();
distant_tab.session_tag = session_tag;
distant_tab.tab_id = session_tab.tab_id;
int index = session_tab.current_navigation_index;
if (index < 0) {
index = 0;
}
if (index > (int)session_tab.navigations.size() - 1) {
index = session_tab.navigations.size() - 1;
}
const sessions::SerializedNavigationEntry* navigation =
&session_tab.navigations[index];
distant_tab.title = navigation->title();
distant_tab.virtual_url = navigation->virtual_url();
if (distant_tab.title.empty()) {
std::string url = navigation->virtual_url().spec();
distant_tab.title = base::UTF8ToUTF16(url);
}
}
}

} // namespace

namespace synced_sessions {

DistantSession::DistantSession() = default;

DistantSession::DistantSession(sync_sessions::SessionSyncService* sync_service,
const std::string& tag) {
sync_sessions::OpenTabsUIDelegate* open_tabs =
sync_service->GetOpenTabsUIDelegate();

if (open_tabs) {
std::vector<const sync_sessions::SyncedSession*> sessions;
open_tabs->GetAllForeignSessions(&sessions);
for (const auto* session : sessions) {
if (tag == session->GetSessionTag()) {
this->InitWithSyncedSession(session, open_tabs);
}
}
}
}

DistantSession::~DistantSession() = default;

void DistantSession::InitWithSyncedSession(
const sync_sessions::SyncedSession* synced_session,
sync_sessions::OpenTabsUIDelegate* open_tabs_delegate) {
tag = synced_session->GetSessionTag();
name = synced_session->GetSessionName();
modified_time = synced_session->GetModifiedTime();
form_factor = synced_session->GetDeviceFormFactor();

std::vector<const sessions::SessionTab*> open_tabs;
open_tabs_delegate->GetForeignSessionTabs(tag, &open_tabs);
for (const sessions::SessionTab* session_tab : open_tabs) {
AddTabToDistantSession(*session_tab, tag, this);
}
}

} // namespace synced_sessions
59 changes: 59 additions & 0 deletions ios/chrome/browser/synced_sessions/distant_tab.h
@@ -0,0 +1,59 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef IOS_CHROME_BROWSER_SYNCED_SESSIONS_DISTANT_TAB_H_
#define IOS_CHROME_BROWSER_SYNCED_SESSIONS_DISTANT_TAB_H_

#import <string>
#import <vector>

#import "components/sessions/core/session_id.h"
#import "third_party/abseil-cpp/absl/types/optional.h"
#import "url/gurl.h"

namespace synced_sessions {

// Data holder that contains the data of the distant tabs to show in the UI.
struct DistantTab {
DistantTab();

DistantTab(const DistantTab&) = delete;
DistantTab& operator=(const DistantTab&) = delete;

// Uniquely identifies the distant session this DistantTab belongs to.
std::string session_tag;
// Uniquely identifies this tab in its distant session.
SessionID tab_id;
// The title of the page shown in this DistantTab.
std::u16string title;
// The url shown in this DistantTab.
GURL virtual_url;
// Returns a hash the fields `virtual_url` and `title`.
// By design, two tabs in the same distant session can have the same
// `hashOfUserVisibleProperties`.
size_t hashOfUserVisibleProperties();
};

// A list of DistantTab objects.
using DistantTabVector = std::vector<std::unique_ptr<DistantTab>>;

// Data holder that contains a set of distant tabs to show in the UI.
struct DistantTabsSet {
DistantTabsSet();
~DistantTabsSet();

DistantTabsSet(const DistantTabsSet&);

// The tag of the DistantSession which owns the tabs referenced in `tabs`.
std::string session_tag;
// A selection of `DistantTab`s from the session with tag `session_tag`. A
// null value for `filtered_tabs` represents that the session's tabs are
// not filtered. This shortcut representation prevents having to copy over
// pointers to each tab within a session when every tab is included.
absl::optional<std::vector<DistantTab*>> filtered_tabs;
};

} // namespace synced_sessions

#endif // IOS_CHROME_BROWSER_SYNCED_SESSIONS_DISTANT_TAB_H_
29 changes: 29 additions & 0 deletions ios/chrome/browser/synced_sessions/distant_tab.mm
@@ -0,0 +1,29 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ios/chrome/browser/synced_sessions/distant_tab.h"

#import "components/sync_sessions/open_tabs_ui_delegate.h"

#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif

namespace synced_sessions {

DistantTab::DistantTab() : tab_id(SessionID::InvalidValue()) {}

size_t DistantTab::hashOfUserVisibleProperties() {
std::stringstream ss;
ss << title << std::endl << virtual_url.spec();
return std::hash<std::string>()(ss.str());
}

DistantTabsSet::DistantTabsSet() = default;

DistantTabsSet::~DistantTabsSet() = default;

DistantTabsSet::DistantTabsSet(const DistantTabsSet&) = default;

} // namespace synced_sessions
48 changes: 48 additions & 0 deletions ios/chrome/browser/synced_sessions/synced_sessions.h
@@ -0,0 +1,48 @@
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef IOS_CHROME_BROWSER_SYNCED_SESSIONS_SYNCED_SESSIONS_H_
#define IOS_CHROME_BROWSER_SYNCED_SESSIONS_SYNCED_SESSIONS_H_

#import <memory>
#import <string>
#import <vector>

namespace sync_sessions {
class SessionSyncService;
}

namespace synced_sessions {

struct DistantSession;

// Class containing distant sessions.
class SyncedSessions {
public:
// Initializes with no distant sessions.
SyncedSessions();
// Initializes with all the distant sessions obtained from `sync_service`.
// `sync_service` must not be null.
explicit SyncedSessions(sync_sessions::SessionSyncService* sync_service);

SyncedSessions(const SyncedSessions&) = delete;
SyncedSessions& operator=(const SyncedSessions&) = delete;

~SyncedSessions();
// Returns the distant session with `index` in the list of sessions.
DistantSession const* GetSession(size_t index) const;
// Returns the session with the unique identifier `tag`.
DistantSession const* GetSessionWithTag(const std::string& tag) const;
// Returns the number of distant sessions.
size_t GetSessionCount() const;
// Removes the session with the unique identifier `tag` from this session.
void EraseSessionWithTag(const std::string& tag);

private:
std::vector<std::unique_ptr<const DistantSession>> sessions_;
};

} // namespace synced_sessions

#endif // IOS_CHROME_BROWSER_SYNCED_SESSIONS_SYNCED_SESSIONS_H_

0 comments on commit a834e82

Please sign in to comment.