]> khamidou — GitHub mirrors - electron.git/commitdiff
Call the JS handler to get the type of job.
authorCheng Zhao <zcbenz@gmail.com>
Sat, 24 Aug 2013 12:18:12 +0000 (20:18 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Sat, 24 Aug 2013 12:18:12 +0000 (20:18 +0800)
browser/api/atom_api_protocol.cc
browser/api/atom_api_protocol.h
spec/api/protocol.coffee

index 8d5f0d104e0c5faf0cf21929ab1974139f0ca506..91d7f5cc8d07c79d048d754aa4c18d461622d9bf 100644 (file)
@@ -20,6 +20,9 @@ namespace api {
 
 namespace {
 
+typedef std::map<std::string, v8::Persistent<v8::Function>> HandlersMap;
+static HandlersMap g_handlers;
+
 net::URLRequestJobFactoryImpl* GetRequestJobFactory() {
   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
   // Get the job factory.
@@ -96,6 +99,26 @@ class AdapterRequestJob : public net::URLRequestJob {
 
   void GetJobTypeInUI() {
     DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+
+    // Call the JS handler.
+    v8::HandleScope scope;
+    v8::Handle<v8::Value> argv[] = {
+      v8::String::New(request()->url().spec().c_str()),
+      v8::String::New(request()->referrer().c_str()),
+    };
+    v8::Handle<v8::Value> result = g_handlers[request()->url().scheme()]->Call(
+        v8::Context::GetCurrent()->Global(), arraysize(argv), argv);
+
+    // Determine the type of the job we are going to create.
+    if (result->IsString()) {
+      type_ = STRING_JOB;
+      string_ = *v8::String::Utf8Value(result);
+    } else {
+      type_ = ERROR_JOB;
+      error_code_ = ERR_NOT_IMPLEMENTED;
+    }
+
+    // Go back to the IO thread.
     content::BrowserThread::PostTask(
         content::BrowserThread::IO,
         FROM_HERE,
@@ -113,6 +136,9 @@ class AdapterRequestJob : public net::URLRequestJob {
   scoped_refptr<net::URLRequestJob> real_job_;
 
   JOB_TYPE type_;
+  int error_code_;
+  std::string string_;
+  base::FilePath file_path_;
 
   DISALLOW_COPY_AND_ASSIGN(AdapterRequestJob);
 };
@@ -137,18 +163,16 @@ class AdapterProtocolHandler
 
 }  // namespace
 
-Protocol::HandlersMap Protocol::handlers_;
-
 // static
 v8::Handle<v8::Value> Protocol::RegisterProtocol(const v8::Arguments& args) {
   std::string scheme(*v8::String::Utf8Value(args[0]));
-  if (handlers_.find(scheme) != handlers_.end())
+  if (g_handlers.find(scheme) != g_handlers.end())
     return node::ThrowError("The scheme is already registered");
 
   // Store the handler in a map.
   if (!args[1]->IsFunction())
     return node::ThrowError("Handler must be a function");
-  handlers_[scheme] = v8::Persistent<v8::Function>::New(
+  g_handlers[scheme] = v8::Persistent<v8::Function>::New(
       node::node_isolate, v8::Handle<v8::Function>::Cast(args[1]));
 
   content::BrowserThread::PostTask(content::BrowserThread::IO,
@@ -163,10 +187,10 @@ v8::Handle<v8::Value> Protocol::UnregisterProtocol(const v8::Arguments& args) {
   std::string scheme(*v8::String::Utf8Value(args[0]));
 
   // Erase the handler from map.
-  HandlersMap::iterator it(handlers_.find(scheme));
-  if (it == handlers_.end())
+  HandlersMap::iterator it(g_handlers.find(scheme));
+  if (it == g_handlers.end())
     return node::ThrowError("The scheme has not been registered");
-  handlers_.erase(it);
+  g_handlers.erase(it);
 
   content::BrowserThread::PostTask(content::BrowserThread::IO,
                                    FROM_HERE,
index 4f53c1d287a9f570928785a090f76aa6330abaf2..7e2f0bf36d27f7eb73bc1ba7181dd1d3a0f362a4 100644 (file)
@@ -26,9 +26,6 @@ class Protocol {
   static void RegisterProtocolInIO(const std::string& scheme);
   static void UnregisterProtocolInIO(const std::string& scheme);
 
-  typedef std::map<std::string, v8::Persistent<v8::Function>> HandlersMap;
-  static HandlersMap handlers_;
-
   DISALLOW_IMPLICIT_CONSTRUCTORS(Protocol);
 };
 
index 332afecf8d387312bd664d57707445738305448c..5ca7ed1bc8de9a476a9d60078f0959fc5520671e 100644 (file)
@@ -4,12 +4,17 @@ protocol = require('remote').require 'protocol'
 describe 'protocol API', ->
   describe 'protocol.registerProtocol', ->
     it 'throws error when scheme is already registered', ->
-      register = -> protocol.registerProtocol('test', ->)
+      register = -> protocol.registerProtocol('test1', ->)
       register()
       assert.throws register, /The scheme is already registered/
-      protocol.unregisterProtocol 'test'
+      protocol.unregisterProtocol 'test1'
+
+    it 'calls the callback when scheme is visited', (done) ->
+      protocol.registerProtocol 'test2', -> done()
+      $.get 'test2://test2', ->
+      protocol.unregisterProtocol 'test2'
 
   describe 'protocol.unregisterProtocol', ->
     it 'throws error when scheme does not exist', ->
-      unregister = -> protocol.unregisterProtocol 'test'
+      unregister = -> protocol.unregisterProtocol 'test3'
       assert.throws unregister, /The scheme has not been registered/