Bug 449480 - Add a unique identifier to nsIMsgIncomingServer. r=mkmelin

Adds a 'UID' attribute, stored in the server's preferences as 'uid', just like nsIAbDirectory. If there's no value, a new UUID is created on access.

Differential Revision: https://phabricator.services.mozilla.com/D153823

--HG--
extra : rebase_source : e67a8932d3058d966e34aa99522a60587b05580a
This commit is contained in:
Geoff Lankow 2022-08-02 15:36:38 +12:00
Родитель 3f7a6ffddd
Коммит 1536b98f35
7 изменённых файлов: 169 добавлений и 1 удалений

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

@ -35,6 +35,12 @@ interface nsIMsgIncomingServer : nsISupports {
*/
attribute ACString key;
/**
* A unique identifier for this server that can be used for the same
* server synced across multiple profiles. Auto-generated on first use.
*/
attribute AUTF8String UID;
/**
* pretty name - should be "userid on hostname"
* if the pref is not set

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

@ -286,6 +286,24 @@ class MsgIncomingServer {
this._defaultPrefs = Services.prefs.getBranch("mail.server.default.");
}
get UID() {
let uid = this._prefs.getStringPref("uid", "");
if (uid) {
return uid;
}
return (this.UID = Services.uuid
.generateUUID()
.toString()
.substring(1, 37));
}
set UID(uid) {
if (this._prefs.prefHasUserValue("uid")) {
throw new Components.Exception("uid is already set", Cr.NS_ERROR_ABORT);
}
this._prefs.setStringPref("uid", uid);
}
get hostName() {
let hostname = this.getUnicharValue("hostname");
if (hostname.includes(":")) {

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

@ -50,6 +50,7 @@
#include "nsIMsgFilter.h"
#include "nsIObserverService.h"
#include "mozilla/Unused.h"
#include "nsIUUIDGenerator.h"
#define PORT_NOT_SET -1
@ -157,6 +158,41 @@ nsMsgIncomingServer::SetKey(const nsACString& serverKey) {
getter_AddRefs(mDefPrefBranch));
}
NS_IMETHODIMP
nsMsgIncomingServer::GetUID(nsACString& uid) {
bool hasValue;
nsresult rv = mPrefBranch->PrefHasUserValue("uid", &hasValue);
NS_ENSURE_SUCCESS(rv, rv);
if (hasValue) {
return GetCharValue("uid", uid);
}
nsCOMPtr<nsIUUIDGenerator> uuidgen =
mozilla::components::UUIDGenerator::Service();
NS_ENSURE_TRUE(uuidgen, NS_ERROR_FAILURE);
nsID id;
rv = uuidgen->GenerateUUIDInPlace(&id);
NS_ENSURE_SUCCESS(rv, rv);
char idString[NSID_LENGTH];
id.ToProvidedString(idString);
uid.AppendASCII(idString + 1, NSID_LENGTH - 3);
return SetUID(uid);
}
NS_IMETHODIMP
nsMsgIncomingServer::SetUID(const nsACString& uid) {
bool hasValue;
nsresult rv = mPrefBranch->PrefHasUserValue("uid", &hasValue);
NS_ENSURE_SUCCESS(rv, rv);
if (hasValue) {
return NS_ERROR_ABORT;
}
return SetCharValue("uid", uid);
}
NS_IMETHODIMP
nsMsgIncomingServer::SetRootFolder(nsIMsgFolder* aRootFolder) {
m_rootFolder = aRootFolder;

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

@ -3,7 +3,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
XPCSHELL_TESTS_MANIFESTS += ["unit/xpcshell.ini"]
XPCSHELL_TESTS_MANIFESTS += ["unit/xpcshell-imap.ini", "unit/xpcshell.ini"]
FINAL_LIBRARY = "xul-gtest"

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

@ -0,0 +1,98 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at http://mozilla.org/MPL/2.0/. */
function subtestUID(type) {
const UUID_REGEXP = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
// Create a server and check it the UID is set when accessed.
let serverA = MailServices.accounts.createIncomingServer(
"userA",
"hostA",
type
);
Assert.stringMatches(
serverA.UID,
UUID_REGEXP,
"server A's UID should exist and be a UUID"
);
Assert.equal(
Services.prefs.getStringPref(`mail.server.${serverA.key}.uid`),
serverA.UID,
"server A's UID should be saved to the preferences"
);
Assert.throws(
() => (serverA.UID = "00001111-2222-3333-4444-555566667777"),
/NS_ERROR_ABORT/,
"server A's UID should be unchangeable after it is set"
);
// Create a second server and check the two UIDs don't match.
let serverB = MailServices.accounts.createIncomingServer(
"userB",
"hostB",
type
);
Assert.stringMatches(
serverB.UID,
UUID_REGEXP,
"server B's UID should exist and be a UUID"
);
Assert.equal(
Services.prefs.getStringPref(`mail.server.${serverB.key}.uid`),
serverB.UID,
"server B's UID should be saved to the preferences"
);
Assert.notEqual(
serverB.UID,
serverA.UID,
"server B's UID should not be the same as server A's"
);
// Create a third server and set the UID before it is accessed.
let serverC = MailServices.accounts.createIncomingServer(
"userC",
"hostC",
type
);
serverC.UID = "11112222-3333-4444-5555-666677778888";
Assert.equal(
serverC.UID,
"11112222-3333-4444-5555-666677778888",
"server C's UID set correctly"
);
Assert.equal(
Services.prefs.getStringPref(`mail.server.${serverC.key}.uid`),
"11112222-3333-4444-5555-666677778888",
"server C's UID should be saved to the preferences"
);
Assert.throws(
() => (serverC.UID = "22223333-4444-5555-6666-777788889999"),
/NS_ERROR_ABORT/,
"server C's UID should be unchangeable after it is set"
);
}
/**
* Tests the UID attribute of IMAP servers.
*/
add_task(function testUID_IMAP() {
subtestUID("imap");
});
/**
* Tests the UID attribute of NNTP servers.
*/
add_task(function testUID_NNTP() {
subtestUID("nntp");
});
/**
* Tests the UID attribute of POP3 servers.
*/
add_task(function testUID_POP3() {
subtestUID("pop3");
});

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

@ -0,0 +1,8 @@
[DEFAULT]
head = head_mailbase.js
tail =
prefs =
mailnews.imap.jsmodule=true
dupe-manifest =
[test_incomingServer.js]

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

@ -1,6 +1,7 @@
[DEFAULT]
head = head_mailbase.js
tail =
dupe-manifest =
support-files = nodelist_test.xml data/*
[test_accountMgr.js]
@ -33,6 +34,7 @@ support-files = nodelist_test.xml data/*
[test_hostnameUtils.js]
[test_identity.js]
[test_imapPump.js]
[test_incomingServer.js]
[test_inheritedFolderProperties.js]
[test_jsTreeSelection.js]
[test_junkingWhenDisabled.js]