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/native_extension_bindings_system.h" | 5 #include "extensions/renderer/native_extension_bindings_system.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "content/public/common/content_switches.h" | 9 #include "content/public/common/content_switches.h" |
10 #include "extensions/common/constants.h" | 10 #include "extensions/common/constants.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 void CallJsFunction(v8::Local<v8::Function> function, | 75 void CallJsFunction(v8::Local<v8::Function> function, |
76 v8::Local<v8::Context> context, | 76 v8::Local<v8::Context> context, |
77 int argc, | 77 int argc, |
78 v8::Local<v8::Value> argv[]) { | 78 v8::Local<v8::Value> argv[]) { |
79 ScriptContext* script_context = | 79 ScriptContext* script_context = |
80 ScriptContextSet::GetContextByV8Context(context); | 80 ScriptContextSet::GetContextByV8Context(context); |
81 CHECK(script_context); | 81 CHECK(script_context); |
82 script_context->SafeCallFunction(function, argc, argv); | 82 script_context->SafeCallFunction(function, argc, argv); |
83 } | 83 } |
84 | 84 |
| 85 v8::Global<v8::Value> CallJsFunctionSync(v8::Local<v8::Function> function, |
| 86 v8::Local<v8::Context> context, |
| 87 int argc, |
| 88 v8::Local<v8::Value> argv[]) { |
| 89 bool did_complete = false; |
| 90 v8::Global<v8::Value> result; |
| 91 auto callback = base::Bind([]( |
| 92 v8::Isolate* isolate, |
| 93 bool* did_complete_out, |
| 94 v8::Global<v8::Value>* result_out, |
| 95 const std::vector<v8::Local<v8::Value>>& results) { |
| 96 *did_complete_out = true; |
| 97 // The locals are released after the callback is executed, so we need to |
| 98 // grab a persistent handle. |
| 99 if (!results.empty() && !results[0].IsEmpty()) |
| 100 result_out->Reset(isolate, results[0]); |
| 101 }, base::Unretained(context->GetIsolate()), |
| 102 base::Unretained(&did_complete), base::Unretained(&result)); |
| 103 |
| 104 ScriptContext* script_context = |
| 105 ScriptContextSet::GetContextByV8Context(context); |
| 106 CHECK(script_context); |
| 107 script_context->SafeCallFunction(function, argc, argv, callback); |
| 108 CHECK(did_complete) << "expected script to execute synchronously"; |
| 109 return result; |
| 110 } |
| 111 |
85 // Returns the API schema indicated by |api_name|. | 112 // Returns the API schema indicated by |api_name|. |
86 const base::DictionaryValue& GetAPISchema(const std::string& api_name) { | 113 const base::DictionaryValue& GetAPISchema(const std::string& api_name) { |
87 const base::DictionaryValue* schema = | 114 const base::DictionaryValue* schema = |
88 ExtensionAPI::GetSharedInstance()->GetSchema(api_name); | 115 ExtensionAPI::GetSharedInstance()->GetSchema(api_name); |
89 CHECK(schema); | 116 CHECK(schema); |
90 return *schema; | 117 return *schema; |
91 } | 118 } |
92 | 119 |
93 // Returns true if the method specified by |method_name| is available to the | 120 // Returns true if the method specified by |method_name| is available to the |
94 // given |context|. | 121 // given |context|. |
95 bool IsAPIMethodAvailable(ScriptContext* context, | 122 bool IsAPIMethodAvailable(ScriptContext* context, |
96 const std::string& method_name) { | 123 const std::string& method_name) { |
97 return context->GetAvailability(method_name).is_available(); | 124 return context->GetAvailability(method_name).is_available(); |
98 } | 125 } |
99 | 126 |
100 } // namespace | 127 } // namespace |
101 | 128 |
102 NativeExtensionBindingsSystem::NativeExtensionBindingsSystem( | 129 NativeExtensionBindingsSystem::NativeExtensionBindingsSystem( |
103 const SendIPCMethod& send_ipc) | 130 const SendIPCMethod& send_ipc) |
104 : send_ipc_(send_ipc), | 131 : send_ipc_(send_ipc), |
105 api_system_(base::Bind(&CallJsFunction), | 132 api_system_(base::Bind(&CallJsFunction), |
| 133 base::Bind(&CallJsFunctionSync), |
106 base::Bind(&GetAPISchema), | 134 base::Bind(&GetAPISchema), |
107 base::Bind(&NativeExtensionBindingsSystem::SendRequest, | 135 base::Bind(&NativeExtensionBindingsSystem::SendRequest, |
108 base::Unretained(this))), | 136 base::Unretained(this))), |
109 weak_factory_(this) {} | 137 weak_factory_(this) {} |
110 | 138 |
111 NativeExtensionBindingsSystem::~NativeExtensionBindingsSystem() {} | 139 NativeExtensionBindingsSystem::~NativeExtensionBindingsSystem() {} |
112 | 140 |
113 void NativeExtensionBindingsSystem::DidCreateScriptContext( | 141 void NativeExtensionBindingsSystem::DidCreateScriptContext( |
114 ScriptContext* context) {} | 142 ScriptContext* context) {} |
115 | 143 |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 params.has_callback = request->has_callback; | 318 params.has_callback = request->has_callback; |
291 params.user_gesture = request->has_user_gesture; | 319 params.user_gesture = request->has_user_gesture; |
292 // TODO(devlin): Make this work in ServiceWorkers. | 320 // TODO(devlin): Make this work in ServiceWorkers. |
293 params.worker_thread_id = -1; | 321 params.worker_thread_id = -1; |
294 params.service_worker_version_id = kInvalidServiceWorkerVersionId; | 322 params.service_worker_version_id = kInvalidServiceWorkerVersionId; |
295 | 323 |
296 send_ipc_.Run(script_context, params); | 324 send_ipc_.Run(script_context, params); |
297 } | 325 } |
298 | 326 |
299 } // namespace extensions | 327 } // namespace extensions |
OLD | NEW |