| 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_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) { | |
| 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_uninitialized; | |
| 74 state_uninitialized.status = media_perception::STATUS_UNINITIALIZED; | |
| 75 callback.Run(CallbackStatus::SUCCESS, std::move(state_uninitialized)); | |
| 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."; | |
| 84 | |
| 85 if (analytics_process_state_ == AnalyticsProcessState::RUNNING) { | |
| 86 SetStateInternal(callback, state_proto); | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 if (analytics_process_state_ == AnalyticsProcessState::LAUNCHING) { | |
| 91 callback.Run(CallbackStatus::PROCESS_LAUNCHING_ERROR, | |
| 92 media_perception::State()); | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 // Analytics process is in state IDLE. | |
| 97 if (state_proto.status() == mri::State::RUNNING) { | |
| 98 analytics_process_state_ = AnalyticsProcessState::LAUNCHING; | |
| 99 chromeos::UpstartClient* dbus_client = | |
| 100 chromeos::DBusThreadManager::Get()->GetUpstartClient(); | |
| 101 dbus_client->StartMediaAnalytics( | |
| 102 base::Bind(&MediaPerceptionAPIManager::UpstartCallback, | |
| 103 weak_ptr_factory_.GetWeakPtr(), callback, state_proto)); | |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 callback.Run(CallbackStatus::PROCESS_IDLE_ERROR, media_perception::State()); | |
| 108 } | |
| 109 | |
| 110 void MediaPerceptionAPIManager::SetStateInternal( | |
| 111 const APIStateCallback& callback, | |
| 112 const mri::State& state) { | |
| 113 chromeos::MediaAnalyticsClient* dbus_client = | |
| 114 chromeos::DBusThreadManager::Get()->GetMediaAnalyticsClient(); | |
| 115 dbus_client->SetState(state, | |
| 116 base::Bind(&MediaPerceptionAPIManager::StateCallback, | |
| 117 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 118 } | |
| 119 | |
| 120 void MediaPerceptionAPIManager::GetDiagnostics( | |
| 121 const APIGetDiagnosticsCallback& callback) { | |
| 122 chromeos::MediaAnalyticsClient* dbus_client = | |
| 123 chromeos::DBusThreadManager::Get()->GetMediaAnalyticsClient(); | |
| 124 dbus_client->GetDiagnostics( | |
| 125 base::Bind(&MediaPerceptionAPIManager::GetDiagnosticsCallback, | |
| 126 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 127 } | |
| 128 | |
| 129 void MediaPerceptionAPIManager::UpstartCallback( | |
| 130 const APIStateCallback& callback, | |
| 131 const mri::State& state, | |
| 132 bool succeeded) { | |
| 133 if (!succeeded) { | |
| 134 analytics_process_state_ = AnalyticsProcessState::IDLE; | |
| 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, | |
| 151 media_perception::StateProtoToIdl(state_proto)); | |
| 152 } | |
| 153 | |
| 154 void MediaPerceptionAPIManager::GetDiagnosticsCallback( | |
| 155 const APIGetDiagnosticsCallback& callback, | |
| 156 bool succeeded, | |
| 157 const mri::Diagnostics& diagnostics_proto) { | |
| 158 if (!succeeded) { | |
| 159 callback.Run(CallbackStatus::DBUS_ERROR, media_perception::Diagnostics()); | |
| 160 return; | |
| 161 } | |
| 162 callback.Run(CallbackStatus::SUCCESS, | |
| 163 media_perception::DiagnosticsProtoToIdl(diagnostics_proto)); | |
| 164 } | |
| 165 | |
| 166 void MediaPerceptionAPIManager::MediaPerceptionSignalHandler( | |
| 167 const mri::MediaPerception& media_perception_proto) { | |
| 168 EventRouter* router = EventRouter::Get(browser_context_); | |
| 169 DCHECK(router) << "EventRouter is null."; | |
| 170 | |
| 171 media_perception::MediaPerception media_perception = | |
| 172 media_perception::MediaPerceptionProtoToIdl(media_perception_proto); | |
| 173 std::unique_ptr<Event> event( | |
| 174 new Event(events::MEDIA_PERCEPTION_PRIVATE_ON_MEDIA_PERCEPTION, | |
| 175 media_perception::OnMediaPerception::kEventName, | |
| 176 media_perception::OnMediaPerception::Create(media_perception))); | |
| 177 router->BroadcastEvent(std::move(event)); | |
| 178 } | |
| 179 | |
| 180 } // namespace extensions | |
| OLD | NEW |