1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
|
/*
* Copyright 2009, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <build/build_config.h>
#ifdef OS_WIN
#include <windows.h>
#include <shellapi.h>
#endif // OS_WIN
#include <string>
#include <algorithm>
#include "core/cross/renderer.h"
#include "core/cross/client_info.h"
#include "plugin/cross/o3d_glue.h"
#include "plugin/cross/config.h"
#include "plugin/cross/stream_manager.h"
#include "client_glue.h"
#include "globals_glue.h"
#include "third_party/nixysa/static_glue/npapi/common.h"
#ifdef OS_MACOSX
#include "plugin_mac.h"
#endif
namespace glue {
namespace _o3d {
void RegisterType(NPP npp, const ObjectBase::Class *clientclass,
NPClass *npclass) {
PluginObject *plugin_object = static_cast<PluginObject *>(npp->pdata);
plugin_object->RegisterType(clientclass, npclass);
}
bool CheckObject(NPP npp, NPObject *npobject,
const ObjectBase::Class *clientclass) {
PluginObject *plugin_object = static_cast<PluginObject *>(npp->pdata);
return plugin_object->CheckObject(npobject, clientclass);
}
NPAPIObject *GetNPObject(NPP npp, ObjectBase *object) {
PluginObject *plugin_object = static_cast<PluginObject *>(npp->pdata);
return plugin_object->GetNPObject(object);
}
NPObject *Allocate(NPP npp, NPClass *npclass) {
return new NPAPIObject(npp);
}
void Deallocate(NPObject *object) {
NPAPIObject *npobject = static_cast<NPAPIObject *>(object);
if (npobject->mapped()) {
NPP npp = npobject->npp();
PluginObject *plugin_object = static_cast<PluginObject *>(npp->pdata);
plugin_object->UnmapObject(npobject);
}
delete npobject;
}
ServiceLocator *GetServiceLocator(NPP npp) {
PluginObject *plugin_object = static_cast<PluginObject *>(npp->pdata);
return plugin_object->service_locator();
}
Client *GetClient(NPP npp) {
PluginObject *plugin_object = static_cast<PluginObject *>(npp->pdata);
return plugin_object->client();
}
PluginObject::PluginObject(NPP npp)
: npp_(npp),
evaluation_counter_(&service_locator_),
class_manager_(&service_locator_),
client_info_manager_(&service_locator_),
object_manager_(&service_locator_),
profiler_(&service_locator_),
main_thread_task_poster_(&service_locator_, npp),
fullscreen_(false),
renderer_(NULL),
features_(NULL),
fullscreen_region_valid_(false),
renderer_init_status_(Renderer::UNINITIALIZED),
pending_ticks_(0),
#ifdef OS_WIN
hWnd_(NULL),
plugin_hWnd_(NULL),
content_hWnd_(NULL),
got_dblclick_(false),
painted_once_(false),
#endif
#ifdef OS_MACOSX
mac_fullscreen_state_(NULL),
renderer_is_software_(false),
scroll_is_in_progress_(false),
drawing_model_(NPDrawingModelQuickDraw),
event_model_(NPEventModelCarbon),
mac_window_(0),
mac_fullscreen_window_(0),
mac_fullscreen_overlay_window_(0),
mac_window_selected_tab_(0),
mac_cocoa_window_(0),
mac_surface_hidden_(0),
mac_2d_context_(0),
mac_agl_context_(0),
mac_cgl_context_(0),
last_mac_event_time_(0),
time_to_hide_overlay_(0.0),
#endif
#ifdef OS_LINUX
display_(NULL),
window_(0),
xt_widget_(NULL),
xt_app_context_(NULL),
xt_interval_(0),
last_click_time_(0),
drawable_(0),
gtk_container_(NULL),
gtk_fullscreen_container_(NULL),
gtk_event_source_(NULL),
event_handler_id_(0),
timeout_id_(0),
fullscreen_pending_(false),
draw_(true),
in_plugin_(false),
#endif
np_v8_bridge_(&service_locator_, npp),
stream_manager_(new StreamManager(npp)),
cursor_type_(o3d::Cursor::DEFAULT),
prev_width_(0),
prev_height_(0) {
#ifdef OS_WIN
memset(cursors_, 0, sizeof(cursors_));
#endif
#ifdef OS_MACOSX
memset(last_buffer_rect_, 0, sizeof(last_buffer_rect_));
#endif
#ifdef OS_LINUX
memset(got_double_click_, 0, sizeof(got_double_click_));
#endif
// create an O3D object
client_ = new Client(&service_locator_);
globals_npobject_ = glue::CreateStaticNPObject(npp);
client_npobject_ =
glue::namespace_o3d::class_Client::GetNPObject(npp, client_);
user_agent_ = o3d::GetUserAgent(npp);
}
PluginObject::~PluginObject() {
}
void PluginObject::Init(int argc, char* argn[], char* argv[]) {
DCHECK(argc == 0 || (argn != NULL && argv != NULL));
features_ = new Features(&service_locator_);
std::string o3d_name("o3d_features");
for (int ii = 0; ii < argc; ++ii) {
DCHECK(argn[ii]);
const char* name = argn[ii];
if (o3d_name.compare(name) == 0) {
DCHECK(argv[ii]);
features_->Init(argv[ii]);
break;
}
}
NPObject* np_window;
NPN_GetValue(npp_, NPNVWindowNPObject, &np_window);
o3d::NPObjectPtr<NPObject> np_window_ptr =
o3d::NPObjectPtr<NPObject>::AttachToReturned(np_window);
np_v8_bridge_.Initialize(np_window_ptr);
o3d::NPObjectPtr<NPObject> np_plugin_ptr(this);
np_v8_bridge_.SetGlobalProperty(o3d::String("plugin"),
np_plugin_ptr);
}
void PluginObject::TearDown() {
#ifdef OS_WIN
ClearPluginProperty(hWnd_);
#endif // OS_WIN
#ifdef OS_MACOSX
o3d::ReleaseSafariBrowserWindow(mac_cocoa_window_);
#endif
UnmapAll();
// Delete the StreamManager to cleanup any streams that are in midflight.
// This needs to happen here, before the client is deleted as the streams
// could be holding references to FileRequest objects.
stream_manager_.reset(NULL);
delete client_;
client_ = NULL;
// Release the graphics context before deletion.
DeleteRenderer();
delete features_;
features_ = NULL;
// There is a reference cycle between the V8 bridge and the plugin.
// Explicitly remove all V8 references during tear-down, so that the cycle is
// broken, and the reference counting system will successfully delete the
// plugin.
np_v8_bridge_.ReleaseNPObjects();
}
void PluginObject::CreateRenderer(const o3d::DisplayWindow& display_window) {
if (!o3d::CheckConfig(npp_)) {
renderer_init_status_ = o3d::Renderer::GPU_NOT_UP_TO_SPEC;
} else {
renderer_ = o3d::Renderer::CreateDefaultRenderer(&service_locator_);
DCHECK(renderer_);
// Attempt to initialize renderer.
renderer_init_status_ = renderer_->Init(display_window, false);
if (renderer_init_status_ != o3d::Renderer::SUCCESS) {
DeleteRenderer();
}
}
}
void PluginObject::DeleteRenderer() {
if (renderer_) {
delete renderer_;
renderer_ = NULL;
}
}
#ifdef OS_MACOSX
// These following 3 methods are needed for a Safari workaround - basically it
// does not notify us when our tab is hidden by tab switching, and just stops
// sending us events. The workaround is to keep track of when the browser last
// sent us an event, and what the selected tab was at that time. If we find that
// we are no longer getting events and the selected tab value has changed,
// DetectTabHiding() starts returning true when the timer calls it and code
// elsewhere can take action. SafariBrowserWindowForWindowRef() and
// SelectedTabForSafariBrowserWindow() are both written in such a way that
// non-Safari browsers should yield NULL, so DetectTabHiding would always
// return false and the workaround would not be triggered.
// Function gets notified every time we receive a Mac event.
// It records the time of the event and tries to read the selected tab value
// from Safari (on other browsers this tab value should always be NULL).
// Written so that last_mac_event_time_ is always valid or NULL.
void PluginObject::MacEventReceived() {
CFDateRef now = CFDateCreate(NULL, CFAbsoluteTimeGetCurrent());
CFDateRef previousTime = last_mac_event_time_;
last_mac_event_time_ = now;
if (previousTime != NULL) {
CFRelease(previousTime);
}
if (!mac_cocoa_window_) {
mac_cocoa_window_ = o3d::SafariBrowserWindowForWindowRef(mac_window_);
}
mac_window_selected_tab_ =
o3d::SelectedTabForSafariBrowserWindow(mac_cocoa_window_);
}
// Returns the time elapsed since the MacEventReceived function was last called.
CFTimeInterval PluginObject::TimeSinceLastMacEvent() {
CFTimeInterval elapsed = 0.0;
if (last_mac_event_time_ != NULL) {
CFDateRef now = CFDateCreate(NULL, CFAbsoluteTimeGetCurrent());
elapsed = CFDateGetTimeIntervalSinceDate(now, last_mac_event_time_);
CFRelease(now);
}
return elapsed;
}
// Detects if Safari has hidden our tab.
// The heuristic is that we have not received any Mac events for a certain time
// and also the value for selected tab is different from the value it had the
// last time we did get a Mac event.
bool PluginObject::DetectTabHiding() {
const CFTimeInterval kMacTimeOut = 0.2; // a fifth of a second
if (TimeSinceLastMacEvent() < kMacTimeOut)
return false;
if (!mac_cocoa_window_) {
mac_cocoa_window_ = o3d::SafariBrowserWindowForWindowRef(mac_window_);
}
return o3d::SelectedTabForSafariBrowserWindow(mac_cocoa_window_) !=
mac_window_selected_tab_;
}
// Pick a constant way out of Apple's 0-22 range for our "no theme cursor"
// constant.
const ThemeCursor kNoThemeCursorForThat = 1000;
// Map o3d cursors to Mac theme cursors if possible, otherwise return
// kNoThemeCursorForThat.
ThemeCursor O3DToMacThemeCursor(o3d::Cursor::CursorType cursor_type) {
switch (cursor_type) {
case o3d::Cursor::DEFAULT:
return kThemeArrowCursor;
case o3d::Cursor::NONE: // There is no standard blank cursor.
return kNoThemeCursorForThat;
case o3d::Cursor::CROSSHAIR:
return kThemeCrossCursor;
case o3d::Cursor::POINTER:
return kThemePointingHandCursor;
case o3d::Cursor::E_RESIZE:
return kThemeResizeRightCursor;
case o3d::Cursor::NE_RESIZE: // No diagonal resize directions on Mac.
return kNoThemeCursorForThat;
case o3d::Cursor::NW_RESIZE: // No diagonal resize directions on Mac.
return kNoThemeCursorForThat;
case o3d::Cursor::N_RESIZE:
return kThemeResizeUpCursor;
case o3d::Cursor::SE_RESIZE: // No diagonal resize directions on Mac.
return kNoThemeCursorForThat;
case o3d::Cursor::SW_RESIZE: // No diagonal resize directions on Mac.
return kNoThemeCursorForThat;
case o3d::Cursor::S_RESIZE:
return kThemeResizeDownCursor;
case o3d::Cursor::W_RESIZE:
return kThemeResizeLeftCursor;
case o3d::Cursor::MOVE:
return kThemeOpenHandCursor;
case o3d::Cursor::TEXT:
return kThemeIBeamCursor;
case o3d::Cursor::WAIT:
return kThemeWatchCursor;
case o3d::Cursor::PROGRESS:
return kThemeSpinningCursor;
case o3d::Cursor::HELP: // No standard Help cursor.
return kNoThemeCursorForThat;
}
return kNoThemeCursorForThat;
}
// Set cursor to the one specified in cursor_type_.
// TODO add support for cursors that don't have equivalent Mac
// theme cursors.
void PluginObject::PlatformSpecificSetCursor() {
if (cursor_type_ == o3d::Cursor::NONE) {
// hide cursor if visible
if (CGCursorIsVisible()) {
CGDisplayHideCursor(kCGDirectMainDisplay);
}
} else {
ThemeCursor the_id = O3DToMacThemeCursor(cursor_type_);
if (the_id != kNoThemeCursorForThat) {
SetThemeCursor(the_id);
} else {
// could add code here to set other cursors by other means
SetThemeCursor(kThemeArrowCursor);
}
// show cursor if hidden
if (!CGCursorIsVisible())
CGDisplayShowCursor(kCGDirectMainDisplay);
}
}
bool PluginObject::SetRendererIsSoftware(bool state) {
renderer_is_software_ = state;
ClientInfoManager* client_info_manager =
service_locator()->GetService<ClientInfoManager>();
client_info_manager->SetSoftwareRenderer(state);
}
#endif // OS_MACOSX
void PluginObject::RegisterType(const ObjectBase::Class *clientclass,
NPClass *npclass) {
client_to_np_class_map_[clientclass] = npclass;
np_to_client_class_map_[npclass] = clientclass;
}
bool PluginObject::CheckObject(NPObject *npobject,
const ObjectBase::Class *clientclass) const {
if (!npobject) return true;
NPClass *npclass = npobject->_class;
NPToClientClassMap::const_iterator it = np_to_client_class_map_.find(npclass);
if (it == np_to_client_class_map_.end()) return false;
if (static_cast<NPAPIObject *>(npobject)->npp()->pdata != this) {
// The object was created by another plug-in instance. Don't allow direct
// references to these objects, that would cause havok.
return false;
}
return ObjectBase::ClassIsA(it->second, clientclass);
}
NPAPIObject *PluginObject::GetNPObject(ObjectBase *object) {
if (!object) return NULL;
NPAPIObject *npobject = object_map_[object->id()];
if (!npobject) {
NPClass *npclass = GetNPClass(object->GetClass());
GLUE_PROFILE_START(npp_, "createobject");
npobject = static_cast<NPAPIObject *>(NPN_CreateObject(npp_, npclass));
GLUE_PROFILE_STOP(npp_, "createobject");
npobject->Initialize(object);
object_map_[object->id()] = npobject;
npobject->set_mapped(true);
} else {
GLUE_PROFILE_START(npp_, "retainobject");
NPN_RetainObject(npobject);
GLUE_PROFILE_STOP(npp_, "retainobject");
}
return npobject;
}
void PluginObject::UnmapObject(NPAPIObject *npobject) {
npobject->set_mapped(false);
object_map_.erase(npobject->id());
}
void PluginObject::UnmapAll() {
for (ClientObjectMap::iterator it = object_map_.begin();
it != object_map_.end(); ++it) {
it->second->set_mapped(false);
}
object_map_.clear();
}
NPClass *PluginObject::GetNPClass(const ObjectBase::Class *clientclass) {
const ObjectBase::Class *cursor = clientclass;
while (cursor) {
ClientToNPClassMap::const_iterator it =
client_to_np_class_map_.find(cursor);
if (it != client_to_np_class_map_.end()) {
NPClass *npclass = it->second;
if (cursor != clientclass) client_to_np_class_map_[clientclass] = npclass;
return npclass;
}
cursor = cursor->parent();
}
return NULL;
}
// Static function to handle log asserts in the FATAL ERROR case
void PluginObject::LogAssertHandlerFunction(const std::string& str) {
DLOG(ERROR) << "FATAL LOG ERROR: " << str;
}
enum {
PROP_CLIENT,
PROP_GPU_CONFIG,
NUM_PROPERTY_IDS
};
static NPIdentifier property_ids[NUM_PROPERTY_IDS];
static const NPUTF8 *property_names[NUM_PROPERTY_IDS] = {
"client",
"gpuConfig",
};
enum {
METHOD_EVAL,
NUM_METHOD_IDS,
};
static NPIdentifier method_ids[NUM_METHOD_IDS];
static const NPUTF8 *method_names[NUM_METHOD_IDS] = {
"eval",
};
static NPObject *PluginAllocate(NPP npp, NPClass *npclass) {
return new PluginObject(npp);
}
static void PluginDeallocate(NPObject *object) {
delete static_cast<PluginObject *>(object);
}
static bool PluginHasMethod(NPObject *header, NPIdentifier name) {
DebugScopedId id(name);
PluginObject *plugin_object = static_cast<PluginObject *>(header);
for (int i = 0; i < NUM_METHOD_IDS; ++i) {
if (name == method_ids[i]) {
return true;
}
}
NPObject *globals = plugin_object->globals_npobject();
return globals->_class->hasMethod(globals, name);
}
static bool PluginInvoke(NPObject *header, NPIdentifier name,
const NPVariant *args, uint32_t argCount,
NPVariant *np_result) {
DebugScopedId id(name);
PluginObject *plugin_object = static_cast<PluginObject *>(header);
if (name == method_ids[METHOD_EVAL]) {
return plugin_object->np_v8_bridge()->Evaluate(args, argCount, np_result);
} else {
NPObject *globals = plugin_object->globals_npobject();
return globals->_class->invoke(globals, name, args, argCount, np_result);
}
}
static bool PluginInvokeDefault(NPObject *header, const NPVariant *args,
uint32_t argCount, NPVariant *result) {
PluginObject *plugin_object = static_cast<PluginObject *>(header);
NPP npp = plugin_object->npp();
NPObject *globals = plugin_object->globals_npobject();
return globals->_class->invokeDefault(globals, args, argCount, result);
}
static bool PluginHasProperty(NPObject *header, NPIdentifier name) {
DebugScopedId id(name);
PluginObject *plugin_object = static_cast<PluginObject *>(header);
NPP npp = plugin_object->npp();
for (unsigned int i = 0; i < NUM_PROPERTY_IDS; ++i) {
if (name == property_ids[i]) return true;
}
NPObject *globals = plugin_object->globals_npobject();
return globals->_class->hasProperty(globals, name);
}
static bool PluginGetProperty(NPObject *header, NPIdentifier name,
NPVariant *variant) {
DebugScopedId id(name);
PluginObject *plugin_object = static_cast<PluginObject *>(header);
NPP npp = plugin_object->npp();
if (name == property_ids[PROP_GPU_CONFIG]) {
// Gets the GPU config (VendorID, DeviceID, name) as a string.
// NOTE: this should probably be removed before we ship.
o3d::GPUDevice device;
bool result = o3d::GetGPUDevice(npp, &device);
if (!result) return false;
std::string return_value = std::string("VendorID = 0x");
char id_text[9];
base::snprintf(id_text, sizeof(id_text), "%04x", device.vendor_id);
return_value += id_text;
return_value += ", DeviceID = 0x";
base::snprintf(id_text, sizeof(id_text), "%04x", device.device_id);
return_value += id_text;
return_value += ", DeviceName = '";
return_value += device.name + "'";
return_value += ", Driver = '";
return_value += device.driver + "'";
return_value += ", Description = '";
return_value += device.description + "'";
return_value += ", GUID = 0x";
base::snprintf(id_text, sizeof(id_text), "%08x", device.guid);
return_value += id_text;
GLUE_PROFILE_START(npp, "StringToNPVariant");
bool temp = StringToNPVariant(return_value, variant);
GLUE_PROFILE_STOP(npp, "StringToNPVariant");
return temp;
}
if (name == property_ids[PROP_CLIENT]) {
NPObject *npobject = plugin_object->client_npobject();
GLUE_PROFILE_START(npp, "retainobject");
NPN_RetainObject(npobject);
GLUE_PROFILE_STOP(npp, "retainobject");
OBJECT_TO_NPVARIANT(npobject, *variant);
return true;
}
NPObject *globals = plugin_object->globals_npobject();
return globals->_class->getProperty(globals, name, variant);
}
static bool PluginSetProperty(NPObject *header, NPIdentifier name,
const NPVariant *variant) {
DebugScopedId id(name);
PluginObject *plugin_object = static_cast<PluginObject *>(header);
NPP npp = plugin_object->npp();
if (name == property_ids[PROP_CLIENT]) {
return false;
}
NPObject *globals = plugin_object->globals_npobject();
return globals->_class->setProperty(globals, name, variant);
}
static bool PluginEnumerate(NPObject *header, NPIdentifier **value,
uint32_t *count) {
*count = NUM_PROPERTY_IDS + NUM_METHOD_IDS + glue::GetStaticPropertyCount();
PluginObject *plugin_object = static_cast<PluginObject *>(header);
NPP npp = plugin_object->npp();
GLUE_PROFILE_START(npp, "memalloc");
*value = static_cast<NPIdentifier *>(
NPN_MemAlloc(*count * sizeof(NPIdentifier)));
GLUE_PROFILE_STOP(npp, "memalloc");
memcpy(*value, property_ids, NUM_PROPERTY_IDS * sizeof(NPIdentifier));
memcpy(*value + NUM_PROPERTY_IDS, method_ids,
NUM_METHOD_IDS * sizeof(NPIdentifier));
glue::StaticEnumeratePropertyHelper(
*value + NUM_PROPERTY_IDS + NUM_METHOD_IDS);
return true;
}
static NPClass plugin_npclass = {
NP_CLASS_STRUCT_VERSION,
PluginAllocate,
PluginDeallocate,
0,
PluginHasMethod,
PluginInvoke,
0,
PluginHasProperty,
PluginGetProperty,
PluginSetProperty,
0,
PluginEnumerate,
};
PluginObject *PluginObject::Create(NPP npp) {
GLUE_PROFILE_START(npp, "createobject");
PluginObject *plugin_object =
static_cast<PluginObject *>(NPN_CreateObject(npp, &plugin_npclass));
GLUE_PROFILE_STOP(npp, "createobject");
return plugin_object;
}
void InitializeGlue(NPP npp) {
GLUE_PROFILE_START(npp, "getstringidentifiers");
NPN_GetStringIdentifiers(property_names, NUM_PROPERTY_IDS, property_ids);
NPN_GetStringIdentifiers(method_names, NUM_METHOD_IDS, method_ids);
GLUE_PROFILE_STOP(npp, "getstringidentifiers");
glue::InitializeGlue(npp);
}
// Plugin has been resized.
void PluginObject::Resize(int width, int height) {
// check that the window size has actually changed
if (prev_width_ != width || prev_height_ != height) {
prev_width_ = width;
prev_height_ = height;
if (renderer_ && !fullscreen_) {
// Tell the renderer and client that our window has been resized.
// If we're in fullscreen mode when this happens, we don't want to pass
// the information through; the renderer will pick it up when we switch
// back to plugin mode.
renderer_->Resize(width, height);
// This is just so that the client can send an event to the user.
client()->SendResizeEvent(width, height, fullscreen_);
}
}
}
int PluginObject::width() const {
if (renderer_) {
return renderer_->width();
}
return 0;
}
int PluginObject::height() const {
if (renderer_) {
return renderer_->height();
}
return 0;
}
bool PluginObject::SetFullscreenClickRegion(int x, int y, int width, int height,
int mode_id) {
bool success = false;
o3d::DisplayMode mode;
// Make sure it's a valid ID first.
if (GetDisplayMode(mode_id, &mode)) {
fullscreen_region_valid_ = true;
fullscreen_region_x_ = x;
fullscreen_region_y_ = y;
fullscreen_region_width_ = width;
fullscreen_region_height_ = height;
fullscreen_region_mode_id_ = mode_id;
success = true;
}
return success;
}
// On Mac there is a different implementation in plugin_mac.mm.
#ifndef OS_MACOSX
void PluginObject::GetDisplayModes(std::vector<o3d::DisplayMode> *modes) {
if (renderer()) {
renderer()->GetDisplayModes(modes);
} else {
modes->clear();
}
}
#endif
void PluginObject::RedirectToFile(const char *url) {
char cmd[] = "window.location = 'file:///%s';";
scoped_array<char> full_cmd(new char[strlen(url) + sizeof(cmd)]);
sprintf(full_cmd.get(), cmd, url);
NPObject *global_object;
NPN_GetValue(npp(), NPNVWindowNPObject, &global_object);
NPString string;
string.utf8characters = full_cmd.get();
string.utf8length = strlen(string.utf8characters);
NPVariant result;
bool temp = NPN_Evaluate(npp(), global_object, &string, &result);
if (temp) {
NPN_ReleaseVariantValue(&result);
}
}
o3d::Cursor::CursorType PluginObject::cursor() const {
return cursor_type_;
}
void PluginObject::set_cursor(o3d::Cursor::CursorType cursor_type) {
cursor_type_ = cursor_type;
PlatformSpecificSetCursor();
}
#ifdef OS_WIN
static const wchar_t* kWindowPropertyName = L"o3d";
void PluginObject::StorePluginProperty(HWND hWnd, PluginObject *obj) {
if (obj->GetHWnd()) { // Clear out the record from the old window first.
ClearPluginProperty(obj->GetHWnd());
}
obj->SetHWnd(hWnd);
StorePluginPropertyUnsafe(hWnd, obj);
}
void PluginObject::StorePluginPropertyUnsafe(HWND hWnd, PluginObject *obj) {
if (hWnd) {
SetProp(hWnd, kWindowPropertyName, static_cast<HANDLE>(obj));
::DragAcceptFiles(hWnd, true);
}
}
PluginObject *PluginObject::GetPluginProperty(HWND hWnd) {
return static_cast<PluginObject*>(GetProp(hWnd, kWindowPropertyName));
}
void PluginObject::ClearPluginProperty(HWND hWnd) {
if (hWnd) {
RemoveProp(hWnd, kWindowPropertyName);
::DragAcceptFiles(hWnd, false);
}
}
static LPCTSTR O3DToWindowsCursor(o3d::Cursor::CursorType cursor_type) {
switch (cursor_type) {
case o3d::Cursor::DEFAULT:
return IDC_ARROW;
case o3d::Cursor::NONE:
return NULL;
case o3d::Cursor::CROSSHAIR:
return IDC_CROSS;
case o3d::Cursor::POINTER:
return IDC_HAND;
case o3d::Cursor::E_RESIZE:
return IDC_SIZEWE;
case o3d::Cursor::NE_RESIZE:
return IDC_SIZENESW;
case o3d::Cursor::NW_RESIZE:
return IDC_SIZENWSE;
case o3d::Cursor::N_RESIZE:
return IDC_SIZENS;
case o3d::Cursor::SE_RESIZE:
return IDC_SIZENWSE;
case o3d::Cursor::SW_RESIZE:
return IDC_SIZENESW;
case o3d::Cursor::S_RESIZE:
return IDC_SIZENS;
case o3d::Cursor::W_RESIZE:
return IDC_SIZEWE;
case o3d::Cursor::MOVE:
return IDC_SIZEALL;
case o3d::Cursor::TEXT:
return IDC_IBEAM;
case o3d::Cursor::WAIT:
return IDC_WAIT;
case o3d::Cursor::PROGRESS:
return IDC_APPSTARTING;
case o3d::Cursor::HELP:
return IDC_HELP;
}
return IDC_ARROW;
}
void PluginObject::PlatformSpecificSetCursor() {
if (!cursors_[cursor_type_]) {
cursors_[cursor_type_] = ::LoadCursor(NULL,
O3DToWindowsCursor(cursor_type_));
}
::SetCursor(cursors_[cursor_type_]);
}
#endif // OS_WIN
#ifdef OS_LINUX
void PluginObject::PlatformSpecificSetCursor() {
// TODO: fill this in.
}
#endif // OS_LINUX
namespace {
void TickPluginObject(void* data) {
PluginObject* plugin_object = static_cast<PluginObject*>(data);
// Check the plugin has not been destroyed already. Chrome sometimes invokes
// async callbacks after destruction.
if (!plugin_object->client())
return;
// Don't allow reentrancy through asynchronous ticks. Chrome sometimes does
// this. It is also possible for the asyncronous call to be invoked while
// a message is being handled. This prevents that.
Client::ScopedIncrement reentrance_count(plugin_object->client());
if (reentrance_count.get() > 1)
return;
plugin_object->Tick();
}
}
void PluginObject::AsyncTick() {
if (pending_ticks_ >= 1)
return;
++pending_ticks_;
// In Chrome NPN_PluginThreadAsyncCall doesn't seem to function properly.
// We resort to loading a data: url with zero bytes to get a tick callback
// asynchronously.
// TODO(vangelis): Remove this special path when Chrome's
// NPN_PluginThreadAsyncCall is fixed.
if (IsChrome()) {
class TickCallback : public StreamManager::FinishedCallback {
public:
explicit TickCallback(PluginObject* plugin_object)
: plugin_object_(plugin_object) {
}
virtual void Run(DownloadStream*,
bool,
const std::string&,
const std::string&) {
plugin_object_->Tick();
}
private:
PluginObject* plugin_object_;
};
if (!stream_manager_->LoadURL("data:,", NULL, NULL, NULL,
new TickCallback(this), NP_NORMAL)) {
DLOG(ERROR) << "Chrome failed to access data url";
// If the async call fails then tick synchronously.
Tick();
}
} else {
// Invoke Tick asynchronously if NPN_PluginThreadAsyncCall is supported.
// Otherwise invoke it synchronously.
int plugin_major, plugin_minor, browser_major, browser_minor;
NPN_Version(&plugin_major, &plugin_minor, &browser_major, &browser_minor);
if (browser_major > 0 ||
browser_minor >= NPVERS_HAS_PLUGIN_THREAD_ASYNC_CALL) {
NPN_PluginThreadAsyncCall(npp_, TickPluginObject, this);
} else {
Tick();
}
}
}
void PluginObject::Tick() {
DCHECK(pending_ticks_ > 0);
--pending_ticks_;
client_->Tick();
if (renderer_ && renderer_->need_to_render()) {
client_->RenderClient(true);
}
}
} // namespace _o3d
namespace globals {
using _o3d::PluginObject;
// This implements the definition in common.h of a function to receive
// all glue error reports.
void SetLastError(NPP npp, const char *error) {
PluginObject *plugin_object = static_cast<PluginObject *>(npp->pdata);
O3D_ERROR(plugin_object->service_locator()) << error;
}
// These implement the definitions in common.h of functions to receive
// all profiling calls.
void ProfileStart(NPP npp, const std::string& key) {
PluginObject *plugin_object = static_cast<PluginObject *>(npp->pdata);
if (plugin_object) { // May not be initialized yet.
plugin_object->client()->ProfileStart(key);
}
}
void ProfileStop(NPP npp, const std::string& key) {
PluginObject *plugin_object = static_cast<PluginObject *>(npp->pdata);
if (plugin_object) { // May not be initialized yet.
plugin_object->client()->ProfileStop(key);
}
}
void ProfileReset(NPP npp) {
PluginObject *plugin_object = static_cast<PluginObject *>(npp->pdata);
if (plugin_object) { // May not be initialized yet.
plugin_object->client()->ProfileReset();
}
}
std::string ProfileToString(NPP npp) {
PluginObject *plugin_object = static_cast<PluginObject *>(npp->pdata);
if (plugin_object) { // May not be initialized yet.
return plugin_object->client()->ProfileToString();
} else {
return "";
}
}
} // namespace globals
} // namespace glue
|