Skip to content

Commit

Permalink
Fx19+ compatibility: syncGetCacheEntry for work with cache entries in…
Browse files Browse the repository at this point in the history
… synchronize mode.

Fix some errors from session store service.
  • Loading branch information
imfo committed Mar 3, 2013
1 parent 1fe990f commit c9ea950
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 31 deletions.
13 changes: 3 additions & 10 deletions chrome/content/imglikeopera.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ var ILO = {
this.openPrefWindow();
else if (aEvent.button == 2)
this.indicatorClick("prev");
return;
}
},

Expand Down Expand Up @@ -180,14 +179,8 @@ var ILO = {
if (!button)
return;

let indicatorPopupItems = button.lastChild.childNodes;

for (let k = 4; --k > -1;) {
if (indicatorPopupItems[k].getAttribute("checked") != "false")
indicatorPopupItems[k].setAttribute("checked", "false");
}

indicatorPopupItems[ 4 - this.browser.selectedBrowser.iloTabPolicy ].setAttribute("checked", "true");
let selectedIndex = 4 - this.browser.selectedBrowser.iloTabPolicy;
button.lastChild.children[selectedIndex].setAttribute("checked", "true");
},

indicatorSet: function ILO_indicatorSet(policy) {
Expand Down Expand Up @@ -537,7 +530,7 @@ var ILO = {
}

case "keypress": {
if (aEvent.keyCode == 27 && this.settings.esc_key) {// "Esc"
if (aEvent.keyCode == 27 && this.settings.esc_key) { // "Esc"
let _doc = aEvent.target.ownerDocument;
while (_doc.defaultView.frameElement)
_doc = _doc.defaultView.frameElement.ownerDocument;
Expand Down
10 changes: 5 additions & 5 deletions chrome/content/imglikeopera.xul
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
<menupopup onpopupshowing="ILO.indicatorPopup();"
oncommand="ILO.indicatorClick(event.target.getAttribute('value')); event.stopPropagation();">

<menuitem value="4" label="&ilo-policy-load-dont.label;" type="checkbox"/>
<menuitem value="3" label="&ilo-policy-load-cached.label;" type="checkbox"/>
<menuitem value="2" label="&ilo-policy-load-origsite-short.label;" type="checkbox"/>
<menuitem value="1" label="&ilo-policy-load-all.label;" type="checkbox"/>
<menuitem value="4" label="&ilo-policy-load-dont.label;" type="radio" name="ilo-tab-policy"/>
<menuitem value="3" label="&ilo-policy-load-cached.label;" type="radio" name="ilo-tab-policy"/>
<menuitem value="2" label="&ilo-policy-load-origsite-short.label;" type="radio" name="ilo-tab-policy"/>
<menuitem value="1" label="&ilo-policy-load-all.label;" type="radio" name="ilo-tab-policy"/>

<menuseparator/>

Expand All @@ -51,4 +51,4 @@
</toolbarbutton>
</toolbarpalette>

</overlay>
</overlay>
87 changes: 71 additions & 16 deletions components/nsImgLikeOpera.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,45 @@ Cu.import("resource://gre/modules/Services.jsm");
*
************************************************************************/

(function(that) {
function CacheListener() {
this.done = false;
}

CacheListener.prototype = {
onCacheEntryAvailable: function CacheListener_onCacheEntryAvailable(descriptor, accessGranted, status) {
this.descriptor = descriptor;
this.status = status;
this.done = true;
},

onCacheEntryDoomed: function CacheListener_onCacheEntryDoomed() {},

QueryInterface: function(iid) {
if (iid.equals(Ci.nsICacheListener))
return this;
throw Cr.NS_NOINTERFACE;
}
};

const THREAD_MANAGER = Cc["@mozilla.org/thread-manager;1"].getService();

that.syncGetCacheEntry = function syncGetCacheEntry(aCacheSession, aKey, aAccessMode) {
let startTime = Date.now();
let listener = new CacheListener();
aCacheSession.asyncOpenCacheEntry(aKey, aAccessMode, listener, true);

while (!(listener.done || Math.abs(Date.now() - startTime) > 5000)) {
// if (Math.abs(Date.now() - startTime) > 5000)
// dump("Too long syncGetCacheEntry\n");

THREAD_MANAGER.currentThread.processNextEvent(false);
}

return listener;
}
})(this);

function nsImgLikeOpera() {
this.debug = false;

Expand Down Expand Up @@ -332,15 +371,23 @@ nsImgLikeOpera.prototype = {
},

setTabPolicy: function SStore_setTabPolicy(aTab, aPolicy) {
if (this._enabled)
if (!this._enabled)
return;

try {
this._sessionStoreService.setTabValue(aTab, "ilo-tab-policy", aPolicy);
} catch(e) {}
},

getTabPolicy: function SStore_getTabPolicy(aTab) {
if (!this._enabled)
return null;

let policy = parseInt(this._sessionStoreService.getTabValue(aTab, "ilo-tab-policy"), 10);
let policy = NaN;
try {
policy = parseInt(this._sessionStoreService.getTabValue(aTab, "ilo-tab-policy"), 10);
} catch(e) {}

return isNaN(policy) ? null : policy;
}
},
Expand Down Expand Up @@ -379,6 +426,11 @@ nsImgLikeOpera.prototype = {
* So... should load?
*/
shouldLoad: function ILO_shouldLoad(contentType, contentLocation, context, obj) {
if (/yandex/.test(contentLocation.spec)) {
dump(contentLocation.spec + "\n");
return ACCEPT;
}

if (contentLocation.scheme != "http" && contentLocation.scheme != "https")
return ACCEPT;

Expand Down Expand Up @@ -599,41 +651,44 @@ nsImgLikeOpera.prototype = {
if (forcedExpTime > 0) {
for each (let cacheSession in this.cacheSessions) {
try {
cacheEntryDescriptor = cacheSession.openCacheEntry(url, nsICacheEntryDescriptor, false);
cacheEntryDescriptor = syncGetCacheEntry(cacheSession, url, nsICache.ACCESS_READ_WRITE).descriptor;
} catch(e) {}

if (cacheEntryDescriptor) {
let expirationTime = cacheEntryDescriptor.lastModified + forcedExpTime;

if (expirationTime < timenow) {// || !cacheEntryDescriptor.dataSize(?)) {
cacheEntryDescriptor.doom();
} else {
expired = false;
if (cacheEntryDescriptor.lastModified) {
let expirationTime = cacheEntryDescriptor.lastModified + forcedExpTime;

if (cacheEntryDescriptor.expirationTime != expirationTime) {
cacheEntryDescriptor.setExpirationTime(expirationTime);
cacheEntryDescriptor.markValid();
if (expirationTime < timenow) { // || !cacheEntryDescriptor.dataSize(?)) {
cacheEntryDescriptor.doom();
} else {
expired = false;

if (cacheEntryDescriptor.expirationTime != expirationTime) {
cacheEntryDescriptor.setExpirationTime(expirationTime);
cacheEntryDescriptor.markValid();
}
}
}

cacheEntryDescriptor.close();

if (!expired) {
try {
cacheSession.openCacheEntry(url, nsICache.ACCESS_READ, false);
// Need to call this after setExpirationTime for properly expiration time.
syncGetCacheEntry(cacheSession, url, nsICache.ACCESS_READ);
} catch(ex) {
expired = true;
}
}

break;//for each
break; // for each
}
}

} else if (docpolicy == 3) {
for each (let cacheSession in this.cacheSessions) {
try {
cacheEntryDescriptor = cacheSession.openCacheEntry(url, nsICacheEntryDescriptor, false);
cacheEntryDescriptor = syncGetCacheEntry(cacheSession, url, nsICache.ACCESS_READ).descriptor;
} catch(e) {}

if (cacheEntryDescriptor) {
Expand All @@ -642,7 +697,7 @@ nsImgLikeOpera.prototype = {

cacheEntryDescriptor.close();

break;//for each
break; // for each
}
}
}
Expand Down

0 comments on commit c9ea950

Please sign in to comment.