1
/* vim:set ts=2 sw=2 et cindent: */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is Mozilla IPC.
17
* The Initial Developer of the Original Code is IBM Corporation.
18
* Portions created by the Initial Developer are Copyright (C) 2004
19
* the Initial Developer. All Rights Reserved.
22
* Darin Fisher <darin@meer.net>
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the MPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the MPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
38
#include "ipcIService.h"
39
#include "ipcIDConnectService.h"
42
#include "nsIEventQueueService.h"
43
#include "nsIServiceManager.h"
44
#include "nsIComponentRegistrar.h"
46
#include "nsXPCOMCID.h"
47
#include "nsILocalFile.h"
52
#if defined( XP_WIN ) || defined( XP_OS2 )
53
#define TEST_PATH "c:"
55
#define TEST_PATH "/tmp"
58
#define RETURN_IF_FAILED(rv, step) \
60
if (NS_FAILED(rv)) { \
61
printf("*** %s failed: rv=%x\n", step, rv); \
66
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
67
static nsIEventQueue* gEventQ = nsnull;
68
static PRBool gKeepRunning = PR_TRUE;
70
static ipcIService *gIpcServ = nsnull;
72
static nsresult DoTest()
76
nsCOMPtr<ipcIDConnectService> dcon = do_GetService("@mozilla.org/ipc/dconnect-service;1", &rv);
77
RETURN_IF_FAILED(rv, "getting dconnect service");
79
PRUint32 remoteClientID = 1;
81
nsCOMPtr<nsIFile> file;
82
rv = dcon->CreateInstanceByContractID(remoteClientID,
83
NS_LOCAL_FILE_CONTRACTID,
85
getter_AddRefs(file));
86
NS_ENSURE_SUCCESS(rv, rv);
88
nsCOMPtr<nsISupports> sup = do_QueryInterface(file, &rv);
89
NS_ENSURE_SUCCESS(rv, rv);
91
printf("*** calling QueryInterface\n");
92
nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(file, &rv);
93
NS_ENSURE_SUCCESS(rv, rv);
96
path.AssignLiteral(TEST_PATH);
98
printf("*** calling InitWithNativePath\n");
99
rv = localFile->InitWithPath(path);
100
NS_ENSURE_SUCCESS(rv, rv);
103
rv = file->GetPath(buf);
104
NS_ENSURE_SUCCESS(rv, rv);
106
if (!buf.Equals(path))
108
NS_ConvertUTF16toUTF8 temp(buf);
109
printf("*** GetPath erroneously returned [%s]\n", temp.get());
110
return NS_ERROR_FAILURE;
114
rv = file->Exists(&exists);
117
printf("*** Exists test failed [rv=%x]\n", rv);
118
return NS_ERROR_FAILURE;
120
printf("File exists? [%d]\n", exists);
122
nsCOMPtr<nsIFile> clone;
123
rv = file->Clone(getter_AddRefs(clone));
126
printf("*** Clone test failed [rv=%x]\n", rv);
127
return NS_ERROR_FAILURE;
131
node.AssignLiteral("hello.txt");
133
rv = clone->Append(node);
136
printf("*** Append test failed [rv=%x]\n", rv);
137
return NS_ERROR_FAILURE;
141
rv = file->Equals(clone, &match);
144
printf("*** Equals test failed [rv=%x]\n", rv);
145
return NS_ERROR_FAILURE;
147
printf("Files are equals? [%d]\n", match);
149
// now test passing null for interface pointer
151
rv = clone->Exists(&exists);
154
printf("*** Exists test failed [rv=%x]\n", rv);
155
return NS_ERROR_FAILURE;
159
rv = clone->Create(nsIFile::NORMAL_FILE_TYPE, 0600);
162
printf("*** Create test failed [rv=%x]\n", rv);
163
return NS_ERROR_FAILURE;
167
rv = clone->MoveTo(nsnull, NS_LITERAL_STRING("helloworld.txt"));
170
printf("*** MoveTo test failed [rv=%x]\n", rv);
171
return NS_ERROR_FAILURE;
174
// now test passing local objects to a remote object
176
nsCOMPtr<nsILocalFile> myLocalFile;
177
rv = NS_NewLocalFile(path, PR_TRUE, getter_AddRefs(myLocalFile));
180
printf("*** NS_NewLocalFile failed [rv=%x]\n", rv);
181
return NS_ERROR_FAILURE;
184
rv = file->Equals(myLocalFile, &match);
187
printf("*** second Equals test failed [rv=%x]\n", rv);
188
return NS_ERROR_FAILURE;
190
printf("Files are equals? [%d]\n", match);
192
printf("*** DoTest completed successfully :-)\n");
196
int main(int argc, char **argv)
200
PRBool serverMode = PR_FALSE;
203
if (strcmp(argv[1], "-server") == 0)
205
serverMode = PR_TRUE;
209
printf("usage: %s [-server]\n", argv[0]);
215
nsCOMPtr<nsIServiceManager> servMan;
216
NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
217
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
218
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
220
registrar->AutoRegister(nsnull);
222
// Create the Event Queue for this thread...
223
nsCOMPtr<nsIEventQueueService> eqs =
224
do_GetService(kEventQueueServiceCID, &rv);
225
RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
227
rv = eqs->CreateMonitoredThreadEventQueue();
228
RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
230
rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
231
RETURN_IF_FAILED(rv, "GetThreadEventQueue");
233
nsCOMPtr<ipcIService> ipcServ(do_GetService(IPC_SERVICE_CONTRACTID, &rv));
234
RETURN_IF_FAILED(rv, "do_GetService(ipcServ)");
235
NS_ADDREF(gIpcServ = ipcServ);
240
RETURN_IF_FAILED(rv, "DoTest()");
244
gIpcServ->AddName("DConnectServer");
250
gEventQ->WaitForEvent(&ev);
251
gEventQ->HandleEvent(ev);
254
NS_RELEASE(gIpcServ);
256
printf("*** processing remaining events\n");
258
// process any remaining events
259
while (NS_SUCCEEDED(gEventQ->GetEvent(&ev)) && ev)
260
gEventQ->HandleEvent(ev);
262
printf("*** done\n");
263
} // this scopes the nsCOMPtrs
265
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
266
rv = NS_ShutdownXPCOM(nsnull);
267
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");