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

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: ImageFrameProtoToIdl and unittest 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/api/media_perception_private/media_perception_api_m anager.h"
7 #include "extensions/browser/extension_function.h" 8 #include "extensions/browser/extension_function.h"
8 9
9 namespace media_perception = extensions::api::media_perception_private; 10 namespace media_perception = extensions::api::media_perception_private;
10 11
11 namespace extensions { 12 namespace extensions {
12 13
14 namespace {
15
16 std::string StateStatusToErrorMessage(const media_perception::State& state) {
17 switch (state.status) {
18 case media_perception::STATUS_TIMEOUT:
19 return "Failed to get or set state because the D-Bus communication "
tbarzic 2017/05/11 00:38:27 can you add constants for the error strigns, I thi
Luke Sorenson 2017/05/11 23:57:59 Done.
20 "with the analytics process failed.";
21 case media_perception::STATUS_UNINITIALIZED:
22 return "Failed to get or set state because the analytics process "
23 "is not running.";
24 case media_perception::STATUS_ERROR:
25 // TODO(lasoren): Propagate errors reported over D-Bus by the analytics
26 // process.
27 return "An error was reported by the analytics process.";
28 default:
29 return "Failed to get or set state.";
30 }
31 }
32
33 } // namespace
34
13 MediaPerceptionPrivateGetStateFunction :: 35 MediaPerceptionPrivateGetStateFunction ::
14 MediaPerceptionPrivateGetStateFunction() {} 36 MediaPerceptionPrivateGetStateFunction() {}
15 37
16 MediaPerceptionPrivateGetStateFunction :: 38 MediaPerceptionPrivateGetStateFunction ::
17 ~MediaPerceptionPrivateGetStateFunction() {} 39 ~MediaPerceptionPrivateGetStateFunction() {}
18 40
19 ExtensionFunction::ResponseAction 41 ExtensionFunction::ResponseAction
20 MediaPerceptionPrivateGetStateFunction::Run() { 42 MediaPerceptionPrivateGetStateFunction::Run() {
21 return RespondNow(Error("Not implemented.")); 43 MediaPerceptionAPIManager* manager =
44 MediaPerceptionAPIManager::Get(browser_context());
45 DCHECK(manager) << "Can't get manager.";
46 manager->GetState(base::Bind(
47 &MediaPerceptionPrivateGetStateFunction::GetStateCallback, this));
48 return RespondLater();
49 }
50
51 void MediaPerceptionPrivateGetStateFunction::GetStateCallback(
52 bool succeeded,
53 media_perception::State state) {
54 if (!succeeded) {
55 Respond(Error(StateStatusToErrorMessage(state)));
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 // Check that the desired status is settable.
tbarzic 2017/05/11 00:38:27 I'd remove the comment, it's evident from the code
Luke Sorenson 2017/05/11 23:57:59 Done.
73 if (params->state.status != media_perception::STATUS_RUNNING &&
74 params->state.status != media_perception::STATUS_SUSPENDED) {
75 return RespondNow(
76 Error("Status can only be set to RUNNING and SUSPENDED."));
77 }
78 // Check that device context is only provided with SetState RUNNING.
tbarzic 2017/05/11 00:38:27 nit: new line before this
Luke Sorenson 2017/05/11 23:57:59 Done.
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 DCHECK(manager) << "Can't get manager.";
87 manager->SetState(
88 params->state,
89 base::Bind(&MediaPerceptionPrivateSetStateFunction::SetStateCallback,
90 this));
91 return RespondLater();
92 }
93
94 void MediaPerceptionPrivateSetStateFunction::SetStateCallback(
95 bool succeeded,
96 media_perception::State state) {
97 if (!succeeded) {
98 Respond(Error(StateStatusToErrorMessage(state)));
99 return;
100 }
101 Respond(OneArgument(state.ToValue()));
33 } 102 }
34 103
35 MediaPerceptionPrivateGetDiagnosticsFunction :: 104 MediaPerceptionPrivateGetDiagnosticsFunction ::
36 MediaPerceptionPrivateGetDiagnosticsFunction() {} 105 MediaPerceptionPrivateGetDiagnosticsFunction() {}
37 106
38 MediaPerceptionPrivateGetDiagnosticsFunction :: 107 MediaPerceptionPrivateGetDiagnosticsFunction ::
39 ~MediaPerceptionPrivateGetDiagnosticsFunction() {} 108 ~MediaPerceptionPrivateGetDiagnosticsFunction() {}
40 109
41 ExtensionFunction::ResponseAction 110 ExtensionFunction::ResponseAction
42 MediaPerceptionPrivateGetDiagnosticsFunction::Run() { 111 MediaPerceptionPrivateGetDiagnosticsFunction::Run() {
43 return RespondNow(Error("Not implemented.")); 112 MediaPerceptionAPIManager* manager =
113 MediaPerceptionAPIManager::Get(browser_context());
114 DCHECK(manager) << "Can't get manager.";
115 manager->GetDiagnostics(base::Bind(
116 &MediaPerceptionPrivateGetDiagnosticsFunction::GetDiagnosticsCallback,
117 this));
118 return RespondLater();
119 }
120
121 void MediaPerceptionPrivateGetDiagnosticsFunction::GetDiagnosticsCallback(
122 bool succeeded,
123 media_perception::Diagnostics diagnostics) {
124 if (!succeeded) {
125 Respond(Error("Failed to getDiagnostics."));
126 return;
127 }
128 Respond(OneArgument(diagnostics.ToValue()));
44 } 129 }
45 130
46 } // namespace extensions 131 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698