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
|
/* -*- Mode: C++; tab-width: 2; 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 "nsWyciwyg.h"
#include "nsWyciwygChannel.h"
#include "nsILoadGroup.h"
#include "nsNetUtil.h"
#include "nsNetCID.h"
#include "LoadContextInfo.h"
#include "nsICacheService.h" // only to initialize
#include "nsICacheStorageService.h"
#include "nsICacheStorage.h"
#include "nsICacheEntry.h"
#include "nsCharsetSource.h"
#include "nsProxyRelease.h"
#include "nsThreadUtils.h"
#include "nsIInputStream.h"
#include "nsIInputStreamPump.h"
#include "nsIOutputStream.h"
#include "nsIProgressEventSink.h"
#include "nsIURI.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Unused.h"
#include "mozilla/BasePrincipal.h"
#include "nsProxyRelease.h"
#include "nsContentSecurityManager.h"
#include "nsContentUtils.h"
typedef mozilla::net::LoadContextInfo LoadContextInfo;
// nsWyciwygChannel methods
nsWyciwygChannel::nsWyciwygChannel()
: mMode(NONE),
mStatus(NS_OK),
mIsPending(false),
mNeedToWriteCharset(false),
mCharsetSource(kCharsetUninitialized),
mContentLength(-1),
mLoadFlags(LOAD_NORMAL),
mNeedToSetSecurityInfo(false)
{
}
nsWyciwygChannel::~nsWyciwygChannel()
{
if (mLoadInfo) {
NS_ReleaseOnMainThread(mLoadInfo.forget(), false);
}
}
NS_IMPL_ISUPPORTS(nsWyciwygChannel,
nsIChannel,
nsIRequest,
nsIStreamListener,
nsIRequestObserver,
nsICacheEntryOpenCallback,
nsIWyciwygChannel,
nsIPrivateBrowsingChannel)
nsresult
nsWyciwygChannel::Init(nsIURI* uri)
{
NS_ENSURE_ARG_POINTER(uri);
mURI = uri;
mOriginalURI = uri;
return NS_OK;
}
///////////////////////////////////////////////////////////////////////////////
// nsIRequest methods:
///////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP
nsWyciwygChannel::GetName(nsACString &aName)
{
return mURI->GetSpec(aName);
}
NS_IMETHODIMP
nsWyciwygChannel::IsPending(bool *aIsPending)
{
*aIsPending = mIsPending;
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::GetStatus(nsresult *aStatus)
{
if (NS_SUCCEEDED(mStatus) && mPump)
mPump->GetStatus(aStatus);
else
*aStatus = mStatus;
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::Cancel(nsresult status)
{
mStatus = status;
if (mPump)
mPump->Cancel(status);
// else we're waiting for OnCacheEntryAvailable
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::Suspend()
{
if (mPump)
mPump->Suspend();
// XXX else, we'll ignore this ... and that's probably bad!
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::Resume()
{
if (mPump)
mPump->Resume();
// XXX else, we'll ignore this ... and that's probably bad!
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::GetLoadGroup(nsILoadGroup* *aLoadGroup)
{
*aLoadGroup = mLoadGroup;
NS_IF_ADDREF(*aLoadGroup);
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::SetLoadGroup(nsILoadGroup* aLoadGroup)
{
if (!CanSetLoadGroup(aLoadGroup)) {
return NS_ERROR_FAILURE;
}
mLoadGroup = aLoadGroup;
NS_QueryNotificationCallbacks(mCallbacks,
mLoadGroup,
NS_GET_IID(nsIProgressEventSink),
getter_AddRefs(mProgressSink));
UpdatePrivateBrowsing();
NS_GetOriginAttributes(this, mOriginAttributes);
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::SetLoadFlags(uint32_t aLoadFlags)
{
mLoadFlags = aLoadFlags;
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::GetLoadFlags(uint32_t * aLoadFlags)
{
*aLoadFlags = mLoadFlags;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// nsIChannel methods:
///////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP
nsWyciwygChannel::GetOriginalURI(nsIURI* *aURI)
{
*aURI = mOriginalURI;
NS_ADDREF(*aURI);
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::SetOriginalURI(nsIURI* aURI)
{
NS_ENSURE_ARG_POINTER(aURI);
mOriginalURI = aURI;
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::GetURI(nsIURI* *aURI)
{
*aURI = mURI;
NS_IF_ADDREF(*aURI);
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::GetOwner(nsISupports **aOwner)
{
NS_IF_ADDREF(*aOwner = mOwner);
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::SetOwner(nsISupports* aOwner)
{
mOwner = aOwner;
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::GetLoadInfo(nsILoadInfo **aLoadInfo)
{
NS_IF_ADDREF(*aLoadInfo = mLoadInfo);
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::SetLoadInfo(nsILoadInfo* aLoadInfo)
{
mLoadInfo = aLoadInfo;
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::GetNotificationCallbacks(nsIInterfaceRequestor* *aCallbacks)
{
*aCallbacks = mCallbacks.get();
NS_IF_ADDREF(*aCallbacks);
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::SetNotificationCallbacks(nsIInterfaceRequestor* aNotificationCallbacks)
{
if (!CanSetCallbacks(aNotificationCallbacks)) {
return NS_ERROR_FAILURE;
}
mCallbacks = aNotificationCallbacks;
NS_QueryNotificationCallbacks(mCallbacks,
mLoadGroup,
NS_GET_IID(nsIProgressEventSink),
getter_AddRefs(mProgressSink));
UpdatePrivateBrowsing();
NS_GetOriginAttributes(this, mOriginAttributes);
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::GetSecurityInfo(nsISupports * *aSecurityInfo)
{
NS_IF_ADDREF(*aSecurityInfo = mSecurityInfo);
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::GetContentType(nsACString &aContentType)
{
aContentType.AssignLiteral(WYCIWYG_TYPE);
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::SetContentType(const nsACString &aContentType)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsWyciwygChannel::GetContentCharset(nsACString &aContentCharset)
{
aContentCharset.AssignLiteral("UTF-16");
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::SetContentCharset(const nsACString &aContentCharset)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsWyciwygChannel::GetContentDisposition(uint32_t *aContentDisposition)
{
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsWyciwygChannel::SetContentDisposition(uint32_t aContentDisposition)
{
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsWyciwygChannel::GetContentDispositionFilename(nsAString &aContentDispositionFilename)
{
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsWyciwygChannel::SetContentDispositionFilename(const nsAString &aContentDispositionFilename)
{
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsWyciwygChannel::GetContentDispositionHeader(nsACString &aContentDispositionHeader)
{
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsWyciwygChannel::GetContentLength(int64_t *aContentLength)
{
*aContentLength = mContentLength;
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::SetContentLength(int64_t aContentLength)
{
mContentLength = aContentLength;
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::Open(nsIInputStream ** aReturn)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsWyciwygChannel::Open2(nsIInputStream** aStream)
{
nsCOMPtr<nsIStreamListener> listener;
nsresult rv = nsContentSecurityManager::doContentSecurityCheck(this, listener);
NS_ENSURE_SUCCESS(rv, rv);
return Open(aStream);
}
NS_IMETHODIMP
nsWyciwygChannel::AsyncOpen(nsIStreamListener *listener, nsISupports *ctx)
{
MOZ_ASSERT(!mLoadInfo ||
mLoadInfo->GetSecurityMode() == 0 ||
mLoadInfo->GetInitialSecurityCheckDone() ||
(mLoadInfo->GetSecurityMode() == nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL &&
nsContentUtils::IsSystemPrincipal(mLoadInfo->LoadingPrincipal())),
"security flags in loadInfo but asyncOpen2() not called");
LOG(("nsWyciwygChannel::AsyncOpen [this=%p]\n", this));
MOZ_ASSERT(mMode == NONE, "nsWyciwygChannel already open");
NS_ENSURE_TRUE(!mIsPending, NS_ERROR_IN_PROGRESS);
NS_ENSURE_TRUE(mMode == NONE, NS_ERROR_IN_PROGRESS);
NS_ENSURE_ARG_POINTER(listener);
mMode = READING;
// open a cache entry for this channel...
// mIsPending set to true since OnCacheEntryAvailable may be called
// synchronously and fails when mIsPending found false.
mIsPending = true;
nsresult rv = OpenCacheEntryForReading(mURI);
if (NS_FAILED(rv)) {
LOG(("nsWyciwygChannel::OpenCacheEntryForReading failed [rv=%x]\n", rv));
mIsPending = false;
return rv;
}
// There is no code path that would invoke the listener sooner than
// we get to this line in case OnCacheEntryAvailable is invoked
// synchronously.
mListener = listener;
mListenerContext = ctx;
if (mLoadGroup)
mLoadGroup->AddRequest(this, nullptr);
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::AsyncOpen2(nsIStreamListener *aListener)
{
nsCOMPtr<nsIStreamListener> listener = aListener;
nsresult rv = nsContentSecurityManager::doContentSecurityCheck(this, listener);
NS_ENSURE_SUCCESS(rv, rv);
return AsyncOpen(listener, nullptr);
}
//////////////////////////////////////////////////////////////////////////////
// nsIWyciwygChannel
//////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP
nsWyciwygChannel::WriteToCacheEntry(const nsAString &aData)
{
LOG(("nsWyciwygChannel::WriteToCacheEntry [this=%p]", this));
nsresult rv;
if (mMode == READING) {
LOG(("nsWyciwygChannel::WriteToCacheEntry already open for reading"));
MOZ_ASSERT(false);
return NS_ERROR_UNEXPECTED;
}
mMode = WRITING;
if (!mCacheEntry) {
nsresult rv = OpenCacheEntryForWriting(mURI);
if (NS_FAILED(rv) || !mCacheEntry) {
LOG((" could not synchronously open cache entry for write!"));
return NS_ERROR_FAILURE;
}
}
if (mLoadFlags & INHIBIT_PERSISTENT_CACHING) {
rv = mCacheEntry->SetMetaDataElement("inhibit-persistent-caching", "1");
if (NS_FAILED(rv)) return rv;
}
if (mNeedToSetSecurityInfo) {
mCacheEntry->SetSecurityInfo(mSecurityInfo);
mNeedToSetSecurityInfo = false;
}
if (mNeedToWriteCharset) {
WriteCharsetAndSourceToCache(mCharsetSource, mCharset);
mNeedToWriteCharset = false;
}
uint32_t out;
if (!mCacheOutputStream) {
// Get the outputstream from the cache entry.
rv = mCacheEntry->OpenOutputStream(0, getter_AddRefs(mCacheOutputStream));
if (NS_FAILED(rv)) return rv;
// Write out a Byte Order Mark, so that we'll know if the data is
// BE or LE when we go to read it.
char16_t bom = 0xFEFF;
rv = mCacheOutputStream->Write((char *)&bom, sizeof(bom), &out);
if (NS_FAILED(rv)) return rv;
}
return mCacheOutputStream->Write((const char *)PromiseFlatString(aData).get(),
aData.Length() * sizeof(char16_t), &out);
}
NS_IMETHODIMP
nsWyciwygChannel::CloseCacheEntry(nsresult reason)
{
if (mCacheEntry) {
LOG(("nsWyciwygChannel::CloseCacheEntry [this=%p ]", this));
mCacheOutputStream = nullptr;
mCacheInputStream = nullptr;
if (NS_FAILED(reason)) {
mCacheEntry->AsyncDoom(nullptr);
}
mCacheEntry = nullptr;
}
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::SetSecurityInfo(nsISupports *aSecurityInfo)
{
if (mMode == READING) {
MOZ_ASSERT(false);
return NS_ERROR_UNEXPECTED;
}
mSecurityInfo = aSecurityInfo;
if (mCacheEntry) {
return mCacheEntry->SetSecurityInfo(mSecurityInfo);
}
mNeedToSetSecurityInfo = true;
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::SetCharsetAndSource(int32_t aSource,
const nsACString& aCharset)
{
NS_ENSURE_ARG(!aCharset.IsEmpty());
if (mCacheEntry) {
WriteCharsetAndSourceToCache(mCharsetSource, mCharset);
} else {
MOZ_ASSERT(mMode != WRITING, "We must have an entry!");
if (mMode == READING) {
return NS_ERROR_NOT_AVAILABLE;
}
mNeedToWriteCharset = true;
mCharsetSource = aSource;
mCharset = aCharset;
}
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::GetCharsetAndSource(int32_t* aSource, nsACString& aCharset)
{
MOZ_ASSERT(mMode == READING);
if (!mCacheEntry) {
return NS_ERROR_NOT_AVAILABLE;
}
nsXPIDLCString data;
mCacheEntry->GetMetaDataElement("charset", getter_Copies(data));
if (data.IsEmpty()) {
return NS_ERROR_NOT_AVAILABLE;
}
nsXPIDLCString sourceStr;
mCacheEntry->GetMetaDataElement("charset-source", getter_Copies(sourceStr));
int32_t source;
nsresult err;
source = sourceStr.ToInteger(&err);
if (NS_FAILED(err) || source == 0) {
return NS_ERROR_NOT_AVAILABLE;
}
*aSource = source;
aCharset = data;
return NS_OK;
}
//////////////////////////////////////////////////////////////////////////////
// nsICacheEntryOpenCallback
//////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP
nsWyciwygChannel::OnCacheEntryCheck(nsICacheEntry* entry, nsIApplicationCache* appCache,
uint32_t* aResult)
{
*aResult = ENTRY_WANTED;
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::OnCacheEntryAvailable(nsICacheEntry *aCacheEntry,
bool aNew,
nsIApplicationCache* aAppCache,
nsresult aStatus)
{
LOG(("nsWyciwygChannel::OnCacheEntryAvailable [this=%p entry=%p "
"new=%d status=%x]\n", this, aCacheEntry, aNew, aStatus));
MOZ_RELEASE_ASSERT(!aNew, "New entry must not be returned when flag "
"OPEN_READONLY is used!");
// if the channel's already fired onStopRequest,
// then we should ignore this event.
if (!mIsPending)
return NS_OK;
if (NS_SUCCEEDED(mStatus)) {
if (NS_SUCCEEDED(aStatus)) {
MOZ_ASSERT(aCacheEntry);
mCacheEntry = aCacheEntry;
nsresult rv = ReadFromCache();
if (NS_FAILED(rv)) {
mStatus = rv;
}
} else {
mStatus = aStatus;
}
}
if (NS_FAILED(mStatus)) {
LOG(("channel was canceled [this=%p status=%x]\n", this, mStatus));
// Since OnCacheEntryAvailable can be called directly from AsyncOpen
// we must dispatch.
NS_DispatchToCurrentThread(mozilla::NewRunnableMethod(
this, &nsWyciwygChannel::NotifyListener));
}
return NS_OK;
}
//-----------------------------------------------------------------------------
// nsWyciwygChannel::nsIStreamListener
//-----------------------------------------------------------------------------
NS_IMETHODIMP
nsWyciwygChannel::OnDataAvailable(nsIRequest *request, nsISupports *ctx,
nsIInputStream *input,
uint64_t offset, uint32_t count)
{
LOG(("nsWyciwygChannel::OnDataAvailable [this=%p request=%x offset=%llu count=%u]\n",
this, request, offset, count));
nsresult rv;
nsCOMPtr<nsIStreamListener> listener = mListener;
nsCOMPtr<nsISupports> listenerContext = mListenerContext;
if (listener) {
rv = listener->OnDataAvailable(this, listenerContext, input, offset, count);
} else {
MOZ_ASSERT(false, "We must have a listener!");
rv = NS_ERROR_UNEXPECTED;
}
// XXX handle 64-bit stuff for real
if (mProgressSink && NS_SUCCEEDED(rv)) {
mProgressSink->OnProgress(this, nullptr, offset + count, mContentLength);
}
return rv; // let the pump cancel on failure
}
//////////////////////////////////////////////////////////////////////////////
// nsIRequestObserver
//////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP
nsWyciwygChannel::OnStartRequest(nsIRequest *request, nsISupports *ctx)
{
LOG(("nsWyciwygChannel::OnStartRequest [this=%p request=%x\n",
this, request));
nsCOMPtr<nsIStreamListener> listener = mListener;
nsCOMPtr<nsISupports> listenerContext = mListenerContext;
if (listener) {
return listener->OnStartRequest(this, listenerContext);
}
MOZ_ASSERT(false, "We must have a listener!");
return NS_ERROR_UNEXPECTED;
}
NS_IMETHODIMP
nsWyciwygChannel::OnStopRequest(nsIRequest *request, nsISupports *ctx, nsresult status)
{
LOG(("nsWyciwygChannel::OnStopRequest [this=%p request=%x status=%d\n",
this, request, status));
if (NS_SUCCEEDED(mStatus))
mStatus = status;
mIsPending = false;
nsCOMPtr<nsIStreamListener> listener;
nsCOMPtr<nsISupports> listenerContext;
listener.swap(mListener);
listenerContext.swap(mListenerContext);
if (listener) {
listener->OnStopRequest(this, listenerContext, mStatus);
} else {
MOZ_ASSERT(false, "We must have a listener!");
}
if (mLoadGroup)
mLoadGroup->RemoveRequest(this, nullptr, mStatus);
CloseCacheEntry(mStatus);
mPump = nullptr;
// Drop notification callbacks to prevent cycles.
mCallbacks = nullptr;
mProgressSink = nullptr;
return NS_OK;
}
//////////////////////////////////////////////////////////////////////////////
// Helper functions
//////////////////////////////////////////////////////////////////////////////
nsresult
nsWyciwygChannel::GetCacheStorage(nsICacheStorage **_retval)
{
nsresult rv;
nsCOMPtr<nsICacheStorageService> cacheService =
do_GetService("@mozilla.org/netwerk/cache-storage-service;1", &rv);
NS_ENSURE_SUCCESS(rv, rv);
bool anonymous = mLoadFlags & LOAD_ANONYMOUS;
mOriginAttributes.SyncAttributesWithPrivateBrowsing(mPrivateBrowsing);
RefPtr<LoadContextInfo> loadInfo = mozilla::net::GetLoadContextInfo(anonymous, mOriginAttributes);
if (mLoadFlags & INHIBIT_PERSISTENT_CACHING) {
return cacheService->MemoryCacheStorage(loadInfo, _retval);
}
return cacheService->DiskCacheStorage(loadInfo, false, _retval);
}
nsresult
nsWyciwygChannel::OpenCacheEntryForReading(nsIURI *aURI)
{
nsresult rv;
nsCOMPtr<nsICacheStorage> cacheStorage;
rv = GetCacheStorage(getter_AddRefs(cacheStorage));
NS_ENSURE_SUCCESS(rv, rv);
return cacheStorage->AsyncOpenURI(aURI, EmptyCString(),
nsICacheStorage::OPEN_READONLY |
nsICacheStorage::CHECK_MULTITHREADED,
this);
}
nsresult
nsWyciwygChannel::OpenCacheEntryForWriting(nsIURI *aURI)
{
nsresult rv;
nsCOMPtr<nsICacheStorage> cacheStorage;
rv = GetCacheStorage(getter_AddRefs(cacheStorage));
NS_ENSURE_SUCCESS(rv, rv);
return cacheStorage->OpenTruncate(aURI, EmptyCString(),
getter_AddRefs(mCacheEntry));
}
nsresult
nsWyciwygChannel::ReadFromCache()
{
LOG(("nsWyciwygChannel::ReadFromCache [this=%p] ", this));
NS_ENSURE_TRUE(mCacheEntry, NS_ERROR_FAILURE);
nsresult rv;
// Get the stored security info
mCacheEntry->GetSecurityInfo(getter_AddRefs(mSecurityInfo));
nsAutoCString tmpStr;
rv = mCacheEntry->GetMetaDataElement("inhibit-persistent-caching",
getter_Copies(tmpStr));
if (NS_SUCCEEDED(rv) && tmpStr.EqualsLiteral("1"))
mLoadFlags |= INHIBIT_PERSISTENT_CACHING;
// Get a transport to the cached data...
rv = mCacheEntry->OpenInputStream(0, getter_AddRefs(mCacheInputStream));
if (NS_FAILED(rv))
return rv;
NS_ENSURE_TRUE(mCacheInputStream, NS_ERROR_UNEXPECTED);
rv = NS_NewInputStreamPump(getter_AddRefs(mPump), mCacheInputStream);
if (NS_FAILED(rv)) return rv;
// Pump the cache data downstream
return mPump->AsyncRead(this, nullptr);
}
void
nsWyciwygChannel::WriteCharsetAndSourceToCache(int32_t aSource,
const nsCString& aCharset)
{
NS_PRECONDITION(mCacheEntry, "Better have cache entry!");
mCacheEntry->SetMetaDataElement("charset", aCharset.get());
nsAutoCString source;
source.AppendInt(aSource);
mCacheEntry->SetMetaDataElement("charset-source", source.get());
}
void
nsWyciwygChannel::NotifyListener()
{
nsCOMPtr<nsIStreamListener> listener;
nsCOMPtr<nsISupports> listenerContext;
listener.swap(mListener);
listenerContext.swap(mListenerContext);
if (listener) {
listener->OnStartRequest(this, listenerContext);
mIsPending = false;
listener->OnStopRequest(this, listenerContext, mStatus);
} else {
MOZ_ASSERT(false, "We must have the listener!");
mIsPending = false;
}
CloseCacheEntry(mStatus);
// Remove ourselves from the load group.
if (mLoadGroup) {
mLoadGroup->RemoveRequest(this, nullptr, mStatus);
}
}
// vim: ts=2 sw=2
|