[Pkg-mozext-commits] [tabmixplus] 48/107: Move getTitleFromBookmark functions to Places.jsm

David Prévot taffit at moszumanska.debian.org
Tue Dec 29 19:02:49 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository tabmixplus.

commit 01db8b43dff1a94d1940b2b11feb88ae354575e7
Author: onemen <tabmix.onemen at gmail.com>
Date:   Tue Nov 3 15:58:10 2015 +0200

    Move getTitleFromBookmark functions to Places.jsm
---
 chrome/content/places/places.js | 56 +++-------------------------
 modules/Places.jsm              | 81 ++++++++++++++++++++++++++++++++++++++++-
 modules/RenameTab.jsm           |  7 +++-
 3 files changed, 89 insertions(+), 55 deletions(-)

diff --git a/chrome/content/places/places.js b/chrome/content/places/places.js
index 12f0ef6..1e80707 100644
--- a/chrome/content/places/places.js
+++ b/chrome/content/places/places.js
@@ -25,6 +25,8 @@ var TMP_Places = {
   },
 
   init: function TMP_PC_init() {
+    Tabmix.lazy_import(this, "PlacesUtils", "Places", "TabmixPlacesUtils");
+
     this.contextMenu.toggleEventListener(true);
 
     // use tab label for bookmark name when user renamed the tab
@@ -324,61 +326,13 @@ var TMP_Places = {
     return newTitle || title;
   },
 
-  _getBookmarkTitle: function(aUrl, aID) {
-    let aItemId = aID.value || -1;
-    try {
-      if (aItemId > -1) {
-        var _URI = PlacesUtils.bookmarks.getBookmarkURI(aItemId);
-        if (_URI && _URI.spec == aUrl)
-          return PlacesUtils.bookmarks.getItemTitle(aItemId);
-      }
-    } catch (ex) { }
-    try {
-      let uri = Services.io.newURI(aUrl, null, null);
-      aItemId = aID.value = PlacesUtils.getMostRecentBookmarkForURI(uri);
-      if (aItemId > -1)
-        return PlacesUtils.bookmarks.getItemTitle(aItemId);
-    } catch (ex) { }
-    aID.value = null;
-    return null;
-  },
-
   get _titlefrombookmark() {
     delete this._titlefrombookmark;
     return (this._titlefrombookmark = Tabmix.prefs.getBoolPref("titlefrombookmark"));
   },
 
-  applyCallBackOnUrl: function(aUrl, aCallBack) {
- ///XXX need to work with nsURI
-    let hasHref = aUrl.indexOf("#") > -1;
-    let result = aCallBack.apply(this, [aUrl]) ||
-                 hasHref && aCallBack.apply(this, aUrl.split("#"));
-    // when IE Tab is installed try to find url with or without the prefix
-    let ietab = Tabmix.extensions.gIeTab;
-    if (!result && ietab) {
-      let prefix = "chrome://" + ietab.folder + "/content/reloaded.html?url=";
-      if (aUrl != prefix) {
-        let url = aUrl.startsWith(prefix) ?
-            aUrl.replace(prefix, "") : prefix + aUrl;
-        result = aCallBack.apply(this, [url]) ||
-                 hasHref && aCallBack.apply(this, url.split("#"));
-      }
-    }
-    return result;
-  },
-
-  getTitleFromBookmark: function TMP_getTitleFromBookmark(aUrl, aTitle, aItemId, aTab) {
-    if (!this._titlefrombookmark || !aUrl)
-      return aTitle;
-
-    var oID = {value: aTab ? aTab.getAttribute("tabmix_bookmarkId") : aItemId};
-    var getTitle = url => this._getBookmarkTitle(url, oID);
-    var title = this.applyCallBackOnUrl(aUrl, getTitle);
-    // setItem check if aTab exist and remove the attribute if
-    // oID.value is null
-    Tabmix.setItem(aTab, "tabmix_bookmarkId", oID.value);
-
-    return title || aTitle;
+  getTitleFromBookmark: function(aUrl, aTitle, aItemId, aTab) {
+    return this.PlacesUtils.getTitleFromBookmark(aUrl, aTitle, aItemId, aTab);
   },
 
   isUserRenameTab: function(aTab, aUrl) {
@@ -488,7 +442,7 @@ var TMP_Places = {
       let url = tab.linkedBrowser.currentURI.spec;
       if (this.isUserRenameTab(tab, url))
         return;
-      let index = this.applyCallBackOnUrl(url, getIndex);
+      let index = this.PlacesUtils.applyCallBackOnUrl(url, getIndex);
       if (index) {
         tab.setAttribute("tabmix_bookmarkId", aItemId[index - 1]);
         this.setTabTitle(tab, url);
diff --git a/modules/Places.jsm b/modules/Places.jsm
index d9edf0a..28f301d 100644
--- a/modules/Places.jsm
+++ b/modules/Places.jsm
@@ -33,7 +33,15 @@ this.TabmixPlacesUtils = Object.freeze({
 
   onQuitApplication: function() {
     PlacesUtilsInternal.onQuitApplication();
-  }
+  },
+
+  applyCallBackOnUrl: function(aUrl, aCallBack) {
+    return PlacesUtilsInternal.applyCallBackOnUrl(aUrl, aCallBack);
+  },
+
+  getTitleFromBookmark: function(aUrl, aTitle, aItemId, aTab) {
+    return PlacesUtilsInternal.getTitleFromBookmark(aUrl, aTitle, aItemId, aTab);
+  },
 });
 
 var Tabmix = {};
@@ -48,6 +56,7 @@ var PlacesUtilsInternal = {
     this._initialized = true;
 
     Tabmix._debugMode = aWindow.Tabmix._debugMode;
+    Tabmix.gIeTab = aWindow.Tabmix.extensions.gIeTab;
     Services.scriptloader.loadSubScript("chrome://tabmixplus/content/changecode.js");
 
     this.initPlacesUIUtils(aWindow);
@@ -178,5 +187,73 @@ var PlacesUtilsInternal = {
       'bookMarkId: aNode.itemId, initiatingDoc: null,\n' +
       '        $&'
     ).toCode();
-  }
+  },
+
+  // Lazy getter for titlefrombookmark preference
+  get titlefrombookmark() {
+    let updateValue = () => {
+      let value = Services.prefs.getBoolPref(PREF);
+      let definition = {value: value, configurable: true};
+      Object.defineProperty(this, "titlefrombookmark", definition);
+      return value;
+    };
+
+    const PREF = "extensions.tabmix.titlefrombookmark";
+    Services.prefs.addObserver(PREF, updateValue, false);
+    return updateValue();
+  },
+
+  getBookmarkTitle: function(aUrl, aID) {
+    let aItemId = aID.value || -1;
+    try {
+      if (aItemId > -1) {
+        var _URI = PlacesUtils.bookmarks.getBookmarkURI(aItemId);
+        if (_URI && _URI.spec == aUrl)
+          return PlacesUtils.bookmarks.getItemTitle(aItemId);
+      }
+    } catch (ex) { }
+    try {
+      let uri = Services.io.newURI(aUrl, null, null);
+      aItemId = aID.value = PlacesUtils.getMostRecentBookmarkForURI(uri);
+      if (aItemId > -1)
+        return PlacesUtils.bookmarks.getItemTitle(aItemId);
+    } catch (ex) { }
+    aID.value = null;
+    return null;
+  },
+
+  applyCallBackOnUrl: function(aUrl, aCallBack) {
+    let hasHref = aUrl.indexOf("#") > -1;
+    let result = aCallBack.apply(this, [aUrl]) ||
+        hasHref && aCallBack.apply(this, aUrl.split("#"));
+    // when IE Tab is installed try to find url with or without the prefix
+    let ietab = Tabmix.gIeTab;
+    if (!result && ietab) {
+      let prefix = "chrome://" + ietab.folder + "/content/reloaded.html?url=";
+      if (aUrl != prefix) {
+        let url = aUrl.startsWith(prefix) ?
+            aUrl.replace(prefix, "") : prefix + aUrl;
+        result = aCallBack.apply(this, [url]) ||
+          hasHref && aCallBack.apply(this, url.split("#"));
+      }
+    }
+    return result;
+  },
+
+  getTitleFromBookmark: function TMP_getTitleFromBookmark(aUrl, aTitle, aItemId, aTab) {
+    if (!this.titlefrombookmark || !aUrl)
+      return aTitle;
+
+    var oID = {value: aTab ? aTab.getAttribute("tabmix_bookmarkId") : aItemId};
+    var getTitle = url => this.getBookmarkTitle(url, oID);
+    var title = this.applyCallBackOnUrl(aUrl, getTitle);
+    // setItem check if aTab exist and remove the attribute if
+    // oID.value is null
+    if (aTab) {
+      let win = aTab.ownerDocument.defaultView;
+      win.Tabmix.setItem(aTab, "tabmix_bookmarkId", oID.value);
+    }
+
+    return title || aTitle;
+  },
 };
diff --git a/modules/RenameTab.jsm b/modules/RenameTab.jsm
index bfd38d9..eab1dde 100644
--- a/modules/RenameTab.jsm
+++ b/modules/RenameTab.jsm
@@ -2,7 +2,10 @@
 
 this.EXPORTED_SYMBOLS = ["RenameTab"];
 
-Components.utils.import("resource://tabmixplus/Services.jsm");
+const Cu = Components.utils;
+
+Cu.import("resource://tabmixplus/Services.jsm");
+Cu.import("resource://tabmixplus/Places.jsm");
 
 this.RenameTab = {
   window: null,
@@ -25,7 +28,7 @@ this.RenameTab = {
           this.window.TMP_SessionStore.getTitleFromTabState(aTab) :
           browser.contentTitle;
     this.data.url = browser.currentURI.spec;
-    this.data.docTitle = this.window.TMP_Places.getTitleFromBookmark(this.data.url,
+    this.data.docTitle = TabmixPlacesUtils.getTitleFromBookmark(this.data.url,
         docTitle, null, aTab);
     if (!this.data.docTitle)
       this.data.docTitle = this.window.isBlankPageURL(this.data.url) ?

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/tabmixplus.git



More information about the Pkg-mozext-commits mailing list