File: connection_manager.h

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 (131 lines) | stat: -rw-r--r-- 5,075 bytes parent folder | download | duplicates (6)
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
// 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.

#ifndef CHROME_BROWSER_ASH_ANDROID_SMS_CONNECTION_MANAGER_H_
#define CHROME_BROWSER_ASH_ANDROID_SMS_CONNECTION_MANAGER_H_

#include <memory>

#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/ash/android_sms/android_sms_app_manager.h"
#include "chromeos/ash/services/multidevice_setup/public/cpp/multidevice_setup_client.h"
#include "content/public/browser/service_worker_context_observer.h"

class Profile;

namespace content {
class ServiceWorkerContext;
}  // namespace content

namespace ash {
namespace android_sms {

class ConnectionEstablisher;

// ConnectionManager checks to see when the user has no Android Messages for Web
// pages open (in a web page or PWA) and notifies the corresponding
// service worker to start a background connection.
//
// This class is an observer for Android Messages for Web service worker
// lifecycle events and uses ConnectionEstablisher to initiate a background
// connection with Tachyon server when appropriate. Specifically, in the
// following cases
// 1. Startup.
// 2. Activation: This occurs on service worker update, when a new updated
//    version of the service worker replaces the current active version or on
//    service worker cold start.
// 3. NoControllees: When all pages controlled by the service worker are closed.
// The connection establishment may be attempted in more cases than actually
// required. It's left to the service worker to ignore or establish a background
// connection as required. E.g., The service worker will not establish a
// connection if it's is already connected to the Android Messages for Web page
// or if a connection already exists.
class ConnectionManager
    : public content::ServiceWorkerContextObserver,
      public AndroidSmsAppManager::Observer,
      public multidevice_setup::MultiDeviceSetupClient::Observer {
 public:
  ConnectionManager(
      std::unique_ptr<ConnectionEstablisher> connection_establisher,
      Profile* profile,
      AndroidSmsAppManager* android_sms_app_manager,
      multidevice_setup::MultiDeviceSetupClient* multidevice_setup_client);

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

  ~ConnectionManager() override;

  // Sends a start connection message to the service worker.
  void StartConnection();

 private:
  friend class ConnectionManagerTest;

  // Thin wrapper for fetching ServiceWorkerContexts; overridden in tests.
  class ServiceWorkerProvider {
   public:
    ServiceWorkerProvider();

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

    virtual ~ServiceWorkerProvider();

    virtual content::ServiceWorkerContext* Get(const GURL& url,
                                               Profile* profile);
  };

  ConnectionManager(
      std::unique_ptr<ConnectionEstablisher> connection_establisher,
      Profile* profile,
      AndroidSmsAppManager* android_sms_app_manager,
      multidevice_setup::MultiDeviceSetupClient* multidevice_setup_client,
      std::unique_ptr<ServiceWorkerProvider> service_worker_provider);

  // content::ServiceWorkerContextObserver:
  void OnVersionActivated(int64_t version_id, const GURL& scope) override;
  void OnVersionRedundant(int64_t version_id, const GURL& scope) override;
  void OnNoControllees(int64_t version_id, const GURL& scope) override;

  // AndroidSmsAppManager::Observer:
  void OnInstalledAppUrlChanged() override;

  // multidevice_setup::MultideviceSetupClient::Observer:
  void OnFeatureStatesChanged(
      const multidevice_setup::MultiDeviceSetupClient::FeatureStatesMap&
          feature_state_map) override;

  void UpdateConnectionStatus();
  std::optional<GURL> GenerateEnabledPwaUrl();
  content::ServiceWorkerContext* GetCurrentServiceWorkerContext();

  void SetServiceWorkerProviderForTesting(
      std::unique_ptr<ServiceWorkerProvider> service_worker_provider);

  std::unique_ptr<ConnectionEstablisher> connection_establisher_;
  raw_ptr<Profile> profile_;
  raw_ptr<AndroidSmsAppManager> android_sms_app_manager_;
  raw_ptr<multidevice_setup::MultiDeviceSetupClient> multidevice_setup_client_;

  std::unique_ptr<ServiceWorkerProvider> service_worker_provider_;

  // Version ID of the Android Messages for Web service worker that's currently
  // active i.e., capable of handling messages and controlling pages.
  std::optional<int64_t> active_version_id_;

  // Version ID of the previously active Android Messages for Web
  // service worker.
  std::optional<int64_t> prev_active_version_id_;

  // The URL of the Android Messages PWA, if it is currently enabled. If the
  // feature is not currently enabled, this field is null.
  std::optional<GURL> enabled_pwa_url_;
};

}  // namespace android_sms
}  // namespace ash

#endif  // CHROME_BROWSER_ASH_ANDROID_SMS_CONNECTION_MANAGER_H_