| 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 "content/renderer/pepper/message_channel.h" | 5 #include "content/renderer/pepper/message_channel.h" |
| 6 | 6 |
| 7 #include <cstdlib> | 7 #include <cstdlib> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 } | 181 } |
| 182 | 182 |
| 183 MessageChannel::MessageChannel(PepperPluginInstanceImpl* instance) | 183 MessageChannel::MessageChannel(PepperPluginInstanceImpl* instance) |
| 184 : gin::NamedPropertyInterceptor(instance->GetIsolate(), this), | 184 : gin::NamedPropertyInterceptor(instance->GetIsolate(), this), |
| 185 instance_(instance), | 185 instance_(instance), |
| 186 js_message_queue_state_(WAITING_TO_START), | 186 js_message_queue_state_(WAITING_TO_START), |
| 187 blocking_message_depth_(0), | 187 blocking_message_depth_(0), |
| 188 plugin_message_queue_state_(WAITING_TO_START), | 188 plugin_message_queue_state_(WAITING_TO_START), |
| 189 var_converter_(instance->pp_instance(), | 189 var_converter_(instance->pp_instance(), |
| 190 V8VarConverter::kDisallowObjectVars), | 190 V8VarConverter::kDisallowObjectVars), |
| 191 template_cache_(instance->GetIsolate()), |
| 191 weak_ptr_factory_(this) { | 192 weak_ptr_factory_(this) { |
| 192 } | 193 } |
| 193 | 194 |
| 194 gin::ObjectTemplateBuilder MessageChannel::GetObjectTemplateBuilder( | 195 gin::ObjectTemplateBuilder MessageChannel::GetObjectTemplateBuilder( |
| 195 v8::Isolate* isolate) { | 196 v8::Isolate* isolate) { |
| 196 return Wrappable<MessageChannel>::GetObjectTemplateBuilder(isolate) | 197 return Wrappable<MessageChannel>::GetObjectTemplateBuilder(isolate) |
| 197 .AddNamedPropertyInterceptor(); | 198 .AddNamedPropertyInterceptor(); |
| 198 } | 199 } |
| 199 | 200 |
| 200 void MessageChannel::BeginBlockOnSyncMessage() { | 201 void MessageChannel::BeginBlockOnSyncMessage() { |
| 201 js_message_queue_state_ = QUEUE_MESSAGES; | 202 js_message_queue_state_ = QUEUE_MESSAGES; |
| 202 ++blocking_message_depth_; | 203 ++blocking_message_depth_; |
| 203 } | 204 } |
| 204 | 205 |
| 205 void MessageChannel::EndBlockOnSyncMessage() { | 206 void MessageChannel::EndBlockOnSyncMessage() { |
| 206 DCHECK_GT(blocking_message_depth_, 0); | 207 DCHECK_GT(blocking_message_depth_, 0); |
| 207 --blocking_message_depth_; | 208 --blocking_message_depth_; |
| 208 if (!blocking_message_depth_) | 209 if (!blocking_message_depth_) |
| 209 DrainJSMessageQueueSoon(); | 210 DrainJSMessageQueueSoon(); |
| 210 } | 211 } |
| 211 | 212 |
| 212 v8::Local<v8::Value> MessageChannel::GetNamedProperty( | 213 v8::Local<v8::Value> MessageChannel::GetNamedProperty( |
| 213 v8::Isolate* isolate, | 214 v8::Isolate* isolate, |
| 214 const std::string& identifier) { | 215 const std::string& identifier) { |
| 215 if (!instance_) | 216 if (!instance_) |
| 216 return v8::Local<v8::Value>(); | 217 return v8::Local<v8::Value>(); |
| 217 | 218 |
| 218 PepperTryCatchV8 try_catch(instance_, &var_converter_, isolate); | 219 PepperTryCatchV8 try_catch(instance_, &var_converter_, isolate); |
| 219 if (identifier == kPostMessage) { | 220 if (identifier == kPostMessage) { |
| 220 return gin::CreateFunctionTemplate(isolate, | 221 return GetFunctionTemplate(isolate, identifier, |
| 221 base::Bind(&MessageChannel::PostMessageToNative, | 222 &MessageChannel::PostMessageToNative) |
| 222 weak_ptr_factory_.GetWeakPtr()))->GetFunction(); | 223 ->GetFunction(); |
| 223 } else if (identifier == kPostMessageAndAwaitResponse) { | 224 } else if (identifier == kPostMessageAndAwaitResponse) { |
| 224 return gin::CreateFunctionTemplate(isolate, | 225 return GetFunctionTemplate(isolate, identifier, |
| 225 base::Bind(&MessageChannel::PostBlockingMessageToNative, | 226 &MessageChannel::PostBlockingMessageToNative) |
| 226 weak_ptr_factory_.GetWeakPtr()))->GetFunction(); | 227 ->GetFunction(); |
| 227 } | 228 } |
| 228 | 229 |
| 229 std::map<std::string, ScopedPPVar>::const_iterator it = | 230 std::map<std::string, ScopedPPVar>::const_iterator it = |
| 230 internal_named_properties_.find(identifier); | 231 internal_named_properties_.find(identifier); |
| 231 if (it != internal_named_properties_.end()) { | 232 if (it != internal_named_properties_.end()) { |
| 232 v8::Handle<v8::Value> result = try_catch.ToV8(it->second.get()); | 233 v8::Handle<v8::Value> result = try_catch.ToV8(it->second.get()); |
| 233 if (try_catch.ThrowException()) | 234 if (try_catch.ThrowException()) |
| 234 return v8::Local<v8::Value>(); | 235 return v8::Local<v8::Value>(); |
| 235 return result; | 236 return result; |
| 236 } | 237 } |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 weak_ptr_factory_.GetWeakPtr())); | 462 weak_ptr_factory_.GetWeakPtr())); |
| 462 } | 463 } |
| 463 | 464 |
| 464 void MessageChannel::UnregisterSyncMessageStatusObserver() { | 465 void MessageChannel::UnregisterSyncMessageStatusObserver() { |
| 465 if (!unregister_observer_callback_.is_null()) { | 466 if (!unregister_observer_callback_.is_null()) { |
| 466 unregister_observer_callback_.Run(); | 467 unregister_observer_callback_.Run(); |
| 467 unregister_observer_callback_.Reset(); | 468 unregister_observer_callback_.Reset(); |
| 468 } | 469 } |
| 469 } | 470 } |
| 470 | 471 |
| 472 v8::Local<v8::FunctionTemplate> MessageChannel::GetFunctionTemplate( |
| 473 v8::Isolate* isolate, |
| 474 const std::string& name, |
| 475 void (MessageChannel::*memberFuncPtr)(gin::Arguments* args)) { |
| 476 v8::Local<v8::FunctionTemplate> function_template = template_cache_.Get(name); |
| 477 if (!function_template.IsEmpty()) |
| 478 return function_template; |
| 479 function_template = gin::CreateFunctionTemplate( |
| 480 isolate, base::Bind(memberFuncPtr, weak_ptr_factory_.GetWeakPtr())); |
| 481 template_cache_.Set(name, function_template); |
| 482 return function_template; |
| 483 } |
| 484 |
| 471 } // namespace content | 485 } // namespace content |
| OLD | NEW |