| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/api/media_perception_private/media_perception_priva
te_api.h" | 5 #include "extensions/browser/api/media_perception_private/media_perception_priva
te_api.h" |
| 6 | 6 |
| 7 #include "extensions/browser/extension_function.h" | 7 #include "extensions/browser/extension_function.h" |
| 8 | 8 |
| 9 namespace media_perception = extensions::api::media_perception_private; | 9 namespace media_perception = extensions::api::media_perception_private; |
| 10 | 10 |
| 11 namespace extensions { | 11 namespace extensions { |
| 12 | 12 |
| 13 using CallbackStatus = MediaPerceptionAPIManager::CallbackStatus; |
| 14 |
| 15 namespace { |
| 16 |
| 17 const char kErrorStringStatusDbusError[] = "Service is unreachable."; |
| 18 const char kErrorStringStatusIdle[] = "Service is not running."; |
| 19 const char kErrorStringStatusLaunching[] = "Service busy launching."; |
| 20 |
| 21 std::string CallbackStatusToErrorMessage(const CallbackStatus& status) { |
| 22 switch (status) { |
| 23 case CallbackStatus::DBUS_ERROR: |
| 24 return kErrorStringStatusDbusError; |
| 25 case CallbackStatus::PROCESS_IDLE_ERROR: |
| 26 return kErrorStringStatusIdle; |
| 27 case CallbackStatus::PROCESS_LAUNCHING_ERROR: |
| 28 return kErrorStringStatusLaunching; |
| 29 case CallbackStatus::SUCCESS: |
| 30 return "CallbackStatus success."; |
| 31 } |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
| 13 MediaPerceptionPrivateGetStateFunction :: | 36 MediaPerceptionPrivateGetStateFunction :: |
| 14 MediaPerceptionPrivateGetStateFunction() {} | 37 MediaPerceptionPrivateGetStateFunction() {} |
| 15 | 38 |
| 16 MediaPerceptionPrivateGetStateFunction :: | 39 MediaPerceptionPrivateGetStateFunction :: |
| 17 ~MediaPerceptionPrivateGetStateFunction() {} | 40 ~MediaPerceptionPrivateGetStateFunction() {} |
| 18 | 41 |
| 19 ExtensionFunction::ResponseAction | 42 ExtensionFunction::ResponseAction |
| 20 MediaPerceptionPrivateGetStateFunction::Run() { | 43 MediaPerceptionPrivateGetStateFunction::Run() { |
| 21 return RespondNow(Error("Not implemented.")); | 44 MediaPerceptionAPIManager* manager = |
| 45 MediaPerceptionAPIManager::Get(browser_context()); |
| 46 manager->GetState(base::Bind( |
| 47 &MediaPerceptionPrivateGetStateFunction::GetStateCallback, this)); |
| 48 return RespondLater(); |
| 49 } |
| 50 |
| 51 void MediaPerceptionPrivateGetStateFunction::GetStateCallback( |
| 52 CallbackStatus status, |
| 53 media_perception::State state) { |
| 54 if (status != CallbackStatus::SUCCESS) { |
| 55 Respond(Error(CallbackStatusToErrorMessage(status))); |
| 56 return; |
| 57 } |
| 58 Respond(OneArgument(state.ToValue())); |
| 22 } | 59 } |
| 23 | 60 |
| 24 MediaPerceptionPrivateSetStateFunction :: | 61 MediaPerceptionPrivateSetStateFunction :: |
| 25 MediaPerceptionPrivateSetStateFunction() {} | 62 MediaPerceptionPrivateSetStateFunction() {} |
| 26 | 63 |
| 27 MediaPerceptionPrivateSetStateFunction :: | 64 MediaPerceptionPrivateSetStateFunction :: |
| 28 ~MediaPerceptionPrivateSetStateFunction() {} | 65 ~MediaPerceptionPrivateSetStateFunction() {} |
| 29 | 66 |
| 30 ExtensionFunction::ResponseAction | 67 ExtensionFunction::ResponseAction |
| 31 MediaPerceptionPrivateSetStateFunction::Run() { | 68 MediaPerceptionPrivateSetStateFunction::Run() { |
| 32 return RespondNow(Error("Not implemented.")); | 69 std::unique_ptr<media_perception::SetState::Params> params = |
| 70 media_perception::SetState::Params::Create(*args_); |
| 71 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 72 if (params->state.status != media_perception::STATUS_RUNNING && |
| 73 params->state.status != media_perception::STATUS_SUSPENDED) { |
| 74 return RespondNow( |
| 75 Error("Status can only be set to RUNNING and SUSPENDED.")); |
| 76 } |
| 77 |
| 78 // Check that device context is only provided with SetState RUNNING. |
| 79 if (params->state.status != media_perception::STATUS_RUNNING && |
| 80 params->state.device_context.get() != nullptr) { |
| 81 return RespondNow( |
| 82 Error("Only provide deviceContext with SetState RUNNING.")); |
| 83 } |
| 84 MediaPerceptionAPIManager* manager = |
| 85 MediaPerceptionAPIManager::Get(browser_context()); |
| 86 manager->SetState( |
| 87 params->state, |
| 88 base::Bind(&MediaPerceptionPrivateSetStateFunction::SetStateCallback, |
| 89 this)); |
| 90 return RespondLater(); |
| 91 } |
| 92 |
| 93 void MediaPerceptionPrivateSetStateFunction::SetStateCallback( |
| 94 CallbackStatus status, |
| 95 media_perception::State state) { |
| 96 if (status != CallbackStatus::SUCCESS) { |
| 97 Respond(Error(CallbackStatusToErrorMessage(status))); |
| 98 return; |
| 99 } |
| 100 Respond(OneArgument(state.ToValue())); |
| 33 } | 101 } |
| 34 | 102 |
| 35 MediaPerceptionPrivateGetDiagnosticsFunction :: | 103 MediaPerceptionPrivateGetDiagnosticsFunction :: |
| 36 MediaPerceptionPrivateGetDiagnosticsFunction() {} | 104 MediaPerceptionPrivateGetDiagnosticsFunction() {} |
| 37 | 105 |
| 38 MediaPerceptionPrivateGetDiagnosticsFunction :: | 106 MediaPerceptionPrivateGetDiagnosticsFunction :: |
| 39 ~MediaPerceptionPrivateGetDiagnosticsFunction() {} | 107 ~MediaPerceptionPrivateGetDiagnosticsFunction() {} |
| 40 | 108 |
| 41 ExtensionFunction::ResponseAction | 109 ExtensionFunction::ResponseAction |
| 42 MediaPerceptionPrivateGetDiagnosticsFunction::Run() { | 110 MediaPerceptionPrivateGetDiagnosticsFunction::Run() { |
| 43 return RespondNow(Error("Not implemented.")); | 111 MediaPerceptionAPIManager* manager = |
| 112 MediaPerceptionAPIManager::Get(browser_context()); |
| 113 manager->GetDiagnostics(base::Bind( |
| 114 &MediaPerceptionPrivateGetDiagnosticsFunction::GetDiagnosticsCallback, |
| 115 this)); |
| 116 return RespondLater(); |
| 117 } |
| 118 |
| 119 void MediaPerceptionPrivateGetDiagnosticsFunction::GetDiagnosticsCallback( |
| 120 CallbackStatus status, |
| 121 media_perception::Diagnostics diagnostics) { |
| 122 if (status != CallbackStatus::SUCCESS) { |
| 123 Respond(Error(CallbackStatusToErrorMessage(status))); |
| 124 return; |
| 125 } |
| 126 Respond(OneArgument(diagnostics.ToValue())); |
| 44 } | 127 } |
| 45 | 128 |
| 46 } // namespace extensions | 129 } // namespace extensions |
| OLD | NEW |