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
|
// Copyright (c) 2011 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 "chrome/renderer/extensions/event_bindings.h"
#include <vector>
#include "base/basictypes.h"
#include "base/lazy_instance.h"
#include "base/message_loop.h"
#include "chrome/common/extensions/extension_messages.h"
#include "chrome/common/extensions/extension_set.h"
#include "chrome/common/url_constants.h"
#include "chrome/renderer/extensions/chrome_v8_context.h"
#include "chrome/renderer/extensions/chrome_v8_context_set.h"
#include "chrome/renderer/extensions/chrome_v8_extension.h"
#include "chrome/renderer/extensions/event_bindings.h"
#include "chrome/renderer/extensions/extension_dispatcher.h"
#include "chrome/renderer/extensions/extension_process_bindings.h"
#include "chrome/renderer/extensions/user_script_slave.h"
#include "content/renderer/render_thread.h"
#include "content/renderer/render_view.h"
#include "googleurl/src/gurl.h"
#include "grit/renderer_resources.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
#include "v8/include/v8.h"
using WebKit::WebFrame;
using WebKit::WebSecurityOrigin;
using WebKit::WebURL;
namespace {
// Keep a local cache of RenderThread so that we can mock it out for unit tests.
static RenderThreadBase* render_thread = NULL;
// A map of event names to the number of listeners for that event. We notify
// the browser about event listeners when we transition between 0 and 1.
typedef std::map<std::string, int> EventListenerCounts;
struct SingletonData {
// A map of extension IDs to listener counts for that extension.
std::map<std::string, EventListenerCounts> listener_counts_;
};
static base::LazyInstance<SingletonData> g_singleton_data(
base::LINKER_INITIALIZED);
static EventListenerCounts& GetListenerCounts(const std::string& extension_id) {
return g_singleton_data.Get().listener_counts_[extension_id];
}
class ExtensionImpl : public ChromeV8Extension {
public:
explicit ExtensionImpl(ExtensionDispatcher* dispatcher)
: ChromeV8Extension("extensions/event.js",
IDR_EVENT_BINDINGS_JS,
dispatcher) {
}
~ExtensionImpl() {}
virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
v8::Handle<v8::String> name) {
if (name->Equals(v8::String::New("AttachEvent"))) {
return v8::FunctionTemplate::New(AttachEvent, v8::External::New(this));
} else if (name->Equals(v8::String::New("DetachEvent"))) {
return v8::FunctionTemplate::New(DetachEvent, v8::External::New(this));
} else if (name->Equals(v8::String::New("GetExternalFileEntry"))) {
return v8::FunctionTemplate::New(GetExternalFileEntry);
}
return ChromeV8Extension::GetNativeFunction(name);
}
// Attach an event name to an object.
static v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) {
DCHECK(args.Length() == 1);
// TODO(erikkay) should enforce that event name is a string in the bindings
DCHECK(args[0]->IsString() || args[0]->IsUndefined());
if (args[0]->IsString()) {
ExtensionImpl* v8_extension = GetFromArguments<ExtensionImpl>(args);
const ChromeV8ContextSet& context_set =
v8_extension->extension_dispatcher()->v8_context_set();
ChromeV8Context* context = context_set.GetCurrent();
CHECK(context);
EventListenerCounts& listener_counts =
GetListenerCounts(context->extension_id());
std::string event_name(*v8::String::AsciiValue(args[0]));
if (!v8_extension->CheckPermissionForCurrentRenderView(event_name))
return v8::Undefined();
if (++listener_counts[event_name] == 1) {
EventBindings::GetRenderThread()->Send(
new ExtensionHostMsg_AddListener(context->extension_id(),
event_name));
}
}
return v8::Undefined();
}
static v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) {
DCHECK(args.Length() == 1);
// TODO(erikkay) should enforce that event name is a string in the bindings
DCHECK(args[0]->IsString() || args[0]->IsUndefined());
if (args[0]->IsString()) {
ExtensionImpl* v8_extension = GetFromArguments<ExtensionImpl>(args);
const ChromeV8ContextSet& context_set =
v8_extension->extension_dispatcher()->v8_context_set();
ChromeV8Context* context = context_set.GetCurrent();
if (!context)
return v8::Undefined();
EventListenerCounts& listener_counts =
GetListenerCounts(context->extension_id());
std::string event_name(*v8::String::AsciiValue(args[0]));
if (--listener_counts[event_name] == 0) {
EventBindings::GetRenderThread()->Send(
new ExtensionHostMsg_RemoveListener(context->extension_id(),
event_name));
}
}
return v8::Undefined();
}
// Attach an event name to an object.
static v8::Handle<v8::Value> GetExternalFileEntry(
const v8::Arguments& args) {
// TODO(zelidrag): Make this magic work on other platforms when file browser
// matures enough on ChromeOS.
#if defined(OS_CHROMEOS)
DCHECK(args.Length() == 1);
DCHECK(args[0]->IsObject());
v8::Local<v8::Object> file_def = args[0]->ToObject();
std::string file_system_name(
*v8::String::Utf8Value(file_def->Get(
v8::String::New("fileSystemName"))));
std::string file_system_path(
*v8::String::Utf8Value(file_def->Get(
v8::String::New("fileSystemRoot"))));
std::string file_full_path(
*v8::String::Utf8Value(file_def->Get(
v8::String::New("fileFullPath"))));
bool is_directory =
file_def->Get(v8::String::New("fileIsDirectory"))->ToBoolean()->Value();
WebFrame* webframe = WebFrame::frameForCurrentContext();
return webframe->createFileEntry(
WebKit::WebFileSystem::TypeExternal,
WebKit::WebString::fromUTF8(file_system_name.c_str()),
WebKit::WebString::fromUTF8(file_system_path.c_str()),
WebKit::WebString::fromUTF8(file_full_path.c_str()),
is_directory);
#else
return v8::Undefined();
#endif
}
};
} // namespace
v8::Extension* EventBindings::Get(ExtensionDispatcher* dispatcher) {
static v8::Extension* extension = new ExtensionImpl(dispatcher);
return extension;
}
// static
void EventBindings::SetRenderThread(RenderThreadBase* thread) {
render_thread = thread;
}
// static
RenderThreadBase* EventBindings::GetRenderThread() {
return render_thread ? render_thread : RenderThread::current();
}
|