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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/browser_plugin/browser_plugin_bindings.h"
#include <cstdlib>
#include <string>
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "content/common/browser_plugin/browser_plugin_constants.h"
#include "content/public/renderer/v8_value_converter.h"
#include "content/renderer/browser_plugin/browser_plugin.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/web/WebBindings.h"
#include "third_party/WebKit/public/web/WebDOMMessageEvent.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebElement.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebNode.h"
#include "third_party/WebKit/public/web/WebPluginContainer.h"
#include "third_party/WebKit/public/web/WebView.h"
#include "third_party/npapi/bindings/npapi.h"
#include "v8/include/v8.h"
using WebKit::WebBindings;
using WebKit::WebElement;
using WebKit::WebDOMEvent;
using WebKit::WebDOMMessageEvent;
using WebKit::WebPluginContainer;
using WebKit::WebString;
namespace content {
namespace {
BrowserPluginBindings* GetBindings(NPObject* object) {
return static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(object)->
message_channel.get();
}
std::string StringFromNPVariant(const NPVariant& variant) {
if (!NPVARIANT_IS_STRING(variant))
return std::string();
const NPString& np_string = NPVARIANT_TO_STRING(variant);
return std::string(np_string.UTF8Characters, np_string.UTF8Length);
}
bool StringToNPVariant(const std::string &in, NPVariant *variant) {
size_t length = in.size();
NPUTF8 *chars = static_cast<NPUTF8 *>(malloc(length));
if (!chars) {
VOID_TO_NPVARIANT(*variant);
return false;
}
memcpy(chars, in.c_str(), length);
STRINGN_TO_NPVARIANT(chars, length, *variant);
return true;
}
// Depending on where the attribute comes from it could be a string, int32,
// or a double. Javascript tends to produce an int32 or a string, but setting
// the value from the developer tools console may also produce a double.
int IntFromNPVariant(const NPVariant& variant) {
int value = 0;
switch (variant.type) {
case NPVariantType_Double:
value = NPVARIANT_TO_DOUBLE(variant);
break;
case NPVariantType_Int32:
value = NPVARIANT_TO_INT32(variant);
break;
case NPVariantType_String:
base::StringToInt(StringFromNPVariant(variant), &value);
break;
default:
break;
}
return value;
}
//------------------------------------------------------------------------------
// Implementations of NPClass functions. These are here to:
// - Implement src attribute.
//------------------------------------------------------------------------------
NPObject* BrowserPluginBindingsAllocate(NPP npp, NPClass* the_class) {
return new BrowserPluginBindings::BrowserPluginNPObject;
}
void BrowserPluginBindingsDeallocate(NPObject* object) {
BrowserPluginBindings::BrowserPluginNPObject* instance =
static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(object);
delete instance;
}
bool BrowserPluginBindingsHasMethod(NPObject* np_obj, NPIdentifier name) {
if (!np_obj)
return false;
BrowserPluginBindings* bindings = GetBindings(np_obj);
if (!bindings)
return false;
return bindings->HasMethod(name);
}
bool BrowserPluginBindingsInvoke(NPObject* np_obj, NPIdentifier name,
const NPVariant* args, uint32 arg_count,
NPVariant* result) {
if (!np_obj)
return false;
BrowserPluginBindings* bindings = GetBindings(np_obj);
if (!bindings)
return false;
return bindings->InvokeMethod(name, args, arg_count, result);
}
bool BrowserPluginBindingsInvokeDefault(NPObject* np_obj,
const NPVariant* args,
uint32 arg_count,
NPVariant* result) {
NOTIMPLEMENTED();
return false;
}
bool BrowserPluginBindingsHasProperty(NPObject* np_obj, NPIdentifier name) {
if (!np_obj)
return false;
BrowserPluginBindings* bindings = GetBindings(np_obj);
if (!bindings)
return false;
return bindings->HasProperty(name);
}
bool BrowserPluginBindingsGetProperty(NPObject* np_obj, NPIdentifier name,
NPVariant* result) {
if (!np_obj)
return false;
if (!result)
return false;
// All attributes from here on rely on the bindings, so retrieve it once and
// return on failure.
BrowserPluginBindings* bindings = GetBindings(np_obj);
if (!bindings)
return false;
return bindings->GetProperty(name, result);
}
bool BrowserPluginBindingsSetProperty(NPObject* np_obj, NPIdentifier name,
const NPVariant* variant) {
if (!np_obj)
return false;
if (!variant)
return false;
// All attributes from here on rely on the bindings, so retrieve it once and
// return on failure.
BrowserPluginBindings* bindings = GetBindings(np_obj);
if (!bindings)
return false;
if (variant->type == NPVariantType_Null)
return bindings->RemoveProperty(np_obj, name);
return bindings->SetProperty(np_obj, name, variant);
}
bool BrowserPluginBindingsEnumerate(NPObject *np_obj, NPIdentifier **value,
uint32_t *count) {
NOTIMPLEMENTED();
return true;
}
NPClass browser_plugin_message_class = {
NP_CLASS_STRUCT_VERSION,
&BrowserPluginBindingsAllocate,
&BrowserPluginBindingsDeallocate,
NULL,
&BrowserPluginBindingsHasMethod,
&BrowserPluginBindingsInvoke,
&BrowserPluginBindingsInvokeDefault,
&BrowserPluginBindingsHasProperty,
&BrowserPluginBindingsGetProperty,
&BrowserPluginBindingsSetProperty,
NULL,
&BrowserPluginBindingsEnumerate,
};
} // namespace
// BrowserPluginMethodBinding --------------------------------------------------
class BrowserPluginMethodBinding {
public:
BrowserPluginMethodBinding(const char name[], uint32 arg_count)
: name_(name),
arg_count_(arg_count) {
}
virtual ~BrowserPluginMethodBinding() {}
bool MatchesName(NPIdentifier name) const {
return WebBindings::getStringIdentifier(name_.c_str()) == name;
}
uint32 arg_count() const { return arg_count_; }
virtual bool Invoke(BrowserPluginBindings* bindings,
const NPVariant* args,
NPVariant* result) = 0;
private:
std::string name_;
uint32 arg_count_;
DISALLOW_COPY_AND_ASSIGN(BrowserPluginMethodBinding);
};
class BrowserPluginBindingAttach: public BrowserPluginMethodBinding {
public:
BrowserPluginBindingAttach()
: BrowserPluginMethodBinding(
browser_plugin::kMethodInternalAttach, 1) {
}
virtual bool Invoke(BrowserPluginBindings* bindings,
const NPVariant* args,
NPVariant* result) OVERRIDE {
if (!bindings->instance()->render_view())
return false;
scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
v8::Handle<v8::Value> obj(WebKit::WebBindings::toV8Value(&args[0]));
scoped_ptr<base::Value> value(
converter->FromV8Value(obj, bindings->instance()->render_view()->
GetWebView()->mainFrame()->mainWorldScriptContext()));
if (!value)
return false;
if (!value->IsType(Value::TYPE_DICTIONARY))
return false;
scoped_ptr<base::DictionaryValue> extra_params(
static_cast<base::DictionaryValue*>(value.release()));
bindings->instance()->Attach(extra_params.Pass());
return true;
}
private:
DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingAttach);
};
class BrowserPluginBindingAttachWindowTo : public BrowserPluginMethodBinding {
public:
BrowserPluginBindingAttachWindowTo()
: BrowserPluginMethodBinding(
browser_plugin::kMethodInternalAttachWindowTo, 2) {
}
virtual bool Invoke(BrowserPluginBindings* bindings,
const NPVariant* args,
NPVariant* result) OVERRIDE {
WebKit::WebNode node;
WebBindings::getNode(NPVARIANT_TO_OBJECT(args[0]), &node);
int window_id = IntFromNPVariant(args[1]);
BOOLEAN_TO_NPVARIANT(BrowserPlugin::AttachWindowTo(node, window_id),
*result);
return true;
}
private:
DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingAttachWindowTo);
};
// Note: This is a method that is used internally by the <webview> shim only.
// This should not be exposed to developers.
class BrowserPluginBindingGetInstanceID : public BrowserPluginMethodBinding {
public:
BrowserPluginBindingGetInstanceID()
: BrowserPluginMethodBinding(browser_plugin::kMethodGetInstanceId, 0) {
}
virtual bool Invoke(BrowserPluginBindings* bindings,
const NPVariant* args,
NPVariant* result) OVERRIDE {
int instance_id = bindings->instance()->instance_id();
INT32_TO_NPVARIANT(instance_id, *result);
return true;
}
private:
DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingGetInstanceID);
};
// Note: This is a method that is used internally by the <webview> shim only.
// This should not be exposed to developers.
class BrowserPluginBindingGetGuestInstanceID :
public BrowserPluginMethodBinding {
public:
BrowserPluginBindingGetGuestInstanceID()
: BrowserPluginMethodBinding(
browser_plugin::kMethodGetGuestInstanceId, 0) {
}
virtual bool Invoke(BrowserPluginBindings* bindings,
const NPVariant* args,
NPVariant* result) OVERRIDE {
int guest_instance_id = bindings->instance()->guest_instance_id();
INT32_TO_NPVARIANT(guest_instance_id, *result);
return true;
}
private:
DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingGetGuestInstanceID);
};
// Note: This is a method that is used internally by the <webview> shim only.
// This should not be exposed to developers.
class BrowserPluginBindingTrackObjectLifetime
: public BrowserPluginMethodBinding {
public:
BrowserPluginBindingTrackObjectLifetime()
: BrowserPluginMethodBinding(
browser_plugin::kMethodInternalTrackObjectLifetime, 2) {
}
virtual bool Invoke(BrowserPluginBindings* bindings,
const NPVariant* args,
NPVariant* result) OVERRIDE {
bindings->instance()->TrackObjectLifetime(args, IntFromNPVariant(args[1]));
return true;
}
private:
DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingTrackObjectLifetime);
};
// Note: This is a method that is used internally by the <webview> shim only.
// This should not be exposed to developers.
class BrowserPluginBindingSetPermission : public BrowserPluginMethodBinding {
public:
BrowserPluginBindingSetPermission()
: BrowserPluginMethodBinding(
browser_plugin::kMethodInternalSetPermission, 2) {
}
virtual bool Invoke(BrowserPluginBindings* bindings,
const NPVariant* args,
NPVariant* result) OVERRIDE {
int request_id = IntFromNPVariant(args[0]);
bool allow = NPVARIANT_TO_BOOLEAN(args[1]);
bindings->instance()->OnEmbedderDecidedPermission(request_id, allow);
return true;
}
private:
DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingSetPermission);
};
// BrowserPluginPropertyBinding ------------------------------------------------
class BrowserPluginPropertyBinding {
public:
explicit BrowserPluginPropertyBinding(const char name[]) : name_(name) {}
virtual ~BrowserPluginPropertyBinding() {}
const std::string& name() const { return name_; }
bool MatchesName(NPIdentifier name) const {
return WebBindings::getStringIdentifier(name_.c_str()) == name;
}
virtual bool GetProperty(BrowserPluginBindings* bindings,
NPVariant* result) = 0;
virtual bool SetProperty(BrowserPluginBindings* bindings,
NPObject* np_obj,
const NPVariant* variant) = 0;
virtual void RemoveProperty(BrowserPluginBindings* bindings,
NPObject* np_obj) = 0;
// Updates the DOM Attribute value with the current property value.
void UpdateDOMAttribute(BrowserPluginBindings* bindings,
std::string new_value) {
bindings->instance()->UpdateDOMAttribute(name(), new_value);
}
private:
std::string name_;
DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBinding);
};
class BrowserPluginPropertyBindingAutoSize
: public BrowserPluginPropertyBinding {
public:
BrowserPluginPropertyBindingAutoSize()
: BrowserPluginPropertyBinding(browser_plugin::kAttributeAutoSize) {
}
virtual bool GetProperty(BrowserPluginBindings* bindings,
NPVariant* result) OVERRIDE {
bool auto_size = bindings->instance()->GetAutoSizeAttribute();
BOOLEAN_TO_NPVARIANT(auto_size, *result);
return true;
}
virtual bool SetProperty(BrowserPluginBindings* bindings,
NPObject* np_obj,
const NPVariant* variant) OVERRIDE {
std::string value = StringFromNPVariant(*variant);
if (!bindings->instance()->HasDOMAttribute(name())) {
UpdateDOMAttribute(bindings, value);
bindings->instance()->ParseAutoSizeAttribute();
} else {
UpdateDOMAttribute(bindings, value);
}
return true;
}
virtual void RemoveProperty(BrowserPluginBindings* bindings,
NPObject* np_obj) OVERRIDE {
bindings->instance()->RemoveDOMAttribute(name());
bindings->instance()->ParseAutoSizeAttribute();
}
private:
DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingAutoSize);
};
class BrowserPluginPropertyBindingContentWindow
: public BrowserPluginPropertyBinding {
public:
BrowserPluginPropertyBindingContentWindow()
: BrowserPluginPropertyBinding(browser_plugin::kAttributeContentWindow) {
}
virtual bool GetProperty(BrowserPluginBindings* bindings,
NPVariant* result) OVERRIDE {
NPObject* obj = bindings->instance()->GetContentWindow();
if (obj) {
result->type = NPVariantType_Object;
result->value.objectValue = WebBindings::retainObject(obj);
}
return true;
}
virtual bool SetProperty(BrowserPluginBindings* bindings,
NPObject* np_obj,
const NPVariant* variant) OVERRIDE {
return false;
}
virtual void RemoveProperty(BrowserPluginBindings* bindings,
NPObject* np_obj) OVERRIDE {}
private:
DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingContentWindow);
};
class BrowserPluginPropertyBindingMaxHeight
: public BrowserPluginPropertyBinding {
public:
BrowserPluginPropertyBindingMaxHeight()
: BrowserPluginPropertyBinding(browser_plugin::kAttributeMaxHeight) {
}
virtual bool GetProperty(BrowserPluginBindings* bindings,
NPVariant* result) OVERRIDE {
int max_height = bindings->instance()->GetMaxHeightAttribute();
INT32_TO_NPVARIANT(max_height, *result);
return true;
}
virtual bool SetProperty(BrowserPluginBindings* bindings,
NPObject* np_obj,
const NPVariant* variant) OVERRIDE {
int new_value = IntFromNPVariant(*variant);
if (bindings->instance()->GetMaxHeightAttribute() != new_value) {
UpdateDOMAttribute(bindings, base::IntToString(new_value));
bindings->instance()->ParseSizeContraintsChanged();
}
return true;
}
virtual void RemoveProperty(BrowserPluginBindings* bindings,
NPObject* np_obj) OVERRIDE {
bindings->instance()->RemoveDOMAttribute(name());
bindings->instance()->ParseSizeContraintsChanged();
}
private:
DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingMaxHeight);
};
class BrowserPluginPropertyBindingMaxWidth
: public BrowserPluginPropertyBinding {
public:
BrowserPluginPropertyBindingMaxWidth()
: BrowserPluginPropertyBinding(browser_plugin::kAttributeMaxWidth) {
}
virtual bool GetProperty(BrowserPluginBindings* bindings,
NPVariant* result) OVERRIDE {
int max_width = bindings->instance()->GetMaxWidthAttribute();
INT32_TO_NPVARIANT(max_width, *result);
return true;
}
virtual bool SetProperty(BrowserPluginBindings* bindings,
NPObject* np_obj,
const NPVariant* variant) OVERRIDE {
int new_value = IntFromNPVariant(*variant);
if (bindings->instance()->GetMaxWidthAttribute() != new_value) {
UpdateDOMAttribute(bindings, base::IntToString(new_value));
bindings->instance()->ParseSizeContraintsChanged();
}
return true;
}
virtual void RemoveProperty(BrowserPluginBindings* bindings,
NPObject* np_obj) OVERRIDE {
bindings->instance()->RemoveDOMAttribute(name());
bindings->instance()->ParseSizeContraintsChanged();
}
private:
DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingMaxWidth);
};
class BrowserPluginPropertyBindingMinHeight
: public BrowserPluginPropertyBinding {
public:
BrowserPluginPropertyBindingMinHeight()
: BrowserPluginPropertyBinding(browser_plugin::kAttributeMinHeight) {
}
virtual bool GetProperty(BrowserPluginBindings* bindings,
NPVariant* result) OVERRIDE {
int min_height = bindings->instance()->GetMinHeightAttribute();
INT32_TO_NPVARIANT(min_height, *result);
return true;
}
virtual bool SetProperty(BrowserPluginBindings* bindings,
NPObject* np_obj,
const NPVariant* variant) OVERRIDE {
int new_value = IntFromNPVariant(*variant);
if (bindings->instance()->GetMinHeightAttribute() != new_value) {
UpdateDOMAttribute(bindings, base::IntToString(new_value));
bindings->instance()->ParseSizeContraintsChanged();
}
return true;
}
virtual void RemoveProperty(BrowserPluginBindings* bindings,
NPObject* np_obj) OVERRIDE {
bindings->instance()->RemoveDOMAttribute(name());
bindings->instance()->ParseSizeContraintsChanged();
}
private:
DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingMinHeight);
};
class BrowserPluginPropertyBindingMinWidth
: public BrowserPluginPropertyBinding {
public:
BrowserPluginPropertyBindingMinWidth()
: BrowserPluginPropertyBinding(browser_plugin::kAttributeMinWidth) {
}
virtual bool GetProperty(BrowserPluginBindings* bindings,
NPVariant* result) OVERRIDE {
int min_width = bindings->instance()->GetMinWidthAttribute();
INT32_TO_NPVARIANT(min_width, *result);
return true;
}
virtual bool SetProperty(BrowserPluginBindings* bindings,
NPObject* np_obj,
const NPVariant* variant) OVERRIDE {
int new_value = IntFromNPVariant(*variant);
if (bindings->instance()->GetMinWidthAttribute() != new_value) {
UpdateDOMAttribute(bindings, base::IntToString(new_value));
bindings->instance()->ParseSizeContraintsChanged();
}
return true;
}
virtual void RemoveProperty(BrowserPluginBindings* bindings,
NPObject* np_obj) OVERRIDE {
bindings->instance()->RemoveDOMAttribute(name());
bindings->instance()->ParseSizeContraintsChanged();
}
private:
DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingMinWidth);
};
class BrowserPluginPropertyBindingName
: public BrowserPluginPropertyBinding {
public:
BrowserPluginPropertyBindingName()
: BrowserPluginPropertyBinding(browser_plugin::kAttributeName) {
}
virtual bool GetProperty(BrowserPluginBindings* bindings,
NPVariant* result) OVERRIDE {
std::string name = bindings->instance()->GetNameAttribute();
return StringToNPVariant(name, result);
return true;
}
virtual bool SetProperty(BrowserPluginBindings* bindings,
NPObject* np_obj,
const NPVariant* variant) OVERRIDE {
std::string new_value = StringFromNPVariant(*variant);
if (bindings->instance()->GetNameAttribute() != new_value) {
UpdateDOMAttribute(bindings, new_value);
bindings->instance()->ParseNameAttribute();
}
return true;
}
virtual void RemoveProperty(BrowserPluginBindings* bindings,
NPObject* np_obj) OVERRIDE {
bindings->instance()->RemoveDOMAttribute(name());
bindings->instance()->ParseNameAttribute();
}
private:
DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingName);
};
class BrowserPluginPropertyBindingPartition
: public BrowserPluginPropertyBinding {
public:
BrowserPluginPropertyBindingPartition()
: BrowserPluginPropertyBinding(browser_plugin::kAttributePartition) {
}
virtual bool GetProperty(BrowserPluginBindings* bindings,
NPVariant* result) OVERRIDE {
std::string partition_id = bindings->instance()->GetPartitionAttribute();
return StringToNPVariant(partition_id, result);
}
virtual bool SetProperty(BrowserPluginBindings* bindings,
NPObject* np_obj,
const NPVariant* variant) OVERRIDE {
std::string new_value = StringFromNPVariant(*variant);
std::string old_value = bindings->instance()->GetPartitionAttribute();
if (old_value != new_value) {
UpdateDOMAttribute(bindings, new_value);
std::string error_message;
if (!bindings->instance()->ParsePartitionAttribute(&error_message)) {
WebBindings::setException(
np_obj, static_cast<const NPUTF8 *>(error_message.c_str()));
// Reset to old value on error.
UpdateDOMAttribute(bindings, old_value);
return false;
}
}
return true;
}
virtual void RemoveProperty(BrowserPluginBindings* bindings,
NPObject* np_obj) OVERRIDE {
std::string error_message;
if (bindings->instance()->CanRemovePartitionAttribute(&error_message)) {
bindings->instance()->RemoveDOMAttribute(name());
} else {
WebBindings::setException(
np_obj, static_cast<const NPUTF8 *>(error_message.c_str()));
}
}
private:
DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingPartition);
};
class BrowserPluginPropertyBindingSrc : public BrowserPluginPropertyBinding {
public:
BrowserPluginPropertyBindingSrc()
: BrowserPluginPropertyBinding(browser_plugin::kAttributeSrc) {
}
virtual bool GetProperty(BrowserPluginBindings* bindings,
NPVariant* result) OVERRIDE {
std::string src = bindings->instance()->GetSrcAttribute();
return StringToNPVariant(src, result);
}
virtual bool SetProperty(BrowserPluginBindings* bindings,
NPObject* np_obj,
const NPVariant* variant) OVERRIDE {
std::string new_value = StringFromNPVariant(*variant);
// We should not be issuing navigation IPCs if we attempt to set the
// src property to the empty string. Instead, we want to simply restore
// the src attribute back to its old value.
if (new_value.empty()) {
RemoveProperty(bindings, np_obj);
return true;
}
std::string old_value = bindings->instance()->GetSrcAttribute();
bool guest_crashed = bindings->instance()->guest_crashed();
if ((old_value != new_value) || guest_crashed) {
// If the new value was empty then we're effectively resetting the
// attribute to the old value here. This will be picked up by <webview>'s
// mutation observer and will restore the src attribute after it has been
// removed.
UpdateDOMAttribute(bindings, new_value);
std::string error_message;
if (!bindings->instance()->ParseSrcAttribute(&error_message)) {
WebBindings::setException(
np_obj, static_cast<const NPUTF8 *>(error_message.c_str()));
// Reset to old value on error.
UpdateDOMAttribute(bindings, old_value);
return false;
}
}
return true;
}
virtual void RemoveProperty(BrowserPluginBindings* bindings,
NPObject* np_obj) OVERRIDE {
std::string old_value = bindings->instance()->GetSrcAttribute();
// Remove the DOM attribute to trigger the mutation observer when it is
// restored to its original value again.
bindings->instance()->RemoveDOMAttribute(name());
UpdateDOMAttribute(bindings, old_value);
}
private:
DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingSrc);
};
// BrowserPluginBindings ------------------------------------------------------
BrowserPluginBindings::BrowserPluginNPObject::BrowserPluginNPObject() {
}
BrowserPluginBindings::BrowserPluginNPObject::~BrowserPluginNPObject() {
}
BrowserPluginBindings::BrowserPluginBindings(BrowserPlugin* instance)
: instance_(instance),
np_object_(NULL),
weak_ptr_factory_(this) {
NPObject* obj =
WebBindings::createObject(instance->pluginNPP(),
&browser_plugin_message_class);
np_object_ = static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(obj);
np_object_->message_channel = weak_ptr_factory_.GetWeakPtr();
method_bindings_.push_back(new BrowserPluginBindingAttach);
method_bindings_.push_back(new BrowserPluginBindingAttachWindowTo);
method_bindings_.push_back(new BrowserPluginBindingGetInstanceID);
method_bindings_.push_back(new BrowserPluginBindingGetGuestInstanceID);
method_bindings_.push_back(new BrowserPluginBindingSetPermission);
method_bindings_.push_back(new BrowserPluginBindingTrackObjectLifetime);
property_bindings_.push_back(new BrowserPluginPropertyBindingAutoSize);
property_bindings_.push_back(new BrowserPluginPropertyBindingContentWindow);
property_bindings_.push_back(new BrowserPluginPropertyBindingMaxHeight);
property_bindings_.push_back(new BrowserPluginPropertyBindingMaxWidth);
property_bindings_.push_back(new BrowserPluginPropertyBindingMinHeight);
property_bindings_.push_back(new BrowserPluginPropertyBindingMinWidth);
property_bindings_.push_back(new BrowserPluginPropertyBindingName);
property_bindings_.push_back(new BrowserPluginPropertyBindingPartition);
property_bindings_.push_back(new BrowserPluginPropertyBindingSrc);
}
BrowserPluginBindings::~BrowserPluginBindings() {
WebBindings::releaseObject(np_object_);
}
bool BrowserPluginBindings::HasMethod(NPIdentifier name) const {
for (BindingList::const_iterator iter = method_bindings_.begin();
iter != method_bindings_.end();
++iter) {
if ((*iter)->MatchesName(name))
return true;
}
return false;
}
bool BrowserPluginBindings::InvokeMethod(NPIdentifier name,
const NPVariant* args,
uint32 arg_count,
NPVariant* result) {
for (BindingList::iterator iter = method_bindings_.begin();
iter != method_bindings_.end();
++iter) {
if ((*iter)->MatchesName(name) && (*iter)->arg_count() == arg_count)
return (*iter)->Invoke(this, args, result);
}
return false;
}
bool BrowserPluginBindings::HasProperty(NPIdentifier name) const {
for (PropertyBindingList::const_iterator iter = property_bindings_.begin();
iter != property_bindings_.end();
++iter) {
if ((*iter)->MatchesName(name))
return true;
}
return false;
}
bool BrowserPluginBindings::SetProperty(NPObject* np_obj,
NPIdentifier name,
const NPVariant* variant) {
for (PropertyBindingList::iterator iter = property_bindings_.begin();
iter != property_bindings_.end();
++iter) {
if ((*iter)->MatchesName(name)) {
if ((*iter)->SetProperty(this, np_obj, variant)) {
return true;
}
break;
}
}
return false;
}
bool BrowserPluginBindings::RemoveProperty(NPObject* np_obj,
NPIdentifier name) {
for (PropertyBindingList::iterator iter = property_bindings_.begin();
iter != property_bindings_.end();
++iter) {
if ((*iter)->MatchesName(name)) {
(*iter)->RemoveProperty(this, np_obj);
return true;
}
}
return false;
}
bool BrowserPluginBindings::GetProperty(NPIdentifier name, NPVariant* result) {
for (PropertyBindingList::iterator iter = property_bindings_.begin();
iter != property_bindings_.end();
++iter) {
if ((*iter)->MatchesName(name))
return (*iter)->GetProperty(this, result);
}
return false;
}
} // namespace content
|