My favorites | Sign in
Project Logo
Project hosting will be READ-ONLY Wednesday at 8am PST due to brief network maintenance.
             
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
// Copyright 2007, Google Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. 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.
// 3. 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 AUTHOR ``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 AUTHOR 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.
//
// This implementation makes use of window.addEventListener() to watch the
// event. IE does not support this, but it's not a target for the NPAPI port,
// so we're OK.

#include "gears/base/common/html_event_monitor.h"

#include <assert.h>

#include "gears/base/common/dispatcher.h"
#include "gears/base/common/string_utils.h"
#include "gears/base/npapi/np_utils.h"
#include "gears/base/npapi/plugin.h"
#include "gears/base/npapi/scoped_npapi_handles.h"

DECLARE_DISPATCHER(HtmlEventMonitorHook);

class HtmlEventMonitorHook : public PluginBase {
public:
HtmlEventMonitorHook(NPP instance)
: PluginBase(instance), dispatcher_(this) {
PluginBase::Init(&dispatcher_);
}

void Init(HtmlEventMonitor::HtmlEventCallback function, void *user_param) {
function_ = function;
user_param_ = user_param;
}

void HandleEvent(JsCallContext *context) {
function_(user_param_); // invoke user callback
}

private:
HtmlEventMonitor::HtmlEventCallback function_;
void *user_param_;

Dispatcher<HtmlEventMonitorHook> dispatcher_;

DISALLOW_EVIL_CONSTRUCTORS(HtmlEventMonitorHook);
};

// static
template <>
void Dispatcher<HtmlEventMonitorHook>::Init() {
// Safari treats "handleEvent" as a method, while Firefox treats it as a
// property. Go figure.
RegisterMethod("handleEvent", &HtmlEventMonitorHook::HandleEvent);
RegisterProperty("handleEvent", &HtmlEventMonitorHook::HandleEvent, NULL);
}

HtmlEventMonitorHook* CreateHtmlEventMonitorHook(JsContextPtr context) {
static NPClass np_class = GetNPClassTemplate<HtmlEventMonitorHook>();
return static_cast<HtmlEventMonitorHook *>(
NPN_CreateObject(context, &np_class));
}

//
// HtmlEventMonitor
//

bool HtmlEventMonitor::Start(NPP context, NPObject *event_source) {
assert(!event_hook_);
assert(NPVARIANT_IS_VOID(event_source_));

// create the event hook
HtmlEventMonitorHook *event_hook = CreateHtmlEventMonitorHook(context);
ScopedNPObject event_hook_scoped(event_hook);
event_hook->Init(function_, user_param_);

// call window.addEventListener(event_name, event_hook, false)
NPObject* window = event_source;

NPIdentifier add_event_listener_id =
NPN_GetStringIdentifier("addEventListener");

std::string event_name_utf8;
if (!String16ToUTF8(&event_name_[2], &event_name_utf8)) // skip leading "on"
return false;

NPVariant args[3];
STDSTRING_TO_NPVARIANT(event_name_utf8, args[0]);
OBJECT_TO_NPVARIANT(event_hook, args[1]);
BOOLEAN_TO_NPVARIANT(false, args[2]);

// call the method
ScopedNPVariant result;
if (!NPN_Invoke(context, window, add_event_listener_id, args,
ARRAYSIZE(args), &result))
return false;

// only modify data members if everything succeeded
event_hook_scoped.release();
event_context_ = context;
event_hook_ = event_hook;
event_source_.Reset(event_source);
return true;
}

void HtmlEventMonitor::Stop() {
assert(NPVARIANT_IS_OBJECT(event_source_));
assert(event_hook_);

// call window.removeEventListener(event_name, event_hook, false)
NPObject* window = NPVARIANT_TO_OBJECT(event_source_);

NPIdentifier remove_event_listener_id =
NPN_GetStringIdentifier("removeEventListener");

std::string event_name_utf8;
if (!String16ToUTF8(&event_name_[2], &event_name_utf8)) // skip leading "on"
return;

NPVariant args[3];
STDSTRING_TO_NPVARIANT(event_name_utf8, args[0]);
OBJECT_TO_NPVARIANT(event_hook_, args[1]);
BOOLEAN_TO_NPVARIANT(false, args[2]);

// call the method
ScopedNPVariant result;
if (!NPN_Invoke(event_context_, window, remove_event_listener_id,
args, 3, &result))
return;

// destroy the event hook
NPN_ReleaseObject(event_hook_);
event_context_ = NULL;
event_hook_ = NULL;
event_source_.Reset();
}
Show details Hide details

Change log

r2268 by gears.daemon on Jul 07, 2008   Diff
[Author: mpcomplete]

Fix a bug with marshalling objects in
NPAPI, causing some Gears objects to be
misidentified as regular JS objects.

This is  Issue 570 .

BUG=1240457
R=nigeltao
CC=gears-eng@googlegroups.com
DELTA=17  (10 added, 0 deleted, 7 changed)
...
Go to: 
Project members, sign in to write a code review

Older revisions

r630 by gears.daemon on Dec 27, 2007   Diff
[Author: playmobil]

gcc4 fixes + Add MACH export
definitions for NPAPI functions on
OSX.
...
r625 by gears.daemon on Dec 21, 2007   Diff
[Author: mpcomplete]

More refactoring of NPAPI wrapper
classes.  At Aaron's suggestion, I
moved the Register* calls out of the
...
r615 by gears.daemon on Dec 19, 2007   Diff
[Author: miket]

Ran the following commands over the
tree and got it to build again, in
further preparation for addition of
...
All revisions of this file

File info

Size: 5287 bytes, 154 lines
Hosted by Google Code