Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(626)

Side by Side Diff: extensions/browser/api/media_perception_private/media_perception_private_api.cc

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

Powered by Google App Engine
This is Rietveld 408576698