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
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
// Copyright 2006, 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.
//
// The C++ base class that all Gears objects should derive from.

#ifndef GEARS_BASE_COMMON_BASE_CLASS_H__
#define GEARS_BASE_COMMON_BASE_CLASS_H__

#include <map>

#include "gears/base/common/basictypes.h" // for DISALLOW_EVIL_CONSTRUCTORS
#include "gears/base/common/browsing_context.h"
#include "gears/base/common/dispatcher.h"
#include "gears/base/common/js_runner.h"
#include "gears/base/common/js_types.h"
#include "gears/base/common/permissions_manager.h"
#include "gears/base/common/scoped_refptr.h"
#include "gears/base/common/security_model.h"
#include "gears/base/common/string16.h" // for string16
#include "third_party/scoped_ptr/scoped_ptr.h"

class ModuleWrapperBaseClass;

#ifdef WINCE
class GearsFactory;
#endif


// ModuleImplBaseClass objects created in the same environment (i.e. ones from
// the same page and thread) share a reference to the same ModuleEnvironment.
//
// This struct has public members, but is meant to be read-only once
// constructed. Essentially, it should be const, apart from the inherited
// Ref/Unref methods.
struct ModuleEnvironment : public RefCounted {
public:
// CreateFromDOM can only be used from the main thread, and should only be
// used for the first module created in the main thread - i.e. the first
// GearsFactory instance. Other ModuleImplBaseClass instances are initialized
// (i.e. they have InitModuleEnvironment called on them) by CreateModule.
// This function returns NULL on failure. On success, the resultant
// ModuleEnvironment should be immediately held within a scoped_refptr.
#if BROWSER_FF
static ModuleEnvironment *CreateFromDOM();
#elif BROWSER_IE
static ModuleEnvironment *CreateFromDOM(IUnknown *site);
#elif BROWSER_NPAPI
static ModuleEnvironment *CreateFromDOM(JsContextPtr instance);
#endif

ModuleEnvironment(SecurityOrigin security_origin,
#if BROWSER_IE
IUnknown *iunknown_site,
#endif
bool is_worker,
JsRunnerInterface *js_runner,
BrowsingContext *browsing_context);

// Note that the SecurityOrigin may not necessarily be the same as the
// originating page, e.g. in the case of a cross-origin worker, it would be
// the origin of the foreign script. Nonetheless, every module in the same
// thread should share the same SecurityOrigin.
SecurityOrigin security_origin_;

#if BROWSER_FF || BROWSER_NPAPI
// The JavaScript context in which this module was created.
JsContextPtr js_context_;
#elif BROWSER_IE
// Pointer to the object that hosts this object. On Win32, this is the pointer
// passed to SetSite. On WinCE this is the JS IDispatch pointer.
CComPtr<IUnknown> iunknown_site_;
#endif

bool is_worker_;
JsRunnerInterface *js_runner_;
scoped_refptr<BrowsingContext> browsing_context_;

PermissionsManager permissions_manager_;

private:
// This struct is ref-counted and hence has a private destructor (which
// should only be called on the final Unref).
virtual ~ModuleEnvironment();

DISALLOW_EVIL_CONSTRUCTORS(ModuleEnvironment);
};


class MarshaledModule {
public:
MarshaledModule() {}
virtual ~MarshaledModule() {}
virtual bool Unmarshal(ModuleEnvironment *module_environment,
JsScopedToken *out) = 0;
private:
DISALLOW_EVIL_CONSTRUCTORS(MarshaledModule);
};


// Exposes the minimal set of information that Gears objects need to work
// consistently across the main-thread and worker-thread JavaScript engines.
class ModuleImplBaseClass {
public:
explicit ModuleImplBaseClass(const std::string &name);
virtual ~ModuleImplBaseClass();

const std::string &get_module_name() const {
return module_name_;
}

virtual MarshaledModule *AsMarshaledModule() { return NULL; }

void InitModuleEnvironment(ModuleEnvironment *source_module_environment);

// Host environment information
void GetModuleEnvironment(scoped_refptr<ModuleEnvironment> *out) const;
bool EnvIsWorker() const;
const std::string16& EnvPageLocationUrl() const;
const SecurityOrigin& EnvPageSecurityOrigin() const;
BrowsingContext *EnvPageBrowsingContext() const;

JsRunnerInterface *GetJsRunner() const;

PermissionsManager *GetPermissionsManager() const;

// Methods for dealing with the JavaScript wrapper interface.
void SetJsWrapper(ModuleWrapperBaseClass *wrapper) { js_wrapper_ = wrapper; }
ModuleWrapperBaseClass *GetWrapper() const {
assert(js_wrapper_);
return js_wrapper_;
}

void Ref();
void Unref();

// TODO(aa): Remove and replace call sites with GetWrapper()->GetToken().
JsToken GetWrapperToken() const;

protected:
scoped_refptr<ModuleEnvironment> module_environment_;

private:
std::string module_name_;

// Weak pointer to our JavaScript wrapper.
ModuleWrapperBaseClass *js_wrapper_;

#ifdef WINCE
// This method is defined in desktop/ie/factory.cc. It lets us verify that
// privateSetGlobalObject() has been called from JavaScript on WinCE.
friend bool IsFactoryInitialized(GearsFactory *factory);
#endif

DISALLOW_EVIL_CONSTRUCTORS(ModuleImplBaseClass);
};


class DispatcherInterface;

// Interface for the wrapper class that binds the Gears object to the
// JavaScript engine.
class ModuleWrapperBaseClass {
public:
// Returns a token for this wrapper class that can be returned via the
// JsRunnerInterface.
virtual JsToken GetWrapperToken() const = 0;

// Gets the Dispatcher for this module.
virtual DispatcherInterface *GetDispatcher() const = 0;

// Gets the ModuleImplBaseClass for this module.
virtual ModuleImplBaseClass *GetModuleImplBaseClass() const = 0;

// Adds a reference to the wrapper class.
virtual void Ref() = 0;

// Removes a reference to the wrapper class.
virtual void Unref() = 0;

protected:
// Don't allow direct deletion via this interface.
virtual ~ModuleWrapperBaseClass() { }
};


// Creates a new Module of the given type. Returns false on failure.
// On failure, and if context is not NULL, then context's exception string
// will be set.
// Usually, OutType will be GearsClass or ModuleImplBaseClass.
template<class GearsClass, class OutType>
bool CreateModule(ModuleEnvironment *module_environment,
JsCallContext *context,
scoped_refptr<OutType>* module) {
scoped_ptr<GearsClass> impl(new GearsClass());
impl->InitModuleEnvironment(module_environment);
scoped_ptr<Dispatcher<GearsClass> > dispatcher(
new Dispatcher<GearsClass>(impl.get()));

if (!module_environment->js_runner_->
InitializeModuleWrapper(impl.get(), dispatcher.get(), context)) {
return false;
}

dispatcher.release();
module->reset(impl.release());

#if BROWSER_NPAPI
// In NPAPI, objects are created with refcount 1. We want module (the
// scoped_refptr<OutType>) to have the only reference, so we Unref here,
// after the scoped_refptr has taken a reference in the line above.
(*module)->GetWrapper()->Unref();
#endif

return true;
}


#endif // GEARS_BASE_COMMON_BASE_CLASS_H__
Show details Hide details

Change log

r2843 by gears.daemon on Sep 16, 2008   Diff
[Author: nigeltao]

Remove unused EnvPageJsContext and
EnvPageIUnknownSite
methods.

PRESUBMIT=passed
R=mpcomplete
CC=gears-eng@googlegroups.com
APPROVED=mpcomplete
DELTA=18  (0 added, 18 deleted, 0 changed)
OCL=8280550
...
Go to: 
Project members, sign in to write a code review

Older revisions

r2831 by gears.daemon on Sep 10, 2008   Diff
[Author: nigeltao]

Move CreateModule<T> out of
base/xx/module_wrapper.h and into
base/common/base_class.h, unifying the
...
r2666 by gears.daemon on Aug 10, 2008   Diff
[Author: nigeltao]

Plug leaking DocumentJsRunner
instances, on Firefox. DJR instances
are now deleted just after page
...
r2566 by gears.daemon on Jul 31, 2008   Diff
[Author: nigeltao]

Count known memory leaks, so we can
plug them.

...
All revisions of this file

File info

Size: 8521 bytes, 240 lines