File: collaboration_messaging_observer.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (355 lines) | stat: -rw-r--r-- 12,714 bytes parent folder | download | duplicates (5)
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
// Copyright 2024 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/tabs/saved_tab_groups/collaboration_messaging_observer.h"

#include <set>

#include "base/uuid.h"
#include "chrome/browser/collaboration/collaboration_service_factory.h"
#include "chrome/browser/collaboration/messaging/messaging_backend_service_factory.h"
#include "chrome/browser/tab_group_sync/tab_group_sync_service_factory.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/tabs/saved_tab_groups/collaboration_messaging_tab_data.h"
#include "chrome/browser/ui/tabs/saved_tab_groups/saved_tab_group_metrics.h"
#include "chrome/browser/ui/tabs/saved_tab_groups/saved_tab_group_utils.h"
#include "chrome/browser/ui/tabs/saved_tab_groups/tab_group_action_context_desktop.h"
#include "chrome/browser/ui/tabs/tab_group_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/data_sharing/collaboration_controller_delegate_desktop.h"
#include "chrome/browser/ui/views/data_sharing/data_sharing_bubble_controller.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "components/collaboration/public/collaboration_flow_entry_point.h"
#include "components/collaboration/public/collaboration_service.h"
#include "components/saved_tab_groups/public/tab_group_sync_service.h"
#include "components/saved_tab_groups/public/types.h"
#include "components/tabs/public/tab_group.h"

using collaboration::messaging::MessagingBackendServiceFactory;

namespace tab_groups {
namespace {

// Get the TabStrip that contains this group ID.
TabStrip* GetTabStripWithGroup(LocalTabGroupID local_tab_group_id) {
  const Browser* const browser_with_local_group_id =
      SavedTabGroupUtils::GetBrowserWithTabGroupId(local_tab_group_id);
  if (!browser_with_local_group_id) {
    return nullptr;
  }

  auto* browser_view =
      BrowserView::GetBrowserViewForBrowser(browser_with_local_group_id);
  if (!browser_view) {
    return nullptr;
  }

  auto* tab_strip = browser_view->tabstrip();
  if (!tab_strip) {
    return nullptr;
  }

  return tab_strip;
}

// Returns the local tab group ID from the PersistentMessage.
std::optional<LocalTabGroupID> UnwrapTabGroupID(PersistentMessage message) {
  auto tab_group_metadata = message.attribution.tab_group_metadata;
  if (tab_group_metadata.has_value()) {
    return tab_group_metadata->local_tab_group_id;
  }
  return std::nullopt;
}

// Returns the tab strip index of the tab. If the sync service is not
// available, returns std::nullopt. Asserts that the tab can be found.
std::optional<int> GetTabStripIndex(LocalTabID local_tab_id,
                                    LocalTabGroupID local_tab_group_id) {
  const Browser* const browser_with_local_group_id =
      SavedTabGroupUtils::GetBrowserWithTabGroupId(local_tab_group_id);
  if (!browser_with_local_group_id) {
    return std::nullopt;
  }

  TabStripModel* tab_strip_model =
      browser_with_local_group_id->tab_strip_model();
  if (!tab_strip_model || !tab_strip_model->SupportsTabGroups()) {
    return std::nullopt;
  }

  const gfx::Range tab_indices = tab_strip_model->group_model()
                                     ->GetTabGroup(local_tab_group_id)
                                     ->ListTabs();
  for (size_t grouped_tab_index = tab_indices.start();
       grouped_tab_index < tab_indices.end(); grouped_tab_index++) {
    const tabs::TabInterface* const tab =
        tab_strip_model->GetTabAtIndex(grouped_tab_index);
    if (tab->GetHandle().raw_value() == local_tab_id) {
      return grouped_tab_index;
    }
  }

  return std::nullopt;
}

// Unwrapped tab information used to access the tab.
struct TabInfo {
  LocalTabID local_tab_id;
  LocalTabGroupID local_tab_group_id;
  int tabstrip_index;
};

// Returns the unwrapped tab information from the PersistentMessage.
// Asserts
// * tab metadata exists and contains the tab ID
// * tab group metadata exists and contains the group ID
// * the tab can be found in the tab strip
std::optional<TabInfo> UnwrapTabInfo(PersistentMessage message) {
  auto local_tab_group_id = UnwrapTabGroupID(message);
  if (!local_tab_group_id.has_value()) {
    return std::nullopt;
  }

  auto tab_metadata = message.attribution.tab_metadata;
  if (!tab_metadata.has_value()) {
    return std::nullopt;
  }

  auto local_tab_id = tab_metadata->local_tab_id;
  if (!local_tab_id.has_value()) {
    return std::nullopt;
  }

  auto tabstrip_index =
      GetTabStripIndex(local_tab_id.value(), local_tab_group_id.value());
  if (!tabstrip_index.has_value()) {
    return std::nullopt;
  }

  return TabInfo(local_tab_id.value(), local_tab_group_id.value(),
                 tabstrip_index.value());
}

}  // namespace

void CollaborationMessagingObserver::HandleDirtyTabGroup(
    PersistentMessage message,
    MessageDisplayStatus display) {
  // TODO(crbug.com/392604409): Refactor to use a TabGroupFeature.
  std::optional<LocalTabGroupID> local_tab_group_id = UnwrapTabGroupID(message);
  if (!local_tab_group_id) {
    return;
  }

  if (TabStrip* tabstrip = GetTabStripWithGroup(local_tab_group_id.value())) {
    tabstrip->SetTabGroupNeedsAttention(
        local_tab_group_id.value(), display == MessageDisplayStatus::kDisplay);
  }
}

void CollaborationMessagingObserver::HandleDirtyTab(
    PersistentMessage message,
    MessageDisplayStatus display) {
  // TODO(crbug.com/392604409): Refactor to use a TabFeature.
  std::optional<TabInfo> tab_info = UnwrapTabInfo(message);
  if (!tab_info) {
    return;
  }

  if (TabStrip* tabstrip = GetTabStripWithGroup(tab_info->local_tab_group_id)) {
    tabstrip->SetTabNeedsAttention(tab_info->tabstrip_index,
                                   display == MessageDisplayStatus::kDisplay);
  }
}

void CollaborationMessagingObserver::HandleChip(PersistentMessage message,
                                                MessageDisplayStatus display) {
  std::optional<TabInfo> tab_info = UnwrapTabInfo(message);
  if (!tab_info.has_value()) {
    return;
  }

  if (tabs::TabInterface* tab = SavedTabGroupUtils::GetGroupedTab(
          tab_info->local_tab_group_id, tab_info->local_tab_id)) {
    if (display == MessageDisplayStatus::kDisplay) {
      tab->GetTabFeatures()->collaboration_messaging_tab_data()->SetMessage(
          message);
    } else {
      tab->GetTabFeatures()->collaboration_messaging_tab_data()->ClearMessage(
          message);
    }
  }
}

void CollaborationMessagingObserver::DispatchMessage(
    PersistentMessage message,
    MessageDisplayStatus display) {
  using collaboration::messaging::PersistentNotificationType;
  switch (message.type) {
    case PersistentNotificationType::DIRTY_TAB_GROUP:
      HandleDirtyTabGroup(message, display);
      break;
    case PersistentNotificationType::DIRTY_TAB:
      HandleDirtyTab(message, display);
      break;
    case PersistentNotificationType::CHIP:
      HandleChip(message, display);
      break;
    case PersistentNotificationType::TOMBSTONED:
    case PersistentNotificationType::INSTANT_MESSAGE:
    case PersistentNotificationType::UNDEFINED:
      // These notifications have no associated UI on Desktop.
      // Ignore gracefully.
      return;
  }
}

CollaborationMessagingObserver::CollaborationMessagingObserver(Profile* profile)
    : profile_(profile), instant_message_queue_processor_(profile) {
  // This observer is disabled when Shared Tab Groups is not supported.
  if (!tab_groups::SavedTabGroupUtils::SupportsSharedTabGroups()) {
    return;
  }

  service_ = MessagingBackendServiceFactory::GetForProfile(profile_);
  CHECK(service_);

  persistent_message_service_observation_.Observe(service_);
  service_->SetInstantMessageDelegate(this);
}

CollaborationMessagingObserver::~CollaborationMessagingObserver() {
  if (service_) {
    service_->SetInstantMessageDelegate(nullptr);
  }
}

void CollaborationMessagingObserver::OnMessagingBackendServiceInitialized() {
  CHECK(service_);
  auto messages = service_->GetMessages(std::nullopt);
  for (const auto& message : messages) {
    DispatchMessage(message, MessageDisplayStatus::kDisplay);
  }
}

void CollaborationMessagingObserver::DisplayPersistentMessage(
    PersistentMessage message) {
  DispatchMessage(message, MessageDisplayStatus::kDisplay);
}

void CollaborationMessagingObserver::HidePersistentMessage(
    PersistentMessage message) {
  DispatchMessage(message, MessageDisplayStatus::kHide);
}

void CollaborationMessagingObserver::DispatchMessageForTests(
    PersistentMessage message,
    bool display) {
  CHECK(tab_groups::SavedTabGroupUtils::SupportsSharedTabGroups());
  DispatchMessage(message, display ? MessageDisplayStatus::kDisplay
                                   : MessageDisplayStatus::kHide);
}

void CollaborationMessagingObserver::DisplayInstantaneousMessage(
    InstantMessage message,
    InstantMessageSuccessCallback success_callback) {
  instant_message_queue_processor_.Enqueue(message,
                                           std::move(success_callback));
}

void CollaborationMessagingObserver::HideInstantaneousMessage(
    const std::set<base::Uuid>& message_ids) {
  // TODO(crbug.com/416265338): Implement this.
}

void CollaborationMessagingObserver::ReopenTabForCurrentInstantMessage() {
  CHECK(instant_message_queue_processor_.IsMessageShowing());

  const InstantMessage& message =
      instant_message_queue_processor_.GetCurrentMessage();
  const auto& attribution = message.attributions[0];
  auto tab_metadata = attribution.tab_metadata;
  auto tab_group_metadata = attribution.tab_group_metadata;
  if (!tab_metadata || !tab_group_metadata) {
    return;
  }

  std::optional<LocalTabGroupID> group_id =
      tab_group_metadata->local_tab_group_id;
  std::optional<std::string> tab_url = tab_metadata->last_known_url;
  if (!group_id.has_value() || !tab_url.has_value()) {
    return;
  }

  if (Browser* browser =
          tab_groups::SavedTabGroupUtils::GetBrowserWithTabGroupId(
              group_id.value())) {
    SavedTabGroupUtils::OpenTabInBrowser(
        GURL(tab_url.value()), browser, profile_,
        WindowOpenDisposition::NEW_BACKGROUND_TAB, std::nullopt,
        group_id.value());
  }
}

void CollaborationMessagingObserver::ManageSharingForCurrentInstantMessage(
    BrowserWindowInterface* current_browser_window_interface) {
  CHECK(instant_message_queue_processor_.IsMessageShowing());

  const InstantMessage& message =
      instant_message_queue_processor_.GetCurrentMessage();
  auto tab_group_metadata = message.attributions[0].tab_group_metadata;
  if (!tab_group_metadata) {
    return;
  }

  std::optional<LocalTabGroupID> group_id =
      tab_group_metadata->local_tab_group_id;
  if (!group_id.has_value()) {
    // No local group id means the group is not open.
    // Try to open the group first.
    std::optional<base::Uuid> sync_tab_group_id =
        tab_group_metadata->sync_tab_group_id;
    if (!sync_tab_group_id.has_value()) {
      return;
    }
    auto* tab_group_service =
        TabGroupSyncServiceFactory::GetForProfile(profile_);
    CHECK(tab_group_service);
    std::optional<LocalTabGroupID> opened_group_id =
        tab_group_service->OpenTabGroup(
            sync_tab_group_id.value(),
            std::make_unique<TabGroupActionContextDesktop>(
                current_browser_window_interface->GetBrowserForMigrationOnly(),
                OpeningSource::kOpenedFromToastAction));
    if (!opened_group_id) {
      return;
    }

    group_id = opened_group_id;
  }

  if (Browser* browser =
          tab_groups::SavedTabGroupUtils::GetBrowserWithTabGroupId(
              group_id.value())) {
    saved_tab_groups::metrics::RecordSharedTabGroupManageType(
        saved_tab_groups::metrics::SharedTabGroupManageTypeDesktop::
            kManageGroupFromUserJoinNotification);

    data_sharing::RequestInfo request_info(group_id.value(),
                                           data_sharing::FlowType::kManage);
    collaboration::CollaborationService* service =
        collaboration::CollaborationServiceFactory::GetForProfile(
            browser->profile());
    std::unique_ptr<CollaborationControllerDelegateDesktop> delegate =
        std::make_unique<CollaborationControllerDelegateDesktop>(browser);
    service->StartShareOrManageFlow(
        std::move(delegate), group_id.value(),
        collaboration::CollaborationServiceShareOrManageEntryPoint::
            kDesktopNotification);
  }
}

}  // namespace tab_groups