Chromium Code Reviews| Index: chrome/renderer/extensions/pepper_request_proxy.cc | 
| diff --git a/chrome/renderer/extensions/pepper_request_proxy.cc b/chrome/renderer/extensions/pepper_request_proxy.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..d85d91b5150cc40cde8c443bb58ccf92936fb6d2 | 
| --- /dev/null | 
| +++ b/chrome/renderer/extensions/pepper_request_proxy.cc | 
| @@ -0,0 +1,73 @@ | 
| +// Copyright 2013 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/pepper_request_proxy.h" | 
| + | 
| +#include "chrome/renderer/extensions/chrome_v8_context.h" | 
| +#include "content/public/renderer/v8_value_converter.h" | 
| + | 
| +namespace extensions { | 
| + | 
| +PepperRequestProxy::PepperRequestProxy(ChromeV8Context* context) | 
| + : context_(context), next_request_id_(0) {} | 
| + | 
| +PepperRequestProxy::~PepperRequestProxy() {} | 
| + | 
| +bool PepperRequestProxy::StartRequest(Source* source, | 
| + const std::string& request_name, | 
| + bool has_callback, | 
| + base::ListValue& args, | 
| + int* request_id, | 
| + std::string* error) { | 
| + *request_id = next_request_id_++; | 
| + pending_request_map_[*request_id] = source; | 
| + | 
| + args.Insert(0, new base::StringValue(request_name)); | 
| + if (has_callback) | 
| + args.Insert(1, new base::FundamentalValue(*request_id)); | 
| + | 
| + v8::HandleScope scope(v8::Isolate::GetCurrent()); | 
| + scoped_ptr<content::V8ValueConverter> converter( | 
| + content::V8ValueConverter::create()); | 
| + converter->ToV8Value(&args, context_->v8_context()); | 
| 
 
yzshen1
2013/11/26 19:33:11
Why do we need this line?
 
Sam McNally
2013/11/27 23:40:26
Done.
 
 | 
| + std::vector<v8::Handle<v8::Value> > v8_args; | 
| + for (base::ListValue::const_iterator it = args.begin(); it != args.end(); | 
| + ++it) { | 
| + v8_args.push_back(converter->ToV8Value(*it, context_->v8_context())); | 
| + } | 
| + v8::Handle<v8::Value> v8_error = context_->module_system()->CallModuleMethod( | 
| + "pepper_request", has_callback ? "callMethod" : "postMethod", &v8_args); | 
| + if (v8_error->IsString()) { | 
| + if (error) | 
| + *error = *v8::String::Utf8Value(v8_error); | 
| + return false; | 
| 
 
yzshen1
2013/11/26 19:33:11
In this case, do we need to remove the entry added
 
Sam McNally
2013/11/27 23:40:26
Done.
 
 | 
| + } | 
| + | 
| + return true; | 
| +} | 
| + | 
| +void PepperRequestProxy::OnResponse(int request_id, | 
| + bool success, | 
| + const base::ListValue& args, | 
| + const std::string& error) { | 
| + PendingRequestMap::iterator it = pending_request_map_.find(request_id); | 
| + DCHECK(it != pending_request_map_.end()); | 
| + Source* source = it->second; | 
| + pending_request_map_.erase(it); | 
| + if (!source) | 
| + return; | 
| + | 
| + source->OnResponseReceived(request_id, success, args, error); | 
| +} | 
| + | 
| +void PepperRequestProxy::InvalidateSource(Source* source) { | 
| + for (PendingRequestMap::iterator it = pending_request_map_.begin(); | 
| + it != pending_request_map_.end(); | 
| + ++it) { | 
| + if (it->second == source) | 
| + it->second = NULL; | 
| + } | 
| +} | 
| + | 
| +} // namespace extensions |