Chromium Code Reviews| 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..35a12dc72bb93e8dc83d54479d4d9bc3c44ccf50 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 |
| @@ -4,12 +4,44 @@ |
| #include "extensions/browser/api/media_perception_private/media_perception_private_api.h" |
| +#include "extensions/browser/api/media_perception_private/media_perception_api_manager.h" |
| #include "extensions/browser/extension_function.h" |
| namespace media_perception = extensions::api::media_perception_private; |
| namespace extensions { |
| +namespace { |
| + |
| +const char kErrorStringStatusTimeout[] = |
| + "Failed to get or set state because the " |
| + "D-Bus communication with the analytics process failed."; |
| + |
| +const char kErrorStringStatusUninitialized[] = |
| + "Failed to get or set state because " |
| + "the analytics process is not running."; |
| + |
| +const char kErrorStringStatusError[] = |
| + "An error was reported by the " |
| + "analytics process."; |
| + |
| +std::string StateStatusToErrorMessage(const media_perception::State& state) { |
| + switch (state.status) { |
| + case media_perception::STATUS_TIMEOUT: |
| + return kErrorStringStatusTimeout; |
| + case media_perception::STATUS_UNINITIALIZED: |
| + return kErrorStringStatusUninitialized; |
| + case media_perception::STATUS_ERROR: |
| + // TODO(lasoren): Propagate errors reported over D-Bus by the analytics |
| + // process. |
|
tbarzic
2017/05/12 01:00:22
I don't think you need to do this (when an error i
Luke Sorenson
2017/05/12 17:07:31
Acknowledged. Changed this function to use Callbac
|
| + return kErrorStringStatusError; |
| + default: |
| + return "Failed to get or set state."; |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| MediaPerceptionPrivateGetStateFunction :: |
| MediaPerceptionPrivateGetStateFunction() {} |
| @@ -18,7 +50,22 @@ MediaPerceptionPrivateGetStateFunction :: |
| ExtensionFunction::ResponseAction |
| MediaPerceptionPrivateGetStateFunction::Run() { |
| - return RespondNow(Error("Not implemented.")); |
| + MediaPerceptionAPIManager* manager = |
| + MediaPerceptionAPIManager::Get(browser_context()); |
| + DCHECK(manager) << "Can't get manager."; |
| + manager->GetState(base::Bind( |
| + &MediaPerceptionPrivateGetStateFunction::GetStateCallback, this)); |
| + return RespondLater(); |
| +} |
| + |
| +void MediaPerceptionPrivateGetStateFunction::GetStateCallback( |
| + bool succeeded, |
| + media_perception::State state) { |
| + if (!succeeded) { |
| + Respond(Error(StateStatusToErrorMessage(state))); |
| + return; |
| + } |
| + Respond(OneArgument(state.ToValue())); |
| } |
| MediaPerceptionPrivateSetStateFunction :: |
| @@ -29,7 +76,39 @@ 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()); |
| + DCHECK(manager) << "Can't get manager."; |
| + manager->SetState( |
| + params->state, |
| + base::Bind(&MediaPerceptionPrivateSetStateFunction::SetStateCallback, |
| + this)); |
| + return RespondLater(); |
| +} |
| + |
| +void MediaPerceptionPrivateSetStateFunction::SetStateCallback( |
| + bool succeeded, |
| + media_perception::State state) { |
| + if (!succeeded) { |
| + Respond(Error(StateStatusToErrorMessage(state))); |
| + return; |
| + } |
| + Respond(OneArgument(state.ToValue())); |
| } |
| MediaPerceptionPrivateGetDiagnosticsFunction :: |
| @@ -40,7 +119,23 @@ MediaPerceptionPrivateGetDiagnosticsFunction :: |
| ExtensionFunction::ResponseAction |
| MediaPerceptionPrivateGetDiagnosticsFunction::Run() { |
| - return RespondNow(Error("Not implemented.")); |
| + MediaPerceptionAPIManager* manager = |
| + MediaPerceptionAPIManager::Get(browser_context()); |
| + DCHECK(manager) << "Can't get manager."; |
| + manager->GetDiagnostics(base::Bind( |
| + &MediaPerceptionPrivateGetDiagnosticsFunction::GetDiagnosticsCallback, |
| + this)); |
| + return RespondLater(); |
| +} |
| + |
| +void MediaPerceptionPrivateGetDiagnosticsFunction::GetDiagnosticsCallback( |
| + bool succeeded, |
| + media_perception::Diagnostics diagnostics) { |
| + if (!succeeded) { |
| + Respond(Error("Failed to getDiagnostics.")); |
| + return; |
| + } |
| + Respond(OneArgument(diagnostics.ToValue())); |
| } |
| } // namespace extensions |