2002-05-08 10:08:25 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2002-05-08 10:08:25 +04:00
|
|
|
|
2004-10-22 00:57:17 +04:00
|
|
|
#include "TestCommon.h"
|
2015-05-26 21:33:55 +03:00
|
|
|
#include "mozilla/Snprintf.h"
|
2002-05-08 10:08:25 +04:00
|
|
|
#include "nsXPCOM.h"
|
2005-11-08 22:23:00 +03:00
|
|
|
#include "nsStringAPI.h"
|
2002-05-08 10:08:25 +04:00
|
|
|
#include "nsIPersistentProperties2.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIComponentRegistrar.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIComponentManager.h"
|
2004-02-07 05:41:10 +03:00
|
|
|
#include <stdio.h>
|
2005-11-08 22:23:00 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-09-23 21:29:27 +04:00
|
|
|
#include "nsISimpleEnumerator.h"
|
2015-02-17 21:09:31 +03:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsILoadInfo.h"
|
|
|
|
#include "nsNetUtil.h"
|
2002-05-08 10:08:25 +04:00
|
|
|
|
|
|
|
#define TEST_URL "resource:/res/test.properties"
|
2002-08-30 21:13:34 +04:00
|
|
|
static NS_DEFINE_CID(kPersistentPropertiesCID, NS_IPERSISTENTPROPERTIES_CID);
|
2002-05-08 10:08:25 +04:00
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
2004-10-22 00:57:17 +04:00
|
|
|
if (test_common_init(&argc, &argv) != 0)
|
|
|
|
return -1;
|
|
|
|
|
2002-05-08 10:08:25 +04:00
|
|
|
nsresult ret;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIServiceManager> servMan;
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
|
2002-05-08 10:08:25 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIInputStream* in = nullptr;
|
2002-05-08 10:08:25 +04:00
|
|
|
|
2015-02-17 21:09:31 +03:00
|
|
|
nsCOMPtr<nsIScriptSecurityManager> secman =
|
|
|
|
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &ret);
|
|
|
|
if (NS_FAILED(ret)) return 1;
|
|
|
|
nsCOMPtr<nsIPrincipal> systemPrincipal;
|
|
|
|
ret = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal));
|
|
|
|
if (NS_FAILED(ret)) return 1;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
ret = NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING(TEST_URL));
|
2012-08-07 21:17:27 +04:00
|
|
|
if (NS_FAILED(ret)) return 1;
|
2002-05-08 10:08:25 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIChannel *channel = nullptr;
|
2015-02-17 21:09:31 +03:00
|
|
|
ret = NS_NewChannel(&channel,
|
|
|
|
uri,
|
|
|
|
systemPrincipal,
|
2015-09-16 06:12:52 +03:00
|
|
|
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
|
2015-02-17 21:09:31 +03:00
|
|
|
nsIContentPolicy::TYPE_OTHER);
|
2012-08-07 21:17:27 +04:00
|
|
|
if (NS_FAILED(ret)) return 1;
|
2002-05-08 10:08:25 +04:00
|
|
|
|
2015-09-16 06:12:52 +03:00
|
|
|
ret = channel->Open2(&in);
|
2012-08-07 21:17:27 +04:00
|
|
|
if (NS_FAILED(ret)) return 1;
|
2002-05-08 10:08:25 +04:00
|
|
|
|
2004-11-01 21:50:36 +03:00
|
|
|
nsIPersistentProperties* props;
|
|
|
|
ret = CallCreateInstance(kPersistentPropertiesCID, &props);
|
2002-05-08 10:08:25 +04:00
|
|
|
if (NS_FAILED(ret) || (!props)) {
|
|
|
|
printf("create nsIPersistentProperties failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ret = props->Load(in);
|
|
|
|
if (NS_FAILED(ret)) {
|
|
|
|
printf("cannot load properties\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int i = 1;
|
|
|
|
while (1) {
|
|
|
|
char name[16];
|
|
|
|
name[0] = 0;
|
2016-08-15 09:44:00 +03:00
|
|
|
SprintfLiteral(name, "%d", i);
|
2002-05-08 10:08:25 +04:00
|
|
|
nsAutoString v;
|
2002-08-30 21:13:34 +04:00
|
|
|
ret = props->GetStringProperty(nsDependentCString(name), v);
|
2002-05-08 10:08:25 +04:00
|
|
|
if (NS_FAILED(ret) || (!v.Length())) {
|
|
|
|
break;
|
|
|
|
}
|
2005-11-08 22:23:00 +03:00
|
|
|
printf("\"%d\"=\"%s\"\n", i, NS_ConvertUTF16toUTF8(v).get());
|
2002-05-08 10:08:25 +04:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2002-08-30 21:13:34 +04:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> propEnum;
|
|
|
|
ret = props->Enumerate(getter_AddRefs(propEnum));
|
|
|
|
|
2002-05-08 10:08:25 +04:00
|
|
|
if (NS_FAILED(ret)) {
|
2002-08-30 21:13:34 +04:00
|
|
|
printf("cannot enumerate properties\n");
|
|
|
|
return 1;
|
2002-05-08 10:08:25 +04:00
|
|
|
}
|
2002-08-30 21:13:34 +04:00
|
|
|
|
2002-05-08 10:08:25 +04:00
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("\nKey\tValue\n");
|
|
|
|
printf( "---\t-----\n");
|
2002-08-30 21:13:34 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasMore;
|
2002-08-30 21:13:34 +04:00
|
|
|
while (NS_SUCCEEDED(propEnum->HasMoreElements(&hasMore)) &&
|
|
|
|
hasMore) {
|
|
|
|
nsCOMPtr<nsISupports> sup;
|
|
|
|
ret = propEnum->GetNext(getter_AddRefs(sup));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPropertyElement> propElem = do_QueryInterface(sup, &ret);
|
2002-05-08 10:08:25 +04:00
|
|
|
if (NS_FAILED(ret)) {
|
2002-08-30 21:13:34 +04:00
|
|
|
printf("failed to get current item\n");
|
|
|
|
return 1;
|
2002-05-08 10:08:25 +04:00
|
|
|
}
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString key;
|
2002-08-30 21:13:34 +04:00
|
|
|
nsAutoString value;
|
2002-05-08 10:08:25 +04:00
|
|
|
|
2002-08-30 21:13:34 +04:00
|
|
|
ret = propElem->GetKey(key);
|
2002-05-08 10:08:25 +04:00
|
|
|
if (NS_FAILED(ret)) {
|
|
|
|
printf("failed to get current element's key\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2002-08-30 21:13:34 +04:00
|
|
|
ret = propElem->GetValue(value);
|
2002-05-08 10:08:25 +04:00
|
|
|
if (NS_FAILED(ret)) {
|
|
|
|
printf("failed to get current element's value\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-10-27 05:05:09 +04:00
|
|
|
printf("%s\t%s\n", key.get(), NS_ConvertUTF16toUTF8(value).get());
|
2002-05-08 10:08:25 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|