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
|
// Copyright (c) 2009 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.
#ifndef CHROME_RENDERER_EXTENSIONS_BINDINGS_UTILS_H_
#define CHROME_RENDERER_EXTENSIONS_BINDINGS_UTILS_H_
#include "app/resource_bundle.h"
#include "base/linked_ptr.h"
#include "base/singleton.h"
#include "base/string_piece.h"
#include "v8/include/v8.h"
#include <list>
#include <string>
class RenderView;
namespace WebKit {
class WebFrame;
}
namespace bindings_utils {
// This is a base class for chrome extension bindings. Common features that
// are shared by different modules go here.
class ExtensionBase : public v8::Extension {
public:
ExtensionBase(const char* name,
const char* source,
int dep_count,
const char** deps)
: v8::Extension(name, source, dep_count, deps) {}
// Note: do not call this function before or during the chromeHidden.onLoad
// event dispatch. The URL might not have been committed yet and might not
// be an extension URL.
static std::string ExtensionIdForCurrentContext();
// Derived classes should call this at the end of their implementation in
// order to expose common native functions, like GetChromeHidden, to the
// v8 extension.
virtual v8::Handle<v8::FunctionTemplate>
GetNativeFunction(v8::Handle<v8::String> name);
protected:
// Returns a hidden variable for use by the bindings that is unreachable
// by the page.
static v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args);
};
template<int kResourceId>
struct StringResourceTemplate {
StringResourceTemplate()
: resource(ResourceBundle::GetSharedInstance().GetRawDataResource(
kResourceId).as_string()) {
}
std::string resource;
};
template<int kResourceId>
const char* GetStringResource() {
return
Singleton< StringResourceTemplate<kResourceId> >::get()->resource.c_str();
}
// Contains information about a single javascript context.
struct ContextInfo {
v8::Persistent<v8::Context> context;
std::string extension_id; // empty if the context is not an extension
// If this context is a content script, parent will be the frame that it
// was injected in. This is NULL if the context is not a content script.
WebKit::WebFrame* parent_frame;
// The RenderView that this context belongs to. This is not guaranteed to be
// a valid pointer, and is used for comparisons only. Do not dereference.
RenderView* render_view;
// A count of the number of events that are listening in this context. When
// this is zero, |context| will be a weak handle.
int num_connected_events;
ContextInfo(v8::Persistent<v8::Context> context,
const std::string& extension_id,
WebKit::WebFrame* parent_frame,
RenderView* render_view)
: context(context), extension_id(extension_id),
parent_frame(parent_frame), render_view(render_view),
num_connected_events(0) {}
};
typedef std::list< linked_ptr<ContextInfo> > ContextList;
// Returns a mutable reference to the ContextList. Note: be careful using this.
// Calling into javascript may result in the list being modified, so don't rely
// on iterators remaining valid between calls to javascript.
ContextList& GetContexts();
// Returns a (copied) list of contexts that have the given extension_id.
ContextList GetContextsForExtension(const std::string& extension_id);
// Returns the ContextInfo item that has the given context.
ContextList::iterator FindContext(v8::Handle<v8::Context> context);
// Returns the ContextInfo for the current v8 context.
ContextInfo* GetInfoForCurrentContext();
// Contains info relevant to a pending API request.
struct PendingRequest {
public :
PendingRequest(v8::Persistent<v8::Context> context, const std::string& name)
: context(context), name(name) {
}
v8::Persistent<v8::Context> context;
std::string name;
};
typedef std::map<int, linked_ptr<PendingRequest> > PendingRequestMap;
// Returns a mutable reference to the PendingRequestMap.
PendingRequestMap& GetPendingRequestMap();
// Returns the current RenderView, based on which V8 context is current. It is
// an error to call this when not in a V8 context.
RenderView* GetRenderViewForCurrentContext();
// Call the named javascript function with the given arguments in a context.
// The function name should be reachable from the chromeHidden object, and can
// be a sub-property like "Port.dispatchOnMessage". Returns the result of
// the function call. If an exception is thrown an empty Handle will be
// returned.
v8::Handle<v8::Value> CallFunctionInContext(v8::Handle<v8::Context> context,
const std::string& function_name, int argc,
v8::Handle<v8::Value>* argv);
} // namespace bindings_utils
#endif // CHROME_RENDERER_EXTENSIONS_BINDINGS_UTILS_H_
|