Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / renderer / resources / extensions / page_capture_custom_bindings.js
index f9147d7..a7a140a 100644 (file)
@@ -6,6 +6,7 @@
 
 var binding = require('binding').Binding.create('pageCapture');
 
+var handleUncaughtException = require('uncaught_exception_handler').handle;
 var pageCaptureNatives = requireNative('page_capture');
 var CreateBlob = pageCaptureNatives.CreateBlob;
 var SendResponseAck = pageCaptureNatives.SendResponseAck;
@@ -15,16 +16,23 @@ binding.registerCustomHook(function(bindingsAPI) {
 
   apiFunctions.setCustomCallback('saveAsMHTML',
                                  function(name, request, response) {
-    var path = response.mhtmlFilePath;
-    var size = response.mhtmlFileLength;
-
-    if (request.callback)
-      request.callback(CreateBlob(path, size));
+    var callback = request.callback;
     request.callback = null;
-
-    // Notify the browser. Now that the blob is referenced from JavaScript,
-    // the browser can drop its reference to it.
-    SendResponseAck(request.id);
+    if (response)
+      response = CreateBlob(response.mhtmlFilePath, response.mhtmlFileLength);
+
+    try {
+      callback(response);
+    } catch (e) {
+      var message = "Error in chrome.pageCapture.saveAsMHTML callback: " + e;
+      if (request.stack)
+        message += "\n" + request.stack;
+      handleUncaughtException(message, e);
+    } finally {
+      // Notify the browser. Now that the blob is referenced from JavaScript,
+      // the browser can drop its reference to it.
+      SendResponseAck(request.id);
+    }
   });
 });