a9e4462d5f3a9dbdd7164f9be04ece69574c0f4a
[platform/core/api/webapi-plugins.git] / src / sound / sound_instance.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include "sound/sound_instance.h"
18
19 #include <functional>
20
21 #include "common/logger.h"
22 #include "common/picojson.h"
23 #include "common/platform_exception.h"
24 #include "common/tools.h"
25 #include "sound_manager.h"
26 #include "sound_manager_internal.h"
27
28 namespace extension {
29 namespace sound {
30
31 namespace {
32 // The privileges that required in Sound API
33 const std::string kPrivilegeSound = "http://tizen.org/privilege/volume.set";
34
35 }  // namespace
36
37 using namespace common;
38 using namespace extension::sound;
39
40 SoundInstance::SoundInstance() : manager_(*this) {
41   ScopeLogger();
42   using std::placeholders::_1;
43   using std::placeholders::_2;
44
45 #define REGISTER_METHOD(M) RegisterSyncHandler(#M, std::bind(&SoundInstance::M, this, _1, _2))
46   REGISTER_METHOD(SoundManagerSetVolume);
47   REGISTER_METHOD(SoundManagerUnsetSoundModeChangeListener);
48   REGISTER_METHOD(SoundManagerGetVolume);
49   REGISTER_METHOD(SoundManagerUnsetVolumeChangeListener);
50   REGISTER_METHOD(SoundManagerSetSoundModeChangeListener);
51   REGISTER_METHOD(SoundManagerSetVolumeChangeListener);
52   REGISTER_METHOD(SoundManagerGetSoundMode);
53   REGISTER_METHOD(SoundManagerGetConnectedDeviceList);
54   REGISTER_METHOD(SoundManagerGetActivatedDeviceList);
55   REGISTER_METHOD(SoundManagerAddDeviceStateChangeListener);
56   REGISTER_METHOD(SoundManagerRemoveDeviceStateChangeListener);
57 #undef REGISTER_METHOD
58 }
59
60 SoundInstance::~SoundInstance() {
61   ScopeLogger();
62 }
63
64 #define CHECK_EXIST(args, name, out)                                             \
65   if (!args.contains(name)) {                                                    \
66     LogAndReportError(TypeMismatchException(name " is required argument"), out); \
67     return;                                                                      \
68   }
69
70 void SoundInstance::SoundManagerGetSoundMode(const picojson::value& args, picojson::object& out) {
71   ScopeLogger();
72   std::string sound_mode_type;
73   PlatformResult status = manager_.GetSoundMode(&sound_mode_type);
74
75   if (status.IsSuccess()) {
76     ReportSuccess(picojson::value(sound_mode_type), out);
77   } else {
78     LogAndReportError(status, &out);
79   }
80 }
81
82 void SoundInstance::SoundManagerSetVolume(const picojson::value& args, picojson::object& out) {
83   ScopeLogger();
84   CHECK_PRIVILEGE_ACCESS(kPrivilegeSound, &out);
85   PlatformResult status = manager_.SetVolume(args.get<picojson::object>());
86
87   if (status.IsSuccess()) {
88     ReportSuccess(out);
89   } else {
90     LogAndReportError(status, &out);
91   }
92 }
93
94 void SoundInstance::SoundManagerGetVolume(const picojson::value& args, picojson::object& out) {
95   ScopeLogger();
96   double volume;
97   PlatformResult status = manager_.GetVolume(args.get<picojson::object>(), &volume);
98
99   if (status.IsSuccess()) {
100     ReportSuccess(picojson::value(volume), out);
101   } else {
102     LogAndReportError(status, &out);
103   }
104 }
105
106 void SoundInstance::SoundManagerSetSoundModeChangeListener(const picojson::value& args,
107                                                            picojson::object& out) {
108   ScopeLogger();
109   PlatformResult status = manager_.SetSoundModeChangeListener(this);
110
111   if (status.IsSuccess()) {
112     ReportSuccess(out);
113   } else {
114     LogAndReportError(status, &out);
115   }
116 }
117
118 void SoundInstance::SoundManagerUnsetSoundModeChangeListener(const picojson::value& args,
119                                                              picojson::object& out) {
120   PlatformResult status = manager_.UnsetSoundModeChangeListener();
121
122   ScopeLogger();
123
124   if (status.IsSuccess()) {
125     ReportSuccess(out);
126   } else {
127     LogAndReportError(status, &out);
128   }
129 }
130
131 void SoundInstance::OnSoundModeChange(const std::string& newmode) {
132   ScopeLogger();
133   if (current_sound_mode == newmode) {
134     LoggerD("New sound mode equals to current sound mode");
135     return;
136   }
137   current_sound_mode = newmode;
138   picojson::value event = picojson::value(picojson::object());
139   picojson::object& obj = event.get<picojson::object>();
140   picojson::value result = picojson::value(newmode);
141   ReportSuccess(result, obj);
142   obj["listenerId"] = picojson::value("SoundModeChangeListener");
143   LoggerD("Posting: %s", event.serialize().c_str());
144   Instance::PostMessage(this, event.serialize().c_str());
145 }
146
147 void SoundInstance::SoundManagerSetVolumeChangeListener(const picojson::value& args,
148                                                         picojson::object& out) {
149   ScopeLogger();
150   PlatformResult status = manager_.SetVolumeChangeListener();
151
152   if (status.IsSuccess()) {
153     ReportSuccess(out);
154   } else {
155     LogAndReportError(status, &out);
156   }
157 }
158
159 void SoundInstance::SoundManagerUnsetVolumeChangeListener(const picojson::value& args,
160                                                           picojson::object& out) {
161   ScopeLogger();
162   PlatformResult status = manager_.UnsetVolumeChangeListener();
163
164   if (status.IsSuccess()) {
165     ReportSuccess(out);
166   } else {
167     LogAndReportError(status, &out);
168   }
169 }
170
171 void SoundInstance::SoundManagerGetConnectedDeviceList(const picojson::value& args,
172                                                        picojson::object& out) {
173   ScopeLogger();
174   manager_.GetDeviceList(SOUND_DEVICE_ALL_MASK, out);
175 }
176
177 void SoundInstance::SoundManagerGetActivatedDeviceList(const picojson::value& args,
178                                                        picojson::object& out) {
179   ScopeLogger();
180   manager_.GetDeviceList((sound_device_mask_e)SOUND_DEVICE_STATE_DEPRECATED_ACTIVATED_MASK, out);
181 }
182
183 void SoundInstance::SoundManagerAddDeviceStateChangeListener(const picojson::value& args,
184                                                              picojson::object& out) {
185   ScopeLogger();
186   PlatformResult result = manager_.AddDeviceStateChangeListener();
187
188   if (result.IsSuccess()) {
189     ReportSuccess(out);
190   } else {
191     LogAndReportError(result, &out);
192   }
193 }
194
195 void SoundInstance::SoundManagerRemoveDeviceStateChangeListener(const picojson::value& args,
196                                                                 picojson::object& out) {
197   ScopeLogger();
198   PlatformResult result = manager_.RemoveDeviceStateChangeListener();
199
200   if (result.IsSuccess()) {
201     ReportSuccess(out);
202   } else {
203     LogAndReportError(result, &out);
204   }
205 }
206
207 #undef CHECK_EXIST
208
209 }  // namespace sound
210 }  // namespace extension