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