Implement protocol.registerBufferProtocol
authorCheng Zhao <zcbenz@gmail.com>
Wed, 12 Aug 2015 12:37:52 +0000 (20:37 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 12 Aug 2015 12:37:52 +0000 (20:37 +0800)
atom/browser/api/atom_api_protocol.cc
atom/browser/lib/chrome-extension.coffee
atom/browser/net/url_request_buffer_job.cc
atom/browser/net/url_request_buffer_job.h

index 0bd6728..366b673 100644 (file)
@@ -7,8 +7,9 @@
 #include "atom/browser/atom_browser_client.h"
 #include "atom/browser/atom_browser_context.h"
 #include "atom/browser/atom_browser_main_parts.h"
-#include "atom/browser/net/url_request_string_job.h"
 #include "atom/browser/net/url_request_async_asar_job.h"
+#include "atom/browser/net/url_request_buffer_job.h"
+#include "atom/browser/net/url_request_string_job.h"
 #include "atom/common/native_mate_converters/callback.h"
 #include "native_mate/dictionary.h"
 
@@ -48,6 +49,8 @@ mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder(
       .SetMethod("registerStandardSchemes", &Protocol::RegisterStandardSchemes)
       .SetMethod("registerStringProtocol",
                  &Protocol::RegisterProtocol<URLRequestStringJob>)
+      .SetMethod("registerBufferProtocol",
+                 &Protocol::RegisterProtocol<URLRequestBufferJob>)
       .SetMethod("registerFileProtocol",
                  &Protocol::RegisterProtocol<UrlRequestAsyncAsarJob>);
 }
index ccb52d3..df0d717 100644 (file)
@@ -72,8 +72,8 @@ app.once 'ready', ->
     directory = getPathForHost parsed.hostname
     return callback() unless directory?
     callback path.join(directory, parsed.path)
-  protocol.registerFileProtocol 'chrome-extension', chromeExtensionHandler, ->
-    console.error 'Unable to register chrome-extension protocol'
+  protocol.registerFileProtocol 'chrome-extension', chromeExtensionHandler, (error) ->
+    console.error 'Unable to register chrome-extension protocol' if error
 
   BrowserWindow::_loadDevToolsExtensions = (extensionInfoArray) ->
     @devToolsWebContents?.executeJavaScript "DevToolsAPI.addExtensions(#{JSON.stringify(extensionInfoArray)});"
index a8233b8..5cfb60a 100644 (file)
@@ -13,13 +13,34 @@ namespace atom {
 URLRequestBufferJob::URLRequestBufferJob(
     net::URLRequest* request,
     net::NetworkDelegate* network_delegate,
-    const std::string& mime_type,
-    const std::string& charset,
-    scoped_refptr<base::RefCountedBytes> data)
-    : net::URLRequestSimpleJob(request, network_delegate),
-      mime_type_(mime_type),
-      charset_(charset),
-      buffer_data_(data) {
+    v8::Isolate* isolate,
+    const JavaScriptHandler& handler)
+    : JsAsker<net::URLRequestSimpleJob>(request, network_delegate, isolate,
+                                        handler) {
+}
+
+void URLRequestBufferJob::StartAsync(scoped_ptr<base::Value> options) {
+  const base::BinaryValue* binary = nullptr;
+  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->GetBinary("data", &binary);
+  } else if (options->IsType(base::Value::TYPE_BINARY)) {
+    options->GetAsBinary(&binary);
+  }
+
+  if (!binary) {
+    NotifyStartError(net::URLRequestStatus(
+          net::URLRequestStatus::FAILED, net::ERR_NOT_IMPLEMENTED));
+    return;
+  }
+
+  data_ = new base::RefCountedBytes(
+      reinterpret_cast<const unsigned char*>(binary->GetBuffer()),
+      binary->GetSize());
+  net::URLRequestSimpleJob::Start();
 }
 
 int URLRequestBufferJob::GetRefCountedData(
@@ -29,7 +50,7 @@ int URLRequestBufferJob::GetRefCountedData(
     const net::CompletionCallback& callback) const {
   *mime_type = mime_type_;
   *charset = charset_;
-  *data = buffer_data_;
+  *data = data_;
   return net::OK;
 }
 
index cbdfbfa..c72e5b8 100644 (file)
@@ -7,19 +7,22 @@
 
 #include <string>
 
+#include "atom/browser/net/js_asker.h"
+#include "atom/common/node_includes.h"
 #include "base/memory/ref_counted_memory.h"
 #include "net/url_request/url_request_simple_job.h"
-#include "atom/common/node_includes.h"
 
 namespace atom {
 
-class URLRequestBufferJob : public net::URLRequestSimpleJob {
+class URLRequestBufferJob : public JsAsker<net::URLRequestSimpleJob> {
  public:
   URLRequestBufferJob(net::URLRequest* request,
                       net::NetworkDelegate* network_delegate,
-                      const std::string& mime_type,
-                      const std::string& charset,
-                      scoped_refptr<base::RefCountedBytes> data);
+                      v8::Isolate* isolate,
+                      const JavaScriptHandler& handler);
+
+  // JsAsker:
+  void StartAsync(scoped_ptr<base::Value> options) override;
 
   // URLRequestSimpleJob:
   int GetRefCountedData(std::string* mime_type,
@@ -30,7 +33,7 @@ class URLRequestBufferJob : public net::URLRequestSimpleJob {
  private:
   std::string mime_type_;
   std::string charset_;
-  scoped_refptr<base::RefCountedBytes> buffer_data_;
+  scoped_refptr<base::RefCountedBytes> data_;
 
   DISALLOW_COPY_AND_ASSIGN(URLRequestBufferJob);
 };