OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ppapi/c/dev/ppb_alarms_dev.h" |
| 6 #include "ppapi/c/dev/ppb_memory_dev.h" |
| 7 #include "ppapi/c/ppb_console.h" |
| 8 #include "ppapi/c/ppb_input_event.h" |
| 9 #include "ppapi/c/ppb_var.h" |
| 10 #include "ppapi/cpp/dev/alarms_dev.h" |
| 11 #include "ppapi/cpp/graphics_2d.h" |
| 12 #include "ppapi/cpp/image_data.h" |
| 13 #include "ppapi/cpp/input_event.h" |
| 14 #include "ppapi/cpp/instance.h" |
| 15 #include "ppapi/cpp/logging.h" |
| 16 #include "ppapi/cpp/module.h" |
| 17 #include "ppapi/cpp/rect.h" |
| 18 #include "ppapi/cpp/var.h" |
| 19 #include "ppapi/utility/completion_callback_factory.h" |
| 20 |
| 21 using namespace pp; |
| 22 |
| 23 const PPB_Console* console = NULL; |
| 24 const PPB_Alarms_Dev* alarms_interface = NULL; |
| 25 const PPB_Var* var_interface = NULL; |
| 26 |
| 27 class MyInstance : public Instance { |
| 28 public: |
| 29 explicit MyInstance(PP_Instance instance) |
| 30 : Instance(instance), |
| 31 width_(0), |
| 32 height_(0), |
| 33 pending_paint_(false), |
| 34 waiting_for_flush_completion_(false), |
| 35 callback_factory_(this), |
| 36 alarms_(InstanceHandle(instance)) { |
| 37 } |
| 38 virtual ~MyInstance() { |
| 39 } |
| 40 |
| 41 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
| 42 console = static_cast<const PPB_Console*>( |
| 43 Module::Get()->GetBrowserInterface(PPB_CONSOLE_INTERFACE)); |
| 44 if (!console) |
| 45 return false; |
| 46 |
| 47 alarms_interface = static_cast<const PPB_Alarms_Dev*>( |
| 48 Module::Get()->GetBrowserInterface(PPB_ALARMS_DEV_INTERFACE)); |
| 49 if (!alarms_interface) |
| 50 return false; |
| 51 |
| 52 var_interface = static_cast<const PPB_Var*>( |
| 53 Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); |
| 54 if (!var_interface) |
| 55 return false; |
| 56 |
| 57 RequestInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD); |
| 58 return true; |
| 59 } |
| 60 |
| 61 virtual bool HandleInputEvent(const InputEvent& event) { |
| 62 switch (event.GetType()) { |
| 63 case PP_INPUTEVENT_TYPE_KEYDOWN: { |
| 64 KeyboardInputEvent key_event(event); |
| 65 switch (key_event.GetKeyCode()) { |
| 66 ///////////////////////////////////////////////////////////////////// |
| 67 // Exercise the C interface PPB_Alarms_Dev. |
| 68 ///////////////////////////////////////////////////////////////////// |
| 69 case '1' : { // Create() |
| 70 PP_Var name = var_interface->VarFromUtf8("alarms2", |
| 71 strlen("alarms2")); |
| 72 PP_Alarms_AlarmCreateInfo_Dev create_info = { |
| 73 {0, PP_FALSE}, |
| 74 {1, PP_TRUE}, |
| 75 {1, PP_TRUE} |
| 76 }; |
| 77 |
| 78 alarms_interface->Create(pp_instance(), name, &create_info); |
| 79 |
| 80 var_interface->Release(name); |
| 81 break; |
| 82 } |
| 83 case '2' : { // Get() |
| 84 PP_Var name = var_interface->VarFromUtf8("alarms2", |
| 85 strlen("alarms2")); |
| 86 PP_Alarms_Alarm_Dev* alarm = new PP_Alarms_Alarm_Dev(); |
| 87 |
| 88 // GetCallbackInC() will delete |alarm|. |
| 89 // NOTE: |callback_factory_| should not be used in this case. |
| 90 // Otherwise, |alarm| is leaked if this object is destroyed before |
| 91 // the callback is executed. |
| 92 PP_CompletionCallback callback = PP_MakeCompletionCallback( |
| 93 &MyInstance::GetCallbackInC, alarm); |
| 94 |
| 95 int32_t result = alarms_interface->Get(pp_instance(), name, alarm, |
| 96 callback); |
| 97 PP_DCHECK(result == PP_OK_COMPLETIONPENDING); |
| 98 var_interface->Release(name); |
| 99 break; |
| 100 } |
| 101 case '3' : { // GetAll() |
| 102 PP_Alarms_Alarm_Array_Dev* alarms = new PP_Alarms_Alarm_Array_Dev(); |
| 103 PP_ArrayOutput array_allocator = |
| 104 { &MyInstance::GetDataBuffer, NULL }; |
| 105 |
| 106 // GetAllCallbackInC() will delete |alarms|. |
| 107 // NOTE: |callback_factory_| should not be used in this case. |
| 108 // Otherwise, |alarms| is leaked if this object is destroyed before |
| 109 // the callback is executed. |
| 110 PP_CompletionCallback callback = PP_MakeCompletionCallback( |
| 111 &MyInstance::GetAllCallbackInC, alarms); |
| 112 |
| 113 int32_t result = alarms_interface->GetAll( |
| 114 pp_instance(), alarms, array_allocator, callback); |
| 115 PP_DCHECK(result == PP_OK_COMPLETIONPENDING); |
| 116 break; |
| 117 } |
| 118 case '4' : { // Clear() |
| 119 PP_Var name = var_interface->VarFromUtf8("alarms2", |
| 120 strlen("alarms2")); |
| 121 alarms_interface->Clear(pp_instance(), name); |
| 122 var_interface->Release(name); |
| 123 break; |
| 124 } |
| 125 case '5' : { // Register event |
| 126 break; |
| 127 } |
| 128 case '6' : { // Unregister event |
| 129 break; |
| 130 } |
| 131 |
| 132 //////////////////////////////////////////////////////////////////////
|
| 133 // Exercise the C++ interface pp::alarms::Alarms_Dev. |
| 134 ////////////////////////////////////////////////////////////////////// |
| 135 case 'A' : { // Create() |
| 136 std::string name("alarm1"); |
| 137 |
| 138 alarms::AlarmCreateInfo_Dev create_info; |
| 139 create_info.set_delay_in_minutes(1); |
| 140 create_info.set_period_in_minutes(1); |
| 141 |
| 142 alarms_.Create(name, create_info); |
| 143 break; |
| 144 } |
| 145 case 'B' : { // Get() |
| 146 std::string name("alarm1"); |
| 147 |
| 148 CompletionCallbackWithOutput<alarms::Alarm_Dev> |
| 149 callback = callback_factory_.NewCallbackWithOutput( |
| 150 &MyInstance::GetCallbackInCpp); |
| 151 |
| 152 int32_t result = alarms_.Get(name, callback); |
| 153 PP_DCHECK(result == PP_OK_COMPLETIONPENDING); |
| 154 break; |
| 155 } |
| 156 case 'C' : { // GetAll() |
| 157 CompletionCallbackWithOutput<Array<alarms::Alarm_Dev> > |
| 158 callback = callback_factory_.NewCallbackWithOutput( |
| 159 &MyInstance::GetAllCallbackInCpp); |
| 160 |
| 161 int32_t result = alarms_.GetAll(callback); |
| 162 PP_DCHECK(result == PP_OK_COMPLETIONPENDING); |
| 163 break; |
| 164 } |
| 165 case 'D' : { // Clear() |
| 166 std::string name("alarm1"); |
| 167 alarms_.Clear(name); |
| 168 break; |
| 169 } |
| 170 case 'E' : { // Register event |
| 171 break; |
| 172 } |
| 173 case 'F' : { // Unregister event |
| 174 break; |
| 175 } |
| 176 default: |
| 177 break; |
| 178 } |
| 179 return true; |
| 180 } |
| 181 default: |
| 182 return false; |
| 183 } |
| 184 } |
| 185 |
| 186 virtual void DidChangeView(const Rect& position, const Rect& clip) { |
| 187 if (position.size().width() == width_ && |
| 188 position.size().height() == height_) |
| 189 return; // We don't care about the position, only the size. |
| 190 |
| 191 width_ = position.size().width(); |
| 192 height_ = position.size().height(); |
| 193 |
| 194 device_context_ = Graphics2D(this, Size(width_, height_), false); |
| 195 if (!BindGraphics(device_context_)) |
| 196 return; |
| 197 |
| 198 Paint(); |
| 199 } |
| 200 |
| 201 private: |
| 202 ////////////////////////////////////////////////////////////////////////////// |
| 203 // Exercise the C interface. |
| 204 ////////////////////////////////////////////////////////////////////////////// |
| 205 static void DestroyAlarmStruct(PP_Alarms_Alarm_Dev* alarm) { |
| 206 var_interface->Release(alarm->name); |
| 207 alarm->name = PP_MakeUndefined(); |
| 208 } |
| 209 |
| 210 static void* GetDataBuffer(void* user_data, uint32_t count, uint32_t size) { |
| 211 if (size != sizeof(PP_Alarms_Alarm_Dev)) { |
| 212 PP_NOTREACHED(); |
| 213 return NULL; |
| 214 } |
| 215 |
| 216 if (count == 0) |
| 217 return NULL; |
| 218 |
| 219 // We don't have to remember the pointer. The browser is responsible to |
| 220 // update |elements| and |size| in PP_Alarms_Alarm_Array_Dev. |
| 221 return calloc(count, size); |
| 222 } |
| 223 |
| 224 static void DestroyAlarmArrayStruct(PP_Alarms_Alarm_Array_Dev* alarms) { |
| 225 for (uint32_t i = 0; i < alarms->size; ++i) |
| 226 DestroyAlarmStruct(alarms->elements + i); |
| 227 |
| 228 free(alarms->elements); |
| 229 alarms->elements = NULL; |
| 230 alarms->size = 0; |
| 231 } |
| 232 |
| 233 static void GetAllCallbackInC(void* user_data, int32_t result) { |
| 234 PP_Alarms_Alarm_Array_Dev* alarms = |
| 235 static_cast<PP_Alarms_Alarm_Array_Dev*>(user_data); |
| 236 if (result == PP_OK) { |
| 237 for (uint32_t i = 0; i < alarms->size; ++i) { |
| 238 PP_Alarms_Alarm_Dev& element = alarms->elements[i]; |
| 239 // The following fields are available. |
| 240 element.name; |
| 241 element.scheduled_time; |
| 242 if (element.period_in_minutes.is_set) |
| 243 element.period_in_minutes.value; |
| 244 } |
| 245 } |
| 246 DestroyAlarmArrayStruct(alarms); |
| 247 delete alarms; |
| 248 } |
| 249 |
| 250 static void GetCallbackInC(void* user_data, int32_t result) { |
| 251 PP_Alarms_Alarm_Dev* alarm = static_cast<PP_Alarms_Alarm_Dev*>(user_data); |
| 252 if (result == PP_OK) { |
| 253 // The following fields are available. |
| 254 alarm->name; |
| 255 alarm->scheduled_time; |
| 256 if (alarm->period_in_minutes.is_set) |
| 257 alarm->period_in_minutes.value; |
| 258 } |
| 259 DestroyAlarmStruct(alarm); |
| 260 delete alarm; |
| 261 } |
| 262 |
| 263 ////////////////////////////////////////////////////////////////////////////// |
| 264 // Exercise the C++ interface. |
| 265 ////////////////////////////////////////////////////////////////////////////// |
| 266 void GetAllCallbackInCpp(int32_t result, |
| 267 Array<alarms::Alarm_Dev>& alarms) { |
| 268 if (result == PP_OK) { |
| 269 for (size_t i = 0; i < alarms.size(); ++i) { |
| 270 // The following fields are available. |
| 271 alarms::Alarm_Dev& alarm = alarms[i]; |
| 272 alarm.name(); |
| 273 alarm.scheduled_time(); |
| 274 if (alarm.is_period_in_minutes_set()) |
| 275 alarm.period_in_minutes(); |
| 276 } |
| 277 } |
| 278 } |
| 279 |
| 280 void GetCallbackInCpp(int32_t result, alarms::Alarm_Dev& alarm) { |
| 281 if (result == PP_OK) { |
| 282 // The following fields are available. |
| 283 alarm.name(); |
| 284 alarm.scheduled_time(); |
| 285 if (alarm.is_period_in_minutes_set()) |
| 286 alarm.period_in_minutes(); |
| 287 } |
| 288 } |
| 289 |
| 290 void DidFlush(int32_t result) { |
| 291 waiting_for_flush_completion_ = false; |
| 292 if (pending_paint_) { |
| 293 pending_paint_ = false; |
| 294 Paint(); |
| 295 } |
| 296 } |
| 297 |
| 298 void Paint() { |
| 299 if (waiting_for_flush_completion_) { |
| 300 pending_paint_ = true; |
| 301 return; |
| 302 } |
| 303 |
| 304 ImageData image = PaintImage(width_, height_); |
| 305 if (!image.is_null()) { |
| 306 device_context_.ReplaceContents(&image); |
| 307 waiting_for_flush_completion_ = true; |
| 308 device_context_.Flush( |
| 309 callback_factory_.NewCallback(&MyInstance::DidFlush)); |
| 310 } |
| 311 } |
| 312 |
| 313 ImageData PaintImage(int width, int height) { |
| 314 ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
| 315 Size(width, height), false); |
| 316 if (image.is_null()) |
| 317 return image; |
| 318 |
| 319 for (int y = 0; y < image.size().height(); ++y) { |
| 320 for (int x = 0; x < image.size().width(); ++x) { |
| 321 *image.GetAddr32(Point(x, y)) = 0xfff0f0f0; |
| 322 } |
| 323 } |
| 324 |
| 325 return image; |
| 326 } |
| 327 |
| 328 void Log(PP_LogLevel level, const char* format, ...) { |
| 329 va_list args; |
| 330 va_start(args, format); |
| 331 char buf[512]; |
| 332 vsnprintf(buf, sizeof(buf) - 1, format, args); |
| 333 buf[sizeof(buf) - 1] = '\0'; |
| 334 va_end(args); |
| 335 |
| 336 Var value(buf); |
| 337 console->Log(pp_instance(), level, value.pp_var()); |
| 338 } |
| 339 |
| 340 int width_; |
| 341 int height_; |
| 342 |
| 343 bool pending_paint_; |
| 344 bool waiting_for_flush_completion_; |
| 345 |
| 346 CompletionCallbackFactory<MyInstance> callback_factory_; |
| 347 |
| 348 alarms::Alarms_Dev alarms_; |
| 349 |
| 350 Graphics2D device_context_; |
| 351 }; |
| 352 |
| 353 // This object is the global object representing this plugin library as long |
| 354 // as it is loaded. |
| 355 class MyModule : public Module { |
| 356 public: |
| 357 MyModule() : Module() {} |
| 358 virtual ~MyModule() {} |
| 359 |
| 360 // Override CreateInstance to create your customized Instance object. |
| 361 virtual Instance* CreateInstance(PP_Instance instance) { |
| 362 return new MyInstance(instance); |
| 363 } |
| 364 }; |
| 365 |
| 366 namespace pp { |
| 367 |
| 368 // Factory function for your specialization of the Module object. |
| 369 Module* CreateModule() { |
| 370 return new MyModule(); |
| 371 } |
| 372 |
| 373 } // namespace pp |
| 374 |
OLD | NEW |