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

Side by Side Diff: extensions/renderer/safe_builtins.cc

Issue 1167423002: Use V8 Maybe APIs in extensions/renderer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 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
« no previous file with comments | « extensions/renderer/runtime_custom_bindings.cc ('k') | extensions/renderer/script_context.cc » ('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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "extensions/renderer/safe_builtins.h" 5 #include "extensions/renderer/safe_builtins.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/stl_util.h" 8 #include "base/stl_util.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "extensions/renderer/script_context.h" 10 #include "extensions/renderer/script_context.h"
11 #include "extensions/renderer/v8_maybe_helpers.h"
11 12
12 namespace extensions { 13 namespace extensions {
13 14
14 namespace { 15 namespace {
15 16
16 const char kClassName[] = "extensions::SafeBuiltins"; 17 const char kClassName[] = "extensions::SafeBuiltins";
17 18
18 // Documentation for makeCallback in the JavaScript, out here to reduce the 19 // Documentation for makeCallback in the JavaScript, out here to reduce the
19 // (very small) amount of effort that the v8 parser needs to do: 20 // (very small) amount of effort that the v8 parser needs to do:
20 // 21 //
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 " builtinTypes[i].prototype.toJSON = savedToJSONs[i];\n" 117 " builtinTypes[i].prototype.toJSON = savedToJSONs[i];\n"
117 " } catch (e) {}\n" 118 " } catch (e) {}\n"
118 " }\n" 119 " }\n"
119 " }\n" 120 " }\n"
120 " }\n" 121 " }\n"
121 "});\n" 122 "});\n"
122 "\n" 123 "\n"
123 "}());\n"; 124 "}());\n";
124 125
125 v8::Local<v8::String> MakeKey(const char* name, v8::Isolate* isolate) { 126 v8::Local<v8::String> MakeKey(const char* name, v8::Isolate* isolate) {
126 return v8::String::NewFromUtf8( 127 std::string key = base::StringPrintf("%s::%s", kClassName, name);
127 isolate, base::StringPrintf("%s::%s", kClassName, name).c_str()); 128 CHECK(key.size() < v8::String::kMaxLength);
129 return ToV8String(isolate, key.c_str());
128 } 130 }
129 131
130 void SaveImpl(const char* name, 132 void SaveImpl(const char* name,
131 v8::Local<v8::Value> value, 133 v8::Local<v8::Value> value,
132 v8::Local<v8::Context> context) { 134 v8::Local<v8::Context> context) {
133 CHECK(!value.IsEmpty() && value->IsObject()) << name; 135 CHECK(!value.IsEmpty() && value->IsObject()) << name;
134 context->Global()->SetHiddenValue(MakeKey(name, context->GetIsolate()), 136 context->Global()->SetHiddenValue(MakeKey(name, context->GetIsolate()),
135 value); 137 value);
136 } 138 }
137 139
138 v8::Local<v8::Object> Load(const char* name, v8::Local<v8::Context> context) { 140 v8::Local<v8::Object> Load(const char* name, v8::Local<v8::Context> context) {
139 v8::Local<v8::Value> value = 141 v8::Local<v8::Value> value =
140 context->Global()->GetHiddenValue(MakeKey(name, context->GetIsolate())); 142 context->Global()->GetHiddenValue(MakeKey(name, context->GetIsolate()));
141 CHECK(!value.IsEmpty() && value->IsObject()) << name; 143 CHECK(!value.IsEmpty() && value->IsObject()) << name;
142 return v8::Local<v8::Object>::Cast(value); 144 return v8::Local<v8::Object>::Cast(value);
143 } 145 }
144 146
145 class ExtensionImpl : public v8::Extension { 147 class ExtensionImpl : public v8::Extension {
146 public: 148 public:
147 ExtensionImpl() : v8::Extension(kClassName, kScript) {} 149 ExtensionImpl() : v8::Extension(kClassName, kScript) {}
148 150
149 private: 151 private:
150 v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( 152 v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate(
151 v8::Isolate* isolate, 153 v8::Isolate* isolate,
152 v8::Local<v8::String> name) override { 154 v8::Local<v8::String> name) override {
153 if (name->Equals(v8::String::NewFromUtf8(isolate, "Apply"))) 155 v8::Local<v8::Context> context = isolate->GetCurrentContext();
156 if (CheckV8Call(name->Equals(context, ToV8String(isolate, "Apply"))))
154 return v8::FunctionTemplate::New(isolate, Apply); 157 return v8::FunctionTemplate::New(isolate, Apply);
155 if (name->Equals(v8::String::NewFromUtf8(isolate, "Save"))) 158 if (CheckV8Call(name->Equals(context, ToV8String(isolate, "Save"))))
156 return v8::FunctionTemplate::New(isolate, Save); 159 return v8::FunctionTemplate::New(isolate, Save);
157 NOTREACHED() << *v8::String::Utf8Value(name); 160 NOTREACHED() << *v8::String::Utf8Value(name);
158 return v8::Local<v8::FunctionTemplate>(); 161 return v8::Local<v8::FunctionTemplate>();
159 } 162 }
160 163
161 static void Apply(const v8::FunctionCallbackInfo<v8::Value>& info) { 164 static void Apply(const v8::FunctionCallbackInfo<v8::Value>& info) {
162 CHECK(info.Length() == 5 && info[0]->IsFunction() && // function 165 CHECK(info.Length() == 5 && info[0]->IsFunction() && // function
163 // info[1] could be an object or a string 166 // info[1] could be an object or a string
164 info[2]->IsObject() && // args 167 info[2]->IsObject() && // args
165 info[3]->IsInt32() && // first_arg_index 168 info[3]->IsInt32() && // first_arg_index
166 info[4]->IsInt32()); // args_length 169 info[4]->IsInt32()); // args_length
167 v8::Local<v8::Function> function = info[0].As<v8::Function>(); 170 v8::Local<v8::Function> function = info[0].As<v8::Function>();
168 v8::Local<v8::Object> recv; 171 v8::Local<v8::Object> recv;
169 if (info[1]->IsObject()) { 172 if (info[1]->IsObject()) {
170 recv = v8::Local<v8::Object>::Cast(info[1]); 173 recv = v8::Local<v8::Object>::Cast(info[1]);
171 } else if (info[1]->IsString()) { 174 } else if (info[1]->IsString()) {
172 recv = v8::StringObject::New(v8::Local<v8::String>::Cast(info[1])) 175 recv = v8::StringObject::New(v8::Local<v8::String>::Cast(info[1]))
173 ->ToObject(info.GetIsolate()); 176 .As<v8::Object>();
174 } else { 177 } else {
175 info.GetIsolate()->ThrowException( 178 info.GetIsolate()->ThrowException(
176 v8::Exception::TypeError(v8::String::NewFromUtf8( 179 v8::Exception::TypeError(ToV8String(
177 info.GetIsolate(), 180 info.GetIsolate(),
178 "The first argument is the receiver and must be an object"))); 181 "The first argument is the receiver and must be an object")));
179 return; 182 return;
180 } 183 }
181 v8::Local<v8::Object> args = v8::Local<v8::Object>::Cast(info[2]); 184 v8::Local<v8::Object> args = v8::Local<v8::Object>::Cast(info[2]);
182 int first_arg_index = info[3]->ToInt32(info.GetIsolate())->Value(); 185 int first_arg_index = info[3].As<v8::Int32>()->Value();
183 int args_length = info[4]->ToInt32(info.GetIsolate())->Value(); 186 int args_length = info[4].As<v8::Int32>()->Value();
184 187
188 v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
185 int argc = args_length - first_arg_index; 189 int argc = args_length - first_arg_index;
186 scoped_ptr<v8::Local<v8::Value> []> argv(new v8::Local<v8::Value>[argc]); 190 scoped_ptr<v8::Local<v8::Value> []> argv(new v8::Local<v8::Value>[argc]);
187 for (int i = 0; i < argc; ++i) { 191 for (int i = 0; i < argc; ++i) {
188 CHECK(args->Has(i + first_arg_index)); 192 CHECK(CheckV8Call(args->Has(context, i + first_arg_index)));
189 argv[i] = args->Get(i + first_arg_index); 193 argv[i] = args->Get(context, i + first_arg_index).ToLocalChecked();
190 } 194 }
191 195
192 v8::Local<v8::Value> return_value = function->Call(recv, argc, argv.get()); 196 v8::Local<v8::Value> return_value;
193 if (!return_value.IsEmpty()) 197 if (function->Call(context, recv, argc, argv.get()).ToLocal(&return_value))
194 info.GetReturnValue().Set(return_value); 198 info.GetReturnValue().Set(return_value);
195 } 199 }
196 200
197 static void Save(const v8::FunctionCallbackInfo<v8::Value>& info) { 201 static void Save(const v8::FunctionCallbackInfo<v8::Value>& info) {
198 CHECK(info.Length() == 2 && info[0]->IsString() && info[1]->IsObject()); 202 CHECK(info.Length() == 2 && info[0]->IsString() && info[1]->IsObject());
199 SaveImpl(*v8::String::Utf8Value(info[0]), 203 SaveImpl(*v8::String::Utf8Value(info[0]),
200 info[1], 204 info[1],
201 info.GetIsolate()->GetCallingContext()); 205 info.GetIsolate()->GetCallingContext());
202 } 206 }
203 }; 207 };
(...skipping 29 matching lines...) Expand all
233 237
234 v8::Local<v8::Object> SafeBuiltins::GetString() const { 238 v8::Local<v8::Object> SafeBuiltins::GetString() const {
235 return Load("String", context_->v8_context()); 239 return Load("String", context_->v8_context());
236 } 240 }
237 241
238 v8::Local<v8::Object> SafeBuiltins::GetError() const { 242 v8::Local<v8::Object> SafeBuiltins::GetError() const {
239 return Load("Error", context_->v8_context()); 243 return Load("Error", context_->v8_context());
240 } 244 }
241 245
242 } // namespace extensions 246 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/runtime_custom_bindings.cc ('k') | extensions/renderer/script_context.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698