| OLD | NEW |
| 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" |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 v8::Local<v8::Context> context) { | 131 v8::Local<v8::Context> context) { |
| 132 CHECK(!value.IsEmpty() && value->IsObject()) << name; | 132 CHECK(!value.IsEmpty() && value->IsObject()) << name; |
| 133 context->Global()->SetHiddenValue(MakeKey(name, context->GetIsolate()), | 133 context->Global()->SetHiddenValue(MakeKey(name, context->GetIsolate()), |
| 134 value); | 134 value); |
| 135 } | 135 } |
| 136 | 136 |
| 137 v8::Local<v8::Object> Load(const char* name, v8::Handle<v8::Context> context) { | 137 v8::Local<v8::Object> Load(const char* name, v8::Handle<v8::Context> context) { |
| 138 v8::Local<v8::Value> value = | 138 v8::Local<v8::Value> value = |
| 139 context->Global()->GetHiddenValue(MakeKey(name, context->GetIsolate())); | 139 context->Global()->GetHiddenValue(MakeKey(name, context->GetIsolate())); |
| 140 CHECK(!value.IsEmpty() && value->IsObject()) << name; | 140 CHECK(!value.IsEmpty() && value->IsObject()) << name; |
| 141 return value->ToObject(); | 141 return v8::Local<v8::Object>::Cast(value); |
| 142 } | 142 } |
| 143 | 143 |
| 144 class ExtensionImpl : public v8::Extension { | 144 class ExtensionImpl : public v8::Extension { |
| 145 public: | 145 public: |
| 146 ExtensionImpl() : v8::Extension(kClassName, kScript) {} | 146 ExtensionImpl() : v8::Extension(kClassName, kScript) {} |
| 147 | 147 |
| 148 private: | 148 private: |
| 149 v8::Handle<v8::FunctionTemplate> GetNativeFunctionTemplate( | 149 v8::Handle<v8::FunctionTemplate> GetNativeFunctionTemplate( |
| 150 v8::Isolate* isolate, | 150 v8::Isolate* isolate, |
| 151 v8::Handle<v8::String> name) override { | 151 v8::Handle<v8::String> name) override { |
| 152 if (name->Equals(v8::String::NewFromUtf8(isolate, "Apply"))) | 152 if (name->Equals(v8::String::NewFromUtf8(isolate, "Apply"))) |
| 153 return v8::FunctionTemplate::New(isolate, Apply); | 153 return v8::FunctionTemplate::New(isolate, Apply); |
| 154 if (name->Equals(v8::String::NewFromUtf8(isolate, "Save"))) | 154 if (name->Equals(v8::String::NewFromUtf8(isolate, "Save"))) |
| 155 return v8::FunctionTemplate::New(isolate, Save); | 155 return v8::FunctionTemplate::New(isolate, Save); |
| 156 NOTREACHED() << *v8::String::Utf8Value(name); | 156 NOTREACHED() << *v8::String::Utf8Value(name); |
| 157 return v8::Handle<v8::FunctionTemplate>(); | 157 return v8::Handle<v8::FunctionTemplate>(); |
| 158 } | 158 } |
| 159 | 159 |
| 160 static void Apply(const v8::FunctionCallbackInfo<v8::Value>& info) { | 160 static void Apply(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 161 CHECK(info.Length() == 5 && info[0]->IsFunction() && // function | 161 CHECK(info.Length() == 5 && info[0]->IsFunction() && // function |
| 162 // info[1] could be an object or a string | 162 // info[1] could be an object or a string |
| 163 info[2]->IsObject() && // args | 163 info[2]->IsObject() && // args |
| 164 info[3]->IsInt32() && // first_arg_index | 164 info[3]->IsInt32() && // first_arg_index |
| 165 info[4]->IsInt32()); // args_length | 165 info[4]->IsInt32()); // args_length |
| 166 v8::Local<v8::Function> function = info[0].As<v8::Function>(); | 166 v8::Local<v8::Function> function = info[0].As<v8::Function>(); |
| 167 v8::Local<v8::Object> recv; | 167 v8::Local<v8::Object> recv; |
| 168 if (info[1]->IsObject()) { | 168 if (info[1]->IsObject()) { |
| 169 recv = info[1]->ToObject(); | 169 recv = v8::Local<v8::Object>::Cast(info[1]); |
| 170 } else if (info[1]->IsString()) { | 170 } else if (info[1]->IsString()) { |
| 171 recv = v8::StringObject::New(info[1]->ToString())->ToObject(); | 171 recv = v8::StringObject::New(v8::Local<v8::String>::Cast(info[1])) |
| 172 ->ToObject(info.GetIsolate()); |
| 172 } else { | 173 } else { |
| 173 info.GetIsolate()->ThrowException( | 174 info.GetIsolate()->ThrowException( |
| 174 v8::Exception::TypeError(v8::String::NewFromUtf8( | 175 v8::Exception::TypeError(v8::String::NewFromUtf8( |
| 175 info.GetIsolate(), | 176 info.GetIsolate(), |
| 176 "The first argument is the receiver and must be an object"))); | 177 "The first argument is the receiver and must be an object"))); |
| 177 return; | 178 return; |
| 178 } | 179 } |
| 179 v8::Local<v8::Object> args = info[2]->ToObject(); | 180 v8::Local<v8::Object> args = v8::Local<v8::Object>::Cast(info[2]); |
| 180 int first_arg_index = static_cast<int>(info[3]->ToInt32()->Value()); | 181 int first_arg_index = info[3]->ToInt32(info.GetIsolate())->Value(); |
| 181 int args_length = static_cast<int>(info[4]->ToInt32()->Value()); | 182 int args_length = info[4]->ToInt32(info.GetIsolate())->Value(); |
| 182 | 183 |
| 183 int argc = args_length - first_arg_index; | 184 int argc = args_length - first_arg_index; |
| 184 scoped_ptr<v8::Local<v8::Value> []> argv(new v8::Local<v8::Value>[argc]); | 185 scoped_ptr<v8::Local<v8::Value> []> argv(new v8::Local<v8::Value>[argc]); |
| 185 for (int i = 0; i < argc; ++i) { | 186 for (int i = 0; i < argc; ++i) { |
| 186 CHECK(args->Has(i + first_arg_index)); | 187 CHECK(args->Has(i + first_arg_index)); |
| 187 argv[i] = args->Get(i + first_arg_index); | 188 argv[i] = args->Get(i + first_arg_index); |
| 188 } | 189 } |
| 189 | 190 |
| 190 v8::Local<v8::Value> return_value = function->Call(recv, argc, argv.get()); | 191 v8::Local<v8::Value> return_value = function->Call(recv, argc, argv.get()); |
| 191 if (!return_value.IsEmpty()) | 192 if (!return_value.IsEmpty()) |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 | 232 |
| 232 v8::Local<v8::Object> SafeBuiltins::GetString() const { | 233 v8::Local<v8::Object> SafeBuiltins::GetString() const { |
| 233 return Load("String", context_->v8_context()); | 234 return Load("String", context_->v8_context()); |
| 234 } | 235 } |
| 235 | 236 |
| 236 v8::Local<v8::Object> SafeBuiltins::GetError() const { | 237 v8::Local<v8::Object> SafeBuiltins::GetError() const { |
| 237 return Load("Error", context_->v8_context()); | 238 return Load("Error", context_->v8_context()); |
| 238 } | 239 } |
| 239 | 240 |
| 240 } // namespace extensions | 241 } // namespace extensions |
| OLD | NEW |