| 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/messaging_bindings.h" | 5 #include "chrome/renderer/extensions/messaging_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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 if (!renderview) | 102 if (!renderview) |
| 103 return; | 103 return; |
| 104 | 104 |
| 105 // Arguments are (int32 port_id, string message). | 105 // Arguments are (int32 port_id, string message). |
| 106 CHECK(args.Length() == 2 && | 106 CHECK(args.Length() == 2 && |
| 107 args[0]->IsInt32() && | 107 args[0]->IsInt32() && |
| 108 args[1]->IsString()); | 108 args[1]->IsString()); |
| 109 | 109 |
| 110 int port_id = args[0]->Int32Value(); | 110 int port_id = args[0]->Int32Value(); |
| 111 if (!HasPortData(port_id)) { | 111 if (!HasPortData(port_id)) { |
| 112 v8::ThrowException(v8::Exception::Error( | 112 args.GetIsolate()->ThrowException(v8::Exception::Error( |
| 113 v8::String::NewFromUtf8(args.GetIsolate(), kPortClosedError))); | 113 v8::String::NewFromUtf8(args.GetIsolate(), kPortClosedError))); |
| 114 return; | 114 return; |
| 115 } | 115 } |
| 116 | 116 |
| 117 renderview->Send(new ExtensionHostMsg_PostMessage( | 117 renderview->Send(new ExtensionHostMsg_PostMessage( |
| 118 renderview->GetRoutingID(), port_id, | 118 renderview->GetRoutingID(), port_id, |
| 119 extensions::Message( | 119 extensions::Message( |
| 120 *v8::String::AsciiValue(args[1]), | 120 *v8::String::Utf8Value(args[1]), |
| 121 blink::WebUserGestureIndicator::isProcessingUserGesture()))); | 121 blink::WebUserGestureIndicator::isProcessingUserGesture()))); |
| 122 } | 122 } |
| 123 | 123 |
| 124 // Forcefully disconnects a port. | 124 // Forcefully disconnects a port. |
| 125 void CloseChannel(const v8::FunctionCallbackInfo<v8::Value>& args) { | 125 void CloseChannel(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 126 // Arguments are (int32 port_id, boolean notify_browser). | 126 // Arguments are (int32 port_id, boolean notify_browser). |
| 127 CHECK_EQ(2, args.Length()); | 127 CHECK_EQ(2, args.Length()); |
| 128 CHECK(args[0]->IsInt32()); | 128 CHECK(args[0]->IsInt32()); |
| 129 CHECK(args[1]->IsBoolean()); | 129 CHECK(args[1]->IsBoolean()); |
| 130 | 130 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 } | 171 } |
| 172 | 172 |
| 173 // Holds a |callback| to run sometime after |object| is GC'ed. |callback| will | 173 // Holds a |callback| to run sometime after |object| is GC'ed. |callback| will |
| 174 // not be executed re-entrantly to avoid running JS in an unexpected state. | 174 // not be executed re-entrantly to avoid running JS in an unexpected state. |
| 175 class GCCallback { | 175 class GCCallback { |
| 176 public: | 176 public: |
| 177 static void Bind(v8::Handle<v8::Object> object, | 177 static void Bind(v8::Handle<v8::Object> object, |
| 178 v8::Handle<v8::Function> callback, | 178 v8::Handle<v8::Function> callback, |
| 179 v8::Isolate* isolate) { | 179 v8::Isolate* isolate) { |
| 180 GCCallback* cb = new GCCallback(object, callback, isolate); | 180 GCCallback* cb = new GCCallback(object, callback, isolate); |
| 181 cb->object_.MakeWeak(cb, NearDeathCallback); | 181 cb->object_.SetWeak(cb, NearDeathCallback); |
| 182 } | 182 } |
| 183 | 183 |
| 184 private: | 184 private: |
| 185 static void NearDeathCallback(v8::Isolate* isolate, | 185 static void NearDeathCallback( |
| 186 v8::Persistent<v8::Object>* object, | 186 const v8::WeakCallbackData<v8::Object, GCCallback>& data) { |
| 187 GCCallback* self) { | |
| 188 // v8 says we need to explicitly reset weak handles from their callbacks. | 187 // v8 says we need to explicitly reset weak handles from their callbacks. |
| 189 // It's not implicit as one might expect. | 188 // It's not implicit as one might expect. |
| 190 self->object_.reset(); | 189 data.GetParameter()->object_.reset(); |
| 191 base::MessageLoop::current()->PostTask(FROM_HERE, | 190 base::MessageLoop::current()->PostTask( |
| 192 base::Bind(&GCCallback::RunCallback, base::Owned(self))); | 191 FROM_HERE, |
| 192 base::Bind(&GCCallback::RunCallback, |
| 193 base::Owned(data.GetParameter()))); |
| 193 } | 194 } |
| 194 | 195 |
| 195 GCCallback(v8::Handle<v8::Object> object, | 196 GCCallback(v8::Handle<v8::Object> object, |
| 196 v8::Handle<v8::Function> callback, | 197 v8::Handle<v8::Function> callback, |
| 197 v8::Isolate* isolate) | 198 v8::Isolate* isolate) |
| 198 : object_(object), callback_(callback), isolate_(isolate) {} | 199 : object_(object), callback_(callback), isolate_(isolate) {} |
| 199 | 200 |
| 200 void RunCallback() { | 201 void RunCallback() { |
| 201 v8::HandleScope handle_scope(isolate_); | 202 v8::HandleScope handle_scope(isolate_); |
| 202 v8::Handle<v8::Function> callback = callback_.NewHandle(isolate_); | 203 v8::Handle<v8::Function> callback = callback_.NewHandle(isolate_); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 it != contexts.end(); ++it) { | 263 it != contexts.end(); ++it) { |
| 263 if (restrict_to_render_view && | 264 if (restrict_to_render_view && |
| 264 restrict_to_render_view != (*it)->GetRenderView()) { | 265 restrict_to_render_view != (*it)->GetRenderView()) { |
| 265 continue; | 266 continue; |
| 266 } | 267 } |
| 267 | 268 |
| 268 // TODO(kalman): remove when ContextSet::ForEach is available. | 269 // TODO(kalman): remove when ContextSet::ForEach is available. |
| 269 if ((*it)->v8_context().IsEmpty()) | 270 if ((*it)->v8_context().IsEmpty()) |
| 270 continue; | 271 continue; |
| 271 | 272 |
| 272 v8::Handle<v8::Value> tab = v8::Null(); | 273 v8::Handle<v8::Value> tab = v8::Null(isolate); |
| 273 if (!source_tab.empty()) | 274 if (!source_tab.empty()) |
| 274 tab = converter->ToV8Value(&source_tab, (*it)->v8_context()); | 275 tab = converter->ToV8Value(&source_tab, (*it)->v8_context()); |
| 275 | 276 |
| 276 v8::Handle<v8::Value> tls_channel_id_value = v8::Undefined(); | 277 v8::Handle<v8::Value> tls_channel_id_value = v8::Undefined(isolate); |
| 277 if ((*it)->extension()) { | 278 if ((*it)->extension()) { |
| 278 ExternallyConnectableInfo* externally_connectable = | 279 ExternallyConnectableInfo* externally_connectable = |
| 279 ExternallyConnectableInfo::Get((*it)->extension()); | 280 ExternallyConnectableInfo::Get((*it)->extension()); |
| 280 if (externally_connectable && | 281 if (externally_connectable && |
| 281 externally_connectable->accepts_tls_channel_id) { | 282 externally_connectable->accepts_tls_channel_id) { |
| 282 tls_channel_id_value = | 283 tls_channel_id_value = |
| 283 v8::String::NewFromUtf8(isolate, | 284 v8::String::NewFromUtf8(isolate, |
| 284 tls_channel_id.c_str(), | 285 tls_channel_id.c_str(), |
| 285 v8::String::kNormalString, | 286 v8::String::kNormalString, |
| 286 tls_channel_id.size()); | 287 tls_channel_id.size()); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 // TODO(kalman): remove when ContextSet::ForEach is available. | 401 // TODO(kalman): remove when ContextSet::ForEach is available. |
| 401 if ((*it)->v8_context().IsEmpty()) | 402 if ((*it)->v8_context().IsEmpty()) |
| 402 continue; | 403 continue; |
| 403 | 404 |
| 404 std::vector<v8::Handle<v8::Value> > arguments; | 405 std::vector<v8::Handle<v8::Value> > arguments; |
| 405 arguments.push_back(v8::Integer::New(port_id)); | 406 arguments.push_back(v8::Integer::New(port_id)); |
| 406 if (!error_message.empty()) { | 407 if (!error_message.empty()) { |
| 407 arguments.push_back( | 408 arguments.push_back( |
| 408 v8::String::NewFromUtf8(isolate, error_message.c_str())); | 409 v8::String::NewFromUtf8(isolate, error_message.c_str())); |
| 409 } else { | 410 } else { |
| 410 arguments.push_back(v8::Null()); | 411 arguments.push_back(v8::Null(isolate)); |
| 411 } | 412 } |
| 412 (*it)->module_system()->CallModuleMethod("messaging", | 413 (*it)->module_system()->CallModuleMethod("messaging", |
| 413 "dispatchOnDisconnect", | 414 "dispatchOnDisconnect", |
| 414 &arguments); | 415 &arguments); |
| 415 } | 416 } |
| 416 } | 417 } |
| 417 | 418 |
| 418 } // namespace extensions | 419 } // namespace extensions |
| OLD | NEW |