Bug 1596665 - P2. Add ability to copy all property bag contents to another. r=mattwoodrow

Add static CopyFrom method which is to be used in a follow-up patch. The static method takes a nsIWritablePropertyBag so that we don't need to cast to the concrete class.

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jean-Yves Avenard 2019-11-26 01:49:28 +00:00
parent 490c24b75a
commit cc5f1886ed
4 changed files with 50 additions and 43 deletions

View File

@ -137,11 +137,8 @@ nsresult nsBaseChannel::Redirect(nsIChannel* newChannel, uint32_t redirectFlags,
}
}
nsCOMPtr<nsIWritablePropertyBag> bag = ::do_QueryInterface(newChannel);
if (bag) {
for (auto iter = mPropertyHash.Iter(); !iter.Done(); iter.Next()) {
bag->SetProperty(iter.Key(), iter.UserData());
}
if (nsCOMPtr<nsIWritablePropertyBag> bag = ::do_QueryInterface(newChannel)) {
nsHashPropertyBag::CopyFrom(bag, static_cast<nsIPropertyBag2*>(this));
}
// Notify consumer, giving chance to cancel redirect.

View File

@ -44,35 +44,8 @@ nsDNSServiceInfo::nsDNSServiceInfo(nsIDNSServiceInfo* aServiceInfo) {
nsCOMPtr<nsIPropertyBag2> attributes; // deep copy
if (NS_SUCCEEDED(aServiceInfo->GetAttributes(getter_AddRefs(attributes)))) {
nsCOMPtr<nsISimpleEnumerator> enumerator;
if (NS_WARN_IF(
NS_FAILED(attributes->GetEnumerator(getter_AddRefs(enumerator))))) {
return;
}
nsCOMPtr<nsIWritablePropertyBag2> newAttributes = new nsHashPropertyBag();
bool hasMoreElements;
while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMoreElements)) &&
hasMoreElements) {
nsCOMPtr<nsISupports> element;
Unused << NS_WARN_IF(
NS_FAILED(enumerator->GetNext(getter_AddRefs(element))));
nsCOMPtr<nsIProperty> property = do_QueryInterface(element);
MOZ_ASSERT(property);
nsAutoString name;
nsCOMPtr<nsIVariant> value;
Unused << NS_WARN_IF(NS_FAILED(property->GetName(name)));
Unused << NS_WARN_IF(
NS_FAILED(property->GetValue(getter_AddRefs(value))));
nsAutoCString valueStr;
Unused << NS_WARN_IF(NS_FAILED(value->GetAsACString(valueStr)));
Unused << NS_WARN_IF(
NS_FAILED(newAttributes->SetPropertyAsACString(name, valueStr)));
}
RefPtr<nsHashPropertyBag> newAttributes = new nsHashPropertyBag();
newAttributes->CopyFrom(attributes);
Unused << NS_WARN_IF(NS_FAILED(SetAttributes(newAttributes)));
}
}

View File

@ -5,14 +5,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsHashPropertyBag.h"
#include "nsArray.h"
#include "nsArrayEnumerator.h"
#include "nsIVariant.h"
#include "nsIProperty.h"
#include "nsThreadUtils.h"
#include "nsVariant.h"
#include "mozilla/Attributes.h"
#include "mozilla/Move.h"
#include "mozilla/SimpleEnumerator.h"
#include "nsArray.h"
#include "nsArrayEnumerator.h"
#include "nsIProperty.h"
#include "nsIVariant.h"
#include "nsThreadUtils.h"
#include "nsVariant.h"
extern "C" {
@ -228,6 +230,37 @@ nsHashPropertyBagBase::SetPropertyAsInterface(const nsAString& aProp,
return SetProperty(aProp, var);
}
void nsHashPropertyBagBase::CopyFrom(const nsHashPropertyBagBase* aOther) {
for (auto iter = aOther->mPropertyHash.ConstIter(); !iter.Done();
iter.Next()) {
SetProperty(iter.Key(), iter.UserData());
}
}
void nsHashPropertyBagBase::CopyFrom(nsIPropertyBag* aOther) {
CopyFrom(this, aOther);
}
/* static */ void nsHashPropertyBagBase::CopyFrom(nsIWritablePropertyBag* aTo,
nsIPropertyBag* aFrom) {
if (aTo && aFrom) {
nsCOMPtr<nsISimpleEnumerator> enumerator;
if (NS_SUCCEEDED(aFrom->GetEnumerator(getter_AddRefs(enumerator)))) {
for (auto& property : SimpleEnumerator<nsIProperty>(enumerator)) {
nsString name;
nsCOMPtr<nsIVariant> value;
Unused << NS_WARN_IF(NS_FAILED(property->GetName(name)));
Unused << NS_WARN_IF(
NS_FAILED(property->GetValue(getter_AddRefs(value))));
Unused << NS_WARN_IF(
NS_FAILED(aTo->SetProperty(std::move(name), value)));
}
} else {
NS_WARNING("Unable to copy nsIPropertyBag");
}
}
}
/*
* nsHashPropertyBag implementation.
*/

View File

@ -17,7 +17,11 @@
class nsHashPropertyBagBase : public nsIWritablePropertyBag,
public nsIWritablePropertyBag2 {
public:
nsHashPropertyBagBase() {}
nsHashPropertyBagBase() = default;
void CopyFrom(const nsHashPropertyBagBase* aOther);
void CopyFrom(nsIPropertyBag* aOther);
static void CopyFrom(nsIWritablePropertyBag* aTo, nsIPropertyBag* aFrom);
NS_DECL_NSIPROPERTYBAG
NS_DECL_NSIPROPERTYBAG2
@ -47,12 +51,12 @@ class nsHashPropertyBag : public nsHashPropertyBagBase {
/* A cycle collected nsHashPropertyBag for main-thread-only use. */
class nsHashPropertyBagCC final : public nsHashPropertyBagBase {
public:
nsHashPropertyBagCC() {}
nsHashPropertyBagCC() = default;
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsHashPropertyBagCC,
nsIWritablePropertyBag)
protected:
virtual ~nsHashPropertyBagCC() {}
virtual ~nsHashPropertyBagCC() = default;
};
inline nsISupports* ToSupports(nsHashPropertyBagBase* aPropertyBag) {