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..126d6840bf4d025130028d535538179171415f0a 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,34 @@ |
| #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 { |
| + |
| +std::string StateStatusToErrorMessage(const media_perception::State& state) { |
| + switch (state.status) { |
| + case media_perception::STATUS_TIMEOUT: |
| + return "Failed to get or set state because the D-Bus communication " |
|
tbarzic
2017/05/11 00:38:27
can you add constants for the error strigns, I thi
Luke Sorenson
2017/05/11 23:57:59
Done.
|
| + "with the analytics process failed."; |
| + case media_perception::STATUS_UNINITIALIZED: |
| + return "Failed to get or set state because the analytics process " |
| + "is not running."; |
| + case media_perception::STATUS_ERROR: |
| + // TODO(lasoren): Propagate errors reported over D-Bus by the analytics |
| + // process. |
| + return "An error was reported by the analytics process."; |
| + default: |
| + return "Failed to get or set state."; |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| MediaPerceptionPrivateGetStateFunction :: |
| MediaPerceptionPrivateGetStateFunction() {} |
| @@ -18,7 +40,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 +66,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()); |
| + // Check that the desired status is settable. |
|
tbarzic
2017/05/11 00:38:27
I'd remove the comment, it's evident from the code
Luke Sorenson
2017/05/11 23:57:59
Done.
|
| + 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. |
|
tbarzic
2017/05/11 00:38:27
nit: new line before this
Luke Sorenson
2017/05/11 23:57:59
Done.
|
| + 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 +109,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 |