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