Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/renderer/extensions/pepper_request_proxy.h" | |
| 6 | |
| 7 #include "base/values.h" | |
| 8 #include "chrome/renderer/extensions/chrome_v8_context.h" | |
| 9 #include "content/public/renderer/v8_value_converter.h" | |
| 10 | |
| 11 namespace extensions { | |
| 12 | |
| 13 PepperRequestProxy::PepperRequestProxy(ChromeV8Context* context) | |
| 14 : context_(context), next_request_id_(0) {} | |
| 15 | |
| 16 PepperRequestProxy::~PepperRequestProxy() {} | |
| 17 | |
| 18 bool PepperRequestProxy::StartRequest(const ResponseCallback& callback, | |
| 19 const std::string& request_name, | |
| 20 const base::ListValue& args, | |
| 21 std::string* error) { | |
| 22 int request_id = next_request_id_++; | |
| 23 pending_request_map_[request_id] = callback; | |
| 24 | |
| 25 // TODO(sammc): Converting from base::Value to v8::Value and then back to | |
| 26 // base::Value is not optimal. For most API calls the JS code doesn't do much. | |
| 27 // http://crbug.com/324115. | |
| 28 v8::HandleScope scope(v8::Isolate::GetCurrent()); | |
|
not at google - send to devlin
2013/12/10 22:22:16
hold onto the context's Isolate here somewhere, th
Sam McNally
2013/12/10 23:19:09
Done.
| |
| 29 scoped_ptr<content::V8ValueConverter> converter( | |
| 30 content::V8ValueConverter::create()); | |
| 31 std::vector<v8::Handle<v8::Value> > v8_args; | |
| 32 v8_args.push_back(v8::String::NewFromUtf8( | |
| 33 context_->v8_context()->GetIsolate(), request_name.c_str())); | |
| 34 v8_args.push_back(v8::Integer::New(request_id)); | |
| 35 for (base::ListValue::const_iterator it = args.begin(); it != args.end(); | |
| 36 ++it) { | |
| 37 v8_args.push_back(converter->ToV8Value(*it, context_->v8_context())); | |
| 38 } | |
| 39 v8::Handle<v8::Value> v8_error = context_->module_system()->CallModuleMethod( | |
| 40 "pepper_request", "startRequest", &v8_args); | |
| 41 if (v8_error->IsString()) { | |
| 42 if (error) { | |
| 43 *error = *v8::String::Utf8Value(v8_error); | |
| 44 pending_request_map_.erase(request_id); | |
| 45 } | |
| 46 return false; | |
| 47 } | |
| 48 | |
| 49 return true; | |
| 50 } | |
| 51 | |
| 52 void PepperRequestProxy::OnResponseReceived(int request_id, | |
| 53 bool success, | |
| 54 const base::ListValue& args, | |
| 55 const std::string& error) { | |
| 56 PendingRequestMap::iterator it = pending_request_map_.find(request_id); | |
| 57 DCHECK(it != pending_request_map_.end()); | |
| 58 it->second.Run(success, args, error); | |
| 59 pending_request_map_.erase(it); | |
| 60 } | |
| 61 | |
| 62 } // namespace extensions | |
| OLD | NEW |