summaryrefslogtreecommitdiffstats
path: root/modules/libjar/nsJARURI.cpp
blob: e6837e998fe9a0b170d0f02ea9b975762a0694f0 (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 *
 * 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/. */

#include "base/basictypes.h"

#include "nsJARURI.h"
#include "nsNetUtil.h"
#include "nsIClassInfoImpl.h"
#include "nsIIOService.h"
#include "nsIStandardURL.h"
#include "nsCRT.h"
#include "nsReadableUtils.h"
#include "nsNetCID.h"
#include "nsIObjectInputStream.h"
#include "nsIObjectOutputStream.h"
#include "nsQueryObject.h"
#include "mozilla/ipc/URIUtils.h"

using namespace mozilla::ipc;

////////////////////////////////////////////////////////////////////////////////

NS_IMPL_CLASSINFO(nsJARURI, nullptr, nsIClassInfo::THREADSAFE, NS_JARURI_CID)
// Empty CI getter. We only need nsIClassInfo for Serialization
NS_IMPL_CI_INTERFACE_GETTER0(nsJARURI)

nsJARURI::nsJARURI() {}

nsJARURI::~nsJARURI() {}

// XXX Why is this threadsafe?
NS_IMPL_ADDREF(nsJARURI)
NS_IMPL_RELEASE(nsJARURI)
NS_INTERFACE_MAP_BEGIN(nsJARURI)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIJARURI)
  NS_INTERFACE_MAP_ENTRY(nsIURI)
  NS_INTERFACE_MAP_ENTRY(nsIURL)
  NS_INTERFACE_MAP_ENTRY(nsIJARURI)
  NS_INTERFACE_MAP_ENTRY(nsISerializable)
  NS_IMPL_QUERY_CLASSINFO(nsJARURI)
  NS_INTERFACE_MAP_ENTRY(nsINestedURI)
  NS_INTERFACE_MAP_ENTRY_CONCRETE(nsJARURI)
NS_INTERFACE_MAP_END

nsresult nsJARURI::Init(const char* charsetHint) {
  mCharsetHint = charsetHint;
  return NS_OK;
}

#define NS_JAR_SCHEME "jar:"_ns
#define NS_JAR_DELIMITER "!/"_ns
#define NS_BOGUS_ENTRY_SCHEME "x:///"_ns

// FormatSpec takes the entry spec (including the "x:///" at the
// beginning) and gives us a full JAR spec.
nsresult nsJARURI::FormatSpec(const nsACString& entrySpec, nsACString& result,
                              bool aIncludeScheme) {
  // The entrySpec MUST start with "x:///"
  NS_ASSERTION(StringBeginsWith(entrySpec, NS_BOGUS_ENTRY_SCHEME),
               "bogus entry spec");

  nsAutoCString fileSpec;
  nsresult rv = mJARFile->GetSpec(fileSpec);
  if (NS_FAILED(rv)) return rv;

  if (aIncludeScheme)
    result = NS_JAR_SCHEME;
  else
    result.Truncate();

  result.Append(fileSpec + NS_JAR_DELIMITER +
                Substring(entrySpec, 5, entrySpec.Length() - 5));
  return NS_OK;
}

nsresult nsJARURI::CreateEntryURL(const nsACString& entryFilename,
                                  const char* charset, nsIURL** url) {
  *url = nullptr;
  // Flatten the concatenation, just in case.  See bug 128288
  nsAutoCString spec(NS_BOGUS_ENTRY_SCHEME + entryFilename);
  return NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID)
      .Apply(&nsIStandardURLMutator::Init, nsIStandardURL::URLTYPE_NO_AUTHORITY,
             -1, spec, charset, nullptr, nullptr)
      .Finalize(url);
}

////////////////////////////////////////////////////////////////////////////////
// nsISerializable methods:

NS_IMETHODIMP
nsJARURI::Read(nsIObjectInputStream* aStream) {
  MOZ_ASSERT_UNREACHABLE("Use nsIURIMutator.read() instead");
  return NS_ERROR_NOT_IMPLEMENTED;
}

nsresult nsJARURI::ReadPrivate(nsIObjectInputStream* aInputStream) {
  nsresult rv;

  nsCOMPtr<nsISupports> supports;
  rv = aInputStream->ReadObject(true, getter_AddRefs(supports));
  NS_ENSURE_SUCCESS(rv, rv);

  mJARFile = do_QueryInterface(supports, &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = aInputStream->ReadObject(true, getter_AddRefs(supports));
  NS_ENSURE_SUCCESS(rv, rv);

  mJAREntry = do_QueryInterface(supports);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = aInputStream->ReadCString(mCharsetHint);
  return rv;
}

NS_IMETHODIMP
nsJARURI::Write(nsIObjectOutputStream* aOutputStream) {
  nsresult rv;

  rv = aOutputStream->WriteCompoundObject(mJARFile, NS_GET_IID(nsIURI), true);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = aOutputStream->WriteCompoundObject(mJAREntry, NS_GET_IID(nsIURL), true);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = aOutputStream->WriteStringZ(mCharsetHint.get());
  return rv;
}

////////////////////////////////////////////////////////////////////////////////
// nsIURI methods:

NS_IMETHODIMP
nsJARURI::GetSpec(nsACString& aSpec) {
  nsAutoCString entrySpec;
  mJAREntry->GetSpec(entrySpec);
  return FormatSpec(entrySpec, aSpec);
}

NS_IMETHODIMP
nsJARURI::GetSpecIgnoringRef(nsACString& aSpec) {
  nsAutoCString entrySpec;
  mJAREntry->GetSpecIgnoringRef(entrySpec);
  return FormatSpec(entrySpec, aSpec);
}

NS_IMETHODIMP
nsJARURI::GetDisplaySpec(nsACString& aUnicodeSpec) {
  return GetSpec(aUnicodeSpec);
}

NS_IMETHODIMP
nsJARURI::GetDisplayHostPort(nsACString& aUnicodeHostPort) {
  return GetHostPort(aUnicodeHostPort);
}

NS_IMETHODIMP
nsJARURI::GetDisplayPrePath(nsACString& aPrePath) {
  return GetPrePath(aPrePath);
}

NS_IMETHODIMP
nsJARURI::GetDisplayHost(nsACString& aUnicodeHost) {
  return GetHost(aUnicodeHost);
}

NS_IMETHODIMP
nsJARURI::GetHasRef(bool* result) { return mJAREntry->GetHasRef(result); }

nsresult nsJARURI::SetSpecInternal(const nsACString& aSpec) {
  return SetSpecWithBase(aSpec, nullptr);
}

// Queries this list of interfaces. If none match, it queries mURI.
NS_IMPL_NSIURIMUTATOR_ISUPPORTS(nsJARURI::Mutator, nsIURISetters, nsIURIMutator,
                                nsIURLMutator, nsISerializable,
                                nsIJARURIMutator)

NS_IMETHODIMP
nsJARURI::Mutator::SetFileName(const nsACString& aFileName,
                               nsIURIMutator** aMutator) {
  if (!mURI) {
    return NS_ERROR_NULL_POINTER;
  }
  if (aMutator) {
    nsCOMPtr<nsIURIMutator> mutator = this;
    mutator.forget(aMutator);
  }
  return mURI->SetFileNameInternal(aFileName);
}

NS_IMETHODIMP
nsJARURI::Mutator::SetFileBaseName(const nsACString& aFileBaseName,
                                   nsIURIMutator** aMutator) {
  if (!mURI) {
    return NS_ERROR_NULL_POINTER;
  }
  if (aMutator) {
    nsCOMPtr<nsIURIMutator> mutator = this;
    mutator.forget(aMutator);
  }
  return mURI->SetFileBaseNameInternal(aFileBaseName);
}

NS_IMETHODIMP
nsJARURI::Mutator::SetFileExtension(const nsACString& aFileExtension,
                                    nsIURIMutator** aMutator) {
  if (!mURI) {
    return NS_ERROR_NULL_POINTER;
  }
  if (aMutator) {
    nsCOMPtr<nsIURIMutator> mutator = this;
    mutator.forget(aMutator);
  }
  return mURI->SetFileExtensionInternal(aFileExtension);
}

NS_IMETHODIMP
nsJARURI::Mutate(nsIURIMutator** aMutator) {
  RefPtr<nsJARURI::Mutator> mutator = new nsJARURI::Mutator();
  nsresult rv = mutator->InitFromURI(this);
  if (NS_FAILED(rv)) {
    return rv;
  }
  mutator.forget(aMutator);
  return NS_OK;
}

nsresult nsJARURI::SetSpecWithBase(const nsACString& aSpec, nsIURI* aBaseURL) {
  nsresult rv;

  nsCOMPtr<nsIIOService> ioServ(do_GetIOService(&rv));
  NS_ENSURE_SUCCESS(rv, rv);

  nsAutoCString scheme;
  rv = ioServ->ExtractScheme(aSpec, scheme);
  if (NS_FAILED(rv)) {
    // not an absolute URI
    if (!aBaseURL) return NS_ERROR_MALFORMED_URI;

    RefPtr<nsJARURI> otherJAR = do_QueryObject(aBaseURL);
    NS_ENSURE_TRUE(otherJAR, NS_NOINTERFACE);

    mJARFile = otherJAR->mJARFile;

    nsCOMPtr<nsIURI> entry;

    rv = NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID)
             .Apply(&nsIStandardURLMutator::Init,
                    nsIStandardURL::URLTYPE_NO_AUTHORITY, -1, aSpec,
                    mCharsetHint.get(), otherJAR->mJAREntry, nullptr)
             .Finalize(entry);
    if (NS_FAILED(rv)) {
      return rv;
    }

    mJAREntry = do_QueryInterface(entry);
    if (!mJAREntry) return NS_NOINTERFACE;

    return NS_OK;
  }

  NS_ENSURE_TRUE(scheme.EqualsLiteral("jar"), NS_ERROR_MALFORMED_URI);

  nsACString::const_iterator begin, end;
  aSpec.BeginReading(begin);
  aSpec.EndReading(end);

  while (begin != end && *begin != ':') ++begin;

  ++begin;  // now we're past the "jar:"

  nsACString::const_iterator delim_begin = begin;
  nsACString::const_iterator delim_end = end;
  nsACString::const_iterator frag = begin;

  if (FindInReadable(NS_JAR_DELIMITER, delim_begin, delim_end)) {
    frag = delim_end;
  }
  while (frag != end && (*frag != '#' && *frag != '?')) {
    ++frag;
  }
  if (frag != end) {
    // there was a fragment or query, mark that as the end of the URL to scan
    end = frag;
  }

  // Search backward from the end for the "!/" delimiter. Remember, jar URLs
  // can nest, e.g.:
  //    jar:jar:http://www.foo.com/bar.jar!/a.jar!/b.html
  // This gets the b.html document from out of the a.jar file, that's
  // contained within the bar.jar file.
  // Also, the outermost "inner" URI may be a relative URI:
  //   jar:../relative.jar!/a.html

  delim_begin = begin;
  delim_end = end;

  if (!RFindInReadable(NS_JAR_DELIMITER, delim_begin, delim_end)) {
    return NS_ERROR_MALFORMED_URI;
  }

  rv = ioServ->NewURI(Substring(begin, delim_begin), mCharsetHint.get(),
                      aBaseURL, getter_AddRefs(mJARFile));
  if (NS_FAILED(rv)) return rv;

  // skip over any extra '/' chars
  while (*delim_end == '/') ++delim_end;

  aSpec.EndReading(end);  // set to the original 'end'
  return SetJAREntry(Substring(delim_end, end));
}

NS_IMETHODIMP
nsJARURI::GetPrePath(nsACString& prePath) {
  prePath = NS_JAR_SCHEME;
  return NS_OK;
}

NS_IMETHODIMP
nsJARURI::GetScheme(nsACString& aScheme) {
  aScheme = "jar";
  return NS_OK;
}

nsresult nsJARURI::SetScheme(const nsACString& aScheme) {
  // doesn't make sense to set the scheme of a jar: URL
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
nsJARURI::GetUserPass(nsACString& aUserPass) { return NS_ERROR_FAILURE; }

nsresult nsJARURI::SetUserPass(const nsACString& aUserPass) {
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
nsJARURI::GetUsername(nsACString& aUsername) { return NS_ERROR_FAILURE; }

nsresult nsJARURI::SetUsername(const nsACString& aUsername) {
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
nsJARURI::GetPassword(nsACString& aPassword) { return NS_ERROR_FAILURE; }

nsresult nsJARURI::SetPassword(const nsACString& aPassword) {
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
nsJARURI::GetHostPort(nsACString& aHostPort) { return NS_ERROR_FAILURE; }

nsresult nsJARURI::SetHostPort(const nsACString& aHostPort) {
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
nsJARURI::GetHost(nsACString& aHost) { return NS_ERROR_FAILURE; }

nsresult nsJARURI::SetHost(const nsACString& aHost) { return NS_ERROR_FAILURE; }

NS_IMETHODIMP
nsJARURI::GetPort(int32_t* aPort) { return NS_ERROR_FAILURE; }

nsresult nsJARURI::SetPort(int32_t aPort) { return NS_ERROR_FAILURE; }

nsresult nsJARURI::GetPathQueryRef(nsACString& aPath) {
  nsAutoCString entrySpec;
  mJAREntry->GetSpec(entrySpec);
  return FormatSpec(entrySpec, aPath, false);
}

nsresult nsJARURI::SetPathQueryRef(const nsACString& aPath) {
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
nsJARURI::GetAsciiSpec(nsACString& aSpec) {
  // XXX Shouldn't this like... make sure it returns ASCII or something?
  return GetSpec(aSpec);
}

NS_IMETHODIMP
nsJARURI::GetAsciiHostPort(nsACString& aHostPort) { return NS_ERROR_FAILURE; }

NS_IMETHODIMP
nsJARURI::GetAsciiHost(nsACString& aHost) { return NS_ERROR_FAILURE; }

NS_IMETHODIMP
nsJARURI::Equals(nsIURI* other, bool* result) {
  return EqualsInternal(other, eHonorRef, result);
}

NS_IMETHODIMP
nsJARURI::EqualsExceptRef(nsIURI* other, bool* result) {
  return EqualsInternal(other, eIgnoreRef, result);
}

// Helper method:
/* virtual */
nsresult nsJARURI::EqualsInternal(nsIURI* other,
                                  nsJARURI::RefHandlingEnum refHandlingMode,
                                  bool* result) {
  *result = false;

  if (!other) return NS_OK;  // not equal

  RefPtr<nsJARURI> otherJAR = do_QueryObject(other);
  if (!otherJAR) return NS_OK;  // not equal

  bool equal;
  nsresult rv = mJARFile->Equals(otherJAR->mJARFile, &equal);
  if (NS_FAILED(rv) || !equal) {
    return rv;  // not equal
  }

  return refHandlingMode == eHonorRef
             ? mJAREntry->Equals(otherJAR->mJAREntry, result)
             : mJAREntry->EqualsExceptRef(otherJAR->mJAREntry, result);
}

NS_IMETHODIMP
nsJARURI::SchemeIs(const char* i_Scheme, bool* o_Equals) {
  MOZ_ASSERT(o_Equals);
  if (!i_Scheme) {
    *o_Equals = false;
    return NS_OK;
  }

  *o_Equals = nsCRT::strcasecmp("jar", i_Scheme) == 0;
  return NS_OK;
}

nsresult nsJARURI::Clone(nsIURI** result) {
  RefPtr<nsJARURI> uri = new nsJARURI();
  uri->mJARFile = mJARFile;
  uri->mJAREntry = mJAREntry;
  uri.forget(result);

  return NS_OK;
}

NS_IMETHODIMP
nsJARURI::Resolve(const nsACString& relativePath, nsACString& result) {
  nsresult rv;

  nsCOMPtr<nsIIOService> ioServ(do_GetIOService(&rv));
  if (NS_FAILED(rv)) return rv;

  nsAutoCString scheme;
  rv = ioServ->ExtractScheme(relativePath, scheme);
  if (NS_SUCCEEDED(rv)) {
    // then aSpec is absolute
    result = relativePath;
    return NS_OK;
  }

  nsAutoCString resolvedPath;
  mJAREntry->Resolve(relativePath, resolvedPath);

  return FormatSpec(resolvedPath, result);
}

////////////////////////////////////////////////////////////////////////////////
// nsIURL methods:

NS_IMETHODIMP
nsJARURI::GetFilePath(nsACString& filePath) {
  return mJAREntry->GetFilePath(filePath);
}

nsresult nsJARURI::SetFilePath(const nsACString& filePath) {
  return NS_MutateURI(mJAREntry).SetFilePath(filePath).Finalize(mJAREntry);
}

NS_IMETHODIMP
nsJARURI::GetQuery(nsACString& query) { return mJAREntry->GetQuery(query); }

nsresult nsJARURI::SetQuery(const nsACString& query) {
  return NS_MutateURI(mJAREntry).SetQuery(query).Finalize(mJAREntry);
}

nsresult nsJARURI::SetQueryWithEncoding(const nsACString& query,
                                        const mozilla::Encoding* encoding) {
  return NS_MutateURI(mJAREntry)
      .SetQueryWithEncoding(query, encoding)
      .Finalize(mJAREntry);
}

NS_IMETHODIMP
nsJARURI::GetRef(nsACString& ref) { return mJAREntry->GetRef(ref); }

nsresult nsJARURI::SetRef(const nsACString& ref) {
  return NS_MutateURI(mJAREntry).SetRef(ref).Finalize(mJAREntry);
}

NS_IMETHODIMP
nsJARURI::GetDirectory(nsACString& directory) {
  return mJAREntry->GetDirectory(directory);
}

NS_IMETHODIMP
nsJARURI::GetFileName(nsACString& fileName) {
  return mJAREntry->GetFileName(fileName);
}

nsresult nsJARURI::SetFileNameInternal(const nsACString& fileName) {
  return NS_MutateURI(mJAREntry)
      .Apply(&nsIURLMutator::SetFileName, fileName, nullptr)
      .Finalize(mJAREntry);
}

NS_IMETHODIMP
nsJARURI::GetFileBaseName(nsACString& fileBaseName) {
  return mJAREntry->GetFileBaseName(fileBaseName);
}

nsresult nsJARURI::SetFileBaseNameInternal(const nsACString& fileBaseName) {
  return NS_MutateURI(mJAREntry)
      .Apply(&nsIURLMutator::SetFileBaseName, fileBaseName, nullptr)
      .Finalize(mJAREntry);
}

NS_IMETHODIMP
nsJARURI::GetFileExtension(nsACString& fileExtension) {
  return mJAREntry->GetFileExtension(fileExtension);
}

nsresult nsJARURI::SetFileExtensionInternal(const nsACString& fileExtension) {
  return NS_MutateURI(mJAREntry)
      .Apply(&nsIURLMutator::SetFileExtension, fileExtension, nullptr)
      .Finalize(mJAREntry);
}

NS_IMETHODIMP
nsJARURI::GetCommonBaseSpec(nsIURI* uriToCompare, nsACString& commonSpec) {
  commonSpec.Truncate();

  NS_ENSURE_ARG_POINTER(uriToCompare);

  commonSpec.Truncate();
  nsCOMPtr<nsIJARURI> otherJARURI(do_QueryInterface(uriToCompare));
  if (!otherJARURI) {
    // Nothing in common
    return NS_OK;
  }

  nsCOMPtr<nsIURI> otherJARFile;
  nsresult rv = otherJARURI->GetJARFile(getter_AddRefs(otherJARFile));
  if (NS_FAILED(rv)) return rv;

  bool equal;
  rv = mJARFile->Equals(otherJARFile, &equal);
  if (NS_FAILED(rv)) return rv;

  if (!equal) {
    // See what the JAR file URIs have in common
    nsCOMPtr<nsIURL> ourJARFileURL(do_QueryInterface(mJARFile));
    if (!ourJARFileURL) {
      // Not a URL, so nothing in common
      return NS_OK;
    }
    nsAutoCString common;
    rv = ourJARFileURL->GetCommonBaseSpec(otherJARFile, common);
    if (NS_FAILED(rv)) return rv;

    commonSpec = NS_JAR_SCHEME + common;
    return NS_OK;
  }

  // At this point we have the same JAR file.  Compare the JAREntrys
  nsAutoCString otherEntry;
  rv = otherJARURI->GetJAREntry(otherEntry);
  if (NS_FAILED(rv)) return rv;

  nsCOMPtr<nsIURL> url;
  rv = CreateEntryURL(otherEntry, nullptr, getter_AddRefs(url));
  if (NS_FAILED(rv)) return rv;

  nsAutoCString common;
  rv = mJAREntry->GetCommonBaseSpec(url, common);
  if (NS_FAILED(rv)) return rv;

  rv = FormatSpec(common, commonSpec);
  return rv;
}

NS_IMETHODIMP
nsJARURI::GetRelativeSpec(nsIURI* uriToCompare, nsACString& relativeSpec) {
  GetSpec(relativeSpec);

  NS_ENSURE_ARG_POINTER(uriToCompare);

  nsCOMPtr<nsIJARURI> otherJARURI(do_QueryInterface(uriToCompare));
  if (!otherJARURI) {
    // Nothing in common
    return NS_OK;
  }

  nsCOMPtr<nsIURI> otherJARFile;
  nsresult rv = otherJARURI->GetJARFile(getter_AddRefs(otherJARFile));
  if (NS_FAILED(rv)) return rv;

  bool equal;
  rv = mJARFile->Equals(otherJARFile, &equal);
  if (NS_FAILED(rv)) return rv;

  if (!equal) {
    // We live in different JAR files.  Nothing in common.
    return rv;
  }

  // Same JAR file.  Compare the JAREntrys
  nsAutoCString otherEntry;
  rv = otherJARURI->GetJAREntry(otherEntry);
  if (NS_FAILED(rv)) return rv;

  nsCOMPtr<nsIURL> url;
  rv = CreateEntryURL(otherEntry, nullptr, getter_AddRefs(url));
  if (NS_FAILED(rv)) return rv;

  nsAutoCString relativeEntrySpec;
  rv = mJAREntry->GetRelativeSpec(url, relativeEntrySpec);
  if (NS_FAILED(rv)) return rv;

  if (!StringBeginsWith(relativeEntrySpec, NS_BOGUS_ENTRY_SCHEME)) {
    // An actual relative spec!
    relativeSpec = relativeEntrySpec;
  }
  return rv;
}

////////////////////////////////////////////////////////////////////////////////
// nsIJARURI methods:

NS_IMETHODIMP
nsJARURI::GetJARFile(nsIURI** jarFile) { return GetInnerURI(jarFile); }

NS_IMETHODIMP
nsJARURI::GetJAREntry(nsACString& entryPath) {
  nsAutoCString filePath;
  mJAREntry->GetFilePath(filePath);
  NS_ASSERTION(filePath.Length() > 0, "path should never be empty!");
  // Trim off the leading '/'
  entryPath = Substring(filePath, 1, filePath.Length() - 1);
  return NS_OK;
}

nsresult nsJARURI::SetJAREntry(const nsACString& entryPath) {
  return CreateEntryURL(entryPath, mCharsetHint.get(),
                        getter_AddRefs(mJAREntry));
}

////////////////////////////////////////////////////////////////////////////////

NS_IMETHODIMP
nsJARURI::GetInnerURI(nsIURI** aURI) {
  nsCOMPtr<nsIURI> uri = mJARFile;
  uri.forget(aURI);
  return NS_OK;
}

NS_IMETHODIMP
nsJARURI::GetInnermostURI(nsIURI** uri) {
  return NS_ImplGetInnermostURI(this, uri);
}

void nsJARURI::Serialize(URIParams& aParams) {
  JARURIParams params;

  SerializeURI(mJARFile, params.jarFile());
  SerializeURI(mJAREntry, params.jarEntry());
  params.charset() = mCharsetHint;

  aParams = params;
}

bool nsJARURI::Deserialize(const URIParams& aParams) {
  if (aParams.type() != URIParams::TJARURIParams) {
    NS_ERROR("Received unknown parameters from the other process!");
    return false;
  }

  const JARURIParams& params = aParams.get_JARURIParams();

  nsCOMPtr<nsIURI> file = DeserializeURI(params.jarFile());
  if (!file) {
    NS_ERROR("Couldn't deserialize jar file URI!");
    return false;
  }

  nsCOMPtr<nsIURI> entry = DeserializeURI(params.jarEntry());
  if (!entry) {
    NS_ERROR("Couldn't deserialize jar entry URI!");
    return false;
  }

  nsCOMPtr<nsIURL> entryURL = do_QueryInterface(entry);
  if (!entryURL) {
    NS_ERROR("Couldn't QI jar entry URI to nsIURL!");
    return false;
  }

  mJARFile.swap(file);
  mJAREntry.swap(entryURL);
  mCharsetHint = params.charset();

  return true;
}