OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/safe_builtins.h" | 5 #include "chrome/renderer/extensions/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 "chrome/renderer/extensions/chrome_v8_context.h" | 10 #include "chrome/renderer/extensions/chrome_v8_context.h" |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 return v8::FunctionTemplate::New(Apply); | 143 return v8::FunctionTemplate::New(Apply); |
144 if (name->Equals(v8::String::New("Save"))) | 144 if (name->Equals(v8::String::New("Save"))) |
145 return v8::FunctionTemplate::New(Save); | 145 return v8::FunctionTemplate::New(Save); |
146 NOTREACHED() << *v8::String::AsciiValue(name); | 146 NOTREACHED() << *v8::String::AsciiValue(name); |
147 return v8::Handle<v8::FunctionTemplate>(); | 147 return v8::Handle<v8::FunctionTemplate>(); |
148 } | 148 } |
149 | 149 |
150 static void Apply(const v8::FunctionCallbackInfo<v8::Value>& info) { | 150 static void Apply(const v8::FunctionCallbackInfo<v8::Value>& info) { |
151 CHECK(info.Length() == 5 && | 151 CHECK(info.Length() == 5 && |
152 info[0]->IsFunction() && // function | 152 info[0]->IsFunction() && // function |
153 // info[1]->Object() // recv (will throw error not check) | 153 // info[1] could be an object or a string |
154 info[2]->IsObject() && // args | 154 info[2]->IsObject() && // args |
155 info[3]->IsInt32() && // first_arg_index | 155 info[3]->IsInt32() && // first_arg_index |
156 info[4]->IsInt32()); // args_length | 156 info[4]->IsInt32()); // args_length |
157 if (!info[1]->IsObject()) { | 157 v8::Local<v8::Function> function = info[0].As<v8::Function>(); |
| 158 v8::Local<v8::Object> recv; |
| 159 if (info[1]->IsObject()) { |
| 160 recv = info[1]->ToObject(); |
| 161 } else if (info[1]->IsString()) { |
| 162 recv = v8::StringObject::New(info[1]->ToString())->ToObject(); |
| 163 } else { |
158 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | 164 v8::ThrowException(v8::Exception::TypeError(v8::String::New( |
159 "The first argument is the receiver and must be an object"))); | 165 "The first argument is the receiver and must be an object"))); |
160 return; | 166 return; |
161 } | 167 } |
162 v8::Local<v8::Function> function = info[0].As<v8::Function>(); | |
163 v8::Local<v8::Object> recv = info[1]->ToObject(); | |
164 v8::Local<v8::Object> args = info[2]->ToObject(); | 168 v8::Local<v8::Object> args = info[2]->ToObject(); |
165 int first_arg_index = static_cast<int>(info[3]->ToInt32()->Value()); | 169 int first_arg_index = static_cast<int>(info[3]->ToInt32()->Value()); |
166 int args_length = static_cast<int>(info[4]->ToInt32()->Value()); | 170 int args_length = static_cast<int>(info[4]->ToInt32()->Value()); |
167 | 171 |
168 int argc = args_length - first_arg_index; | 172 int argc = args_length - first_arg_index; |
169 scoped_ptr<v8::Local<v8::Value>[]> argv(new v8::Local<v8::Value>[argc]); | 173 scoped_ptr<v8::Local<v8::Value>[]> argv(new v8::Local<v8::Value>[argc]); |
170 for (int i = 0; i < argc; ++i) { | 174 for (int i = 0; i < argc; ++i) { |
171 CHECK(args->Has(i + first_arg_index)); | 175 CHECK(args->Has(i + first_arg_index)); |
172 argv[i] = args->Get(i + first_arg_index); | 176 argv[i] = args->Get(i + first_arg_index); |
173 } | 177 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 } | 211 } |
208 | 212 |
209 v8::Local<v8::Object> SafeBuiltins::GetJSON() const { | 213 v8::Local<v8::Object> SafeBuiltins::GetJSON() const { |
210 return Load("JSON", context_->v8_context()); | 214 return Load("JSON", context_->v8_context()); |
211 } | 215 } |
212 | 216 |
213 v8::Local<v8::Object> SafeBuiltins::GetObjekt() const { | 217 v8::Local<v8::Object> SafeBuiltins::GetObjekt() const { |
214 return Load("Object", context_->v8_context()); | 218 return Load("Object", context_->v8_context()); |
215 } | 219 } |
216 | 220 |
| 221 v8::Local<v8::Object> SafeBuiltins::GetRegExp() const { |
| 222 return Load("RegExp", context_->v8_context()); |
| 223 } |
| 224 |
| 225 v8::Local<v8::Object> SafeBuiltins::GetString() const { |
| 226 return Load("String", context_->v8_context()); |
| 227 } |
| 228 |
217 } // namespace extensions | 229 } // namespace extensions |
OLD | NEW |