forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nsChromeRegistry.cpp
910 lines (773 loc) · 29.7 KB
/
nsChromeRegistry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "nsCOMPtr.h"
#include "nsFileSpec.h"
#include "nsSpecialSystemDirectory.h"
#include "nsIChromeRegistry.h"
#include "nsIRDFDataSource.h"
#include "nsIRDFObserver.h"
#include "nsIRDFRemoteDataSource.h"
#include "nsCRT.h"
#include "rdf.h"
#include "nsIServiceManager.h"
#include "nsIRDFService.h"
#include "nsRDFCID.h"
#include "nsIRDFResource.h"
#include "nsIRDFDataSource.h"
#include "nsHashtable.h"
#include "nsString.h"
#include "nsXPIDLString.h"
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
static NS_DEFINE_IID(kIRDFResourceIID, NS_IRDFRESOURCE_IID);
static NS_DEFINE_IID(kIRDFLiteralIID, NS_IRDFLITERAL_IID);
static NS_DEFINE_IID(kIRDFServiceIID, NS_IRDFSERVICE_IID);
static NS_DEFINE_IID(kIRDFDataSourceIID, NS_IRDFDATASOURCE_IID);
static NS_DEFINE_IID(kIRDFIntIID, NS_IRDFINT_IID);
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID);
#define CHROME_NAMESPACE_URI "http://chrome.mozilla.org/rdf#"
DEFINE_RDF_VOCAB(CHROME_NAMESPACE_URI, CHROME, chrome);
DEFINE_RDF_VOCAB(CHROME_NAMESPACE_URI, CHROME, skin);
DEFINE_RDF_VOCAB(CHROME_NAMESPACE_URI, CHROME, content);
DEFINE_RDF_VOCAB(CHROME_NAMESPACE_URI, CHROME, locale);
DEFINE_RDF_VOCAB(CHROME_NAMESPACE_URI, CHROME, platform);
DEFINE_RDF_VOCAB(CHROME_NAMESPACE_URI, CHROME, behavior);
DEFINE_RDF_VOCAB(CHROME_NAMESPACE_URI, CHROME, base);
DEFINE_RDF_VOCAB(CHROME_NAMESPACE_URI, CHROME, main);
DEFINE_RDF_VOCAB(CHROME_NAMESPACE_URI, CHROME, archive);
DEFINE_RDF_VOCAB(CHROME_NAMESPACE_URI, CHROME, displayname);
DEFINE_RDF_VOCAB(CHROME_NAMESPACE_URI, CHROME, name);
DEFINE_RDF_VOCAB(RDF_NAMESPACE_URI, RDF, Description);
// This nasty function should disappear when we land Necko completely and
// change chrome://global/skin/foo to chrome://skin@global/foo
//
void BreakProviderAndRemainingFromPath(const char* i_path, char** o_provider, char** o_remaining);
////////////////////////////////////////////////////////////////////////////////
class nsChromeRegistry : public nsIChromeRegistry,
public nsIRDFDataSource,
public nsIRDFRemoteDataSource
{
public:
NS_DECL_ISUPPORTS
// nsIChromeRegistry methods:
NS_IMETHOD InitRegistry();
NS_IMETHOD ConvertChromeURL(nsIURI* aChromeURL);
// nsIRDFDataSource methods
NS_IMETHOD GetURI(char** uri);
NS_IMETHOD GetSource(nsIRDFResource* property,
nsIRDFNode* target,
PRBool tv,
nsIRDFResource** source /* out */) ;
NS_IMETHOD GetSources(nsIRDFResource* property,
nsIRDFNode* target,
PRBool tv,
nsISimpleEnumerator** sources /* out */) ;
NS_IMETHOD GetTarget(nsIRDFResource* source,
nsIRDFResource* property,
PRBool tv,
nsIRDFNode** target /* out */) ;
NS_IMETHOD GetTargets(nsIRDFResource* source,
nsIRDFResource* property,
PRBool tv,
nsISimpleEnumerator** targets /* out */) ;
NS_IMETHOD Assert(nsIRDFResource* source,
nsIRDFResource* property,
nsIRDFNode* target,
PRBool tv) ;
NS_IMETHOD Unassert(nsIRDFResource* source,
nsIRDFResource* property,
nsIRDFNode* target) ;
NS_IMETHOD Change(nsIRDFResource* aSource,
nsIRDFResource* aProperty,
nsIRDFNode* aOldTarget,
nsIRDFNode* aNewTarget);
NS_IMETHOD Move(nsIRDFResource* aOldSource,
nsIRDFResource* aNewSource,
nsIRDFResource* aProperty,
nsIRDFNode* aTarget);
NS_IMETHOD HasAssertion(nsIRDFResource* source,
nsIRDFResource* property,
nsIRDFNode* target,
PRBool tv,
PRBool* hasAssertion /* out */) ;
NS_IMETHOD AddObserver(nsIRDFObserver* n) ;
NS_IMETHOD RemoveObserver(nsIRDFObserver* n) ;
NS_IMETHOD ArcLabelsIn(nsIRDFNode* node,
nsISimpleEnumerator** labels /* out */) ;
NS_IMETHOD ArcLabelsOut(nsIRDFResource* source,
nsISimpleEnumerator** labels /* out */) ;
NS_IMETHOD GetAllResources(nsISimpleEnumerator** aResult) ;
NS_IMETHOD GetAllCommands(nsIRDFResource* source,
nsIEnumerator/*<nsIRDFResource>*/** commands) ;
NS_IMETHOD GetAllCmds(nsIRDFResource* source,
nsISimpleEnumerator/*<nsIRDFResource>*/** commands) ;
NS_IMETHOD IsCommandEnabled(nsISupportsArray/*<nsIRDFResource>*/* aSources,
nsIRDFResource* aCommand,
nsISupportsArray/*<nsIRDFResource>*/* aArguments,
PRBool* retVal) ;
NS_IMETHOD DoCommand(nsISupportsArray/*<nsIRDFResource>*/* aSources,
nsIRDFResource* aCommand,
nsISupportsArray/*<nsIRDFResource>*/* aArguments) ;
// nsIRDFRemoteDataSource methods
NS_IMETHOD Init(const char* aURI);
NS_IMETHOD Refresh(PRBool aBlocking);
NS_IMETHOD Flush();
// nsChromeRegistry methods:
nsChromeRegistry();
virtual ~nsChromeRegistry();
static PRUint32 gRefCnt;
static nsIRDFService* gRDFService;
static nsIRDFResource* kCHROME_chrome;
static nsIRDFResource* kCHROME_skin;
static nsIRDFResource* kCHROME_content;
static nsIRDFResource* kCHROME_platform;
static nsIRDFResource* kCHROME_locale;
static nsIRDFResource* kCHROME_behavior;
static nsIRDFResource* kCHROME_base;
static nsIRDFResource* kCHROME_main;
static nsIRDFResource* kCHROME_archive;
static nsIRDFResource* kCHROME_name;
static nsIRDFResource* kCHROME_displayname;
static nsIRDFDataSource* mInner;
protected:
nsresult GetPackageTypeResource(const nsString& aChromeType, nsIRDFResource** aResult);
nsresult GetChromeResource(nsString& aResult, nsIRDFResource* aChromeResource,
nsIRDFResource* aProperty);
};
PRUint32 nsChromeRegistry::gRefCnt ;
nsIRDFService* nsChromeRegistry::gRDFService = nsnull;
nsIRDFResource* nsChromeRegistry::kCHROME_chrome = nsnull;
nsIRDFResource* nsChromeRegistry::kCHROME_skin = nsnull;
nsIRDFResource* nsChromeRegistry::kCHROME_content = nsnull;
nsIRDFResource* nsChromeRegistry::kCHROME_locale = nsnull;
nsIRDFResource* nsChromeRegistry::kCHROME_behavior = nsnull;
nsIRDFResource* nsChromeRegistry::kCHROME_platform = nsnull;
nsIRDFResource* nsChromeRegistry::kCHROME_base = nsnull;
nsIRDFResource* nsChromeRegistry::kCHROME_main = nsnull;
nsIRDFResource* nsChromeRegistry::kCHROME_archive = nsnull;
nsIRDFResource* nsChromeRegistry::kCHROME_name = nsnull;
nsIRDFResource* nsChromeRegistry::kCHROME_displayname = nsnull;
nsIRDFDataSource* nsChromeRegistry::mInner = nsnull;
////////////////////////////////////////////////////////////////////////////////
nsChromeRegistry::nsChromeRegistry()
{
NS_INIT_REFCNT();
}
nsChromeRegistry::~nsChromeRegistry()
{
--gRefCnt;
if (gRefCnt == 0) {
// Release our inner data source
NS_IF_RELEASE(mInner);
// release all the properties:
NS_IF_RELEASE(kCHROME_chrome);
NS_IF_RELEASE(kCHROME_skin);
NS_IF_RELEASE(kCHROME_content);
NS_IF_RELEASE(kCHROME_platform);
NS_IF_RELEASE(kCHROME_locale);
NS_IF_RELEASE(kCHROME_behavior);
NS_IF_RELEASE(kCHROME_base);
NS_IF_RELEASE(kCHROME_main);
NS_IF_RELEASE(kCHROME_archive);
NS_IF_RELEASE(kCHROME_displayname);
NS_IF_RELEASE(kCHROME_name);
if (gRDFService) {
nsServiceManager::ReleaseService(kRDFServiceCID, gRDFService);
gRDFService = nsnull;
}
}
}
NS_IMPL_ADDREF(nsChromeRegistry)
NS_IMPL_RELEASE(nsChromeRegistry)
NS_IMETHODIMP
nsChromeRegistry::QueryInterface(REFNSIID aIID, void** aResult)
{
NS_PRECONDITION(aResult != nsnull, "null ptr");
if (! aResult)
return NS_ERROR_NULL_POINTER;
if (aIID.Equals(nsCOMTypeInfo<nsIChromeRegistry>::GetIID()) ||
aIID.Equals(kISupportsIID)) {
*aResult = NS_STATIC_CAST(nsIChromeRegistry*, this);
}
else if (aIID.Equals(nsCOMTypeInfo<nsIRDFDataSource>::GetIID())) {
*aResult = NS_STATIC_CAST(nsIRDFDataSource*, this);
}
else if (aIID.Equals(nsCOMTypeInfo<nsIRDFRemoteDataSource>::GetIID())) {
*aResult = NS_STATIC_CAST(nsIRDFRemoteDataSource*, this);
}
else {
*aResult = nsnull;
return NS_NOINTERFACE;
}
NS_ADDREF(this);
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// nsIChromeRegistry methods:
/*
The ConvertChromeURL takes a chrome URL and converts it into a resource: or
an HTTP: url type with certain rules. Here are the current portions of a
chrome: url that make up the chrome-
chrome://global/skin/foo?bar
\------/ \----/\---/ \-----/
| | | |
| | | `-- RemainingPortion
| | |
| | `-- Provider
| |
| `-- Package
|
'-- Always "chrome://"
Sometime in future when Necko lands completely this will change to the
following syntax-
chrome://skin@global/foo?bar
This will make the parsing simpler and quicker (since the URL parsing already
takes this into account)
*/
NS_IMETHODIMP
nsChromeRegistry::ConvertChromeURL(nsIURI* aChromeURL)
#ifdef NECKO
{
nsresult rv = NS_OK;
NS_ASSERTION(aChromeURL, "null url!");
if (!aChromeURL)
return NS_ERROR_NULL_POINTER;
#ifdef NS_DEBUG
//Ensure that we got a chrome url!
nsXPIDLCString scheme;
rv = aChromeURL->GetScheme(getter_Copies(scheme));
if (NS_FAILED(rv)) return rv;
NS_ASSERTION((0 == PL_strncmp(scheme, "chrome", 6)),
"Bad scheme URL in chrome URL conversion!");
if (0 != PL_strncmp(scheme, "chrome", 6))
return NS_ERROR_FAILURE;
#endif
// Obtain the package, provider and remaining from the URL
nsXPIDLCString package, provider, remaining;
#if 0 // This change happens when we switch to using chrome://skin@global/foo..
rv = aChromeURL->GetHost(getter_Copies(package));
if (NS_FAILED(rv)) return rv;
rv = aChromeURL->GetPreHost(getter_Copies(provider));
if (NS_FAILED(rv)) return rv;
rv = aChromeURL->GetPath(getter_Copies(remaining));
if (NS_FAILED(rv)) return rv;
#else // For now however...
rv = aChromeURL->GetHost(getter_Copies(package));
if (NS_FAILED(rv)) return rv;
nsXPIDLCString tempPath;
rv = aChromeURL->GetPath(getter_Copies(tempPath));
if (NS_FAILED(rv)) return rv;
BreakProviderAndRemainingFromPath(
(const char*)tempPath,
getter_Copies(provider),
getter_Copies(remaining));
#endif
// Construct the lookup string-
// which is basically chrome:// + package + provider
nsAutoString lookup("chrome://");
lookup += package; // no trailing slash here
NS_ASSERTION(*provider == '/', "No leading slash here!");
//definitely have a leading slash...
if (*provider != '/')
lookup += '/';
lookup += provider;
// end it on a slash if none is present
if (lookup.CharAt(lookup.Length()-1) != '/')
lookup += '/';
// Get the chromeResource from this lookup string
nsCOMPtr<nsIRDFResource> chromeResource;
if (NS_FAILED(rv = GetPackageTypeResource(lookup, getter_AddRefs(chromeResource)))) {
NS_ERROR("Unable to retrieve the resource corresponding to the chrome skin or content.");
return rv;
}
// Using this chrome resource get the three basic things of a chrome entry-
// base, name, main. and don't bail if they don't exist.
nsAutoString base, name, main;
rv = GetChromeResource(name, chromeResource, kCHROME_name);
if (NS_FAILED (rv)) {
// No name entry was found. No problem.
}
rv = GetChromeResource(base, chromeResource, kCHROME_base);
if (NS_FAILED(rv))
{
// No base entry was found, default it to our cache.
base = "resource:/chrome/";
base += package;
if ((base.CharAt(base.Length()-1) != '/') && *provider != '/')
base += '/';
base += provider;
if (base.CharAt(base.Length()-1) != '/')
base += '/';
if (name.Length())
base += name;
if (base.CharAt(base.Length()-1) != '/')
base += '/';
}
NS_ASSERTION(base.CharAt(base.Length()-1) == '/', "Base doesn't end in a slash!");
if ('/' != base.CharAt(base.Length()-1))
base += '/';
// Now we construct our finalString
nsAutoString finalString(base);
if (!remaining || (0 == PL_strlen(remaining)))
{
rv = GetChromeResource(main, chromeResource, kCHROME_main);
if (NS_FAILED(rv))
{
//we'd definitely need main for an empty remaining
NS_ERROR("Unable to retrieve the main file registry entry for a chrome URL.");
return rv;
}
finalString += main;
}
else
finalString += remaining;
char* finalURI = finalString.ToNewCString();
aChromeURL->SetSpec(finalURI);
/*
#ifndef NECKO
// Clean out possible // in the path
char* path;
rv = aChromeURL->GetPath(&path);
char* cleanPath = path;
for (; '\0' != *path; ++path)
{
if (*path == '/' && *(path+1) == '/')
++path;
*cleanPath++ = *path;
}
aChrome->SetRelativePath(path);
nsCRT::free(path);
#endif
*/
nsCRT::free(finalURI);
return NS_OK;
}
#else
{
nsresult rv = NS_OK;
// Retrieve the resource for this chrome element.
const char* host;
if (NS_FAILED(rv = aChromeURL->GetHost(&host))) {
NS_ERROR("Unable to retrieve the host for a chrome URL.");
return rv;
}
nsAutoString hostStr(host);
const char* file;
// Construct a chrome URL and use it to look up a resource.
nsAutoString windowType = nsAutoString("chrome://") + hostStr + "/";
// Stash any search part of the URL for later
aChromeURL->GetSearch(&file);
nsAutoString searchStr(file);
// Find out the package type of the URL
aChromeURL->GetFile(&file);
nsAutoString restOfURL(file);
// Find the second slash.
nsAutoString packageType("content");
nsAutoString path("");
PRInt32 slashIndex = -1;
if (restOfURL.Length() > 1)
{
// There is something to the right of that slash. A provider type must have
// been specified.
slashIndex = restOfURL.FindChar('/', PR_FALSE,1);
if (slashIndex == -1)
slashIndex = restOfURL.Length();
restOfURL.Mid(packageType, 1, slashIndex - 1);
if (slashIndex < restOfURL.Length()-1)
{
// There are some extra subdirectories to remember.
restOfURL.Right(path, restOfURL.Length()-slashIndex-1);
}
}
windowType += packageType + "/";
// We have the resource URI that we wish to retrieve. Fetch it.
nsCOMPtr<nsIRDFResource> chromeResource;
if (NS_FAILED(rv = GetPackageTypeResource(windowType, getter_AddRefs(chromeResource)))) {
NS_ERROR("Unable to retrieve the resource corresponding to the chrome skin or content.");
return rv;
}
nsAutoString chromeName;
if (NS_FAILED(rv = GetChromeResource(chromeName, chromeResource, kCHROME_name))) {
// No name entry was found. Don't use one.
chromeName = "";
}
nsAutoString chromeBase;
if (NS_FAILED(rv = GetChromeResource(chromeBase, chromeResource, kCHROME_base))) {
// No base entry was found. Default to our cache.
chromeBase = "resource:/chrome/";
chromeBase += hostStr;
chromeBase += "/";
chromeBase += packageType + "/";
if (chromeName != "")
chromeBase += chromeName + "/";
}
// Make sure base ends in a slash
PRInt32 length = chromeBase.Length();
if (length > 0)
{
PRUnichar c = chromeBase.CharAt(length-1);
if (c != '/')
chromeBase += "/"; // Ensure that a slash is present.
}
// Check to see if we should append the "main" entry in the registry.
// Only do this when the user doesn't have anything following "skin"
// or "content" in the specified URL.
if (path.IsEmpty())
{
PRInt32 length = restOfURL.Length();
if (length > 0)
{
PRUnichar c = restOfURL.CharAt(length-1);
if (c != '/')
restOfURL += "/"; // Ensure that a slash is present.
}
// Append the "main" entry.
nsAutoString mainFile;
if (NS_FAILED(rv = GetChromeResource(mainFile, chromeResource, kCHROME_main))) {
NS_ERROR("Unable to retrieve the main file registry entry for a chrome URL.");
return rv;
}
chromeBase += mainFile;
}
else
{
// XXX Just append the rest of the URL to base to get the actual URL to look up.
chromeBase += path;
}
char* finalDecision = chromeBase.ToNewCString();
char* search = searchStr.ToNewCString();
aChromeURL->SetSpec(finalDecision);
if (search && *search) {
aChromeURL->SetSearch(search);
nsCRT::free(search);
}
}
#endif
NS_IMETHODIMP
nsChromeRegistry::InitRegistry()
{
gRefCnt++;
if (gRefCnt == 1) {
nsresult rv;
rv = nsServiceManager::GetService(kRDFServiceCID,
kIRDFServiceIID,
(nsISupports**)&gRDFService);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get RDF service");
if (NS_FAILED(rv)) return rv;
// get all the properties we'll need:
rv = gRDFService->GetResource(kURICHROME_chrome, &kCHROME_chrome);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get resource");
if (NS_FAILED(rv)) return rv;
rv = gRDFService->GetResource(kURICHROME_skin, &kCHROME_skin);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get resource");
if (NS_FAILED(rv)) return rv;
rv = gRDFService->GetResource(kURICHROME_content, &kCHROME_content);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get resource");
if (NS_FAILED(rv)) return rv;
rv = gRDFService->GetResource(kURICHROME_content, &kCHROME_platform);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get resource");
if (NS_FAILED(rv)) return rv;
rv = gRDFService->GetResource(kURICHROME_content, &kCHROME_locale);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get resource");
if (NS_FAILED(rv)) return rv;
rv = gRDFService->GetResource(kURICHROME_content, &kCHROME_behavior);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get resource");
if (NS_FAILED(rv)) return rv;
rv = gRDFService->GetResource(kURICHROME_base, &kCHROME_base);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get resource");
if (NS_FAILED(rv)) return rv;
rv = gRDFService->GetResource(kURICHROME_main, &kCHROME_main);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get resource");
if (NS_FAILED(rv)) return rv;
rv = gRDFService->GetResource(kURICHROME_archive, &kCHROME_archive);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get resource");
if (NS_FAILED(rv)) return rv;
rv = gRDFService->GetResource(kURICHROME_name, &kCHROME_name);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get resource");
if (NS_FAILED(rv)) return rv;
rv = gRDFService->GetResource(kURICHROME_displayname, &kCHROME_displayname);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get resource");
if (NS_FAILED(rv)) return rv;
rv = nsComponentManager::CreateInstance(kRDFXMLDataSourceCID,
nsnull,
nsIRDFDataSource::GetIID(),
(void**) &mInner);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(mInner);
if (! remote)
return NS_ERROR_UNEXPECTED;
// Retrieve the mInner data source.
nsSpecialSystemDirectory chromeFile(nsSpecialSystemDirectory::OS_CurrentProcessDirectory);
chromeFile += "chrome";
chromeFile += "registry.rdf";
nsFileURL chromeURL(chromeFile);
const char* innerURI = chromeURL.GetAsString();
rv = remote->Init(innerURI);
if (NS_FAILED(rv)) return rv;
// We need to read this synchronously.
rv = remote->Refresh(PR_TRUE);
if (NS_FAILED(rv)) return rv;
}
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// nsIRDFObserver methods:
////////////////////////////////////////////////////////////////////////////////
nsresult
nsChromeRegistry::GetPackageTypeResource(const nsString& aChromeType,
nsIRDFResource** aResult)
{
nsresult rv = NS_OK;
char* url = aChromeType.ToNewCString();
if (NS_FAILED(rv = gRDFService->GetResource(url, aResult))) {
NS_ERROR("Unable to retrieve a resource for this package type.");
*aResult = nsnull;
nsCRT::free(url);
return rv;
}
nsCRT::free(url);
return NS_OK;
}
nsresult
nsChromeRegistry::GetChromeResource(nsString& aResult,
nsIRDFResource* aChromeResource,
nsIRDFResource* aProperty)
{
nsresult rv = NS_OK;
if (mInner == nsnull)
return NS_ERROR_FAILURE; // Must have a DB to attempt this operation.
nsCOMPtr<nsIRDFNode> chromeBase;
if (NS_FAILED(rv = GetTarget(aChromeResource, aProperty, PR_TRUE, getter_AddRefs(chromeBase)))) {
NS_ERROR("Unable to obtain a base resource.");
return rv;
}
if (chromeBase == nsnull)
return NS_ERROR_FAILURE;
nsCOMPtr<nsIRDFResource> resource;
nsCOMPtr<nsIRDFLiteral> literal;
if (NS_SUCCEEDED(rv = chromeBase->QueryInterface(kIRDFResourceIID,
(void**) getter_AddRefs(resource)))) {
nsXPIDLCString uri;
resource->GetValue( getter_Copies(uri) );
aResult = uri;
}
else if (NS_SUCCEEDED(rv = chromeBase->QueryInterface(kIRDFLiteralIID,
(void**) getter_AddRefs(literal)))) {
nsXPIDLString s;
literal->GetValue( getter_Copies(s) );
aResult = s;
}
else {
// This should _never_ happen.
NS_ERROR("uh, this isn't a resource or a literal!");
return NS_ERROR_UNEXPECTED;
}
return NS_OK;
}
nsresult
NS_NewChromeRegistry(nsIChromeRegistry** aResult)
{
NS_PRECONDITION(aResult != nsnull, "null ptr");
if (! aResult)
return NS_ERROR_NULL_POINTER;
nsChromeRegistry* chromeRegistry = new nsChromeRegistry();
if (chromeRegistry == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(chromeRegistry);
*aResult = chromeRegistry;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// nsIRDFDataSource methods
NS_IMETHODIMP
nsChromeRegistry::GetURI(char** uri)
{
*uri = nsXPIDLCString::Copy("rdf:chrome");
if (! *uri)
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
NS_IMETHODIMP
nsChromeRegistry::GetSource(nsIRDFResource* property,
nsIRDFNode* target,
PRBool tv,
nsIRDFResource** source /* out */)
{
return mInner->GetSource(property, target, tv, source);
}
NS_IMETHODIMP
nsChromeRegistry::GetSources(nsIRDFResource* property,
nsIRDFNode* target,
PRBool tv,
nsISimpleEnumerator** sources /* out */)
{
return mInner->GetSources(property, target, tv, sources);
}
NS_IMETHODIMP
nsChromeRegistry::GetTarget(nsIRDFResource* source,
nsIRDFResource* property,
PRBool tv,
nsIRDFNode** target /* out */)
{
return mInner->GetTarget(source, property, tv, target);
}
NS_IMETHODIMP
nsChromeRegistry::GetTargets(nsIRDFResource* source,
nsIRDFResource* property,
PRBool tv,
nsISimpleEnumerator** targets /* out */)
{
return mInner->GetTargets(source, property, tv, targets);
}
NS_IMETHODIMP
nsChromeRegistry::Assert(nsIRDFResource* source,
nsIRDFResource* property,
nsIRDFNode* target,
PRBool tv)
{
return mInner->Assert(source, property, target, tv);
}
NS_IMETHODIMP
nsChromeRegistry::Unassert(nsIRDFResource* source,
nsIRDFResource* property,
nsIRDFNode* target)
{
return mInner->Unassert(source, property, target);
}
NS_IMETHODIMP
nsChromeRegistry::Change(nsIRDFResource* aSource,
nsIRDFResource* aProperty,
nsIRDFNode* aOldTarget,
nsIRDFNode* aNewTarget)
{
return mInner->Change(aSource, aProperty, aOldTarget, aNewTarget);
}
NS_IMETHODIMP
nsChromeRegistry::Move(nsIRDFResource* aOldSource,
nsIRDFResource* aNewSource,
nsIRDFResource* aProperty,
nsIRDFNode* aTarget)
{
return mInner->Move(aOldSource, aNewSource, aProperty, aTarget);
}
NS_IMETHODIMP
nsChromeRegistry::HasAssertion(nsIRDFResource* source,
nsIRDFResource* property,
nsIRDFNode* target,
PRBool tv,
PRBool* hasAssertion /* out */)
{
return mInner->HasAssertion(source, property, target, tv, hasAssertion);
}
NS_IMETHODIMP nsChromeRegistry::AddObserver(nsIRDFObserver* n)
{
return mInner->AddObserver(n);
}
NS_IMETHODIMP nsChromeRegistry::RemoveObserver(nsIRDFObserver* n)
{
return mInner->RemoveObserver(n);
}
NS_IMETHODIMP nsChromeRegistry::ArcLabelsIn(nsIRDFNode* node,
nsISimpleEnumerator** labels /* out */)
{
return mInner->ArcLabelsIn(node, labels);
}
NS_IMETHODIMP nsChromeRegistry::ArcLabelsOut(nsIRDFResource* source,
nsISimpleEnumerator** labels /* out */)
{
return mInner->ArcLabelsOut(source, labels);
}
NS_IMETHODIMP nsChromeRegistry::GetAllResources(nsISimpleEnumerator** aCursor)
{
return mInner->GetAllResources(aCursor);
}
NS_IMETHODIMP
nsChromeRegistry::GetAllCommands(nsIRDFResource* source,
nsIEnumerator/*<nsIRDFResource>*/** commands)
{
return mInner->GetAllCommands(source, commands);
}
NS_IMETHODIMP
nsChromeRegistry::GetAllCmds(nsIRDFResource* source,
nsISimpleEnumerator/*<nsIRDFResource>*/** commands)
{
return mInner->GetAllCmds(source, commands);
}
NS_IMETHODIMP
nsChromeRegistry::IsCommandEnabled(nsISupportsArray/*<nsIRDFResource>*/* aSources,
nsIRDFResource* aCommand,
nsISupportsArray/*<nsIRDFResource>*/* aArguments,
PRBool* retVal)
{
return mInner->IsCommandEnabled(aSources, aCommand, aArguments, retVal);
}
NS_IMETHODIMP
nsChromeRegistry::DoCommand(nsISupportsArray/*<nsIRDFResource>*/* aSources,
nsIRDFResource* aCommand,
nsISupportsArray/*<nsIRDFResource>*/* aArguments)
{
return mInner->DoCommand(aSources, aCommand, aArguments);
}
////////////////////////////////////////////////////////////////////////
// nsIRDFRemoteDataSource methods
NS_IMETHODIMP
nsChromeRegistry::Init(const char* aURI)
{
return NS_OK;
}
NS_IMETHODIMP
nsChromeRegistry::Refresh(PRBool aBlocking)
{
nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(mInner);
if (! remote)
return NS_ERROR_UNEXPECTED;
return remote->Refresh(aBlocking);
}
NS_IMETHODIMP
nsChromeRegistry::Flush()
{
nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(mInner);
if (! remote)
return NS_ERROR_UNEXPECTED;
return remote->Flush();
}
////////////////////////////////////////////////////////////////////////
//
// Path = provider/remaining
//
void BreakProviderAndRemainingFromPath(const char* i_path, char** o_provider, char** o_remaining)
{
if (!i_path || !o_provider || !o_remaining)
return;
int len = PL_strlen(i_path);
NS_ASSERTION(len>1, "path is messed up!");
char* slash = PL_strchr(i_path+1, '/'); // +1 to skip the leading slash if any
if (slash)
{
*o_provider = PL_strndup(i_path, (slash - i_path)); // dont include the trailing slash
if (slash != (i_path + len-1)) // if that was not the last trailing slash...
{
// don't include the leading slash here as well...
*o_remaining = PL_strndup(slash+1, len - (slash-i_path + 1));
}
else
*o_remaining = nsnull;
}
else // everything is just the provider
*o_provider = PL_strndup(i_path, len);
}