| Index: extensions/browser/api/media_perception_private/media_perception_private_api.cc
|
| diff --git a/extensions/browser/api/media_perception_private/media_perception_private_api.cc b/extensions/browser/api/media_perception_private/media_perception_private_api.cc
|
| index c6320279d22a85aa4c8e31ea006f074575e0fb06..0f37d8a82a7c4885add83ce1d64700bddb41102f 100644
|
| --- a/extensions/browser/api/media_perception_private/media_perception_private_api.cc
|
| +++ b/extensions/browser/api/media_perception_private/media_perception_private_api.cc
|
| @@ -10,6 +10,31 @@ namespace media_perception = extensions::api::media_perception_private;
|
|
|
| namespace extensions {
|
|
|
| +using CallbackStatus = MediaPerceptionAPIManager::CallbackStatus;
|
| +
|
| +namespace {
|
| +
|
| +const char kErrorStringStatusDbusError[] = "Service is unreachable.";
|
| +
|
| +const char kErrorStringStatusIdle[] = "Service is not running.";
|
| +
|
| +const char kErrorStringStatusLaunching[] = "Service busy launching.";
|
| +
|
| +std::string CallbackStatusToErrorMessage(const CallbackStatus& status) {
|
| + switch (status) {
|
| + case CallbackStatus::DBUS_ERROR:
|
| + return kErrorStringStatusDbusError;
|
| + case CallbackStatus::PROCESS_IDLE_ERROR:
|
| + return kErrorStringStatusIdle;
|
| + case CallbackStatus::PROCESS_LAUNCHING_ERROR:
|
| + return kErrorStringStatusLaunching;
|
| + default:
|
| + return "API call failed.";
|
| + }
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| MediaPerceptionPrivateGetStateFunction ::
|
| MediaPerceptionPrivateGetStateFunction() {}
|
|
|
| @@ -18,7 +43,21 @@ MediaPerceptionPrivateGetStateFunction ::
|
|
|
| ExtensionFunction::ResponseAction
|
| MediaPerceptionPrivateGetStateFunction::Run() {
|
| - return RespondNow(Error("Not implemented."));
|
| + MediaPerceptionAPIManager* manager =
|
| + MediaPerceptionAPIManager::Get(browser_context());
|
| + manager->GetState(base::Bind(
|
| + &MediaPerceptionPrivateGetStateFunction::GetStateCallback, this));
|
| + return RespondLater();
|
| +}
|
| +
|
| +void MediaPerceptionPrivateGetStateFunction::GetStateCallback(
|
| + CallbackStatus status,
|
| + media_perception::State state) {
|
| + if (status != CallbackStatus::SUCCESS) {
|
| + Respond(Error(CallbackStatusToErrorMessage(status)));
|
| + return;
|
| + }
|
| + Respond(OneArgument(state.ToValue()));
|
| }
|
|
|
| MediaPerceptionPrivateSetStateFunction ::
|
| @@ -29,7 +68,38 @@ MediaPerceptionPrivateSetStateFunction ::
|
|
|
| ExtensionFunction::ResponseAction
|
| MediaPerceptionPrivateSetStateFunction::Run() {
|
| - return RespondNow(Error("Not implemented."));
|
| + std::unique_ptr<media_perception::SetState::Params> params =
|
| + media_perception::SetState::Params::Create(*args_);
|
| + EXTENSION_FUNCTION_VALIDATE(params.get());
|
| + if (params->state.status != media_perception::STATUS_RUNNING &&
|
| + params->state.status != media_perception::STATUS_SUSPENDED) {
|
| + return RespondNow(
|
| + Error("Status can only be set to RUNNING and SUSPENDED."));
|
| + }
|
| +
|
| + // Check that device context is only provided with SetState RUNNING.
|
| + if (params->state.status != media_perception::STATUS_RUNNING &&
|
| + params->state.device_context.get() != nullptr) {
|
| + return RespondNow(
|
| + Error("Only provide deviceContext with SetState RUNNING."));
|
| + }
|
| + MediaPerceptionAPIManager* manager =
|
| + MediaPerceptionAPIManager::Get(browser_context());
|
| + manager->SetState(
|
| + params->state,
|
| + base::Bind(&MediaPerceptionPrivateSetStateFunction::SetStateCallback,
|
| + this));
|
| + return RespondLater();
|
| +}
|
| +
|
| +void MediaPerceptionPrivateSetStateFunction::SetStateCallback(
|
| + CallbackStatus status,
|
| + media_perception::State state) {
|
| + if (status != CallbackStatus::SUCCESS) {
|
| + Respond(Error(CallbackStatusToErrorMessage(status)));
|
| + return;
|
| + }
|
| + Respond(OneArgument(state.ToValue()));
|
| }
|
|
|
| MediaPerceptionPrivateGetDiagnosticsFunction ::
|
| @@ -40,7 +110,22 @@ MediaPerceptionPrivateGetDiagnosticsFunction ::
|
|
|
| ExtensionFunction::ResponseAction
|
| MediaPerceptionPrivateGetDiagnosticsFunction::Run() {
|
| - return RespondNow(Error("Not implemented."));
|
| + MediaPerceptionAPIManager* manager =
|
| + MediaPerceptionAPIManager::Get(browser_context());
|
| + manager->GetDiagnostics(base::Bind(
|
| + &MediaPerceptionPrivateGetDiagnosticsFunction::GetDiagnosticsCallback,
|
| + this));
|
| + return RespondLater();
|
| +}
|
| +
|
| +void MediaPerceptionPrivateGetDiagnosticsFunction::GetDiagnosticsCallback(
|
| + CallbackStatus status,
|
| + media_perception::Diagnostics diagnostics) {
|
| + if (status != CallbackStatus::SUCCESS) {
|
| + Respond(Error(CallbackStatusToErrorMessage(status)));
|
| + return;
|
| + }
|
| + Respond(OneArgument(diagnostics.ToValue()));
|
| }
|
|
|
| } // namespace extensions
|
|
|