Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(576)

Side by Side Diff: extensions/browser/api/media_perception_private/media_perception_api_manager.cc

Issue 2858353002: MediaPerceptionPrivate API impl and testing. (Closed)
Patch Set: Addressed comments and all tests passing. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "extensions/browser/api/media_perception_private/media_perception_api_m anager.h"
6
7 #include "base/lazy_instance.h"
8 #include "chromeos/dbus/dbus_thread_manager.h"
9 #include "chromeos/dbus/media_analytics_client.h"
10 #include "chromeos/dbus/upstart_client.h"
11 #include "extensions/browser/api/media_perception_private/conversion_utils.h"
12 #include "extensions/browser/event_router.h"
13 #include "extensions/browser/extension_function.h"
14
15 namespace media_perception = extensions::api::media_perception_private;
16
17 namespace extensions {
18
19 // static
20 MediaPerceptionAPIManager* MediaPerceptionAPIManager::Get(
21 content::BrowserContext* context) {
22 return GetFactoryInstance()->Get(context);
23 }
24
25 static base::LazyInstance<
26 BrowserContextKeyedAPIFactory<MediaPerceptionAPIManager>>::Leaky g_factory =
27 LAZY_INSTANCE_INITIALIZER;
28
29 // static
30 BrowserContextKeyedAPIFactory<MediaPerceptionAPIManager>*
31 MediaPerceptionAPIManager::GetFactoryInstance() {
32 return g_factory.Pointer();
33 }
34
35 MediaPerceptionAPIManager::MediaPerceptionAPIManager(
36 content::BrowserContext* context)
37 : browser_context_(context),
38 analytics_process_state_(AnalyticsProcessState::IDLE),
39 weak_ptr_factory_(this) {
40 chromeos::MediaAnalyticsClient* dbus_client =
41 chromeos::DBusThreadManager::Get()->GetMediaAnalyticsClient();
42 dbus_client->SetMediaPerceptionSignalHandler(
43 base::Bind(&MediaPerceptionAPIManager::MediaPerceptionSignalHandler,
44 weak_ptr_factory_.GetWeakPtr()));
45 }
46
47 MediaPerceptionAPIManager::~MediaPerceptionAPIManager() {
48 chromeos::MediaAnalyticsClient* dbus_client =
49 chromeos::DBusThreadManager::Get()->GetMediaAnalyticsClient();
50 dbus_client->ClearMediaPerceptionSignalHandler();
51 // Stop the separate media analytics process.
52 chromeos::UpstartClient* upstart_client =
53 chromeos::DBusThreadManager::Get()->GetUpstartClient();
54 upstart_client->StopMediaAnalytics();
55 }
56
57 void MediaPerceptionAPIManager::GetState(const APIStateCallback& callback) {
58 if (analytics_process_state_ == AnalyticsProcessState::RUNNING) {
59 chromeos::MediaAnalyticsClient* dbus_client =
60 chromeos::DBusThreadManager::Get()->GetMediaAnalyticsClient();
61 dbus_client->GetState(base::Bind(&MediaPerceptionAPIManager::StateCallback,
62 weak_ptr_factory_.GetWeakPtr(), callback));
63 return;
64 }
65
66 if (analytics_process_state_ == AnalyticsProcessState::LAUNCHING) {
tbarzic 2017/05/17 21:04:42 this could probably be uninitialized as well
Luke Sorenson 2017/05/17 23:07:49 Prefer PROCESS_LAUNCING_ERROR because then the fro
67 callback.Run(CallbackStatus::PROCESS_LAUNCHING_ERROR,
68 media_perception::State());
69 return;
70 }
71
72 // Calling getState with process not running returns State UNINITIALIZED.
73 media_perception::State state_unitialized;
74 state_unitialized.status = media_perception::STATUS_UNINITIALIZED;
75 callback.Run(CallbackStatus::SUCCESS, std::move(state_unitialized));
76 }
77
78 void MediaPerceptionAPIManager::SetState(const media_perception::State& state,
79 const APIStateCallback& callback) {
80 mri::State state_proto = StateIdlToProto(state);
81 DCHECK(state_proto.status() == mri::State::RUNNING ||
82 state_proto.status() == mri::State::SUSPENDED)
83 << "Cannot set state to something other than RUNNING or SUSPENDED.";
tbarzic 2017/05/17 21:04:42 nit: new line here
Luke Sorenson 2017/05/17 23:07:49 Done.
84 if (analytics_process_state_ == AnalyticsProcessState::RUNNING) {
85 SetStateInternal(callback, state_proto);
86 return;
87 }
88
89 if (analytics_process_state_ == AnalyticsProcessState::LAUNCHING) {
90 callback.Run(CallbackStatus::PROCESS_LAUNCHING_ERROR,
91 media_perception::State());
92 return;
93 }
94
95 // Analytics process is in state IDLE.
96 if (state_proto.status() == mri::State::RUNNING) {
97 analytics_process_state_ = AnalyticsProcessState::LAUNCHING;
98 chromeos::UpstartClient* dbus_client =
99 chromeos::DBusThreadManager::Get()->GetUpstartClient();
100 dbus_client->StartMediaAnalytics(
101 base::Bind(&MediaPerceptionAPIManager::UpstartCallback,
102 weak_ptr_factory_.GetWeakPtr(), callback, state_proto));
103 return;
104 }
105
106 callback.Run(CallbackStatus::PROCESS_IDLE_ERROR, media_perception::State());
107 }
108
109 void MediaPerceptionAPIManager::SetStateInternal(
110 const APIStateCallback& callback,
111 const mri::State& state) {
112 chromeos::MediaAnalyticsClient* dbus_client =
113 chromeos::DBusThreadManager::Get()->GetMediaAnalyticsClient();
114 dbus_client->SetState(state,
115 base::Bind(&MediaPerceptionAPIManager::StateCallback,
116 weak_ptr_factory_.GetWeakPtr(), callback));
117 }
118
119 void MediaPerceptionAPIManager::GetDiagnostics(
120 const APIGetDiagnosticsCallback& callback) {
121 chromeos::MediaAnalyticsClient* dbus_client =
122 chromeos::DBusThreadManager::Get()->GetMediaAnalyticsClient();
123 dbus_client->GetDiagnostics(
124 base::Bind(&MediaPerceptionAPIManager::GetDiagnosticsCallback,
125 weak_ptr_factory_.GetWeakPtr(), callback));
126 }
127
128 void MediaPerceptionAPIManager::UpstartCallback(
129 const APIStateCallback& callback,
130 const mri::State& state,
131 bool succeeded) {
132 if (!succeeded) {
133 analytics_process_state_ = AnalyticsProcessState::IDLE;
134 LOG(ERROR) << "Failed to start media analytics process via Upstart.";
135 callback.Run(CallbackStatus::PROCESS_IDLE_ERROR, media_perception::State());
136 return;
137 }
138 analytics_process_state_ = AnalyticsProcessState::RUNNING;
139 SetStateInternal(callback, state);
140 }
141
142 void MediaPerceptionAPIManager::StateCallback(const APIStateCallback& callback,
143 bool succeeded,
144 const mri::State& state_proto) {
145 media_perception::State state;
146 if (!succeeded) {
147 callback.Run(CallbackStatus::DBUS_ERROR, media_perception::State());
148 return;
149 }
150 callback.Run(CallbackStatus::SUCCESS, StateProtoToIdl(state_proto));
151 }
152
153 void MediaPerceptionAPIManager::GetDiagnosticsCallback(
154 const APIGetDiagnosticsCallback& callback,
155 bool succeeded,
156 const mri::Diagnostics& diagnostics_proto) {
157 if (!succeeded) {
158 callback.Run(CallbackStatus::DBUS_ERROR, media_perception::Diagnostics());
159 return;
160 }
161 callback.Run(CallbackStatus::SUCCESS,
162 DiagnosticsProtoToIdl(diagnostics_proto));
163 }
164
165 void MediaPerceptionAPIManager::MediaPerceptionSignalHandler(
166 const mri::MediaPerception& media_perception_proto) {
167 EventRouter* router = EventRouter::Get(browser_context_);
168 DCHECK(router) << "EventRouter is null.";
169
170 media_perception::MediaPerception media_perception =
171 MediaPerceptionProtoToIdl(media_perception_proto);
172 std::unique_ptr<Event> event(
173 new Event(events::MEDIA_PERCEPTION_PRIVATE_ON_MEDIA_PERCEPTION,
174 media_perception::OnMediaPerception::kEventName,
175 media_perception::OnMediaPerception::Create(media_perception)));
176 router->BroadcastEvent(std::move(event));
177 }
178
179 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698