File: media_perception_api_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 (180 lines) | stat: -rw-r--r-- 7,420 bytes parent folder | download | duplicates (9)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_BROWSER_API_MEDIA_PERCEPTION_PRIVATE_MEDIA_PERCEPTION_API_MANAGER_H_
#define EXTENSIONS_BROWSER_API_MEDIA_PERCEPTION_PRIVATE_MEDIA_PERCEPTION_API_MANAGER_H_

#include <optional>
#include <string>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "chromeos/ash/components/dbus/media_analytics/media_analytics_client.h"
#include "chromeos/ash/components/dbus/media_perception/media_perception.pb.h"
#include "chromeos/services/media_perception/public/mojom/media_perception_service.mojom.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/common/api/media_perception_private.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/remote.h"

namespace extensions {

class MediaPerceptionAPIManager : public BrowserContextKeyedAPI,
                                  public ash::MediaAnalyticsClient::Observer {
 public:
  using APISetAnalyticsComponentCallback = base::OnceCallback<void(
      extensions::api::media_perception_private::ComponentState
          component_state)>;

  using APIComponentProcessStateCallback = base::OnceCallback<void(
      extensions::api::media_perception_private::ProcessState process_state)>;

  using APIStateCallback = base::OnceCallback<void(
      extensions::api::media_perception_private::State state)>;

  using APIGetDiagnosticsCallback = base::OnceCallback<void(
      extensions::api::media_perception_private::Diagnostics diagnostics)>;

  explicit MediaPerceptionAPIManager(content::BrowserContext* context);

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

  ~MediaPerceptionAPIManager() override;

  // Convenience method to get the MediaPeceptionAPIManager for a
  // BrowserContext.
  static MediaPerceptionAPIManager* Get(content::BrowserContext* context);

  // BrowserContextKeyedAPI implementation.
  static BrowserContextKeyedAPIFactory<MediaPerceptionAPIManager>*
  GetFactoryInstance();

  // Handler for clients of the API requesting a MediaPerception Mojo interface.
  void ActivateMediaPerception(
      mojo::PendingReceiver<chromeos::media_perception::mojom::MediaPerception>
          receiver);

  // Public functions for MediaPerceptionPrivateAPI implementation.
  void SetAnalyticsComponent(
      const extensions::api::media_perception_private::Component& component,
      APISetAnalyticsComponentCallback callback);
  void SetComponentProcessState(
      const extensions::api::media_perception_private::ProcessState&
          process_state,
      APIComponentProcessStateCallback callback);
  void GetState(APIStateCallback callback);
  void SetState(const extensions::api::media_perception_private::State& state,
                APIStateCallback callback);
  void GetDiagnostics(APIGetDiagnosticsCallback callback);

  // For testing purposes only. Allows the unittest to set the mount_point to
  // something non-empty.
  void SetMountPointNonEmptyForTesting();

 private:
  friend class BrowserContextKeyedAPIFactory<MediaPerceptionAPIManager>;

  class MediaPerceptionControllerClient;

  // BrowserContextKeyedAPI:
  static const char* service_name() { return "MediaPerceptionAPIManager"; }

  static const bool kServiceIsNULLWhileTesting = true;

  enum class AnalyticsProcessState {
    // The process is not running.
    IDLE,
    // The process has been launched via Upstart, but waiting for callback to
    // confirm.
    CHANGING_PROCESS_STATE,
    // The process is running.
    RUNNING,
    // The process state is unknown, e.g. when a Upstart Stop request fails.
    UNKNOWN
  };

  // Sets the state of the analytics process.
  void SetStateInternal(APIStateCallback callback, const mri::State& state);

  // MediaAnalyticsClient::Observer overrides.
  void OnDetectionSignal(const mri::MediaPerception& media_perception) override;

  // Callback for State D-Bus method calls to the media analytics process.
  void StateCallback(APIStateCallback callback,
                     std::optional<mri::State> state);

  // Callback for GetDiagnostics D-Bus method calls to the media analytics
  // process.
  void GetDiagnosticsCallback(APIGetDiagnosticsCallback callback,
                              std::optional<mri::Diagnostics> diagnostics);

  // Callbacks for Upstart command to start media analytics process.
  void UpstartStartProcessCallback(APIComponentProcessStateCallback callback,
                                   bool succeeded);

  // Sends a Mojo IPC message pipe invitation to the media analytics process.
  void SendMojoInvitation(APIComponentProcessStateCallback callback);

  // Callback for BootstrapMojoConnection D-Bus method calls to the media
  // analytics process.
  void OnBootstrapMojoConnection(APIComponentProcessStateCallback callback,
                                 bool succeeded);

  // This callback includes a mri::State so that the API manager can immediately
  // set the state of rtanalytics after start-up.
  void UpstartStartCallback(APIStateCallback callback,
                            const mri::State& state,
                            bool succeeded);

  // Callback for Upstart command to restart media analytics process.
  void UpstartRestartCallback(APIStateCallback callback, bool succeeded);

  // Callback for Upstart command to stop media analytics process.
  void UpstartStopProcessCallback(APIComponentProcessStateCallback callback,
                                  bool succeeded);

  void UpstartStopCallback(APIStateCallback callback, bool succeeded);

  // Callback with the mount point for a loaded component.
  void LoadComponentCallback(APISetAnalyticsComponentCallback callback,
                             const extensions::api::media_perception_private::
                                 ComponentInstallationError installation_error,
                             const base::FilePath& mount_point);

  bool ComponentIsLoaded();

  const raw_ptr<content::BrowserContext> browser_context_;

  // Keeps track of whether the analytics process is running so that it can be
  // started with an Upstart D-Bus method call if necessary.
  AnalyticsProcessState analytics_process_state_;

  // Keeps track of the mount point for the current media analytics process
  // component from component updater. If this string is not set, no component
  // is set.
  std::string mount_point_;

  // Pointer to the MediaPerceptionService interface for communicating with the
  // service over Mojo.
  mojo::Remote<chromeos::media_perception::mojom::MediaPerceptionService>
      media_perception_service_;

  mojo::Remote<chromeos::media_perception::mojom::MediaPerceptionController>
      media_perception_controller_;

  std::unique_ptr<MediaPerceptionControllerClient>
      media_perception_controller_client_;

  base::ScopedObservation<ash::MediaAnalyticsClient,
                          ash::MediaAnalyticsClient::Observer>
      scoped_observation_{this};
  base::WeakPtrFactory<MediaPerceptionAPIManager> weak_ptr_factory_{this};
};

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_API_MEDIA_PERCEPTION_PRIVATE_MEDIA_PERCEPTION_API_MANAGER_H_