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..fc91756c8aeba07c7dd46e32d128f01a7b96a9c1 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,36 @@ namespace media_perception = extensions::api::media_perception_private; |
| namespace extensions { |
| +using CallbackStatus = MediaPerceptionAPIManager::CallbackStatus; |
| + |
| +namespace { |
| + |
| +const char kErrorStringStatusDbusError[] = |
| + "API call failed because the D-Bus communication with the analytics " |
|
tbarzic
2017/05/17 21:04:42
can you make the error message less verbose.
e.g.
Luke Sorenson
2017/05/17 23:07:49
Done.
|
| + "process failed."; |
| + |
| +const char kErrorStringStatusIdle[] = |
| + "API call failed because the analytics process is not running."; |
| + |
| +const char kErrorStringStatusLaunching[] = |
| + "API call failed because the analytics process is still in the process " |
| + "of 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 +48,22 @@ MediaPerceptionPrivateGetStateFunction :: |
| ExtensionFunction::ResponseAction |
| MediaPerceptionPrivateGetStateFunction::Run() { |
| - return RespondNow(Error("Not implemented.")); |
| + MediaPerceptionAPIManager* manager = |
| + MediaPerceptionAPIManager::Get(browser_context()); |
| + DCHECK(manager) << "Can't get manager."; |
|
tbarzic
2017/05/17 21:04:42
I think you can remove the DCHECK. If it's null th
Luke Sorenson
2017/05/17 23:07:49
Done.
|
| + 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 +74,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( |
| + CallbackStatus status, |
| + media_perception::State state) { |
| + if (status != CallbackStatus::SUCCESS) { |
| + Respond(Error(CallbackStatusToErrorMessage(status))); |
| + return; |
| + } |
| + Respond(OneArgument(state.ToValue())); |
| } |
| MediaPerceptionPrivateGetDiagnosticsFunction :: |
| @@ -40,7 +117,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( |
| + CallbackStatus status, |
| + media_perception::Diagnostics diagnostics) { |
| + if (status != CallbackStatus::SUCCESS) { |
| + Respond(Error(CallbackStatusToErrorMessage(status))); |
| + return; |
| + } |
| + Respond(OneArgument(diagnostics.ToValue())); |
| } |
| } // namespace extensions |