File: messaging_backend_service_factory.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (118 lines) | stat: -rw-r--r-- 5,251 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
// 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/collaboration/messaging/messaging_backend_service_factory.h"

#include <memory>

#include "base/feature_list.h"
#include "base/no_destructor.h"
#include "build/build_config.h"
#include "chrome/browser/data_sharing/data_sharing_service_factory.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/tab_group_sync/feature_utils.h"
#include "chrome/browser/tab_group_sync/tab_group_sync_service_factory.h"
#include "components/collaboration/internal/messaging/configuration.h"
#include "components/collaboration/internal/messaging/data_sharing_change_notifier_impl.h"
#include "components/collaboration/internal/messaging/instant_message_processor_impl.h"
#include "components/collaboration/internal/messaging/messaging_backend_service_impl.h"
#include "components/collaboration/internal/messaging/storage/empty_messaging_backend_database.h"
#include "components/collaboration/internal/messaging/storage/messaging_backend_database_impl.h"
#include "components/collaboration/internal/messaging/storage/messaging_backend_store_impl.h"
#include "components/collaboration/internal/messaging/tab_group_change_notifier_impl.h"
#include "components/collaboration/public/features.h"
#include "components/collaboration/public/messaging/empty_messaging_backend_service.h"
#include "components/data_sharing/public/features.h"
#include "components/saved_tab_groups/public/features.h"
#include "components/saved_tab_groups/public/tab_group_sync_service.h"
#include "components/sync/model/data_type_store_service.h"

namespace collaboration::messaging {

// static
MessagingBackendServiceFactory* MessagingBackendServiceFactory::GetInstance() {
  static base::NoDestructor<MessagingBackendServiceFactory> instance;
  return instance.get();
}

// static
MessagingBackendService* MessagingBackendServiceFactory::GetForProfile(
    Profile* profile) {
  CHECK(profile);
  return static_cast<MessagingBackendService*>(
      GetInstance()->GetServiceForBrowserContext(profile, /*create=*/true));
}

MessagingBackendServiceFactory::MessagingBackendServiceFactory()
    : ProfileKeyedServiceFactory(
          "MessagingBackendService",
          ProfileSelections::Builder()
              .WithRegular(ProfileSelection::kOriginalOnly)
              .Build()) {
  DependsOn(tab_groups::TabGroupSyncServiceFactory::GetInstance());
  DependsOn(data_sharing::DataSharingServiceFactory::GetInstance());
  DependsOn(IdentityManagerFactory::GetInstance());
}

MessagingBackendServiceFactory::~MessagingBackendServiceFactory() = default;

std::unique_ptr<KeyedService>
MessagingBackendServiceFactory::BuildServiceInstanceForBrowserContext(
    content::BrowserContext* context) const {
  DCHECK(context);
  Profile* profile = static_cast<Profile*>(context);

  // This service requires the data sharing and tab group sync service features
  // to be enabled.
  if (!data_sharing::features::IsDataSharingFunctionalityEnabled() ||
      !tab_groups::IsTabGroupSyncEnabled(profile->GetPrefs()) ||
      !base::FeatureList::IsEnabled(
          collaboration::features::kCollaborationMessaging)) {
    return std::make_unique<EmptyMessagingBackendService>();
  }

  auto* tab_group_sync_service =
      tab_groups::TabGroupSyncServiceFactory::GetForProfile(profile);
  auto* data_sharing_service =
      data_sharing::DataSharingServiceFactory::GetForProfile(profile);
  auto* identity_manager = IdentityManagerFactory::GetForProfile(profile);
  auto tab_group_change_notifier = std::make_unique<TabGroupChangeNotifierImpl>(
      tab_group_sync_service, identity_manager);
  auto data_sharing_change_notifier =
      std::make_unique<DataSharingChangeNotifierImpl>(data_sharing_service);

  std::unique_ptr<MessagingBackendDatabase> messaging_backend_database;
  if (base::FeatureList::IsEnabled(
          collaboration::features::kCollaborationMessagingDatabase)) {
    messaging_backend_database =
        std::make_unique<MessagingBackendDatabaseImpl>(profile->GetPath());
  } else {
    messaging_backend_database =
        std::make_unique<EmptyMessagingBackendDatabase>();
  }

  auto messaging_backend_store = std::make_unique<MessagingBackendStoreImpl>(
      std::move(messaging_backend_database));
  auto instant_message_processor =
      std::make_unique<InstantMessageProcessorImpl>();

  // This configuration object allows us to control platform specific behavior.
  MessagingBackendConfiguration configuration;
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || \
    BUILDFLAG(IS_CHROMEOS)
  configuration.clear_chip_on_tab_selection = false;
#endif

  auto service = std::make_unique<MessagingBackendServiceImpl>(
      configuration, std::move(tab_group_change_notifier),
      std::move(data_sharing_change_notifier),
      std::move(messaging_backend_store), std::move(instant_message_processor),
      tab_group_sync_service, data_sharing_service, identity_manager);

  return std::move(service);
}

}  // namespace collaboration::messaging