1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ppapi/cpp/extensions/dev/alarms_dev.h"
7 #include "ppapi/cpp/completion_callback.h"
8 #include "ppapi/cpp/extensions/optional.h"
9 #include "ppapi/cpp/extensions/to_var_converter.h"
10 #include "ppapi/cpp/logging.h"
11 #include "ppapi/cpp/module_impl.h"
12 #include "ppapi/cpp/var_dictionary.h"
18 template <> const char* interface_name
<PPB_Ext_Alarms_Dev_0_1
>() {
19 return PPB_EXT_ALARMS_DEV_INTERFACE_0_1
;
27 const char* const Alarm_Dev::kName
= "name";
28 const char* const Alarm_Dev::kScheduledTime
= "scheduledTime";
29 const char* const Alarm_Dev::kPeriodInMinutes
= "periodInMinutes";
31 Alarm_Dev::Alarm_Dev()
33 scheduled_time(kScheduledTime
),
34 period_in_minutes(kPeriodInMinutes
) {
37 Alarm_Dev::~Alarm_Dev() {
40 bool Alarm_Dev::Populate(const PP_Ext_Alarms_Alarm_Dev
& value
) {
41 if (value
.type
!= PP_VARTYPE_DICTIONARY
)
44 VarDictionary
dict(value
);
45 bool result
= name
.Populate(dict
);
46 result
= scheduled_time
.Populate(dict
) && result
;
47 result
= period_in_minutes
.Populate(dict
) && result
;
52 Var
Alarm_Dev::CreateVar() const {
55 bool result
= name
.AddTo(&dict
);
56 result
= scheduled_time
.AddTo(&dict
) && result
;
57 result
= period_in_minutes
.MayAddTo(&dict
) && result
;
63 const char* const AlarmCreateInfo_Dev::kWhen
= "when";
64 const char* const AlarmCreateInfo_Dev::kDelayInMinutes
= "delayInMinutes";
65 const char* const AlarmCreateInfo_Dev::kPeriodInMinutes
= "periodInMinutes";
67 AlarmCreateInfo_Dev::AlarmCreateInfo_Dev()
69 delay_in_minutes(kDelayInMinutes
),
70 period_in_minutes(kPeriodInMinutes
) {
73 AlarmCreateInfo_Dev::~AlarmCreateInfo_Dev() {
76 bool AlarmCreateInfo_Dev::Populate(
77 const PP_Ext_Alarms_AlarmCreateInfo_Dev
& value
) {
78 if (value
.type
!= PP_VARTYPE_DICTIONARY
)
81 VarDictionary
dict(value
);
82 bool result
= when
.Populate(dict
);
83 result
= delay_in_minutes
.Populate(dict
) && result
;
84 result
= period_in_minutes
.Populate(dict
) && result
;
89 Var
AlarmCreateInfo_Dev::CreateVar() const {
92 bool result
= when
.MayAddTo(&dict
);
93 result
= delay_in_minutes
.MayAddTo(&dict
) && result
;
94 result
= period_in_minutes
.MayAddTo(&dict
) && result
;
100 Alarms_Dev::Alarms_Dev(const InstanceHandle
& instance
) : instance_(instance
) {
103 Alarms_Dev::~Alarms_Dev() {
106 void Alarms_Dev::Create(const Optional
<std::string
>& name
,
107 const AlarmCreateInfo_Dev
& alarm_info
) {
108 if (!has_interface
<PPB_Ext_Alarms_Dev_0_1
>())
111 internal::ToVarConverter
<Optional
<std::string
> > name_var(name
);
112 internal::ToVarConverter
<AlarmCreateInfo_Dev
> alarm_info_var(alarm_info
);
114 return get_interface
<PPB_Ext_Alarms_Dev_0_1
>()->Create(
115 instance_
.pp_instance(),
117 alarm_info_var
.pp_var());
120 int32_t Alarms_Dev::Get(const Optional
<std::string
>& name
,
121 const GetCallback
& callback
) {
122 if (!has_interface
<PPB_Ext_Alarms_Dev_0_1
>())
123 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
125 internal::ToVarConverter
<Optional
<std::string
> > name_var(name
);
127 return get_interface
<PPB_Ext_Alarms_Dev_0_1
>()->Get(
128 instance_
.pp_instance(),
131 callback
.pp_completion_callback());
134 int32_t Alarms_Dev::GetAll(const GetAllCallback
& callback
) {
135 if (!has_interface
<PPB_Ext_Alarms_Dev_0_1
>())
136 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
138 return get_interface
<PPB_Ext_Alarms_Dev_0_1
>()->GetAll(
139 instance_
.pp_instance(),
141 callback
.pp_completion_callback());
144 void Alarms_Dev::Clear(const Optional
<std::string
>& name
) {
145 if (!has_interface
<PPB_Ext_Alarms_Dev_0_1
>())
148 internal::ToVarConverter
<Optional
<std::string
> > name_var(name
);
150 return get_interface
<PPB_Ext_Alarms_Dev_0_1
>()->Clear(
151 instance_
.pp_instance(),
155 void Alarms_Dev::ClearAll() {
156 if (!has_interface
<PPB_Ext_Alarms_Dev_0_1
>())
159 return get_interface
<PPB_Ext_Alarms_Dev_0_1
>()->ClearAll(
160 instance_
.pp_instance());
163 OnAlarmEvent_Dev::OnAlarmEvent_Dev(
164 const InstanceHandle
& instance
,
166 : internal::EventBase1
<PP_Ext_Alarms_OnAlarm_Dev
, Alarm_Dev
>(instance
),
167 listener_(listener
) {
170 OnAlarmEvent_Dev::~OnAlarmEvent_Dev() {
173 void OnAlarmEvent_Dev::Callback(Alarm_Dev
& alarm
) {
174 listener_
->OnAlarm(alarm
);
177 } // namespace alarms