OLD | NEW |
---|---|
(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_running_(false), | |
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 // Return uninitialized state if the media analytics process isn't running. | |
59 if (!analytics_process_running_) { | |
60 media_perception::State state_unitialized; | |
tbarzic
2017/05/11 00:38:27
can you add a test for this?
Luke Sorenson
2017/05/11 23:57:59
Done.
| |
61 state_unitialized.status = media_perception::STATUS_UNINITIALIZED; | |
62 callback.Run(true, std::move(state_unitialized)); | |
63 return; | |
64 } | |
65 chromeos::MediaAnalyticsClient* dbus_client = | |
66 chromeos::DBusThreadManager::Get()->GetMediaAnalyticsClient(); | |
67 dbus_client->GetState(base::Bind(&MediaPerceptionAPIManager::StateCallback, | |
68 weak_ptr_factory_.GetWeakPtr(), callback)); | |
69 } | |
70 | |
71 void MediaPerceptionAPIManager::SetState(const media_perception::State& state, | |
72 const APIStateCallback& callback) { | |
73 mri::State state_proto = StateIdlToProto(state); | |
74 DCHECK(state_proto.status() == mri::State::RUNNING || | |
75 state_proto.status() == mri::State::SUSPENDED) | |
76 << "Cannot set state to something other than RUNNING or SUSPENDED."; | |
77 if (!analytics_process_running_) { | |
tbarzic
2017/05/11 00:38:27
if (analytics_process_running_) {
SetStateIntern
Luke Sorenson
2017/05/11 23:57:59
Done.
| |
78 if (state_proto.status() == mri::State::RUNNING) { | |
79 chromeos::UpstartClient* dbus_client = | |
80 chromeos::DBusThreadManager::Get()->GetUpstartClient(); | |
81 dbus_client->StartMediaAnalytics( | |
tbarzic
2017/05/11 00:38:27
what if SetState is called before this returns?
e.
Luke Sorenson
2017/05/11 23:57:59
As the client of the API, we can be sure to wait f
tbarzic
2017/05/12 01:00:21
nope, let's avoid mutex variables.
you could chan
Luke Sorenson
2017/05/12 17:07:30
Done.
| |
82 base::Bind(&MediaPerceptionAPIManager::UpstartCallback, | |
83 weak_ptr_factory_.GetWeakPtr(), callback, state_proto)); | |
84 } else { | |
85 media_perception::State state; | |
86 state.status = media_perception::STATUS_UNINITIALIZED; | |
87 callback.Run(false, std::move(state)); | |
88 } | |
89 } else { | |
90 SetStateInternal(callback, state_proto); | |
91 } | |
92 } | |
93 | |
94 void MediaPerceptionAPIManager::SetStateInternal( | |
95 const APIStateCallback& callback, | |
96 const mri::State& state) { | |
97 chromeos::MediaAnalyticsClient* dbus_client = | |
98 chromeos::DBusThreadManager::Get()->GetMediaAnalyticsClient(); | |
99 dbus_client->SetState(state, | |
100 base::Bind(&MediaPerceptionAPIManager::StateCallback, | |
101 weak_ptr_factory_.GetWeakPtr(), callback)); | |
102 } | |
103 | |
104 void MediaPerceptionAPIManager::GetDiagnostics( | |
105 const APIGetDiagnosticsCallback& callback) { | |
106 chromeos::MediaAnalyticsClient* dbus_client = | |
107 chromeos::DBusThreadManager::Get()->GetMediaAnalyticsClient(); | |
108 dbus_client->GetDiagnostics( | |
109 base::Bind(&MediaPerceptionAPIManager::GetDiagnosticsCallback, | |
110 weak_ptr_factory_.GetWeakPtr(), callback)); | |
111 } | |
112 | |
113 void MediaPerceptionAPIManager::UpstartCallback( | |
114 const APIStateCallback& callback, | |
115 const mri::State& state, | |
116 bool succeeded) { | |
117 if (!succeeded) { | |
118 analytics_process_running_ = false; | |
tbarzic
2017/05/11 00:38:27
do you need this?
Luke Sorenson
2017/05/11 23:57:59
Done.
| |
119 LOG(ERROR) << "Failed to start media analytics process via Upstart."; | |
120 media_perception::State state; | |
121 state.status = media_perception::STATUS_UNINITIALIZED; | |
122 callback.Run(false, std::move(state)); | |
123 return; | |
124 } | |
125 analytics_process_running_ = true; | |
126 SetStateInternal(callback, state); | |
127 } | |
128 | |
129 void MediaPerceptionAPIManager::StateCallback(const APIStateCallback& callback, | |
130 bool succeeded, | |
131 const mri::State& state_proto) { | |
132 media_perception::State state; | |
133 if (!succeeded) { | |
134 state.status = media_perception::STATUS_TIMEOUT; | |
135 callback.Run(false, std::move(state)); | |
tbarzic
2017/05/11 00:38:27
instead of passing error code via state, can you c
Luke Sorenson
2017/05/11 23:57:59
The success parameter allows me to easily determin
tbarzic
2017/05/12 01:00:21
well you already have two success enums - only one
Luke Sorenson
2017/05/12 17:07:30
Done.
| |
136 return; | |
137 } | |
138 callback.Run(true, StateProtoToIdl(state_proto)); | |
139 } | |
140 | |
141 void MediaPerceptionAPIManager::GetDiagnosticsCallback( | |
142 const APIGetDiagnosticsCallback& callback, | |
143 bool succeeded, | |
144 const mri::Diagnostics& diagnostics_proto) { | |
145 if (!succeeded) { | |
146 callback.Run(false, media_perception::Diagnostics()); | |
147 return; | |
148 } | |
149 callback.Run(true, DiagnosticsProtoToIdl(diagnostics_proto)); | |
150 } | |
151 | |
152 void MediaPerceptionAPIManager::MediaPerceptionSignalHandler( | |
153 const mri::MediaPerception& media_perception_proto) { | |
154 EventRouter* router = EventRouter::Get(browser_context_); | |
155 if (!router || !router->HasEventListener( | |
156 media_perception::OnMediaPerception::kEventName)) { | |
157 LOG(WARNING) << "Router null or no event listener when receiving a " | |
tbarzic
2017/05/11 00:38:27
Remove this log, no listener being set seems like
Luke Sorenson
2017/05/11 23:57:59
Done.
| |
158 "media perception signal."; | |
159 return; | |
160 } | |
161 media_perception::MediaPerception media_perception = | |
162 MediaPerceptionProtoToIdl(media_perception_proto); | |
163 std::unique_ptr<Event> event( | |
164 new Event(events::MEDIA_PERCEPTION_PRIVATE_ON_MEDIA_PERCEPTION, | |
165 media_perception::OnMediaPerception::kEventName, | |
166 media_perception::OnMediaPerception::Create(media_perception))); | |
167 router->BroadcastEvent(std::move(event)); | |
168 } | |
169 | |
170 } // namespace extensions | |
OLD | NEW |