Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: ppapi/example/example.cc

Issue 6667010: Add a console interface for logging to the JS console from a PPAPI plugin.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ppapi/c/dev/ppb_console_dev.h ('k') | ppapi/ppapi_cpp.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 13
14 #include "ppapi/c/dev/ppb_console_dev.h"
14 #include "ppapi/c/dev/ppp_printing_dev.h" 15 #include "ppapi/c/dev/ppp_printing_dev.h"
15 #include "ppapi/c/pp_errors.h" 16 #include "ppapi/c/pp_errors.h"
16 #include "ppapi/c/pp_input_event.h" 17 #include "ppapi/c/pp_input_event.h"
17 #include "ppapi/c/pp_rect.h" 18 #include "ppapi/c/pp_rect.h"
18 #include "ppapi/cpp/completion_callback.h" 19 #include "ppapi/cpp/completion_callback.h"
19 #include "ppapi/cpp/dev/scriptable_object_deprecated.h" 20 #include "ppapi/cpp/dev/scriptable_object_deprecated.h"
20 #include "ppapi/cpp/graphics_2d.h" 21 #include "ppapi/cpp/graphics_2d.h"
21 #include "ppapi/cpp/image_data.h" 22 #include "ppapi/cpp/image_data.h"
22 #include "ppapi/cpp/instance.h" 23 #include "ppapi/cpp/instance.h"
23 #include "ppapi/cpp/module.h" 24 #include "ppapi/cpp/module.h"
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 if (fetcher_) { 169 if (fetcher_) {
169 delete fetcher_; 170 delete fetcher_;
170 fetcher_ = NULL; 171 fetcher_ = NULL;
171 } 172 }
172 } 173 }
173 174
174 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { 175 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
175 return true; 176 return true;
176 } 177 }
177 178
179 void Log(PP_LogLevel_Dev level, const pp::Var& value) {
180 const PPB_Console_Dev* console = reinterpret_cast<const PPB_Console_Dev*>(
181 pp::Module::Get()->GetBrowserInterface(PPB_CONSOLE_DEV_INTERFACE));
182 if (!console)
183 return;
184 console->Log(pp_instance(), level, value.pp_var());
185 }
186
178 virtual bool HandleDocumentLoad(const pp::URLLoader& loader) { 187 virtual bool HandleDocumentLoad(const pp::URLLoader& loader) {
179 fetcher_ = new MyFetcher(); 188 fetcher_ = new MyFetcher();
180 fetcher_->StartWithOpenedLoader(loader, this); 189 fetcher_->StartWithOpenedLoader(loader, this);
181 return true; 190 return true;
182 } 191 }
183 192
184 virtual bool HandleInputEvent(const PP_InputEvent& event) { 193 virtual bool HandleInputEvent(const PP_InputEvent& event) {
185 switch (event.type) { 194 switch (event.type) {
186 case PP_INPUTEVENT_TYPE_MOUSEDOWN: 195 case PP_INPUTEVENT_TYPE_MOUSEDOWN:
187 //SayHello(); 196 SayHello();
188 return true; 197 return true;
189 case PP_INPUTEVENT_TYPE_MOUSEMOVE: 198 case PP_INPUTEVENT_TYPE_MOUSEMOVE:
190 return true; 199 return true;
191 case PP_INPUTEVENT_TYPE_KEYDOWN: 200 case PP_INPUTEVENT_TYPE_KEYDOWN:
192 return true; 201 return true;
193 default: 202 default:
194 return false; 203 return false;
195 } 204 }
196 } 205 }
197 206
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 pp::ImageData image = PaintImage(width_, height_); 244 pp::ImageData image = PaintImage(width_, height_);
236 if (!image.is_null()) { 245 if (!image.is_null()) {
237 device_context_.ReplaceContents(&image); 246 device_context_.ReplaceContents(&image);
238 device_context_.Flush(pp::CompletionCallback(&FlushCallback, this)); 247 device_context_.Flush(pp::CompletionCallback(&FlushCallback, this));
239 } else { 248 } else {
240 printf("NullImage: %d, %d\n", width_, height_); 249 printf("NullImage: %d, %d\n", width_, height_);
241 } 250 }
242 } 251 }
243 252
244 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) { 253 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) {
254 Log(PP_LOGLEVEL_LOG, "DidChangeView");
245 if (position.size().width() == width_ && 255 if (position.size().width() == width_ &&
246 position.size().height() == height_) 256 position.size().height() == height_)
247 return; // We don't care about the position, only the size. 257 return; // We don't care about the position, only the size.
248 258
249 width_ = position.size().width(); 259 width_ = position.size().width();
250 height_ = position.size().height(); 260 height_ = position.size().height();
251 printf("DidChangeView relevant change: width=%d height:%d\n", 261 printf("DidChangeView relevant change: width=%d height:%d\n",
252 width_, height_); 262 width_, height_);
253 263
254 device_context_ = pp::Graphics2D(this, pp::Size(width_, height_), false); 264 device_context_ = pp::Graphics2D(this, pp::Size(width_, height_), false);
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 } 345 }
336 346
337 void OnFlush() { 347 void OnFlush() {
338 if (animation_counter_ % kStepsPerCircle == 0) 348 if (animation_counter_ % kStepsPerCircle == 0)
339 UpdateFps(); 349 UpdateFps();
340 animation_counter_++; 350 animation_counter_++;
341 Paint(); 351 Paint();
342 } 352 }
343 353
344 private: 354 private:
345 void Log(const pp::Var& var) {
346 pp::Var doc = GetWindowObject().GetProperty("document");
347 if (console_.is_undefined()) {
348 pp::Var body = doc.GetProperty("body");
349 console_ = doc.Call("createElement", "pre");
350 console_.GetProperty("style").SetProperty("backgroundColor", "lightgray");
351 body.Call("appendChild", console_);
352 }
353 console_.Call("appendChild", doc.Call("createTextNode", var));
354 console_.Call("appendChild", doc.Call("createTextNode", "\n"));
355 }
356
357 void SayHello() { 355 void SayHello() {
358 pp::Var window = GetWindowObject(); 356 pp::Var window = GetWindowObject();
359 pp::Var doc = window.GetProperty("document"); 357 pp::Var doc = window.GetProperty("document");
360 pp::Var body = doc.GetProperty("body"); 358 pp::Var body = doc.GetProperty("body");
361 359
362 pp::Var obj(this, new MyScriptableObject(this)); 360 pp::Var obj(this, new MyScriptableObject(this));
363 361
364 // Our object should have its toString method called. 362 // Our object should have its toString method called.
365 Log("Testing MyScriptableObject::toString():"); 363 Log(PP_LOGLEVEL_LOG, "Testing MyScriptableObject::toString():");
366 Log(obj); 364 Log(PP_LOGLEVEL_LOG, obj);
367 365
368 // body.appendChild(body) should throw an exception 366 // body.appendChild(body) should throw an exception
369 Log("\nCalling body.appendChild(body):"); 367 Log(PP_LOGLEVEL_LOG, "Calling body.appendChild(body):");
370 pp::Var exception; 368 pp::Var exception;
371 body.Call("appendChild", body, &exception); 369 body.Call("appendChild", body, &exception);
372 Log(exception); 370 Log(PP_LOGLEVEL_LOG, exception);
373 371
374 Log("\nEnumeration of window properties:"); 372 Log(PP_LOGLEVEL_LOG, "Enumeration of window properties:");
375 std::vector<pp::Var> props; 373 std::vector<pp::Var> props;
376 window.GetAllPropertyNames(&props); 374 window.GetAllPropertyNames(&props);
377 for (size_t i = 0; i < props.size(); ++i) 375 for (size_t i = 0; i < props.size(); ++i)
378 Log(props[i]); 376 Log(PP_LOGLEVEL_LOG, props[i]);
379 377
380 pp::Var location = window.GetProperty("location"); 378 pp::Var location = window.GetProperty("location");
381 pp::Var href = location.GetProperty("href"); 379 pp::Var href = location.GetProperty("href");
382 380
383 if (!fetcher_) { 381 if (!fetcher_) {
384 fetcher_ = new MyFetcher(); 382 fetcher_ = new MyFetcher();
385 fetcher_->Start(*this, href, this); 383 fetcher_->Start(*this, href, this);
386 } 384 }
387 } 385 }
388 386
389 void DidFetch(bool success, const std::string& data) { 387 void DidFetch(bool success, const std::string& data) {
390 Log("\nDownloaded location.href:"); 388 Log(PP_LOGLEVEL_LOG, "Downloaded location.href:");
391 if (success) { 389 if (success) {
392 Log(data); 390 Log(PP_LOGLEVEL_LOG, data);
393 } else { 391 } else {
394 Log("Failed to download."); 392 Log(PP_LOGLEVEL_ERROR, "Failed to download.");
395 } 393 }
396 delete fetcher_; 394 delete fetcher_;
397 fetcher_ = NULL; 395 fetcher_ = NULL;
398 } 396 }
399 397
400 pp::Var console_; 398 pp::Var console_;
401 pp::Graphics2D device_context_; 399 pp::Graphics2D device_context_;
402 400
403 double time_at_last_check_; 401 double time_at_last_check_;
404 402
(...skipping 23 matching lines...) Expand all
428 }; 426 };
429 427
430 namespace pp { 428 namespace pp {
431 429
432 // Factory function for your specialization of the Module object. 430 // Factory function for your specialization of the Module object.
433 Module* CreateModule() { 431 Module* CreateModule() {
434 return new MyModule(); 432 return new MyModule();
435 } 433 }
436 434
437 } // namespace pp 435 } // namespace pp
OLDNEW
« no previous file with comments | « ppapi/c/dev/ppb_console_dev.h ('k') | ppapi/ppapi_cpp.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698