Commits

Christian Krebs  committed d580eca

New class to open a single resource.

  • Participants
  • Parent commits 7644c31

Comments (0)

Files changed (5)

File src/client-en.xml

 <script src="./resource-manager/resource_base.js"/>
 <script src="./ui-scripts/metadata_drawer/metadata_drawer.js"/>
 <script src="./resource-manager/resource_display_broker.js"/>
+<script src="./resource-manager/opensingleresource.js"/>
 <script src="./resource-manager/resourcemanager.1.0.events.onurlload.js"/>
 <script src="./resource-manager/resourcemanager.1.0.events.onurlredirect.js"/>
 <script src="./resource-manager/resourcemanager.1.0.events.onurlfinished.js"/>
 <script src="./resource-manager/resourcemanager.1.0.events.onresponsefinished.js"/>
 <script src="./resource-manager/documentmanager.1.0.events.onabouttoloaddocument.js"/>
 
+
 <script src="./network/network_service.js"/>
 <script src="./network/network_view.js"/>
 <script src="./network/network_request_crafting_view.js"/>

File src/resource-manager/opensingleresource.js

+window.cls || (window.cls = {});
+
+cls.OpenSingleResource = function(resource_manager, url)
+{
+  this._resman = resource_manager;
+  this._url = url;
+  if (!this._tagman)
+  {
+    this._init_prototype();
+  }
+  this._show_resource();
+};
+
+cls.OpenSingleResource.prototype = new function()
+{
+  const
+  TRANSPORT_STRING = 1,
+  TRANSPORT_DATA_URI = 3,
+  DECODE_TRUE = 1,
+  SIZE_LIMIT = 1e7,
+  TRANSPORT_OFF = 4;
+
+  this._show_resource = function()
+  {
+    if (!this._resman.show_resource_for_url(this._url))
+    {
+      if (this._service.requestGetResourceID)
+      {
+        var tag = this._tagman.set_callback(this, this._on_resolve_url);
+        this._service.requestGetResourceID(tag, [this._url]);
+      }
+      else
+      {
+        this._fallback();
+      }
+    }
+  };
+
+  this._on_resolve_url = function(status, message)
+  {
+    if (status)
+    {
+      opera.postError("GetResourceID in OpenSingleResource failed");
+      this._fallback();
+    }
+    else
+    {
+      const RESOURCE_ID = 0;
+      this._rid = message[RESOURCE_ID];
+      if (!this._resman.show_resource_for_id(this._rid))
+      {
+        var tag = this._tagman.set_callback(this, this._on_mime_type);
+        this._service.requestGetResource(tag, [this._rid, [TRANSPORT_OFF]]);
+      }
+    }
+  };
+
+  this._on_mime_type = function(status, message)
+  {
+    if (status)
+    {
+      opera.postError("GetResource in OpenSingleResource failed");
+      this._fallback();
+    }
+    else
+    {
+      this._res = new cls.Resource(this._rid);
+      this._res.update("urlfinished", new this._ResourceData(message));
+      var resptype = this._utils.mime_to_content_mode(this._res.mime);
+      var tag = this._tagman.set_callback(this, this._on_resource);
+      var msg = 
+      [
+        this._rid, 
+        [
+          resptype == "datauri" ? TRANSPORT_DATA_URI : TRANSPORT_STRING,
+          DECODE_TRUE,
+          SIZE_LIMIT
+        ]
+      ];
+      this._service.requestGetResource(tag, msg);
+    }
+  };
+
+  this._on_resource = function(status, message)
+  {
+    if (status)
+    {
+      opera.postError("GetResource in OpenSingleResource failed");
+      this._fallback();
+    }
+    else
+    {
+      this._res.update("urlfinished", new this._ResourceData(message));
+      this._resman.open_resource_tab(this._res);
+    }
+  };
+
+  this._fallback = function()
+  {
+    window.open(this._url);
+  };
+
+  this._init_prototype = function()
+  {
+    this._tagman = window.tagManager;
+    this._service = window.services['resource-manager'];
+    this._ResourceData = cls.ResourceManager["1.0"].ResourceData;
+    this._utils = cls.ResourceUtil;
+  }.bind(this);
+
+}; 

File src/resource-manager/resource_all_view.js

     if (res)
     {
       this._open_resource_tab(res);
+      return true;
     }
-    else
-    {
-      var tag = window.tagManager.set_callback(this, function(status, message)
-      {
-        if (status)
-        {
-          opera.postError("GetResource failed.");
-        }
-        else
-        {
-          var raw = new cls.ResourceManager["1.0"].ResourceData(message);
-          var res = new cls.Resource(rid);
-          res.url = raw.url;
-          res.update("urlfinished", raw);
-          this._open_resource_tab(res);
-        }
-      });
-      window.services["resource-manager"].requestGetResource(tag, [rid, [1]]);
-    }
-
+    return false;
   };
 
   this.show_resource_for_url = function(url)
     if (res)
     {
       this._open_resource_tab(res);
+      return true
     }
-    else if (window.services["resource-manager"].requestGetResourceID)
-    {
-      var tag = window.tagManager.set_callback(this, this._on_resolve_url);
-      window.services["resource-manager"].requestGetResourceID(tag, [url]);
-    }
+    return false;
   };
 
-  this._on_resolve_url = function(status, msg)
-  {
-    var RESOURCE_ID = 0;
-    var rid = msg[RESOURCE_ID];
-    this.show_resource_for_id(rid);
-  }
-
   this._render_main_view = function(container)
   {
     var ctx = this._service.get_resource_context();
     ui.show_view(view.id);
   }
 
+  this.open_resource_tab = this._open_resource_tab;
+
   this._handle_open_resource_tab_bound = function(evt, target) {
     var rid = target.getAttribute("data-resource-id");
     if (rid)

File src/resource-manager/resource_display_broker.js

   {
     if (window.services["resource-manager"] && window.views.resource_all)
     {
-      var view = window.views.resource_all;
-      view.show_resource_for_url(url);
+      new cls.OpenSingleResource(window.views.resource_all, url);
     }
     else
     {

File src/resource-manager/resource_service.js

     }
     else if (eventname == "urlfinished")
     {
+      if (!this.url)
+      {
+        this.url = eventdata.url;
+      }
       this.result = eventdata.result;
       this.mime = eventdata.mimeType;
       this.encoding = eventdata.characterEncoding;