OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/extensions/miscellaneous_bindings.h" | 5 #include "chrome/renderer/extensions/miscellaneous_bindings.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 77 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
78 v8::Handle<v8::String> name) { | 78 v8::Handle<v8::String> name) { |
79 if (name->Equals(v8::String::New("PostMessage"))) { | 79 if (name->Equals(v8::String::New("PostMessage"))) { |
80 return v8::FunctionTemplate::New(PostMessage); | 80 return v8::FunctionTemplate::New(PostMessage); |
81 } else if (name->Equals(v8::String::New("CloseChannel"))) { | 81 } else if (name->Equals(v8::String::New("CloseChannel"))) { |
82 return v8::FunctionTemplate::New(CloseChannel); | 82 return v8::FunctionTemplate::New(CloseChannel); |
83 } else if (name->Equals(v8::String::New("PortAddRef"))) { | 83 } else if (name->Equals(v8::String::New("PortAddRef"))) { |
84 return v8::FunctionTemplate::New(PortAddRef); | 84 return v8::FunctionTemplate::New(PortAddRef); |
85 } else if (name->Equals(v8::String::New("PortRelease"))) { | 85 } else if (name->Equals(v8::String::New("PortRelease"))) { |
86 return v8::FunctionTemplate::New(PortRelease); | 86 return v8::FunctionTemplate::New(PortRelease); |
87 } else if (name->Equals(v8::String::New("BindToGC"))) { | |
88 return v8::FunctionTemplate::New(BindToGC); | |
87 } | 89 } |
88 return ChromeV8Extension::GetNativeFunction(name); | 90 return ChromeV8Extension::GetNativeFunction(name); |
89 } | 91 } |
90 | 92 |
91 // Sends a message along the given channel. | 93 // Sends a message along the given channel. |
92 static v8::Handle<v8::Value> PostMessage(const v8::Arguments& args) { | 94 static v8::Handle<v8::Value> PostMessage(const v8::Arguments& args) { |
93 content::RenderView* renderview = GetCurrentRenderView(); | 95 content::RenderView* renderview = GetCurrentRenderView(); |
94 if (!renderview) | 96 if (!renderview) |
95 return v8::Undefined(); | 97 return v8::Undefined(); |
96 | 98 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 int port_id = args[0]->Int32Value(); | 144 int port_id = args[0]->Int32Value(); |
143 if (HasPortData(port_id) && --GetPortData(port_id).ref_count == 0) { | 145 if (HasPortData(port_id) && --GetPortData(port_id).ref_count == 0) { |
144 // Send via the RenderThread because the RenderView might be closing. | 146 // Send via the RenderThread because the RenderView might be closing. |
145 content::RenderThread::Get()->Send( | 147 content::RenderThread::Get()->Send( |
146 new ExtensionHostMsg_CloseChannel(port_id)); | 148 new ExtensionHostMsg_CloseChannel(port_id)); |
147 ClearPortData(port_id); | 149 ClearPortData(port_id); |
148 } | 150 } |
149 } | 151 } |
150 return v8::Undefined(); | 152 return v8::Undefined(); |
151 } | 153 } |
154 | |
155 struct GCCallbackArgs { | |
156 v8::Persistent<v8::Object> object; | |
157 v8::Persistent<v8::Function> callback; | |
158 }; | |
159 | |
160 static void GCCallback(v8::Persistent<v8::Value> object, void* parameter) { | |
161 v8::HandleScope handle_scope; | |
162 GCCallbackArgs* args = reinterpret_cast<GCCallbackArgs*>(parameter); | |
163 args->callback->Call(args->callback->CreationContext()->Global(), 0, NULL); | |
164 args->callback.Dispose(); | |
165 args->object.Dispose(); | |
166 delete args; | |
167 } | |
168 | |
169 // Binds a callback to be invoked when the given object is garbage collected. | |
170 static v8::Handle<v8::Value> BindToGC(const v8::Arguments& args) { | |
171 if (args.Length() == 2 && args[0]->IsObject() && args[1]->IsFunction()) { | |
172 GCCallbackArgs* context = new GCCallbackArgs; | |
173 context->callback = v8::Persistent<v8::Function>::New( | |
174 v8::Handle<v8::Function>::Cast(args[1])); | |
175 context->object = v8::Persistent<v8::Object>::New( | |
176 v8::Handle<v8::Object>::Cast(args[0])); | |
177 context->object.MakeWeak(context, GCCallback); | |
178 } | |
Matt Perry
2012/03/13 19:54:39
nit: can you add an "else NOTREACHED()" here so we
| |
179 return v8::Undefined(); | |
180 } | |
152 }; | 181 }; |
153 | 182 |
154 } // namespace | 183 } // namespace |
155 | 184 |
156 namespace extensions { | 185 namespace extensions { |
157 | 186 |
158 v8::Extension* MiscellaneousBindings::Get(ExtensionDispatcher* dispatcher) { | 187 v8::Extension* MiscellaneousBindings::Get(ExtensionDispatcher* dispatcher) { |
159 static v8::Extension* extension = new ExtensionImpl(dispatcher); | 188 static v8::Extension* extension = new ExtensionImpl(dispatcher); |
160 return extension; | 189 return extension; |
161 } | 190 } |
(...skipping 30 matching lines...) Expand all Loading... | |
192 arguments.push_back(v8::String::New(message.c_str(), message.size())); | 221 arguments.push_back(v8::String::New(message.c_str(), message.size())); |
193 arguments.push_back(port_id_handle); | 222 arguments.push_back(port_id_handle); |
194 CHECK((*it)->CallChromeHiddenMethod("Port.dispatchOnMessage", | 223 CHECK((*it)->CallChromeHiddenMethod("Port.dispatchOnMessage", |
195 arguments.size(), | 224 arguments.size(), |
196 &arguments[0], | 225 &arguments[0], |
197 NULL)); | 226 NULL)); |
198 } | 227 } |
199 } | 228 } |
200 | 229 |
201 } // namespace extension | 230 } // namespace extension |
OLD | NEW |