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 // A helper class for calling JS function synchronously. | |
86 class CallJsSyncHelper { | |
87 public: | |
88 CallJsSyncHelper(v8::Isolate* isolate) : isolate_(isolate), | |
89 weak_factory_(this) {} | |
90 ~CallJsSyncHelper() {} | |
91 | |
92 ScriptInjectionCallback::CompleteCallback GetCallback() { | |
93 return base::Bind(&CallJsSyncHelper::OnJsComplete, | |
94 weak_factory_.GetWeakPtr()); | |
95 } | |
96 | |
97 bool did_complete() const { return did_complete_; } | |
98 v8::Global<v8::Value> take_result() { return std::move(result_); } | |
99 | |
100 private: | |
101 void OnJsComplete(const std::vector<v8::Local<v8::Value>>& results) { | |
102 did_complete_ = true; | |
103 // The locals are released after the callback is executed, so we need to | |
104 // grab a persistent handle. | |
105 if (!results.empty() && !results[0].IsEmpty()) | |
106 result_.Reset(isolate_, results[0]); | |
107 } | |
108 | |
109 bool did_complete_ = false; | |
110 v8::Isolate* isolate_; | |
111 v8::Global<v8::Value> result_; | |
112 base::WeakPtrFactory<CallJsSyncHelper> weak_factory_; | |
113 | |
114 DISALLOW_COPY_AND_ASSIGN(CallJsSyncHelper); | |
115 }; | |
116 | |
117 v8::Global<v8::Value> CallJsFunctionSync(v8::Local<v8::Function> function, | |
118 v8::Local<v8::Context> context, | |
119 int argc, | |
120 v8::Local<v8::Value> argv[]) { | |
121 CallJsSyncHelper helper(context->GetIsolate()); | |
122 ScriptContext* script_context = | |
123 ScriptContextSet::GetContextByV8Context(context); | |
124 CHECK(script_context); | |
125 script_context->SafeCallFunction(function, argc, argv, helper.GetCallback()); | |
jbroman
2017/01/02 19:46:38
WDYT of this, which seems to do the same but be so
Devlin
2017/01/04 17:57:02
I like it. Done.
I've also added a TODO to expre
| |
126 // If the JS did not, in fact, execute synchronously, there's a big problem. | |
127 CHECK(helper.did_complete()); | |
128 return helper.take_result(); | |
129 } | |
130 | |
85 // Returns the API schema indicated by |api_name|. | 131 // Returns the API schema indicated by |api_name|. |
86 const base::DictionaryValue& GetAPISchema(const std::string& api_name) { | 132 const base::DictionaryValue& GetAPISchema(const std::string& api_name) { |
87 const base::DictionaryValue* schema = | 133 const base::DictionaryValue* schema = |
88 ExtensionAPI::GetSharedInstance()->GetSchema(api_name); | 134 ExtensionAPI::GetSharedInstance()->GetSchema(api_name); |
89 CHECK(schema); | 135 CHECK(schema); |
90 return *schema; | 136 return *schema; |
91 } | 137 } |
92 | 138 |
93 // Returns true if the method specified by |method_name| is available to the | 139 // Returns true if the method specified by |method_name| is available to the |
94 // given |context|. | 140 // given |context|. |
95 bool IsAPIMethodAvailable(ScriptContext* context, | 141 bool IsAPIMethodAvailable(ScriptContext* context, |
96 const std::string& method_name) { | 142 const std::string& method_name) { |
97 return context->GetAvailability(method_name).is_available(); | 143 return context->GetAvailability(method_name).is_available(); |
98 } | 144 } |
99 | 145 |
100 } // namespace | 146 } // namespace |
101 | 147 |
102 NativeExtensionBindingsSystem::NativeExtensionBindingsSystem( | 148 NativeExtensionBindingsSystem::NativeExtensionBindingsSystem( |
103 const SendIPCMethod& send_ipc) | 149 const SendIPCMethod& send_ipc) |
104 : send_ipc_(send_ipc), | 150 : send_ipc_(send_ipc), |
105 api_system_(base::Bind(&CallJsFunction), | 151 api_system_(base::Bind(&CallJsFunction), |
152 base::Bind(&CallJsFunctionSync), | |
106 base::Bind(&GetAPISchema), | 153 base::Bind(&GetAPISchema), |
107 base::Bind(&NativeExtensionBindingsSystem::SendRequest, | 154 base::Bind(&NativeExtensionBindingsSystem::SendRequest, |
108 base::Unretained(this))), | 155 base::Unretained(this))), |
109 weak_factory_(this) {} | 156 weak_factory_(this) {} |
110 | 157 |
111 NativeExtensionBindingsSystem::~NativeExtensionBindingsSystem() {} | 158 NativeExtensionBindingsSystem::~NativeExtensionBindingsSystem() {} |
112 | 159 |
113 void NativeExtensionBindingsSystem::DidCreateScriptContext( | 160 void NativeExtensionBindingsSystem::DidCreateScriptContext( |
114 ScriptContext* context) {} | 161 ScriptContext* context) {} |
115 | 162 |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 params.has_callback = request->has_callback; | 337 params.has_callback = request->has_callback; |
291 params.user_gesture = request->has_user_gesture; | 338 params.user_gesture = request->has_user_gesture; |
292 // TODO(devlin): Make this work in ServiceWorkers. | 339 // TODO(devlin): Make this work in ServiceWorkers. |
293 params.worker_thread_id = -1; | 340 params.worker_thread_id = -1; |
294 params.service_worker_version_id = kInvalidServiceWorkerVersionId; | 341 params.service_worker_version_id = kInvalidServiceWorkerVersionId; |
295 | 342 |
296 send_ipc_.Run(script_context, params); | 343 send_ipc_.Run(script_context, params); |
297 } | 344 } |
298 | 345 |
299 } // namespace extensions | 346 } // namespace extensions |
OLD | NEW |