| OLD | NEW | 
|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <math.h> | 5 #include <math.h> | 
| 6 #include <stdio.h>  // FIXME(brettw) erase me. | 6 #include <stdio.h>  // FIXME(brettw) erase me. | 
| 7 #ifndef _WIN32 | 7 #ifndef _WIN32 | 
| 8 #include <sys/time.h> | 8 #include <sys/time.h> | 
| 9 #endif | 9 #endif | 
| 10 #include <time.h> | 10 #include <time.h> | 
| 11 | 11 | 
| 12 #include <algorithm> | 12 #include <algorithm> | 
| 13 #include <sstream> | 13 #include <sstream> | 
| 14 | 14 | 
| 15 #include "ppapi/c/dev/ppb_console_dev.h" | 15 #include "ppapi/c/dev/ppb_console_dev.h" | 
| 16 #include "ppapi/c/dev/ppb_cursor_control_dev.h" | 16 #include "ppapi/c/dev/ppb_cursor_control_dev.h" | 
| 17 #include "ppapi/c/dev/ppp_printing_dev.h" | 17 #include "ppapi/c/dev/ppp_printing_dev.h" | 
| 18 #include "ppapi/c/pp_errors.h" | 18 #include "ppapi/c/pp_errors.h" | 
| 19 #include "ppapi/c/pp_input_event.h" | 19 #include "ppapi/c/pp_input_event.h" | 
| 20 #include "ppapi/c/pp_rect.h" | 20 #include "ppapi/c/pp_rect.h" | 
| 21 #include "ppapi/c/ppb_var.h" | 21 #include "ppapi/c/ppb_var.h" | 
| 22 #include "ppapi/cpp/completion_callback.h" | 22 #include "ppapi/cpp/completion_callback.h" | 
| 23 #include "ppapi/cpp/dev/scriptable_object_deprecated.h" | 23 #include "ppapi/cpp/dev/scriptable_object_deprecated.h" | 
| 24 #include "ppapi/cpp/graphics_2d.h" | 24 #include "ppapi/cpp/graphics_2d.h" | 
| 25 #include "ppapi/cpp/image_data.h" | 25 #include "ppapi/cpp/image_data.h" | 
| 26 #include "ppapi/cpp/instance.h" | 26 #include "ppapi/cpp/instance.h" | 
| 27 #include "ppapi/cpp/module.h" | 27 #include "ppapi/cpp/module.h" | 
|  | 28 #include "ppapi/cpp/private/var_private.h" | 
| 28 #include "ppapi/cpp/rect.h" | 29 #include "ppapi/cpp/rect.h" | 
| 29 #include "ppapi/cpp/url_loader.h" | 30 #include "ppapi/cpp/url_loader.h" | 
| 30 #include "ppapi/cpp/url_request_info.h" | 31 #include "ppapi/cpp/url_request_info.h" | 
| 31 | 32 | 
| 32 static const int kStepsPerCircle = 800; | 33 static const int kStepsPerCircle = 800; | 
| 33 | 34 | 
| 34 void FlushCallback(void* data, int32_t result); | 35 void FlushCallback(void* data, int32_t result); | 
| 35 | 36 | 
| 36 void FillRect(pp::ImageData* image, int left, int top, int width, int height, | 37 void FillRect(pp::ImageData* image, int left, int top, int width, int height, | 
| 37               uint32_t color) { | 38               uint32_t color) { | 
| (...skipping 16 matching lines...) Expand all  Loading... | 
| 54   } | 55   } | 
| 55 | 56 | 
| 56   virtual bool HasProperty(const pp::Var& name, pp::Var* exception) { | 57   virtual bool HasProperty(const pp::Var& name, pp::Var* exception) { | 
| 57     if (name.is_string() && name.AsString() == "blah") | 58     if (name.is_string() && name.AsString() == "blah") | 
| 58       return true; | 59       return true; | 
| 59     return false; | 60     return false; | 
| 60   } | 61   } | 
| 61 | 62 | 
| 62   virtual pp::Var GetProperty(const pp::Var& name, pp::Var* exception) { | 63   virtual pp::Var GetProperty(const pp::Var& name, pp::Var* exception) { | 
| 63     if (name.is_string() && name.AsString() == "blah") | 64     if (name.is_string() && name.AsString() == "blah") | 
| 64       return pp::Var(instance_, new MyScriptableObject(instance_)); | 65       return pp::VarPrivate(instance_, new MyScriptableObject(instance_)); | 
| 65     return pp::Var(); | 66     return pp::Var(); | 
| 66   } | 67   } | 
| 67 | 68 | 
| 68   virtual void GetAllPropertyNames(std::vector<pp::Var>* names, | 69   virtual void GetAllPropertyNames(std::vector<pp::Var>* names, | 
| 69                                    pp::Var* exception) { | 70                                    pp::Var* exception) { | 
| 70     names->push_back("blah"); | 71     names->push_back("blah"); | 
| 71   } | 72   } | 
| 72 | 73 | 
| 73   virtual pp::Var Call(const pp::Var& method, | 74   virtual pp::Var Call(const pp::Var& method, | 
| 74                        const std::vector<pp::Var>& args, | 75                        const std::vector<pp::Var>& args, | 
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 196   void HandleKeyEvent(const PP_InputEvent_Key& key_event) { | 197   void HandleKeyEvent(const PP_InputEvent_Key& key_event) { | 
| 197     Log(PP_LOGLEVEL_LOG, "HandleKeyDownEvent"); | 198     Log(PP_LOGLEVEL_LOG, "HandleKeyDownEvent"); | 
| 198 | 199 | 
| 199     // Stringify the Windows-style and Native key codes | 200     // Stringify the Windows-style and Native key codes | 
| 200     std::ostringstream last_key_down_text; | 201     std::ostringstream last_key_down_text; | 
| 201     last_key_down_text << "vkey=" << key_event.key_code | 202     last_key_down_text << "vkey=" << key_event.key_code | 
| 202                        << " native=" << key_event.native_key_code | 203                        << " native=" << key_event.native_key_code | 
| 203                        << " usb=" << std::hex << key_event.usb_key_code; | 204                        << " usb=" << std::hex << key_event.usb_key_code; | 
| 204 | 205 | 
| 205     // Locate the field to update in the page DOM | 206     // Locate the field to update in the page DOM | 
| 206     pp::Var window = GetWindowObject(); | 207     pp::VarPrivate window = GetWindowObject(); | 
| 207     pp::Var doc = window.GetProperty("document"); | 208     pp::VarPrivate doc = window.GetProperty("document"); | 
| 208     pp::Var last_key_down = doc.Call("getElementById", "lastKeyDown"); | 209     pp::VarPrivate last_key_down = doc.Call("getElementById", "lastKeyDown"); | 
| 209     last_key_down.SetProperty("innerHTML", last_key_down_text.str()); | 210     last_key_down.SetProperty("innerHTML", last_key_down_text.str()); | 
| 210   } | 211   } | 
| 211 | 212 | 
| 212   virtual bool HandleInputEvent(const PP_InputEvent& event) { | 213   virtual bool HandleInputEvent(const PP_InputEvent& event) { | 
| 213     switch (event.type) { | 214     switch (event.type) { | 
| 214       case PP_INPUTEVENT_TYPE_MOUSEDOWN: | 215       case PP_INPUTEVENT_TYPE_MOUSEDOWN: | 
| 215         SayHello(); | 216         SayHello(); | 
| 216         ToggleCursor(); | 217         ToggleCursor(); | 
| 217         return true; | 218         return true; | 
| 218       case PP_INPUTEVENT_TYPE_MOUSEMOVE: | 219       case PP_INPUTEVENT_TYPE_MOUSEMOVE: | 
| 219         return true; | 220         return true; | 
| 220       case PP_INPUTEVENT_TYPE_KEYDOWN: | 221       case PP_INPUTEVENT_TYPE_KEYDOWN: | 
| 221         HandleKeyEvent(event.u.key); | 222         HandleKeyEvent(event.u.key); | 
| 222         return true; | 223         return true; | 
| 223       default: | 224       default: | 
| 224         return false; | 225         return false; | 
| 225     } | 226     } | 
| 226   } | 227   } | 
| 227 | 228 | 
| 228   virtual pp::Var GetInstanceObject() { | 229   virtual pp::Var GetInstanceObject() { | 
| 229     return pp::Var(this, new MyScriptableObject(this)); | 230     return pp::VarPrivate(this, new MyScriptableObject(this)); | 
| 230   } | 231   } | 
| 231 | 232 | 
| 232   pp::ImageData PaintImage(int width, int height) { | 233   pp::ImageData PaintImage(int width, int height) { | 
| 233     pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, | 234     pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, | 
| 234                         pp::Size(width, height), false); | 235                         pp::Size(width, height), false); | 
| 235     if (image.is_null()) { | 236     if (image.is_null()) { | 
| 236       printf("Couldn't allocate the image data: %d, %d\n", width, height); | 237       printf("Couldn't allocate the image data: %d, %d\n", width, height); | 
| 237       return image; | 238       return image; | 
| 238     } | 239     } | 
| 239 | 240 | 
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 287       printf("Couldn't bind the device context\n"); | 288       printf("Couldn't bind the device context\n"); | 
| 288       return; | 289       return; | 
| 289     } | 290     } | 
| 290 | 291 | 
| 291     Paint(); | 292     Paint(); | 
| 292   } | 293   } | 
| 293 | 294 | 
| 294   void UpdateFps() { | 295   void UpdateFps() { | 
| 295 // Time code doesn't currently compile on Windows, just skip FPS for now. | 296 // Time code doesn't currently compile on Windows, just skip FPS for now. | 
| 296 #ifndef _WIN32 | 297 #ifndef _WIN32 | 
| 297     pp::Var window = GetWindowObject(); | 298     pp::VarPrivate window = GetWindowObject(); | 
| 298     pp::Var doc = window.GetProperty("document"); | 299     pp::VarPrivate doc = window.GetProperty("document"); | 
| 299     pp::Var fps = doc.Call("getElementById", "fps"); | 300     pp::VarPrivate fps = doc.Call("getElementById", "fps"); | 
| 300 | 301 | 
| 301     struct timeval tv; | 302     struct timeval tv; | 
| 302     struct timezone tz = {0, 0}; | 303     struct timezone tz = {0, 0}; | 
| 303     gettimeofday(&tv, &tz); | 304     gettimeofday(&tv, &tz); | 
| 304 | 305 | 
| 305     double time_now = tv.tv_sec + tv.tv_usec / 1000000.0; | 306     double time_now = tv.tv_sec + tv.tv_usec / 1000000.0; | 
| 306 | 307 | 
| 307     if (animation_counter_ > 0) { | 308     if (animation_counter_ > 0) { | 
| 308       char fps_text[64]; | 309       char fps_text[64]; | 
| 309       sprintf(fps_text, "%g fps", | 310       sprintf(fps_text, "%g fps", | 
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 367 | 368 | 
| 368   void OnFlush() { | 369   void OnFlush() { | 
| 369     if (animation_counter_ % kStepsPerCircle == 0) | 370     if (animation_counter_ % kStepsPerCircle == 0) | 
| 370       UpdateFps(); | 371       UpdateFps(); | 
| 371     animation_counter_++; | 372     animation_counter_++; | 
| 372     Paint(); | 373     Paint(); | 
| 373   } | 374   } | 
| 374 | 375 | 
| 375  private: | 376  private: | 
| 376   void SayHello() { | 377   void SayHello() { | 
| 377     pp::Var window = GetWindowObject(); | 378     pp::VarPrivate window = GetWindowObject(); | 
| 378     pp::Var doc = window.GetProperty("document"); | 379     pp::VarPrivate doc = window.GetProperty("document"); | 
| 379     pp::Var body = doc.GetProperty("body"); | 380     pp::VarPrivate body = doc.GetProperty("body"); | 
| 380 | 381 | 
| 381     pp::Var obj(this, new MyScriptableObject(this)); | 382     pp::VarPrivate obj(this, new MyScriptableObject(this)); | 
| 382 | 383 | 
| 383     // Our object should have its toString method called. | 384     // Our object should have its toString method called. | 
| 384     Log(PP_LOGLEVEL_LOG, "Testing MyScriptableObject::toString():"); | 385     Log(PP_LOGLEVEL_LOG, "Testing MyScriptableObject::toString():"); | 
| 385     Log(PP_LOGLEVEL_LOG, obj); | 386     Log(PP_LOGLEVEL_LOG, obj); | 
| 386 | 387 | 
| 387     // body.appendChild(body) should throw an exception | 388     // body.appendChild(body) should throw an exception | 
| 388     Log(PP_LOGLEVEL_LOG, "Calling body.appendChild(body):"); | 389     Log(PP_LOGLEVEL_LOG, "Calling body.appendChild(body):"); | 
| 389     pp::Var exception; | 390     pp::Var exception; | 
| 390     body.Call("appendChild", body, &exception); | 391     body.Call("appendChild", body, &exception); | 
| 391     Log(PP_LOGLEVEL_LOG, exception); | 392     Log(PP_LOGLEVEL_LOG, exception); | 
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 473 }; | 474 }; | 
| 474 | 475 | 
| 475 namespace pp { | 476 namespace pp { | 
| 476 | 477 | 
| 477 // Factory function for your specialization of the Module object. | 478 // Factory function for your specialization of the Module object. | 
| 478 Module* CreateModule() { | 479 Module* CreateModule() { | 
| 479   return new MyModule(); | 480   return new MyModule(); | 
| 480 } | 481 } | 
| 481 | 482 | 
| 482 }  // namespace pp | 483 }  // namespace pp | 
| OLD | NEW | 
|---|