Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(631)

Side by Side Diff: chrome/renderer/extensions/pepper_request_proxy.cc

Issue 61383003: Pass pepper apps API calls through the existing js apps API bindings. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 const 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 scoped_ptr<base::ListValue> mutable_args(args.DeepCopy());
yzshen1 2013/11/27 23:56:52 It is unfortunate that we have to do a deep copy h
Sam McNally 2013/11/28 00:04:50 Done.
26 mutable_args->Insert(0, new base::FundamentalValue(*request_id));
27
28 bool result =
29 StartRequest(request_name, "callMethod", mutable_args.get(), error);
30 if (!result)
31 pending_request_map_.erase(*request_id);
32 return result;
33 }
34
35 bool PepperRequestProxy::StartPost(const std::string& request_name,
36 const base::ListValue& args,
37 std::string* error) {
38 scoped_ptr<base::ListValue> mutable_args(args.DeepCopy());
39 return StartRequest(request_name, "postMethod", mutable_args.get(), error);
40 }
41
42 bool PepperRequestProxy::StartRequest(const std::string& request_name,
43 const std::string& method_name,
44 base::ListValue* args,
45 std::string* error) {
46 args->Insert(0, new base::StringValue(request_name));
47
48 // TODO(sammc): Converting from base::Value to v8::Value and then back to
49 // base::Value is not optimal. For most API calls the JS code doesn't do much.
50 // http://crbug.com/324115.
51 v8::HandleScope scope(v8::Isolate::GetCurrent());
52 scoped_ptr<content::V8ValueConverter> converter(
53 content::V8ValueConverter::create());
54 std::vector<v8::Handle<v8::Value> > v8_args;
55 for (base::ListValue::const_iterator it = args->begin(); it != args->end();
56 ++it) {
57 v8_args.push_back(converter->ToV8Value(*it, context_->v8_context()));
58 }
59 v8::Handle<v8::Value> v8_error = context_->module_system()->CallModuleMethod(
60 "pepper_request", method_name, &v8_args);
61 if (v8_error->IsString()) {
62 if (error)
63 *error = *v8::String::Utf8Value(v8_error);
64 return false;
65 }
66
67 return true;
68 }
69
70 void PepperRequestProxy::OnResponseReceived(int request_id,
71 bool success,
72 const base::ListValue& args,
73 const std::string& error) {
74 PendingRequestMap::iterator it = pending_request_map_.find(request_id);
75 DCHECK(it != pending_request_map_.end());
76 it->second.Run(request_id, success, args, error);
77 pending_request_map_.erase(it);
78 }
79
80 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/renderer/extensions/pepper_request_proxy.h ('k') | chrome/renderer/pepper/pepper_extensions_common_host.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698