What's new? | Help | Directory | Sign in
Google
             
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
// 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.

#include "gears/base/npapi/plugin.h"

#include "gears/base/common/async_router.h"
#include "gears/base/common/js_types.h"
#include "gears/base/common/mutex.h"
#include "gears/base/common/thread_locals.h"
#include "gears/base/npapi/browser_utils.h"
#include "gears/base/npapi/module.h"
#ifdef BROWSER_WEBKIT
#include "gears/localserver/safari/ui_thread.h"
#endif

// In Safari, gc() can be called on a thread which is neither the main thread
// or a worker thread. This happens when executing .PAC files. In this case
// Gears object deletion will fail since we don't have the pointers to the
// NPN functions set up correctly on the thread.
// Fortunately, this can't happen to workers, so it's enough to forward
// any such cases to the main thread for deletion.
#ifdef BROWSER_WEBKIT
static Mutex g_npapi_objects_to_delete_mutex;
static std::vector<PluginBase *> g_objects_to_delete;

class DeletePluginEventsFunctor : public AsyncFunctor {
virtual void Run() {
MutexLock locker(&g_npapi_objects_to_delete_mutex);
for (std::vector<PluginBase *>::iterator it = g_objects_to_delete.begin();
it != g_objects_to_delete.end();
++it) {
delete *it;
}
g_objects_to_delete.clear();
}
};
#endif

// static
void PluginBase::Deallocate(NPObject *npobj) {
PluginBase *plugin_to_delete = static_cast<PluginBase *>(npobj);
#ifdef BROWSER_WEBKIT
// If we have an NPN Function table attached to this thread then it's a
// thread that Gears was initialized, otherwise forward to the main
// thread.
if (!ThreadLocals::HasValue(kNPNFuncsKey)) {
MutexLock locker(&g_npapi_objects_to_delete_mutex);
bool was_empty = g_objects_to_delete.empty();
g_objects_to_delete.push_back(plugin_to_delete);

if (was_empty) {
AsyncRouter::GetInstance()->CallAsync(
GetUiThread(),
new DeletePluginEventsFunctor());
}
return; // Delete on main thread.
}
#endif
delete plugin_to_delete;
}

// static
bool PluginBase::HasMethod(NPObject *npobj, NPIdentifier name) {
PluginBase *plugin = static_cast<PluginBase *>(npobj);
assert(plugin->dispatcher_);
return plugin->dispatcher_->HasMethod(name);
}

// static
bool PluginBase::Invoke(NPObject *npobj, NPIdentifier name,
const NPVariant *args, uint32_t num_args,
NPVariant *result) {
VOID_TO_NPVARIANT(*result);

PluginBase *plugin = static_cast<PluginBase *>(npobj);
assert(plugin->dispatcher_);

JsCallContext context(plugin->instance_, npobj,
static_cast<int>(num_args), args, result);
BrowserUtils::EnterScope(&context);
bool retval = plugin->dispatcher_->CallMethod(name, &context);
BrowserUtils::ExitScope();
return retval;
}

// static
bool PluginBase::HasProperty(NPObject *npobj, NPIdentifier name) {
PluginBase *plugin = static_cast<PluginBase *>(npobj);
assert(plugin->dispatcher_);
return plugin->dispatcher_->HasPropertyGetter(name) ||
plugin->dispatcher_->HasPropertySetter(name);
}

// static
bool PluginBase::GetProperty(NPObject *npobj, NPIdentifier name,
NPVariant *result) {
VOID_TO_NPVARIANT(*result);

PluginBase *plugin = static_cast<PluginBase *>(npobj);
assert(plugin->dispatcher_);

JsCallContext context(plugin->instance_, npobj, 0, NULL, result);
BrowserUtils::EnterScope(&context);
bool retval = plugin->dispatcher_->GetProperty(name, &context);
BrowserUtils::ExitScope();
return retval;
}

// static
bool PluginBase::SetProperty(NPObject *npobj, NPIdentifier name,
const NPVariant *value) {
PluginBase *plugin = static_cast<PluginBase *>(npobj);
assert(plugin->dispatcher_);

JsCallContext context(plugin->instance_, npobj, 1, value, NULL);
BrowserUtils::EnterScope(&context);
bool retval = plugin->dispatcher_->SetProperty(name, &context);
BrowserUtils::ExitScope();
return retval;
}
Show details Hide details

Change log

r2749 by gears.daemon on Aug 21, 2008   Diff
[Author: playmobil]

In Safari, when a proxy is set from a PAC
file the JS in that file will be executed
on it's own thread which can cause a gc.
If that gc tries to kill a Gears object,
that
object will try to use a copy of the npn
function table stored in Thread Local
Storage,
however since the table doesn't exist on
the new thread we crash.
...
Go to: 
Project members, sign in to write a code review

Older revisions

r801 by gears.daemon on Feb 01, 2008   Diff
[Author: aa]

Implements support for Dispatcher-
based modules in Firefox. This
changelist
...
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
...
r620 by gears.daemon on Dec 19, 2007   Diff
[Author: playmobil]

Fix gcc 4 compile errors.

R=mpcomplete
...
All revisions of this file

File info

Size: 5435 bytes, 142 lines