summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/viewsource/nsViewSourceChannel.cpp
blob: 3b5f95d5f39ccfb80bc643f414bab8941203c28a (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
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
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=4 sw=2 sts=2 et: */
/* 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 "nsViewSourceChannel.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/NullPrincipal.h"
#include "nsContentSecurityManager.h"
#include "nsContentUtils.h"
#include "nsHttpChannel.h"
#include "nsIExternalProtocolHandler.h"
#include "nsIHttpHeaderVisitor.h"
#include "nsIIOService.h"
#include "nsIInputStreamChannel.h"
#include "nsIReferrerInfo.h"
#include "nsMimeTypes.h"
#include "nsNetUtil.h"
#include "nsServiceManagerUtils.h"

NS_IMPL_ADDREF(nsViewSourceChannel)
NS_IMPL_RELEASE(nsViewSourceChannel)
/*
  This QI uses NS_INTERFACE_MAP_ENTRY_CONDITIONAL to check for
  non-nullness of mHttpChannel, mCachingChannel, and mUploadChannel.
*/
NS_INTERFACE_MAP_BEGIN(nsViewSourceChannel)
  NS_INTERFACE_MAP_ENTRY(nsIViewSourceChannel)
  NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
  NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
  NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
  NS_INTERFACE_MAP_ENTRY(nsIChannelEventSink)
  NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIHttpChannel, mHttpChannel)
  NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIIdentChannel, mHttpChannel)
  NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIHttpChannelInternal,
                                     mHttpChannelInternal)
  NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsICachingChannel, mCachingChannel)
  NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsICacheInfoChannel, mCacheInfoChannel)
  NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIUploadChannel, mUploadChannel)
  NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIFormPOSTActionChannel, mPostChannel)
  NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIChildChannel, mChildChannel)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsIRequest, nsIViewSourceChannel)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsIChannel, nsIViewSourceChannel)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIViewSourceChannel)
NS_INTERFACE_MAP_END

static nsresult WillUseExternalProtocolHandler(nsIIOService* aIOService,
                                               const char* aScheme) {
  nsCOMPtr<nsIProtocolHandler> handler;
  nsresult rv =
      aIOService->GetProtocolHandler(aScheme, getter_AddRefs(handler));
  if (NS_FAILED(rv)) {
    return rv;
  }

  nsCOMPtr<nsIExternalProtocolHandler> externalHandler =
      do_QueryInterface(handler);
  // We should not allow view-source to open any external app.
  if (externalHandler) {
    NS_WARNING(nsPrintfCString("blocking view-source:%s:", aScheme).get());
    return NS_ERROR_MALFORMED_URI;
  }

  return NS_OK;
}

nsresult nsViewSourceChannel::Init(nsIURI* uri, nsILoadInfo* aLoadInfo) {
  mOriginalURI = uri;

  nsAutoCString path;
  nsresult rv = uri->GetPathQueryRef(path);
  if (NS_FAILED(rv)) return rv;

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

  nsAutoCString scheme;
  rv = pService->ExtractScheme(path, scheme);
  if (NS_FAILED(rv)) return rv;

  // prevent viewing source of javascript URIs (see bug 204779)
  if (scheme.EqualsLiteral("javascript")) {
    NS_WARNING("blocking view-source:javascript:");
    return NS_ERROR_INVALID_ARG;
  }

  rv = WillUseExternalProtocolHandler(pService, scheme.get());
  if (NS_FAILED(rv)) {
    return rv;
  }

  nsCOMPtr<nsIURI> newChannelURI;
  rv = pService->NewURI(path, nullptr, nullptr, getter_AddRefs(newChannelURI));
  NS_ENSURE_SUCCESS(rv, rv);

  rv = pService->NewChannelFromURIWithLoadInfo(newChannelURI, aLoadInfo,
                                               getter_AddRefs(mChannel));
  NS_ENSURE_SUCCESS(rv, rv);

  mIsSrcdocChannel = false;

  mChannel->SetOriginalURI(mOriginalURI);
  UpdateChannelInterfaces();

  return NS_OK;
}

nsresult nsViewSourceChannel::InitSrcdoc(nsIURI* aURI, nsIURI* aBaseURI,
                                         const nsAString& aSrcdoc,
                                         nsILoadInfo* aLoadInfo) {
  nsresult rv;

  nsCOMPtr<nsIURI> inStreamURI;
  // Need to strip view-source: from the URI.  Hardcoded to
  // about:srcdoc as this is the only permissible URI for srcdoc
  // loads
  rv = NS_NewURI(getter_AddRefs(inStreamURI), u"about:srcdoc"_ns);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = NS_NewInputStreamChannelInternal(getter_AddRefs(mChannel), inStreamURI,
                                        aSrcdoc, "text/html"_ns, aLoadInfo,
                                        true);

  NS_ENSURE_SUCCESS(rv, rv);
  mOriginalURI = aURI;
  mIsSrcdocChannel = true;

  mChannel->SetOriginalURI(mOriginalURI);
  UpdateChannelInterfaces();

  rv = UpdateLoadInfoResultPrincipalURI();
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIInputStreamChannel> isc = do_QueryInterface(mChannel);
  MOZ_ASSERT(isc);
  isc->SetBaseURI(aBaseURI);
  return NS_OK;
}

void nsViewSourceChannel::UpdateChannelInterfaces() {
  mHttpChannel = do_QueryInterface(mChannel);
  mHttpChannelInternal = do_QueryInterface(mChannel);
  mCachingChannel = do_QueryInterface(mChannel);
  mCacheInfoChannel = do_QueryInterface(mChannel);
  mUploadChannel = do_QueryInterface(mChannel);
  mPostChannel = do_QueryInterface(mChannel);
  mChildChannel = do_QueryInterface(mChannel);
}

void nsViewSourceChannel::ReleaseListeners() {
  mListener = nullptr;
  mCallbacks = nullptr;
}

nsresult nsViewSourceChannel::UpdateLoadInfoResultPrincipalURI() {
  nsresult rv;

  MOZ_ASSERT(mChannel);

  nsCOMPtr<nsILoadInfo> channelLoadInfo = mChannel->LoadInfo();
  nsCOMPtr<nsIURI> channelResultPrincipalURI;
  rv = channelLoadInfo->GetResultPrincipalURI(
      getter_AddRefs(channelResultPrincipalURI));
  if (NS_FAILED(rv)) {
    return rv;
  }

  if (!channelResultPrincipalURI) {
    mChannel->GetOriginalURI(getter_AddRefs(channelResultPrincipalURI));
    return NS_OK;
  }

  if (!channelResultPrincipalURI) {
    return NS_ERROR_UNEXPECTED;
  }

  bool alreadyViewSource;
  if (NS_SUCCEEDED(channelResultPrincipalURI->SchemeIs("view-source",
                                                       &alreadyViewSource)) &&
      alreadyViewSource) {
    return NS_OK;
  }

  nsCOMPtr<nsIURI> updatedResultPrincipalURI;
  rv = BuildViewSourceURI(channelResultPrincipalURI,
                          getter_AddRefs(updatedResultPrincipalURI));
  if (NS_FAILED(rv)) {
    return rv;
  }

  rv = channelLoadInfo->SetResultPrincipalURI(updatedResultPrincipalURI);
  if (NS_FAILED(rv)) {
    return rv;
  }

  return NS_OK;
}

nsresult nsViewSourceChannel::BuildViewSourceURI(nsIURI* aURI,
                                                 nsIURI** aResult) {
  nsresult rv;

  // protect ourselves against broken channel implementations
  if (!aURI) {
    NS_ERROR("no URI to build view-source uri!");
    return NS_ERROR_UNEXPECTED;
  }

  nsAutoCString spec;
  rv = aURI->GetSpec(spec);
  if (NS_FAILED(rv)) {
    return rv;
  }

  return NS_NewURI(aResult, "view-source:"_ns + spec);
}

////////////////////////////////////////////////////////////////////////////////
// nsIRequest methods:

NS_IMETHODIMP
nsViewSourceChannel::GetName(nsACString& result) {
  nsCOMPtr<nsIURI> uri;
  GetURI(getter_AddRefs(uri));
  if (uri) {
    return uri->GetSpec(result);
  }
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
nsViewSourceChannel::GetTransferSize(uint64_t* aTransferSize) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
nsViewSourceChannel::GetRequestSize(uint64_t* aRequestSize) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
nsViewSourceChannel::GetDecodedBodySize(uint64_t* aDecodedBodySize) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
nsViewSourceChannel::GetEncodedBodySize(uint64_t* aEncodedBodySize) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
nsViewSourceChannel::IsPending(bool* result) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->IsPending(result);
}

NS_IMETHODIMP
nsViewSourceChannel::GetStatus(nsresult* status) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->GetStatus(status);
}

NS_IMETHODIMP nsViewSourceChannel::SetCanceledReason(
    const nsACString& aReason) {
  return nsIViewSourceChannel::SetCanceledReasonImpl(aReason);
}

NS_IMETHODIMP nsViewSourceChannel::GetCanceledReason(nsACString& aReason) {
  return nsIViewSourceChannel::GetCanceledReasonImpl(aReason);
}

NS_IMETHODIMP nsViewSourceChannel::CancelWithReason(nsresult aStatus,
                                                    const nsACString& aReason) {
  return nsIViewSourceChannel::CancelWithReasonImpl(aStatus, aReason);
}

NS_IMETHODIMP
nsViewSourceChannel::Cancel(nsresult status) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->Cancel(status);
}

NS_IMETHODIMP
nsViewSourceChannel::GetCanceled(bool* aCanceled) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->GetCanceled(aCanceled);
}

NS_IMETHODIMP
nsViewSourceChannel::Suspend(void) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->Suspend();
}

NS_IMETHODIMP
nsViewSourceChannel::Resume(void) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->Resume();
}

////////////////////////////////////////////////////////////////////////////////
// nsIChannel methods:

NS_IMETHODIMP
nsViewSourceChannel::GetOriginalURI(nsIURI** aURI) {
  *aURI = do_AddRef(mOriginalURI).take();
  return NS_OK;
}

NS_IMETHODIMP
nsViewSourceChannel::SetOriginalURI(nsIURI* aURI) {
  NS_ENSURE_ARG_POINTER(aURI);
  mOriginalURI = aURI;
  return NS_OK;
}

NS_IMETHODIMP
nsViewSourceChannel::GetURI(nsIURI** aURI) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  nsCOMPtr<nsIURI> uri;
  nsresult rv = mChannel->GetURI(getter_AddRefs(uri));
  if (NS_FAILED(rv)) {
    return rv;
  }

  return BuildViewSourceURI(uri, aURI);
}

NS_IMETHODIMP
nsViewSourceChannel::Open(nsIInputStream** aStream) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);
  return Open(aStream);
}

NS_IMETHODIMP
nsViewSourceChannel::AsyncOpen(nsIStreamListener* aListener) {
  // We can't ensure GetInitialSecurityCheckDone here

  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  mListener = aListener;

  /*
   * We want to add ourselves to the loadgroup before opening
   * mChannel, since we want to make sure we're in the loadgroup
   * when mChannel finishes and fires OnStopRequest()
   */

  nsCOMPtr<nsILoadGroup> loadGroup;
  mChannel->GetLoadGroup(getter_AddRefs(loadGroup));
  if (loadGroup) {
    loadGroup->AddRequest(static_cast<nsIViewSourceChannel*>(this), nullptr);
  }

  nsresult rv = NS_OK;
  rv = mChannel->AsyncOpen(this);

  if (NS_FAILED(rv) && loadGroup) {
    loadGroup->RemoveRequest(static_cast<nsIViewSourceChannel*>(this), nullptr,
                             rv);
  }

  if (NS_SUCCEEDED(rv)) {
    // We do this here to make sure all notification callbacks changes have been
    // made first before we inject this view-source channel.
    mChannel->GetNotificationCallbacks(getter_AddRefs(mCallbacks));
    mChannel->SetNotificationCallbacks(this);

    MOZ_ASSERT(mCallbacks != this, "We have a cycle");

    mOpened = true;
  }

  if (NS_FAILED(rv)) {
    ReleaseListeners();
  }

  return rv;
}

/*
 * Both the view source channel and mChannel are added to the
 * loadgroup.  There should never be more than one request in the
 * loadgroup that has LOAD_DOCUMENT_URI set.  The one that has this
 * flag set is the request whose URI is used to refetch the document,
 * so it better be the viewsource channel.
 *
 * Therefore, we need to make sure that
 * 1) The load flags on mChannel _never_ include LOAD_DOCUMENT_URI
 * 2) The load flags on |this| include LOAD_DOCUMENT_URI when it was
 *    set via SetLoadFlags (mIsDocument keeps track of this flag).
 */

NS_IMETHODIMP
nsViewSourceChannel::GetLoadFlags(uint32_t* aLoadFlags) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  nsresult rv = mChannel->GetLoadFlags(aLoadFlags);
  if (NS_FAILED(rv)) return rv;

  // This should actually be just LOAD_DOCUMENT_URI but the win32 compiler
  // fails to deal due to amiguous inheritance.  nsIChannel::LOAD_DOCUMENT_URI
  // also fails; the Win32 compiler thinks that's supposed to be a method.
  if (mIsDocument) *aLoadFlags |= ::nsIChannel::LOAD_DOCUMENT_URI;

  return rv;
}

NS_IMETHODIMP
nsViewSourceChannel::SetLoadFlags(uint32_t aLoadFlags) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  // "View source" always wants the currently cached content.
  // We also want to have _this_ channel, not mChannel to be the
  // 'document' channel in the loadgroup.

  // These should actually be just LOAD_FROM_CACHE and LOAD_DOCUMENT_URI but
  // the win32 compiler fails to deal due to amiguous inheritance.
  // nsIChannel::LOAD_DOCUMENT_URI/nsIRequest::LOAD_FROM_CACHE also fails; the
  // Win32 compiler thinks that's supposed to be a method.
  mIsDocument = (aLoadFlags & ::nsIChannel::LOAD_DOCUMENT_URI) != 0;

  nsresult rv =
      mChannel->SetLoadFlags((aLoadFlags | ::nsIRequest::LOAD_FROM_CACHE) &
                             ~::nsIChannel::LOAD_DOCUMENT_URI);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  if (mHttpChannel) {
    rv = mHttpChannel->SetIsMainDocumentChannel(
        aLoadFlags & ::nsIChannel::LOAD_DOCUMENT_URI);
    MOZ_ASSERT(NS_SUCCEEDED(rv));
  }

  return NS_OK;
}

NS_IMETHODIMP
nsViewSourceChannel::GetTRRMode(nsIRequest::TRRMode* aTRRMode) {
  return nsIViewSourceChannel::GetTRRModeImpl(aTRRMode);
}

NS_IMETHODIMP
nsViewSourceChannel::SetTRRMode(nsIRequest::TRRMode aTRRMode) {
  return nsIViewSourceChannel::SetTRRModeImpl(aTRRMode);
}

NS_IMETHODIMP
nsViewSourceChannel::GetContentType(nsACString& aContentType) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  aContentType.Truncate();

  if (mContentType.IsEmpty()) {
    // Get the current content type
    nsresult rv;
    nsAutoCString contentType;
    rv = mChannel->GetContentType(contentType);
    if (NS_FAILED(rv)) return rv;

    // If we don't know our type, just say so.  The unknown
    // content decoder will then kick in automatically, and it
    // will call our SetOriginalContentType method instead of our
    // SetContentType method to set the type it determines.
    if (!contentType.EqualsLiteral(UNKNOWN_CONTENT_TYPE) &&
        !contentType.IsEmpty()) {
      contentType = VIEWSOURCE_CONTENT_TYPE;
    }

    mContentType = contentType;
  }

  aContentType = mContentType;
  return NS_OK;
}

NS_IMETHODIMP
nsViewSourceChannel::SetContentType(const nsACString& aContentType) {
  // Our GetContentType() currently returns VIEWSOURCE_CONTENT_TYPE
  //
  // However, during the parsing phase the parser calls our
  // channel's GetContentType(). Returning the string above trips up
  // the parser. In order to avoid messy changes and not to have the
  // parser depend on nsIViewSourceChannel Vidur proposed the
  // following solution:
  //
  // The ViewSourceChannel initially returns a content type of
  // VIEWSOURCE_CONTENT_TYPE.  Based on this type decisions to
  // create a viewer for doing a view source are made.  After the
  // viewer is created, nsLayoutDLF::CreateInstance() calls this
  // SetContentType() with the original content type.  When it's
  // time for the parser to find out the content type it will call
  // our channel's GetContentType() and it will get the original
  // content type, such as, text/html and everything is kosher from
  // then on.

  if (!mOpened) {
    // We do not take hints
    return NS_ERROR_NOT_AVAILABLE;
  }

  mContentType = aContentType;
  return NS_OK;
}

NS_IMETHODIMP
nsViewSourceChannel::GetContentCharset(nsACString& aContentCharset) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->GetContentCharset(aContentCharset);
}

NS_IMETHODIMP
nsViewSourceChannel::SetContentCharset(const nsACString& aContentCharset) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->SetContentCharset(aContentCharset);
}

// We don't forward these methods becacuse content-disposition isn't whitelisted
// (see GetResponseHeader/VisitResponseHeaders).
NS_IMETHODIMP
nsViewSourceChannel::GetContentDisposition(uint32_t* aContentDisposition) {
  return NS_ERROR_NOT_AVAILABLE;
}

NS_IMETHODIMP
nsViewSourceChannel::SetContentDisposition(uint32_t aContentDisposition) {
  return NS_ERROR_NOT_AVAILABLE;
}

NS_IMETHODIMP
nsViewSourceChannel::GetContentDispositionFilename(
    nsAString& aContentDispositionFilename) {
  return NS_ERROR_NOT_AVAILABLE;
}

NS_IMETHODIMP
nsViewSourceChannel::SetContentDispositionFilename(
    const nsAString& aContentDispositionFilename) {
  return NS_ERROR_NOT_AVAILABLE;
}

NS_IMETHODIMP
nsViewSourceChannel::GetContentDispositionHeader(
    nsACString& aContentDispositionHeader) {
  return NS_ERROR_NOT_AVAILABLE;
}

NS_IMETHODIMP
nsViewSourceChannel::GetContentLength(int64_t* aContentLength) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->GetContentLength(aContentLength);
}

NS_IMETHODIMP
nsViewSourceChannel::SetContentLength(int64_t aContentLength) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->SetContentLength(aContentLength);
}

NS_IMETHODIMP
nsViewSourceChannel::GetLoadGroup(nsILoadGroup** aLoadGroup) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->GetLoadGroup(aLoadGroup);
}

NS_IMETHODIMP
nsViewSourceChannel::SetLoadGroup(nsILoadGroup* aLoadGroup) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->SetLoadGroup(aLoadGroup);
}

NS_IMETHODIMP
nsViewSourceChannel::GetOwner(nsISupports** aOwner) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->GetOwner(aOwner);
}

NS_IMETHODIMP
nsViewSourceChannel::SetOwner(nsISupports* aOwner) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->SetOwner(aOwner);
}

NS_IMETHODIMP
nsViewSourceChannel::GetLoadInfo(nsILoadInfo** aLoadInfo) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->GetLoadInfo(aLoadInfo);
}

NS_IMETHODIMP
nsViewSourceChannel::SetLoadInfo(nsILoadInfo* aLoadInfo) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);
  MOZ_RELEASE_ASSERT(aLoadInfo, "loadinfo can't be null");
  return mChannel->SetLoadInfo(aLoadInfo);
}

NS_IMETHODIMP
nsViewSourceChannel::GetIsDocument(bool* aIsDocument) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->GetIsDocument(aIsDocument);
}

NS_IMETHODIMP
nsViewSourceChannel::GetNotificationCallbacks(
    nsIInterfaceRequestor** aNotificationCallbacks) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->GetNotificationCallbacks(aNotificationCallbacks);
}

NS_IMETHODIMP
nsViewSourceChannel::SetNotificationCallbacks(
    nsIInterfaceRequestor* aNotificationCallbacks) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->SetNotificationCallbacks(aNotificationCallbacks);
}

NS_IMETHODIMP
nsViewSourceChannel::GetSecurityInfo(nsITransportSecurityInfo** aSecurityInfo) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->GetSecurityInfo(aSecurityInfo);
}

// nsIViewSourceChannel methods
NS_IMETHODIMP
nsViewSourceChannel::GetOriginalContentType(nsACString& aContentType) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  return mChannel->GetContentType(aContentType);
}

NS_IMETHODIMP
nsViewSourceChannel::SetOriginalContentType(const nsACString& aContentType) {
  NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE);

  // clear our cached content-type value
  mContentType.Truncate();

  return mChannel->SetContentType(aContentType);
}

NS_IMETHODIMP
nsViewSourceChannel::GetIsSrcdocChannel(bool* aIsSrcdocChannel) {
  *aIsSrcdocChannel = mIsSrcdocChannel;
  return NS_OK;
}

NS_IMETHODIMP
nsViewSourceChannel::GetBaseURI(nsIURI** aBaseURI) {
  if (mIsSrcdocChannel) {
    nsCOMPtr<nsIInputStreamChannel> isc = do_QueryInterface(mChannel);
    if (isc) {
      return isc->GetBaseURI(aBaseURI);
    }
  }
  *aBaseURI = do_AddRef(mBaseURI).take();
  return NS_OK;
}

NS_IMETHODIMP
nsViewSourceChannel::SetBaseURI(nsIURI* aBaseURI) {
  mBaseURI = aBaseURI;
  return NS_OK;
}

nsIChannel* nsViewSourceChannel::GetInnerChannel() { return mChannel; }

NS_IMETHODIMP
nsViewSourceChannel::GetProtocolVersion(nsACString& aProtocolVersion) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

// nsIRequestObserver methods
NS_IMETHODIMP
nsViewSourceChannel::OnStartRequest(nsIRequest* aRequest) {
  NS_ENSURE_TRUE(mListener, NS_ERROR_FAILURE);
  // The channel may have gotten redirected... Time to update our info
  mChannel = do_QueryInterface(aRequest);
  UpdateChannelInterfaces();

  nsresult rv = UpdateLoadInfoResultPrincipalURI();
  if (NS_FAILED(rv)) {
    Cancel(rv);
  }

  return mListener->OnStartRequest(static_cast<nsIViewSourceChannel*>(this));
}

NS_IMETHODIMP
nsViewSourceChannel::OnStopRequest(nsIRequest* aRequest, nsresult aStatus) {
  NS_ENSURE_TRUE(mListener, NS_ERROR_FAILURE);
  if (mChannel) {
    nsCOMPtr<nsILoadGroup> loadGroup;
    mChannel->GetLoadGroup(getter_AddRefs(loadGroup));
    if (loadGroup) {
      loadGroup->RemoveRequest(static_cast<nsIViewSourceChannel*>(this),
                               nullptr, aStatus);
    }
  }

  nsresult rv = mListener->OnStopRequest(
      static_cast<nsIViewSourceChannel*>(this), aStatus);

  ReleaseListeners();

  return rv;
}

// nsIStreamListener methods
NS_IMETHODIMP
nsViewSourceChannel::OnDataAvailable(nsIRequest* aRequest,
                                     nsIInputStream* aInputStream,
                                     uint64_t aSourceOffset, uint32_t aLength) {
  NS_ENSURE_TRUE(mListener, NS_ERROR_FAILURE);
  return mListener->OnDataAvailable(static_cast<nsIViewSourceChannel*>(this),
                                    aInputStream, aSourceOffset, aLength);
}

// nsIHttpChannel methods

// We want to forward most of nsIHttpChannel over to mHttpChannel, but we want
// to override GetRequestHeader and VisitHeaders. The reason is that we don't
// want various headers like Link: and Refresh: applying to view-source.
NS_IMETHODIMP
nsViewSourceChannel::GetChannelId(uint64_t* aChannelId) {
  NS_ENSURE_ARG_POINTER(aChannelId);
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->GetChannelId(aChannelId);
}

NS_IMETHODIMP
nsViewSourceChannel::SetChannelId(uint64_t aChannelId) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->SetChannelId(aChannelId);
}

NS_IMETHODIMP
nsViewSourceChannel::GetTopLevelContentWindowId(uint64_t* aWindowId) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->GetTopLevelContentWindowId(aWindowId);
}

NS_IMETHODIMP
nsViewSourceChannel::SetTopLevelContentWindowId(uint64_t aWindowId) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->SetTopLevelContentWindowId(aWindowId);
}

NS_IMETHODIMP
nsViewSourceChannel::GetBrowserId(uint64_t* aId) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->GetBrowserId(aId);
}

NS_IMETHODIMP
nsViewSourceChannel::SetBrowserId(uint64_t aId) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->SetBrowserId(aId);
}

NS_IMETHODIMP
nsViewSourceChannel::GetRequestMethod(nsACString& aRequestMethod) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->GetRequestMethod(aRequestMethod);
}

NS_IMETHODIMP
nsViewSourceChannel::SetRequestMethod(const nsACString& aRequestMethod) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->SetRequestMethod(aRequestMethod);
}

NS_IMETHODIMP
nsViewSourceChannel::GetReferrerInfo(nsIReferrerInfo** aReferrerInfo) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->GetReferrerInfo(aReferrerInfo);
}

NS_IMETHODIMP
nsViewSourceChannel::SetReferrerInfo(nsIReferrerInfo* aReferrerInfo) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->SetReferrerInfo(aReferrerInfo);
}

NS_IMETHODIMP nsViewSourceChannel::SetReferrerInfoWithoutClone(
    nsIReferrerInfo* aReferrerInfo) {
  return !mHttpChannel
             ? NS_ERROR_NULL_POINTER
             : mHttpChannel->SetReferrerInfoWithoutClone(aReferrerInfo);
}

NS_IMETHODIMP
nsViewSourceChannel::GetRequestHeader(const nsACString& aHeader,
                                      nsACString& aValue) {
  aValue.Truncate();
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->GetRequestHeader(aHeader, aValue);
}

NS_IMETHODIMP
nsViewSourceChannel::SetRequestHeader(const nsACString& aHeader,
                                      const nsACString& aValue, bool aMerge) {
  return !mHttpChannel
             ? NS_ERROR_NULL_POINTER
             : mHttpChannel->SetRequestHeader(aHeader, aValue, aMerge);
}

NS_IMETHODIMP
nsViewSourceChannel::SetNewReferrerInfo(
    const nsACString& aUrl, nsIReferrerInfo::ReferrerPolicyIDL aPolicy,
    bool aSendReferrer) {
  return !mHttpChannel
             ? NS_ERROR_NULL_POINTER
             : mHttpChannel->SetNewReferrerInfo(aUrl, aPolicy, aSendReferrer);
}

NS_IMETHODIMP
nsViewSourceChannel::SetEmptyRequestHeader(const nsACString& aHeader) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->SetEmptyRequestHeader(aHeader);
}

NS_IMETHODIMP
nsViewSourceChannel::VisitRequestHeaders(nsIHttpHeaderVisitor* aVisitor) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->VisitRequestHeaders(aVisitor);
}

NS_IMETHODIMP
nsViewSourceChannel::VisitNonDefaultRequestHeaders(
    nsIHttpHeaderVisitor* aVisitor) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->VisitNonDefaultRequestHeaders(aVisitor);
}

NS_IMETHODIMP
nsViewSourceChannel::ShouldStripRequestBodyHeader(const nsACString& aMethod,
                                                  bool* aResult) {
  return !mHttpChannel
             ? NS_ERROR_NULL_POINTER
             : mHttpChannel->ShouldStripRequestBodyHeader(aMethod, aResult);
}

NS_IMETHODIMP
nsViewSourceChannel::GetAllowSTS(bool* aAllowSTS) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->GetAllowSTS(aAllowSTS);
}

NS_IMETHODIMP
nsViewSourceChannel::SetAllowSTS(bool aAllowSTS) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->SetAllowSTS(aAllowSTS);
}

NS_IMETHODIMP
nsViewSourceChannel::GetRedirectionLimit(uint32_t* aRedirectionLimit) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->GetRedirectionLimit(aRedirectionLimit);
}

NS_IMETHODIMP
nsViewSourceChannel::SetRedirectionLimit(uint32_t aRedirectionLimit) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->SetRedirectionLimit(aRedirectionLimit);
}

NS_IMETHODIMP
nsViewSourceChannel::GetResponseStatus(uint32_t* aResponseStatus) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->GetResponseStatus(aResponseStatus);
}

NS_IMETHODIMP
nsViewSourceChannel::GetResponseStatusText(nsACString& aResponseStatusText) {
  return !mHttpChannel
             ? NS_ERROR_NULL_POINTER
             : mHttpChannel->GetResponseStatusText(aResponseStatusText);
}

NS_IMETHODIMP
nsViewSourceChannel::GetRequestSucceeded(bool* aRequestSucceeded) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->GetRequestSucceeded(aRequestSucceeded);
}

NS_IMETHODIMP
nsViewSourceChannel::GetResponseHeader(const nsACString& aHeader,
                                       nsACString& aValue) {
  aValue.Truncate();
  if (!mHttpChannel) return NS_ERROR_NULL_POINTER;

  if (!aHeader.Equals("Content-Type"_ns, nsCaseInsensitiveCStringComparator) &&
      !aHeader.Equals("Content-Security-Policy"_ns,
                      nsCaseInsensitiveCStringComparator) &&
      !aHeader.Equals("Content-Security-Policy-Report-Only"_ns,
                      nsCaseInsensitiveCStringComparator) &&
      !aHeader.Equals("X-Frame-Options"_ns,
                      nsCaseInsensitiveCStringComparator)) {
    // We simulate the NS_ERROR_NOT_AVAILABLE error which is produced by
    // GetResponseHeader via nsHttpHeaderArray::GetHeader when the entry is
    // not present, such that it appears as though no headers except for the
    // whitelisted ones were set on this channel.
    return NS_ERROR_NOT_AVAILABLE;
  }

  return mHttpChannel->GetResponseHeader(aHeader, aValue);
}

NS_IMETHODIMP
nsViewSourceChannel::SetResponseHeader(const nsACString& header,
                                       const nsACString& value, bool merge) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->SetResponseHeader(header, value, merge);
}

NS_IMETHODIMP
nsViewSourceChannel::VisitResponseHeaders(nsIHttpHeaderVisitor* aVisitor) {
  if (!mHttpChannel) return NS_ERROR_NULL_POINTER;

  constexpr auto contentTypeStr = "Content-Type"_ns;
  nsAutoCString contentType;
  nsresult rv = mHttpChannel->GetResponseHeader(contentTypeStr, contentType);
  if (NS_SUCCEEDED(rv)) {
    return aVisitor->VisitHeader(contentTypeStr, contentType);
  }
  return NS_OK;
}

NS_IMETHODIMP
nsViewSourceChannel::GetOriginalResponseHeader(const nsACString& aHeader,
                                               nsIHttpHeaderVisitor* aVisitor) {
  nsAutoCString value;
  nsresult rv = GetResponseHeader(aHeader, value);
  if (NS_FAILED(rv)) {
    return rv;
  }
  return aVisitor->VisitHeader(aHeader, value);
}

NS_IMETHODIMP
nsViewSourceChannel::VisitOriginalResponseHeaders(
    nsIHttpHeaderVisitor* aVisitor) {
  return VisitResponseHeaders(aVisitor);
}

NS_IMETHODIMP
nsViewSourceChannel::IsNoStoreResponse(bool* _retval) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->IsNoStoreResponse(_retval);
}

NS_IMETHODIMP
nsViewSourceChannel::IsNoCacheResponse(bool* _retval) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->IsNoCacheResponse(_retval);
}

NS_IMETHODIMP
nsViewSourceChannel::IsPrivateResponse(bool* _retval) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->IsPrivateResponse(_retval);
}

NS_IMETHODIMP
nsViewSourceChannel::RedirectTo(nsIURI* uri) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER : mHttpChannel->RedirectTo(uri);
}

NS_IMETHODIMP
nsViewSourceChannel::UpgradeToSecure() {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->UpgradeToSecure();
}

NS_IMETHODIMP
nsViewSourceChannel::GetRequestContextID(uint64_t* _retval) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->GetRequestContextID(_retval);
}

NS_IMETHODIMP
nsViewSourceChannel::SetRequestContextID(uint64_t rcid) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->SetRequestContextID(rcid);
}

NS_IMETHODIMP
nsViewSourceChannel::GetIsMainDocumentChannel(bool* aValue) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->GetIsMainDocumentChannel(aValue);
}

NS_IMETHODIMP
nsViewSourceChannel::SetIsMainDocumentChannel(bool aValue) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->SetIsMainDocumentChannel(aValue);
}

NS_IMETHODIMP nsViewSourceChannel::SetClassicScriptHintCharset(
    const nsAString& aClassicScriptHintCharset) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->SetClassicScriptHintCharset(
                             aClassicScriptHintCharset);
}

NS_IMETHODIMP nsViewSourceChannel::GetClassicScriptHintCharset(
    nsAString& aClassicScriptHintCharset) {
  return !mHttpChannel ? NS_ERROR_NULL_POINTER
                       : mHttpChannel->GetClassicScriptHintCharset(
                             aClassicScriptHintCharset);
}

NS_IMETHODIMP nsViewSourceChannel::SetDocumentCharacterSet(
    const nsAString& aDocumentCharacterSet) {
  return !mHttpChannel
             ? NS_ERROR_NULL_POINTER
             : mHttpChannel->SetDocumentCharacterSet(aDocumentCharacterSet);
}

NS_IMETHODIMP nsViewSourceChannel::GetDocumentCharacterSet(
    nsAString& aDocumentCharacterSet) {
  return !mHttpChannel
             ? NS_ERROR_NULL_POINTER
             : mHttpChannel->GetDocumentCharacterSet(aDocumentCharacterSet);
}
// Have to manually forward SetCorsPreflightParameters since it's [notxpcom]
void nsViewSourceChannel::SetCorsPreflightParameters(
    const nsTArray<nsCString>& aUnsafeHeaders,
    bool aShouldStripRequestBodyHeader, bool aShouldStripAuthHeader) {
  mHttpChannelInternal->SetCorsPreflightParameters(
      aUnsafeHeaders, aShouldStripRequestBodyHeader, aShouldStripAuthHeader);
}

void nsViewSourceChannel::SetAltDataForChild(bool aIsForChild) {
  mHttpChannelInternal->SetAltDataForChild(aIsForChild);
}

void nsViewSourceChannel::DisableAltDataCache() {
  mHttpChannelInternal->DisableAltDataCache();
}

NS_IMETHODIMP
nsViewSourceChannel::LogBlockedCORSRequest(const nsAString& aMessage,
                                           const nsACString& aCategory,
                                           bool aIsWarning) {
  if (!mHttpChannel) {
    NS_WARNING(
        "nsViewSourceChannel::LogBlockedCORSRequest mHttpChannel is null");
    return NS_ERROR_UNEXPECTED;
  }
  return mHttpChannel->LogBlockedCORSRequest(aMessage, aCategory, aIsWarning);
}

NS_IMETHODIMP
nsViewSourceChannel::LogMimeTypeMismatch(const nsACString& aMessageName,
                                         bool aWarning, const nsAString& aURL,
                                         const nsAString& aContentType) {
  if (!mHttpChannel) {
    NS_WARNING("nsViewSourceChannel::LogMimeTypeMismatch mHttpChannel is null");
    return NS_ERROR_UNEXPECTED;
  }
  return mHttpChannel->LogMimeTypeMismatch(aMessageName, aWarning, aURL,
                                           aContentType);
}

// FIXME: Should this forward to mHttpChannel? This was previously handled by a
// default empty implementation.
void nsViewSourceChannel::SetSource(
    mozilla::UniquePtr<mozilla::ProfileChunkedBuffer> aSource) {}

const nsTArray<mozilla::net::PreferredAlternativeDataTypeParams>&
nsViewSourceChannel::PreferredAlternativeDataTypes() {
  if (mCacheInfoChannel) {
    return mCacheInfoChannel->PreferredAlternativeDataTypes();
  }
  return mEmptyArray;
}

void nsViewSourceChannel::DoDiagnosticAssertWhenOnStopNotCalledOnDestroy() {
  if (mHttpChannelInternal) {
    mHttpChannelInternal->DoDiagnosticAssertWhenOnStopNotCalledOnDestroy();
  }
}

// FIXME: Should this forward to mHttpChannelInternal? This was previously
// handled by a default empty implementation.
void nsViewSourceChannel::SetConnectionInfo(
    mozilla::net::nsHttpConnectionInfo* aInfo) {}

// nsIChildChannel methods

NS_IMETHODIMP
nsViewSourceChannel::ConnectParent(uint32_t aRegistarId) {
  NS_ENSURE_TRUE(mChildChannel, NS_ERROR_NULL_POINTER);

  return mChildChannel->ConnectParent(aRegistarId);
}

NS_IMETHODIMP
nsViewSourceChannel::CompleteRedirectSetup(nsIStreamListener* aListener) {
  NS_ENSURE_TRUE(mChildChannel, NS_ERROR_NULL_POINTER);

  mListener = aListener;

  /*
   * We want to add ourselves to the loadgroup before opening
   * mChannel, since we want to make sure we're in the loadgroup
   * when mChannel finishes and fires OnStopRequest()
   */

  nsCOMPtr<nsILoadGroup> loadGroup;
  mChannel->GetLoadGroup(getter_AddRefs(loadGroup));
  if (loadGroup) {
    loadGroup->AddRequest(static_cast<nsIViewSourceChannel*>(this), nullptr);
  }

  nsresult rv = NS_OK;
  rv = mChildChannel->CompleteRedirectSetup(this);

  if (NS_FAILED(rv) && loadGroup) {
    loadGroup->RemoveRequest(static_cast<nsIViewSourceChannel*>(this), nullptr,
                             rv);
  }

  if (NS_SUCCEEDED(rv)) {
    mOpened = true;
  }

  return rv;
}

// nsIChannelEventSink

NS_IMETHODIMP
nsViewSourceChannel::AsyncOnChannelRedirect(
    nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
    nsIAsyncVerifyRedirectCallback* callback) {
  nsresult rv;

  // We want consumers of the new inner channel be able to recognize it's
  // actually used for view-source.  Hence we modify its original URI here.
  // This will not affect security checks because those have already been
  // synchronously done before our event sink is called.  Note that result
  // principal URI is still modified at the OnStartRequest notification.
  nsCOMPtr<nsIURI> newChannelOrigURI;
  rv = newChannel->GetOriginalURI(getter_AddRefs(newChannelOrigURI));
  NS_ENSURE_SUCCESS(rv, rv);

  nsAutoCString scheme;
  rv = newChannelOrigURI->GetScheme(scheme);
  if (NS_FAILED(rv)) {
    return rv;
  }

  nsCOMPtr<nsIIOService> ioService(do_GetIOService(&rv));
  if (NS_FAILED(rv)) {
    return rv;
  }

  rv = WillUseExternalProtocolHandler(ioService, scheme.get());
  if (NS_FAILED(rv)) {
    return rv;
  }

  nsCOMPtr<nsIURI> newChannelUpdatedOrigURI;
  rv = BuildViewSourceURI(newChannelOrigURI,
                          getter_AddRefs(newChannelUpdatedOrigURI));
  NS_ENSURE_SUCCESS(rv, rv);

  rv = newChannel->SetOriginalURI(newChannelUpdatedOrigURI);
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIChannelEventSink> sink(do_QueryInterface(mCallbacks));
  if (sink) {
    return sink->AsyncOnChannelRedirect(oldChannel, newChannel, flags,
                                        callback);
  }

  callback->OnRedirectVerifyCallback(NS_OK);
  return NS_OK;
}

// nsIInterfaceRequestor

NS_IMETHODIMP
nsViewSourceChannel::GetInterface(const nsIID& aIID, void** aResult) {
  if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
    nsCOMPtr<nsIChannelEventSink> self(this);
    self.forget(aResult);
    return NS_OK;
  }

  if (mCallbacks) {
    return mCallbacks->GetInterface(aIID, aResult);
  }

  return NS_ERROR_NO_INTERFACE;
}