Bug 539907 - Have getPref use asynchronous statements when called with an optional callback. r=myk, sr=robstrong

This commit is contained in:
Ryan Flint 2010-02-09 05:28:39 -05:00
Родитель 7f63d21546
Коммит f6cd82cadf
4 изменённых файлов: 120 добавлений и 15 удалений

Просмотреть файл

@ -65,7 +65,13 @@ interface nsIContentPrefObserver : nsISupports
void onContentPrefRemoved(in AString aGroup, in AString aName);
};
[scriptable, uuid(b3976fa1-189f-4fcb-9c3b-49d1f6d8073d)]
[scriptable, function, uuid(c1b3d6df-5373-4606-8494-8bcf14a7fc62)]
interface nsIContentPrefCallback : nsISupports
{
void onResult(in nsIVariant aResult);
};
[scriptable, uuid(36715960-de39-457b-9d02-b800d5d3079b)]
interface nsIContentPrefService : nsISupports
{
/**
@ -81,13 +87,17 @@ interface nsIContentPrefService : nsISupports
* (typically in the format of a hostname), or null
* to get the global pref (applies to all sites)
* @param aName the name of the pref to get
* @param aCallback an optional nsIContentPrefCallback to receive the
* result. If desired, JavaScript callers can instead
* provide a function to call upon completion
*
* @returns the value of the pref
* @throws NS_ERROR_ILLEGAL_VALUE if aGroup is not a string, nsIURI, or null
* @throws NS_ERROR_ILLEGAL_VALUE if aName is null or an empty string
*/
nsIVariant getPref(in nsIVariant aGroup, in AString aName);
nsIVariant getPref(in nsIVariant aGroup, in AString aName,
[optional] in nsIContentPrefCallback aCallback);
/**
* Set a pref.
*

Просмотреть файл

@ -447,7 +447,7 @@ nsFileControlFrame::FetchLastUsedDirectory(nsIURI* aURI, nsILocalFile* aFile)
PRBool hasPref;
if (NS_SUCCEEDED(contentPrefService->HasPref(uri, prefName, &hasPref)) && hasPref) {
nsCOMPtr<nsIVariant> pref;
contentPrefService->GetPref(uri, prefName, getter_AddRefs(pref));
contentPrefService->GetPref(uri, prefName, nsnull, getter_AddRefs(pref));
nsString prefStr;
pref->GetAsAString(prefStr);
return aFile->InitWithPath(prefStr);

Просмотреть файл

@ -128,19 +128,17 @@ ContentPrefService.prototype = {
//**************************************************************************//
// nsIContentPrefService
getPref: function ContentPrefService_getPref(aGroup, aName) {
getPref: function ContentPrefService_getPref(aGroup, aName, aCallback) {
if (!aName)
throw Components.Exception("aName cannot be null or an empty string",
Cr.NS_ERROR_ILLEGAL_VALUE);
if (aGroup == null)
return this._selectGlobalPref(aName);
return this._selectGlobalPref(aName, aCallback);
if (aGroup.constructor.name == "String")
return this._selectPref(aGroup.toString(), aName);
if (aGroup instanceof Ci.nsIURI) {
var group = this.grouper.group(aGroup);
return this._selectPref(group, aName);
}
return this._selectPref(aGroup.toString(), aName, aCallback);
if (aGroup instanceof Ci.nsIURI)
return this._selectPref(this.grouper.group(aGroup), aName, aCallback);
throw Components.Exception("aGroup is not a string, nsIURI or null",
Cr.NS_ERROR_ILLEGAL_VALUE);
@ -425,14 +423,16 @@ ContentPrefService.prototype = {
return this.__stmtSelectPref;
},
_selectPref: function ContentPrefService__selectPref(aGroup, aSetting) {
_selectPref: function ContentPrefService__selectPref(aGroup, aSetting, aCallback) {
var value;
try {
this._stmtSelectPref.params.group = aGroup;
this._stmtSelectPref.params.setting = aSetting;
if (this._stmtSelectPref.executeStep())
if (aCallback)
new AsyncStatement(this._stmtSelectPref).execute(aCallback);
else if (this._stmtSelectPref.executeStep())
value = this._stmtSelectPref.row["value"];
}
finally {
@ -456,13 +456,15 @@ ContentPrefService.prototype = {
return this.__stmtSelectGlobalPref;
},
_selectGlobalPref: function ContentPrefService__selectGlobalPref(aName) {
_selectGlobalPref: function ContentPrefService__selectGlobalPref(aName, aCallback) {
var value;
try {
this._stmtSelectGlobalPref.params.name = aName;
if (this._stmtSelectGlobalPref.executeStep())
if (aCallback)
new AsyncStatement(this._stmtSelectGlobalPref).execute(aCallback);
else if (this._stmtSelectGlobalPref.executeStep())
value = this._stmtSelectGlobalPref.row["value"];
}
finally {
@ -1064,6 +1066,32 @@ HostnameGrouper.prototype = {
}
};
function AsyncStatement(aStatement) {
this.stmt = aStatement;
}
AsyncStatement.prototype = {
execute: function AsyncStmt_execute(aCallback) {
let stmt = this.stmt;
stmt.executeAsync({
_callback: aCallback,
_hadResult: false,
handleResult: function(aResult) {
this._hadResult = true;
if (this._callback) {
let row = aResult.getNextRow();
this._callback.onResult(row.getResultByName("value"));
}
},
handleCompletion: function(aReason) {
if (!this._hadResult && this._callback &&
aReason == Ci.mozIStorageStatementCallback.REASON_FINISHED)
this._callback.onResult(undefined);
},
handleError: function(aError) {}
});
}
};
//****************************************************************************//
// XPCOM Plumbing

Просмотреть файл

@ -0,0 +1,67 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Ryan Flint <rflint@mozilla.com> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
var cps = Cc["@mozilla.org/content-pref/service;1"].
getService(Ci.nsIContentPrefService);
var uri = ContentPrefTest.getURI("http://www.example.com/");
function run_test() {
do_test_pending();
cps.setPref(uri, "asynctest", "pie");
do_check_eq(cps.getPref(uri, "asynctest"), "pie");
cps.getPref(uri, "asynctest", function(aValue) {
do_check_eq(aValue, "pie");
testCallbackObj();
});
}
function testCallbackObj() {
cps.getPref(uri, "asynctest", {
onResult: function(aValue) {
do_check_eq(aValue, "pie");
cps.removePref(uri, "asynctest");
testNoResult();
}
});
}
function testNoResult() {
cps.getPref(uri, "asynctest", function(aValue) {
do_check_eq(aValue, undefined);
do_test_finished();
});
}