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

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

Issue 10890002: Make V8ValueConverter.FromV8Value behave similarly to JSON.stringify (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 months 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/test/data/extensions/api_test/settings/simple_test/background.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "chrome/renderer/extensions/send_request_natives.h" 5 #include "chrome/renderer/extensions/send_request_natives.h"
6 6
7 #include <string>
8 #include <vector>
9
10 #include "base/lazy_instance.h"
7 #include "base/json/json_reader.h" 11 #include "base/json/json_reader.h"
12 #include "base/string_util.h"
8 #include "content/public/renderer/v8_value_converter.h" 13 #include "content/public/renderer/v8_value_converter.h"
9 #include "chrome/renderer/extensions/request_sender.h" 14 #include "chrome/renderer/extensions/request_sender.h"
10 15
11 using content::V8ValueConverter; 16 using content::V8ValueConverter;
12 17
13 namespace extensions { 18 namespace extensions {
14 19
20 namespace {
21
22 // Safely converts v8 objects into values depending on the API that's being
23 // called.
24 //
25 // Some APIs can handle nulls in objects, others can't. For some that can
koz (OOO until 15th September) 2012/09/12 02:08:28 "For some that can" -> "For those that can"
26 // (e.g. the storage API) it's important that they can handle nulls since this
27 // actually changes what values would be stored.
28 class NullSafeV8ValueConverter {
29 public:
30 NullSafeV8ValueConverter() {
31 null_safe_apis_.push_back("storage.");
32 }
33
34 scoped_ptr<Value> FromV8Value(const std::string& name,
35 v8::Handle<v8::Value> value) {
36 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
37
38 // Make "undefined" the same as "null" for optional arguments, but for
39 // objects strip nulls (like {foo: null}, and therefore {foo: undefined} as
40 // well) to make it easier for extension APIs to check for optional
41 // arguments (and unless it's a "null safe" API (as explained above).
42 converter->SetUndefinedAllowed(true);
43 if (!IsNullSafe(name)) {
44 converter->SetStripNullFromObjects(true);
45 }
46
47 return make_scoped_ptr(
48 converter->FromV8Value(value, v8::Context::GetCurrent()));
49 }
50
51 private:
52 bool IsNullSafe(const std::string& name) {
53 for (std::vector<std::string>::iterator it = null_safe_apis_.begin();
54 it != null_safe_apis_.end(); ++it) {
55 if (StartsWithASCII(name, *it, true /* case sensitive */))
56 return true;
57 }
58 return false;
59 }
60
61 std::vector<std::string> null_safe_apis_;
62 };
63
64 base::LazyInstance<NullSafeV8ValueConverter> g_null_safe_v8_value_converter =
65 LAZY_INSTANCE_INITIALIZER;
66
67 } // namespace
68
69
15 SendRequestNatives::SendRequestNatives(Dispatcher* dispatcher, 70 SendRequestNatives::SendRequestNatives(Dispatcher* dispatcher,
16 RequestSender* request_sender) 71 RequestSender* request_sender)
17 : ChromeV8Extension(dispatcher), request_sender_(request_sender) { 72 : ChromeV8Extension(dispatcher), request_sender_(request_sender) {
18 RouteFunction("GetNextRequestId", 73 RouteFunction("GetNextRequestId",
19 base::Bind(&SendRequestNatives::GetNextRequestId, 74 base::Bind(&SendRequestNatives::GetNextRequestId,
20 base::Unretained(this))); 75 base::Unretained(this)));
21 RouteFunction("StartRequest", 76 RouteFunction("StartRequest",
22 base::Bind(&SendRequestNatives::StartRequest, 77 base::Bind(&SendRequestNatives::StartRequest,
23 base::Unretained(this))); 78 base::Unretained(this)));
24 } 79 }
25 80
26 v8::Handle<v8::Value> SendRequestNatives::GetNextRequestId( 81 v8::Handle<v8::Value> SendRequestNatives::GetNextRequestId(
27 const v8::Arguments& args) { 82 const v8::Arguments& args) {
28 static int next_request_id = 0; 83 static int next_request_id = 0;
29 return v8::Integer::New(next_request_id++); 84 return v8::Integer::New(next_request_id++);
30 } 85 }
31 86
32 // Starts an API request to the browser, with an optional callback. The 87 // Starts an API request to the browser, with an optional callback. The
33 // callback will be dispatched to EventBindings::HandleResponse. 88 // callback will be dispatched to EventBindings::HandleResponse.
34 v8::Handle<v8::Value> SendRequestNatives::StartRequest( 89 v8::Handle<v8::Value> SendRequestNatives::StartRequest(
35 const v8::Arguments& args) { 90 const v8::Arguments& args) {
36 std::string name = *v8::String::AsciiValue(args[0]); 91 std::string name = *v8::String::AsciiValue(args[0]);
37 int request_id = args[2]->Int32Value(); 92 int request_id = args[2]->Int32Value();
38 bool has_callback = args[3]->BooleanValue(); 93 bool has_callback = args[3]->BooleanValue();
39 bool for_io_thread = args[4]->BooleanValue(); 94 bool for_io_thread = args[4]->BooleanValue();
40 95
41 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); 96 scoped_ptr<Value> value_args =
42 97 g_null_safe_v8_value_converter.Get().FromV8Value(name, args[1]);
43 // Make "undefined" the same as "null" for optional arguments, but for objects
44 // strip nulls (like {foo: null}, and therefore {foo: undefined} as well) to
45 // make it easier for extension APIs to check for optional arguments.
46 converter->SetUndefinedAllowed(true);
47 converter->SetStripNullFromObjects(true);
48
49 scoped_ptr<Value> value_args(
50 converter->FromV8Value(args[1], v8::Context::GetCurrent()));
51 if (!value_args.get() || !value_args->IsType(Value::TYPE_LIST)) { 98 if (!value_args.get() || !value_args->IsType(Value::TYPE_LIST)) {
52 NOTREACHED() << "Unable to convert args passed to StartRequest"; 99 NOTREACHED() << "Unable to convert args passed to StartRequest";
53 return v8::Undefined(); 100 return v8::Undefined();
54 } 101 }
55 102
56 request_sender_->StartRequest(name, request_id, has_callback, for_io_thread, 103 request_sender_->StartRequest(name, request_id, has_callback, for_io_thread,
57 static_cast<ListValue*>(value_args.get())); 104 static_cast<ListValue*>(value_args.get()));
58 return v8::Undefined(); 105 return v8::Undefined();
59 } 106 }
60 107
61 } // namespace extensions 108 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/extensions/api_test/settings/simple_test/background.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698