OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/api_binding.h" | 5 #include "extensions/renderer/api_binding.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 base::StringPrintf("%s.%s", api_name_.c_str(), event_name.c_str()); | 160 base::StringPrintf("%s.%s", api_name_.c_str(), event_name.c_str()); |
161 v8::Local<v8::Object> event = | 161 v8::Local<v8::Object> event = |
162 event_handler->CreateEventInstance(full_event_name, context); | 162 event_handler->CreateEventInstance(full_event_name, context); |
163 DCHECK(!event.IsEmpty()); | 163 DCHECK(!event.IsEmpty()); |
164 v8::Maybe<bool> success = object->CreateDataProperty( | 164 v8::Maybe<bool> success = object->CreateDataProperty( |
165 context, gin::StringToSymbol(isolate, event_name), event); | 165 context, gin::StringToSymbol(isolate, event_name), event); |
166 DCHECK(success.IsJust()); | 166 DCHECK(success.IsJust()); |
167 DCHECK(success.FromJust()); | 167 DCHECK(success.FromJust()); |
168 } | 168 } |
169 | 169 |
170 if (binding_hooks_) | 170 binding_hooks_->InitializeInContext(context, api_name_); |
171 binding_hooks_->InitializeInContext(context, api_name_); | |
172 | 171 |
173 return object; | 172 return object; |
174 } | 173 } |
175 | 174 |
| 175 v8::Local<v8::Object> APIBinding::GetJSHookInterface( |
| 176 v8::Local<v8::Context> context) { |
| 177 return binding_hooks_->GetJSHookInterface(api_name_, context); |
| 178 } |
| 179 |
176 void APIBinding::HandleCall(const std::string& name, | 180 void APIBinding::HandleCall(const std::string& name, |
177 const APISignature* signature, | 181 const APISignature* signature, |
178 gin::Arguments* arguments) { | 182 gin::Arguments* arguments) { |
179 std::string error; | 183 std::string error; |
180 v8::Isolate* isolate = arguments->isolate(); | 184 v8::Isolate* isolate = arguments->isolate(); |
181 v8::HandleScope handle_scope(isolate); | 185 v8::HandleScope handle_scope(isolate); |
182 | 186 |
183 // Since this is called synchronously from the JS entry point, | 187 // Since this is called synchronously from the JS entry point, |
184 // GetCurrentContext() should always be correct. | 188 // GetCurrentContext() should always be correct. |
185 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 189 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
186 | 190 |
187 // Check for a custom hook to handle the method. | 191 // Check for a custom hook to handle the method. |
188 if (binding_hooks_ && | 192 if (binding_hooks_->HandleRequest(api_name_, name, context, |
189 binding_hooks_->HandleRequest(api_name_, name, context, | |
190 signature, arguments)) { | 193 signature, arguments)) { |
191 return; // Handled by a custom hook. | 194 return; // Handled by a custom hook. |
192 } | 195 } |
193 | 196 |
194 std::unique_ptr<base::ListValue> parsed_arguments; | 197 std::unique_ptr<base::ListValue> parsed_arguments; |
195 v8::Local<v8::Function> callback; | 198 v8::Local<v8::Function> callback; |
196 { | 199 { |
197 v8::TryCatch try_catch(isolate); | 200 v8::TryCatch try_catch(isolate); |
198 parsed_arguments = | 201 parsed_arguments = |
199 signature->ParseArguments(arguments, *type_refs_, &callback, &error); | 202 signature->ParseArguments(arguments, *type_refs_, &callback, &error); |
200 if (try_catch.HasCaught()) { | 203 if (try_catch.HasCaught()) { |
201 DCHECK(!parsed_arguments); | 204 DCHECK(!parsed_arguments); |
202 try_catch.ReThrow(); | 205 try_catch.ReThrow(); |
203 return; | 206 return; |
204 } | 207 } |
205 } | 208 } |
206 if (!parsed_arguments) { | 209 if (!parsed_arguments) { |
207 arguments->ThrowTypeError("Invalid invocation"); | 210 arguments->ThrowTypeError("Invalid invocation"); |
208 return; | 211 return; |
209 } | 212 } |
210 | 213 |
211 method_callback_.Run(name, std::move(parsed_arguments), isolate, | 214 method_callback_.Run(name, std::move(parsed_arguments), isolate, |
212 context, callback); | 215 context, callback); |
213 } | 216 } |
214 | 217 |
215 } // namespace extensions | 218 } // namespace extensions |
OLD | NEW |