[sound] Fix double firing of SoundModeChangeCallback
[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
27 namespace extension {
28 namespace sound {
29
30 namespace {
31 // The privileges that required in Sound API
32 const std::string kPrivilegeSound = "http://tizen.org/privilege/volume.set";
33
34 }  // namespace
35
36 using namespace common;
37 using namespace extension::sound;
38
39 SoundInstance::SoundInstance() : manager_(*this) {
40   ScopeLogger();
41   using std::placeholders::_1;
42   using std::placeholders::_2;
43
44 #define REGISTER_SYNC(c, x) RegisterSyncHandler(c, std::bind(&SoundInstance::x, this, _1, _2));
45   REGISTER_SYNC("SoundManager_setVolume", SoundManagerSetVolume);
46   REGISTER_SYNC("SoundManager_unsetSoundModeChangeListener",
47                 SoundManagerUnsetSoundModeChangeListener);
48   REGISTER_SYNC("SoundManager_getVolume", SoundManagerGetVolume);
49   REGISTER_SYNC("SoundManager_unsetVolumeChangeListener", SoundManagerUnsetVolumeChangeListener);
50   REGISTER_SYNC("SoundManager_setSoundModeChangeListener", SoundManagerSetSoundModeChangeListener);
51   REGISTER_SYNC("SoundManager_setVolumeChangeListener", SoundManagerSetVolumeChangeListener);
52   REGISTER_SYNC("SoundManager_getSoundMode", SoundManagerGetSoundMode);
53   REGISTER_SYNC("SoundManager_getConnectedDeviceList", SoundManagerGetConnectedDeviceList);
54   REGISTER_SYNC("SoundManager_getActivatedDeviceList", SoundManagerGetActivatedDeviceList);
55   REGISTER_SYNC("SoundManager_addDeviceStateChangeListener",
56                 SoundManagerAddDeviceStateChangeListener);
57   REGISTER_SYNC("SoundManager_removeDeviceStateChangeListener",
58                 SoundManagerRemoveDeviceStateChangeListener);
59 #undef REGISTER_SYNC
60 }
61
62 SoundInstance::~SoundInstance() {
63   ScopeLogger();
64 }
65
66 #define CHECK_EXIST(args, name, out)                                             \
67   if (!args.contains(name)) {                                                    \
68     LogAndReportError(TypeMismatchException(name " is required argument"), out); \
69     return;                                                                      \
70   }
71
72 void SoundInstance::SoundManagerGetSoundMode(const picojson::value& args, picojson::object& out) {
73   ScopeLogger();
74   std::string sound_mode_type;
75   PlatformResult status = manager_.GetSoundMode(&sound_mode_type);
76
77   if (status.IsSuccess()) {
78     ReportSuccess(picojson::value(sound_mode_type), out);
79   } else {
80     LogAndReportError(status, &out);
81   }
82 }
83
84 void SoundInstance::SoundManagerSetVolume(const picojson::value& args, picojson::object& out) {
85   ScopeLogger();
86   CHECK_PRIVILEGE_ACCESS(kPrivilegeSound, &out);
87   PlatformResult status = manager_.SetVolume(args.get<picojson::object>());
88
89   if (status.IsSuccess()) {
90     ReportSuccess(out);
91   } else {
92     LogAndReportError(status, &out);
93   }
94 }
95
96 void SoundInstance::SoundManagerGetVolume(const picojson::value& args, picojson::object& out) {
97   ScopeLogger();
98   double volume;
99   PlatformResult status = manager_.GetVolume(args.get<picojson::object>(), &volume);
100
101   if (status.IsSuccess()) {
102     ReportSuccess(picojson::value(volume), out);
103   } else {
104     LogAndReportError(status, &out);
105   }
106 }
107
108 void SoundInstance::SoundManagerSetSoundModeChangeListener(const picojson::value& args,
109                                                            picojson::object& out) {
110   ScopeLogger();
111   PlatformResult status = manager_.SetSoundModeChangeListener(this);
112
113   if (status.IsSuccess()) {
114     ReportSuccess(out);
115   } else {
116     LogAndReportError(status, &out);
117   }
118 }
119
120 void SoundInstance::SoundManagerUnsetSoundModeChangeListener(const picojson::value& args,
121                                                              picojson::object& out) {
122   PlatformResult status = manager_.UnsetSoundModeChangeListener();
123
124   ScopeLogger();
125
126   if (status.IsSuccess()) {
127     ReportSuccess(out);
128   } else {
129     LogAndReportError(status, &out);
130   }
131 }
132
133 void SoundInstance::OnSoundModeChange(const std::string& newmode) {
134   ScopeLogger();
135   if (current_sound_mode == newmode) {
136     LoggerD("New sound mode equals to current sound mode");
137     return;
138   }
139   current_sound_mode = newmode;
140   picojson::value event = picojson::value(picojson::object());
141   picojson::object& obj = event.get<picojson::object>();
142   picojson::value result = picojson::value(newmode);
143   ReportSuccess(result, obj);
144   obj["listenerId"] = picojson::value("SoundModeChangeListener");
145   LoggerD("Posting: %s", event.serialize().c_str());
146   Instance::PostMessage(this, event.serialize().c_str());
147 }
148
149 void SoundInstance::SoundManagerSetVolumeChangeListener(const picojson::value& args,
150                                                         picojson::object& out) {
151   ScopeLogger();
152   PlatformResult status = manager_.SetVolumeChangeListener();
153
154   if (status.IsSuccess()) {
155     ReportSuccess(out);
156   } else {
157     LogAndReportError(status, &out);
158   }
159 }
160
161 void SoundInstance::SoundManagerUnsetVolumeChangeListener(const picojson::value& args,
162                                                           picojson::object& out) {
163   ScopeLogger();
164   PlatformResult status = manager_.UnsetVolumeChangeListener();
165
166   if (status.IsSuccess()) {
167     ReportSuccess(out);
168   } else {
169     LogAndReportError(status, &out);
170   }
171 }
172
173 void SoundInstance::SoundManagerGetConnectedDeviceList(const picojson::value& args,
174                                                        picojson::object& out) {
175   ScopeLogger();
176   manager_.GetDeviceList(SOUND_DEVICE_ALL_MASK, out);
177 }
178
179 void SoundInstance::SoundManagerGetActivatedDeviceList(const picojson::value& args,
180                                                        picojson::object& out) {
181   ScopeLogger();
182   manager_.GetDeviceList(SOUND_DEVICE_STATE_ACTIVATED_MASK, out);
183 }
184
185 void SoundInstance::SoundManagerAddDeviceStateChangeListener(const picojson::value& args,
186                                                              picojson::object& out) {
187   ScopeLogger();
188   PlatformResult result = manager_.AddDeviceStateChangeListener();
189
190   if (result.IsSuccess()) {
191     ReportSuccess(out);
192   } else {
193     LogAndReportError(result, &out);
194   }
195 }
196
197 void SoundInstance::SoundManagerRemoveDeviceStateChangeListener(const picojson::value& args,
198                                                                 picojson::object& out) {
199   ScopeLogger();
200   PlatformResult result = manager_.RemoveDeviceStateChangeListener();
201
202   if (result.IsSuccess()) {
203     ReportSuccess(out);
204   } else {
205     LogAndReportError(result, &out);
206   }
207 }
208
209 #undef CHECK_EXIST
210
211 }  // namespace sound
212 }  // namespace extension