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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
|
/*
* 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/image_utils.h"
#include "core/cross/renderer.h"
#include "core/cross/texture.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 "third_party/nixysa/static_glue/npapi/npn_api.h"
#include "client_glue.h"
#include "globals_glue.h"
#ifdef OS_MACOSX
#include "plugin_mac.h"
#endif
#ifdef OS_LINUX
#include <X11/cursorfont.h>
#endif
namespace glue {
namespace _o3d {
using o3d::Bitmap;
using o3d::Texture;
using o3d::Texture2D;
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
renderer_is_software_(false),
scroll_is_in_progress_(false),
drawing_model_(NPDrawingModelQuickDraw),
event_model_(NPEventModelCarbon),
mac_window_(0),
mac_window_selected_tab_(0),
mac_cocoa_window_(0),
mac_surface_hidden_(0),
mac_cg_context_ref_(0),
mac_agl_context_(0),
mac_cgl_context_(0),
mac_cgl_pbuffer_(0),
last_mac_event_time_(0),
gl_layer_(NULL),
mac_fullscreen_window_(NULL),
mac_fullscreen_nsopenglcontext_(NULL),
mac_fullscreen_nsopenglpixelformat_(NULL),
was_offscreen_(false),
#endif // OS_MACOSX
#ifdef OS_LINUX
display_(NULL),
window_(0),
fullscreen_window_(0),
xt_widget_(NULL),
xt_app_context_(NULL),
xt_interval_(0),
last_click_time_(0),
drawable_(0),
gdk_display_(NULL),
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
#if defined(CB_SERVICE_REMOTE)
gpu_plugin_object_(NULL),
#endif
np_v8_bridge_(&service_locator_, npp),
stream_manager_(new StreamManager(npp)),
cursor_type_(o3d::Cursor::DEFAULT),
prev_width_(0),
prev_height_(0),
offscreen_rendering_enabled_(false) {
#if defined(OS_WIN) || defined(OS_LINUX)
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() {
DisableOffscreenRendering();
#ifdef OS_WIN
ClearPluginProperty(hWnd_);
#elif defined(OS_MACOSX)
o3d::ReleaseSafariBrowserWindow(mac_cocoa_window_);
CleanupFullscreenOpenGLContext();
#elif defined(OS_LINUX)
SetDisplay(NULL);
#endif // OS_WIN
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) {
// In case CreateRenderer is called more than once, reset to the
// uninitialized state..
DeleteRenderer();
renderer_init_status_ = o3d::Renderer::UNINITIALIZED;
if (features_->render_modes() != Renderer::RENDER_MODE_2D) {
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();
}
}
#if !defined(SUPPORT_CAIRO)
} else {
// The caller requested 2D only mode, but this platform does not support 2d
renderer_init_status_ = o3d::Renderer::INITIALIZATION_ERROR;
}
#else
}
if ((renderer_init_status_ != o3d::Renderer::SUCCESS) &&
(features_->render_modes() != Renderer::RENDER_MODE_3D)) {
// Attempt to fall back to o2d renderer
renderer_ = o3d::Renderer::Create2DRenderer(&service_locator_);
if (renderer_) {
renderer_init_status_ = renderer_->Init(display_window, false);
if (renderer_init_status_ != o3d::Renderer::SUCCESS) {
DeleteRenderer();
} else {
ClientInfoManager* client_info_manager =
service_locator()->GetService<ClientInfoManager>();
client_info_manager->SetRender2d(true);
}
}
}
#endif
}
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(bool updateTab) {
CFDateRef now = CFDateCreate(NULL, CFAbsoluteTimeGetCurrent());
CFDateRef previousTime = last_mac_event_time_;
last_mac_event_time_ = now;
if (previousTime != NULL) {
CFRelease(previousTime);
}
mac_cocoa_window_ = o3d::SafariBrowserWindowForWindowRef(mac_window_);
if (!mac_window_selected_tab_ || updateTab) {
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;
}
#if defined(CB_SERVICE_REMOTE)
void PluginObject::SetGPUPluginObject(NPObject* gpu_plugin_object) {
if (gpu_plugin_object) {
NPN_RetainObject(gpu_plugin_object);
}
if (gpu_plugin_object_) {
NPN_ReleaseObject(gpu_plugin_object_);
}
gpu_plugin_object_ = gpu_plugin_object;
}
#endif
enum {
kPropClient,
kPropGpuConfig,
kNumPropertyIds
};
static NPIdentifier property_ids[kNumPropertyIds];
static const NPUTF8 *property_names[kNumPropertyIds] = {
"client",
"gpuConfig",
};
enum {
kMethodEval,
#if defined(CB_SERVICE_REMOTE)
kMethodSetGPUPluginObject,
kMethodGetGPUPluginObject,
#endif
kNumMethodIds,
};
static NPIdentifier method_ids[kNumMethodIds];
static const NPUTF8 *method_names[kNumMethodIds] = {
"eval",
#if defined(CB_SERVICE_REMOTE)
"setGPUPluginObject",
"getGPUPluginObject",
#endif
};
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 < kNumMethodIds; ++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 arg_count,
NPVariant *np_result) {
DebugScopedId id(name);
PluginObject *plugin_object = static_cast<PluginObject *>(header);
if (name == method_ids[kMethodEval]) {
return plugin_object->np_v8_bridge()->Evaluate(args, arg_count, np_result);
}
#if defined(CB_SERVICE_REMOTE)
else if (name == method_ids[kMethodGetGPUPluginObject]) {
if (arg_count != 0)
return false;
ValueToNPVariant(plugin_object->GetGPUPluginObject(), np_result);
return true;
} else if (name == method_ids[kMethodSetGPUPluginObject]) {
if (arg_count != 1)
return false;
VOID_TO_NPVARIANT(*np_result);
NPObjectPointer<NPObject> gpu_plugin_object;
if (NPVariantToValue(&gpu_plugin_object, args[0])) {
plugin_object->SetGPUPluginObject(gpu_plugin_object.Get());
return true;
} else {
return false;
}
} // NOLINT
#endif // CB_SERVICE_REMOTE
else { // NOLINT
NPObject *globals = plugin_object->globals_npobject();
return globals->_class->invoke(globals, name, args, arg_count, np_result);
}
}
static bool PluginInvokeDefault(NPObject *header, const NPVariant *args,
uint32_t arg_count, 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, arg_count, 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 < kNumPropertyIds; ++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[kPropGpuConfig]) {
// 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[kPropClient]) {
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[kPropClient]) {
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 = kNumPropertyIds + kNumMethodIds + 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, kNumPropertyIds * sizeof(NPIdentifier));
memcpy(*value + kNumPropertyIds, method_ids,
kNumMethodIds * sizeof(NPIdentifier));
glue::StaticEnumeratePropertyHelper(
*value + kNumPropertyIds + kNumMethodIds);
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, kNumPropertyIds, property_ids);
NPN_GetStringIdentifiers(method_names, kNumMethodIds, 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_) {
// If we are rendering offscreen, we may need to reallocate the
// render surfaces.
if (offscreen_rendering_enabled_) {
AllocateOffscreenRenderSurfaces(width, height);
}
// 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(prev_width_, prev_height_);
// This is just so that the client can send an event to the user.
client()->SendResizeEvent(prev_width_, prev_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) {
// TODO: convert to using app::win::ScopedProp.
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::SetDisplay(Display *display) {
if (display_ != display) {
if (display_) {
for (int i = 0; i < o3d::Cursor::NUM_CURSORS; ++i) {
if (cursors_[i]) {
XFreeCursor(display_, cursors_[i]);
cursors_[i] = 0;
}
}
}
display_ = display;
}
}
static unsigned int O3DToX11CursorShape(o3d::Cursor::CursorType cursor_type) {
switch (cursor_type) {
case o3d::Cursor::DEFAULT:
return XC_arrow;
case o3d::Cursor::CROSSHAIR:
return XC_crosshair;
case o3d::Cursor::POINTER:
return XC_hand2;
case o3d::Cursor::E_RESIZE:
return XC_right_side;
case o3d::Cursor::NE_RESIZE:
return XC_top_right_corner;
case o3d::Cursor::NW_RESIZE:
return XC_top_left_corner;
case o3d::Cursor::N_RESIZE:
return XC_top_side;
case o3d::Cursor::SE_RESIZE:
return XC_bottom_right_corner;
case o3d::Cursor::SW_RESIZE:
return XC_bottom_left_corner;
case o3d::Cursor::S_RESIZE:
return XC_bottom_side;
case o3d::Cursor::W_RESIZE:
return XC_left_side;
case o3d::Cursor::MOVE:
return XC_fleur;
case o3d::Cursor::TEXT:
return XC_xterm;
case o3d::Cursor::WAIT:
return XC_watch;
case o3d::Cursor::PROGRESS:
NOTIMPLEMENTED();
return XC_watch;
case o3d::Cursor::HELP:
NOTIMPLEMENTED();
return XC_arrow;
}
NOTIMPLEMENTED();
return XC_arrow;
}
static Cursor O3DToX11Cursor(Display *display, Window window,
o3d::Cursor::CursorType cursor_type) {
switch (cursor_type) {
case o3d::Cursor::NONE: {
// There is no X11 primitive for hiding the cursor. The standard practice
// is to define a custom cursor from a 1x1 invisible pixmap.
static char zero[1] = {0};
Pixmap zero_pixmap = XCreateBitmapFromData(display, window, zero, 1, 1);
DLOG_ASSERT(zero_pixmap);
if (!zero_pixmap) {
return 0;
}
// This could actually be any colour, since our mask pixmap specifies that
// no pixels are visible.
XColor black;
black.red = 0;
black.green = 0;
black.blue = 0;
Cursor cursor = XCreatePixmapCursor(display, zero_pixmap, zero_pixmap,
&black, &black, 0, 0);
XFreePixmap(display, zero_pixmap);
return cursor;
}
default:
return XCreateFontCursor(display, O3DToX11CursorShape(cursor_type));
}
}
void PluginObject::PlatformSpecificSetCursor() {
if (!cursors_[cursor_type_]) {
// According to the docs, the window here is only relevant for selecting the
// screen, and we always create our fullscreen and embedded windows on the
// same screen, so we can just always use the embedded window.
cursors_[cursor_type_] = O3DToX11Cursor(display_, window_, cursor_type_);
}
Window window = fullscreen_ ? fullscreen_window_ : window_;
XDefineCursor(display_, window, cursors_[cursor_type_]);
}
#endif // OS_LINUX
void PluginObject::TickPluginObject(void* data) {
PluginObject* plugin_object = static_cast<PluginObject*>(data);
plugin_object->ExecuteAsyncTick();
}
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_->ExecuteAsyncTick();
}
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.
if (IsPluginThreadAsyncCallSupported(npp_)) {
NPN_PluginThreadAsyncCall(npp_, TickPluginObject, this);
} else {
Tick();
}
}
}
void PluginObject::ExecuteAsyncTick() {
// Check the plugin has not been destroyed already. Chrome sometimes invokes
// async callbacks after destruction.
if (!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(client());
if (reentrance_count.get() > 1) {
--pending_ticks_;
return;
}
Tick();
}
void PluginObject::Tick() {
DCHECK_GT(pending_ticks_, 0);
--pending_ticks_;
client_->Tick();
if (renderer_ && renderer_->need_to_render()) {
client_->RenderClient(true);
}
}
void PluginObject::EnableOffscreenRendering() {
if (!offscreen_rendering_enabled_) {
AllocateOffscreenRenderSurfaces(width(), height());
offscreen_rendering_enabled_ = true;
}
}
void PluginObject::DisableOffscreenRendering() {
if (offscreen_rendering_enabled_) {
DeallocateOffscreenRenderSurfaces();
offscreen_rendering_enabled_ = false;
}
}
bool PluginObject::IsOffscreenRenderingEnabled() const {
return offscreen_rendering_enabled_;
}
RenderSurface::Ref PluginObject::GetOffscreenRenderSurface() const {
return offscreen_render_surface_;
}
Bitmap::Ref PluginObject::GetOffscreenBitmap() const {
return offscreen_readback_bitmap_;
}
bool PluginObject::AllocateOffscreenRenderSurfaces(int width, int height) {
int pot_width =
static_cast<int>(o3d::image::ComputePOTSize(width));
int pot_height =
static_cast<int>(o3d::image::ComputePOTSize(height));
if (!renderer_ || pot_width == 0 || pot_height == 0) {
return false;
}
bool must_reallocate_render_surfaces =
(offscreen_render_surface_.IsNull() ||
offscreen_depth_render_surface_.IsNull() ||
offscreen_render_surface_->width() != pot_width ||
offscreen_render_surface_->height() != pot_height);
if (must_reallocate_render_surfaces) {
Texture2D::Ref texture = renderer_->CreateTexture2D(
pot_width,
pot_height,
Texture::ARGB8,
1,
true);
if (texture.IsNull()) {
return false;
}
RenderSurface::Ref surface(texture->GetRenderSurface(0));
if (surface.IsNull()) {
return false;
}
RenderDepthStencilSurface::Ref depth(renderer_->CreateDepthStencilSurface(
pot_width,
pot_height));
if (depth.IsNull()) {
return false;
}
offscreen_texture_ = texture;
offscreen_render_surface_ = surface;
offscreen_depth_render_surface_ = depth;
}
offscreen_render_surface_->SetClipSize(width, height);
offscreen_depth_render_surface_->SetClipSize(width, height);
if (offscreen_readback_bitmap_.IsNull() ||
offscreen_readback_bitmap_->width() != width ||
offscreen_readback_bitmap_->height() != height) {
o3d::Bitmap::Ref bitmap = Bitmap::Ref(
new Bitmap(service_locator()));
bitmap->Allocate(Texture::ARGB8,
width, height, 1, Bitmap::IMAGE);
offscreen_readback_bitmap_ = bitmap;
}
// Tell the Client about the newly allocated surfaces so that normal
// calls to RenderClient can automatically do the right thing.
client_->SetOffscreenRenderingSurfaces(offscreen_render_surface_,
offscreen_depth_render_surface_);
return true;
}
void PluginObject::DeallocateOffscreenRenderSurfaces() {
if (client_) {
client_->SetOffscreenRenderingSurfaces(RenderSurface::Ref(),
RenderDepthStencilSurface::Ref());
}
offscreen_render_surface_.Reset();
offscreen_depth_render_surface_.Reset();
offscreen_readback_bitmap_.Reset();
}
} // 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
|