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::StartCall(const ResponseCallback& callback, | |
| 19 const std::string& request_name, | |
| 20 base::ListValue* args, | |
| 21 int* request_id, | |
| 22 std::string* error) { | |
| 23 *request_id = next_request_id_++; | |
| 24 pending_request_map_[*request_id] = callback; | |
| 25 args->Insert(0, new base::FundamentalValue(*request_id)); | |
| 26 | |
| 27 bool result = | |
| 28 StartRequest(request_name, "callMethod", args, error); | |
| 29 if (!result) | |
| 30 pending_request_map_.erase(*request_id); | |
| 31 return result; | |
| 32 } | |
| 33 | |
| 34 bool PepperRequestProxy::StartPost(const std::string& request_name, | |
| 35 base::ListValue* args, | |
| 36 std::string* error) { | |
| 37 return StartRequest(request_name, "postMethod", args, error); | |
| 38 } | |
| 39 | |
| 40 bool PepperRequestProxy::StartRequest(const std::string& request_name, | |
| 41 const std::string& method_name, | |
| 42 base::ListValue* args, | |
| 43 std::string* error) { | |
| 44 args->Insert(0, new base::StringValue(request_name)); | |
|
not at google - send to devlin
2013/12/03 17:32:50
it seems like you can avoid this weirdness of modi
Sam McNally
2013/12/04 00:01:36
Done.
| |
| 45 | |
| 46 // TODO(sammc): Converting from base::Value to v8::Value and then back to | |
| 47 // base::Value is not optimal. For most API calls the JS code doesn't do much. | |
| 48 // http://crbug.com/324115. | |
| 49 v8::HandleScope scope(v8::Isolate::GetCurrent()); | |
| 50 scoped_ptr<content::V8ValueConverter> converter( | |
| 51 content::V8ValueConverter::create()); | |
| 52 std::vector<v8::Handle<v8::Value> > v8_args; | |
| 53 for (base::ListValue::const_iterator it = args->begin(); it != args->end(); | |
| 54 ++it) { | |
| 55 v8_args.push_back(converter->ToV8Value(*it, context_->v8_context())); | |
| 56 } | |
| 57 v8::Handle<v8::Value> v8_error = context_->module_system()->CallModuleMethod( | |
| 58 "pepper_request", method_name, &v8_args); | |
| 59 if (v8_error->IsString()) { | |
| 60 if (error) | |
| 61 *error = *v8::String::Utf8Value(v8_error); | |
| 62 return false; | |
| 63 } | |
| 64 | |
| 65 return true; | |
| 66 } | |
| 67 | |
| 68 void PepperRequestProxy::OnResponseReceived(int request_id, | |
| 69 bool success, | |
| 70 const base::ListValue& args, | |
| 71 const std::string& error) { | |
| 72 PendingRequestMap::iterator it = pending_request_map_.find(request_id); | |
| 73 DCHECK(it != pending_request_map_.end()); | |
| 74 it->second.Run(request_id, success, args, error); | |
| 75 pending_request_map_.erase(it); | |
| 76 } | |
| 77 | |
| 78 } // namespace extensions | |
| OLD | NEW |