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

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: Removing enums.xml from this change. 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 const char kErrorStringStatusTimeout[] =
17 "Failed to get or set state because the "
18 "D-Bus communication with the analytics process failed.";
19
20 const char kErrorStringStatusUninitialized[] =
21 "Failed to get or set state because "
22 "the analytics process is not running.";
23
24 const char kErrorStringStatusError[] =
25 "An error was reported by the "
26 "analytics process.";
27
28 std::string StateStatusToErrorMessage(const media_perception::State& state) {
29 switch (state.status) {
30 case media_perception::STATUS_TIMEOUT:
31 return kErrorStringStatusTimeout;
32 case media_perception::STATUS_UNINITIALIZED:
33 return kErrorStringStatusUninitialized;
34 case media_perception::STATUS_ERROR:
35 // TODO(lasoren): Propagate errors reported over D-Bus by the analytics
36 // process.
tbarzic 2017/05/12 01:00:22 I don't think you need to do this (when an error i
Luke Sorenson 2017/05/12 17:07:31 Acknowledged. Changed this function to use Callbac
37 return kErrorStringStatusError;
38 default:
39 return "Failed to get or set state.";
40 }
41 }
42
43 } // namespace
44
13 MediaPerceptionPrivateGetStateFunction :: 45 MediaPerceptionPrivateGetStateFunction ::
14 MediaPerceptionPrivateGetStateFunction() {} 46 MediaPerceptionPrivateGetStateFunction() {}
15 47
16 MediaPerceptionPrivateGetStateFunction :: 48 MediaPerceptionPrivateGetStateFunction ::
17 ~MediaPerceptionPrivateGetStateFunction() {} 49 ~MediaPerceptionPrivateGetStateFunction() {}
18 50
19 ExtensionFunction::ResponseAction 51 ExtensionFunction::ResponseAction
20 MediaPerceptionPrivateGetStateFunction::Run() { 52 MediaPerceptionPrivateGetStateFunction::Run() {
21 return RespondNow(Error("Not implemented.")); 53 MediaPerceptionAPIManager* manager =
54 MediaPerceptionAPIManager::Get(browser_context());
55 DCHECK(manager) << "Can't get manager.";
56 manager->GetState(base::Bind(
57 &MediaPerceptionPrivateGetStateFunction::GetStateCallback, this));
58 return RespondLater();
59 }
60
61 void MediaPerceptionPrivateGetStateFunction::GetStateCallback(
62 bool succeeded,
63 media_perception::State state) {
64 if (!succeeded) {
65 Respond(Error(StateStatusToErrorMessage(state)));
66 return;
67 }
68 Respond(OneArgument(state.ToValue()));
22 } 69 }
23 70
24 MediaPerceptionPrivateSetStateFunction :: 71 MediaPerceptionPrivateSetStateFunction ::
25 MediaPerceptionPrivateSetStateFunction() {} 72 MediaPerceptionPrivateSetStateFunction() {}
26 73
27 MediaPerceptionPrivateSetStateFunction :: 74 MediaPerceptionPrivateSetStateFunction ::
28 ~MediaPerceptionPrivateSetStateFunction() {} 75 ~MediaPerceptionPrivateSetStateFunction() {}
29 76
30 ExtensionFunction::ResponseAction 77 ExtensionFunction::ResponseAction
31 MediaPerceptionPrivateSetStateFunction::Run() { 78 MediaPerceptionPrivateSetStateFunction::Run() {
32 return RespondNow(Error("Not implemented.")); 79 std::unique_ptr<media_perception::SetState::Params> params =
80 media_perception::SetState::Params::Create(*args_);
81 EXTENSION_FUNCTION_VALIDATE(params.get());
82 if (params->state.status != media_perception::STATUS_RUNNING &&
83 params->state.status != media_perception::STATUS_SUSPENDED) {
84 return RespondNow(
85 Error("Status can only be set to RUNNING and SUSPENDED."));
86 }
87
88 // Check that device context is only provided with SetState RUNNING.
89 if (params->state.status != media_perception::STATUS_RUNNING &&
90 params->state.device_context.get() != nullptr) {
91 return RespondNow(
92 Error("Only provide deviceContext with SetState RUNNING."));
93 }
94 MediaPerceptionAPIManager* manager =
95 MediaPerceptionAPIManager::Get(browser_context());
96 DCHECK(manager) << "Can't get manager.";
97 manager->SetState(
98 params->state,
99 base::Bind(&MediaPerceptionPrivateSetStateFunction::SetStateCallback,
100 this));
101 return RespondLater();
102 }
103
104 void MediaPerceptionPrivateSetStateFunction::SetStateCallback(
105 bool succeeded,
106 media_perception::State state) {
107 if (!succeeded) {
108 Respond(Error(StateStatusToErrorMessage(state)));
109 return;
110 }
111 Respond(OneArgument(state.ToValue()));
33 } 112 }
34 113
35 MediaPerceptionPrivateGetDiagnosticsFunction :: 114 MediaPerceptionPrivateGetDiagnosticsFunction ::
36 MediaPerceptionPrivateGetDiagnosticsFunction() {} 115 MediaPerceptionPrivateGetDiagnosticsFunction() {}
37 116
38 MediaPerceptionPrivateGetDiagnosticsFunction :: 117 MediaPerceptionPrivateGetDiagnosticsFunction ::
39 ~MediaPerceptionPrivateGetDiagnosticsFunction() {} 118 ~MediaPerceptionPrivateGetDiagnosticsFunction() {}
40 119
41 ExtensionFunction::ResponseAction 120 ExtensionFunction::ResponseAction
42 MediaPerceptionPrivateGetDiagnosticsFunction::Run() { 121 MediaPerceptionPrivateGetDiagnosticsFunction::Run() {
43 return RespondNow(Error("Not implemented.")); 122 MediaPerceptionAPIManager* manager =
123 MediaPerceptionAPIManager::Get(browser_context());
124 DCHECK(manager) << "Can't get manager.";
125 manager->GetDiagnostics(base::Bind(
126 &MediaPerceptionPrivateGetDiagnosticsFunction::GetDiagnosticsCallback,
127 this));
128 return RespondLater();
129 }
130
131 void MediaPerceptionPrivateGetDiagnosticsFunction::GetDiagnosticsCallback(
132 bool succeeded,
133 media_perception::Diagnostics diagnostics) {
134 if (!succeeded) {
135 Respond(Error("Failed to getDiagnostics."));
136 return;
137 }
138 Respond(OneArgument(diagnostics.ToValue()));
44 } 139 }
45 140
46 } // namespace extensions 141 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698