]> khamidou — GitHub mirrors - electron.git/commitdiff
Guard against callback being called twice
authorCheng Zhao <zcbenz@gmail.com>
Wed, 12 Aug 2015 05:43:27 +0000 (13:43 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 12 Aug 2015 05:43:27 +0000 (13:43 +0800)
atom/browser/net/js_asker.cc
atom/browser/net/js_asker.h
atom/browser/net/url_request_string_job.cc

index a24e91021792ed99bb4de1360e1151dfcba4ce08..96c6736150e5226bcc1783ec69361bffd69e21e6 100644 (file)
@@ -21,14 +21,21 @@ struct CallbackHolder {
 // Cached JavaScript version of |HandlerCallback|.
 v8::Persistent<v8::FunctionTemplate> g_handler_callback_;
 
-// Cached C++ version of |Function.prototype.bind|.
-base::Callback<v8::Local<v8::Value>(
-    v8::Local<v8::Value>, v8::Local<v8::Value>, v8::Local<v8::Value>)> g_bind;
-
 // The callback which is passed to |handler|.
-void HandlerCallback(v8::Local<v8::External> external, mate::Arguments* args) {
+void HandlerCallback(v8::Isolate* isolate,
+                     v8::Local<v8::External> external,
+                     v8::Local<v8::Object> state,
+                     mate::Arguments* args) {
+  // Check if the callback has already been called.
+  v8::Local<v8::String> called_symbol = mate::StringToSymbol(isolate, "called");
+  if (state->Has(called_symbol))
+    return;  // no nothing
+  else
+    state->Set(called_symbol, v8::Boolean::New(isolate, true));
+
+  // If there is no argument passed then we failed.
   scoped_ptr<CallbackHolder> holder(
-        static_cast<CallbackHolder*>(external->Value()));
+      static_cast<CallbackHolder*>(external->Value()));
   CHECK(holder);
   v8::Local<v8::Value> value;
   if (!args->GetNext(&value)) {
@@ -36,13 +43,14 @@ void HandlerCallback(v8::Local<v8::External> external, mate::Arguments* args) {
     return;
   }
 
+  // Pass whatever user passed to the actaul request job.
   V8ValueConverter converter;
   v8::Local<v8::Context> context = args->isolate()->GetCurrentContext();
   scoped_ptr<base::Value> options(converter.FromV8Value(value, context));
   holder->callback.Run(true, options.Pass());
 }
 
-// func.bind(...).
+// func.bind(func, ...).
 template<typename... ArgTypes>
 v8::Local<v8::Value> BindFunctionWith(v8::Isolate* isolate,
                                       v8::Local<v8::Context> context,
@@ -75,7 +83,8 @@ v8::MaybeLocal<v8::Value> GenerateCallback(v8::Isolate* isolate,
   holder->callback = callback;
   return BindFunctionWith(isolate, context,
                           handler_callback->GetFunction(),
-                          v8::External::New(isolate, holder));
+                          v8::External::New(isolate, holder),
+                          v8::Object::New(isolate));
 }
 
 }  // namespace
index a1bf972f63f9e64e07b781b0a2d33df31db64332..97b384e213f7d8b6173ea77c3b49f5aeae9ff10f 100644 (file)
@@ -64,7 +64,7 @@ class JsAsker : public RequestJob {
   // Called when the JS handler has sent the response, we need to decide whether
   // to start, or fail the job.
   void OnResponse(bool success, scoped_ptr<base::Value> options) {
-    if (success) {
+    if (success && options) {
       StartAsync(options.Pass());
     } else {
       RequestJob::NotifyStartError(
index 1afa95e5128cd3359fb0a9aa1737ea8eafb20697..eec8176d734adf2f2933cddbd2af26ab90662a99 100644 (file)
@@ -19,16 +19,14 @@ URLRequestStringJob::URLRequestStringJob(net::URLRequest* request,
 }
 
 void URLRequestStringJob::StartAsync(scoped_ptr<base::Value> options) {
-  if (options) {
-    if (options->IsType(base::Value::TYPE_DICTIONARY)) {
-      base::DictionaryValue* dict =
-          static_cast<base::DictionaryValue*>(options.get());
-      dict->GetString("mimeType", &mime_type_);
-      dict->GetString("charset", &charset_);
-      dict->GetString("data", &data_);
-    } else if (options->IsType(base::Value::TYPE_STRING)) {
-      options->GetAsString(&data_);
-    }
+  if (options->IsType(base::Value::TYPE_DICTIONARY)) {
+    base::DictionaryValue* dict =
+        static_cast<base::DictionaryValue*>(options.get());
+    dict->GetString("mimeType", &mime_type_);
+    dict->GetString("charset", &charset_);
+    dict->GetString("data", &data_);
+  } else if (options->IsType(base::Value::TYPE_STRING)) {
+    options->GetAsString(&data_);
   }
   net::URLRequestSimpleJob::Start();
 }