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