What's new? | Help | Directory | Sign in
Google
             
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
// Copyright 2005, Google Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <string>
#include <vector>
#include <gecko_sdk/include/nsXPCOM.h>
#include <gecko_sdk/include/nsIURI.h>
#include <gecko_sdk/include/nsIRequest.h>
#include <gecko_sdk/include/nsIOutputStream.h>
#include <gecko_sdk/include/nsIObserverService.h>
#include <gecko_sdk/include/nsIObserver.h>
#include <gecko_sdk/include/nsIInterfaceRequestor.h>
#include <gecko_sdk/include/nsIInputStream.h>
#include <gecko_sdk/include/nsIHttpChannel.h>
#include <gecko_sdk/include/nsIComponentRegistrar.h>
#include <gecko_sdk/include/nsIChannel.h>
#include <gecko_sdk/include/nsICategoryManager.h>
#include <gecko_sdk/include/prtime.h>
#include <gecko_internal/nsICacheEntryDescriptor.h>
#include <gecko_internal/nsICacheListener.h>
#include <gecko_internal/nsICacheService.h>
#include <gecko_internal/nsICacheSession.h>
#include <gecko_internal/nsICacheVisitor.h>
#if BROWSER_FF3
#include <gecko_internal/nsThreadUtils.h>
#endif
#include "genfiles/interfaces.h"
#include "gears/base/common/exception_handler.h"
#include "gears/base/common/message_queue.h"
#include "gears/base/common/string_utils.h"
#include "gears/base/common/trace_buffers_win32/trace_buffers_win32.h"
#include "gears/base/common/user_config.h"
#include "gears/factory/factory_utils.h"
#include "gears/localserver/common/localserver_db.h"
#include "gears/localserver/firefox/cache_intercept.h"
#include "gears/localserver/firefox/http_request_ff.h"

// Used to determine when we're executing on the main thread of control
static ThreadId g_ui_thread = 0;


// Object identifiers
const char *kCacheInterceptContractId = "@google.com/gears/cacheintercept;1";
const char *kCacheInterceptClassName = "CacheIntercept";
const nsCID kCacheInterceptClassId = {0x3ec37181, 0x7f37, 0x4e49, {0xbb, 0xeb,
0x2c, 0xc3, 0xdf, 0x39, 0x16, 0x28}};
// {3EC37181-7F37-4e49-BBEB-2CC3DF391628}


// We use nsIObserverService to get notifications of shift+reload.
const char *kObserverServiceContractId = "@mozilla.org/observer-service;1";
const char *kOnModifyRequestTopic = "http-on-modify-request";


// ClassID of the built-in cache service
#define NS_CACHESERVICE_CID \
{ /* 6c84aec9-29a5-4264-8fbc-bee8f922ea67 */ \
0x6c84aec9, \
0x29a5, \
0x4264, \
{0x8f, 0xbc, 0xbe, 0xe8, 0xf9, 0x22, 0xea, 0x67} \
}
// ContractID of the built-in cache service
#define NS_CACHESERVICE_CONTRACTID \
"@mozilla.org/network/cache-service;1"

//-----------------------------------------------------------------------------
// NowInSeconds
//-----------------------------------------------------------------------------
static PRUint32 NowInSeconds() {
return PRUint32(PR_Now() / PR_USEC_PER_SEC); // PR_Now returns microseconds
}

//-----------------------------------------------------------------------------
// AbstractCacheMetaDataVisitor
//-----------------------------------------------------------------------------
class AbstractCacheMetaDataVisitor : public nsICacheMetaDataVisitor {
public:
// Stack allocated
NS_IMETHODIMP_(nsrefcnt) AddRef() { return 1; }
NS_IMETHODIMP_(nsrefcnt) Release() { return 1; }

NS_IMETHODIMP QueryInterface(const nsIID &iid, void **result) {
if (iid.Equals(NS_GET_IID(nsICacheMetaDataVisitor)) ||
iid.Equals(NS_GET_IID(nsISupports))) {
*result = this;
return NS_OK;
}
return NS_ERROR_NO_INTERFACE;
}
};


//-----------------------------------------------------------------------------
// VisitSerializedCacheEntryMetaData
//-----------------------------------------------------------------------------
static void VisitSerializedCacheEntryMetaData(const nsCString &meta_data,
nsICacheMetaDataVisitor *visitor) {
// Names and values are separated by null bytes

const char *start;
PRUint32 len = NS_CStringGetData(meta_data, &start);

PRBool keep_going = PR_TRUE;

const char *iter = start;
const char *end = start + len;
for (; keep_going && iter < end; ++iter) {
// Find next null byte
if (*iter)
continue;

const char *name = start;
start = ++iter;

// Find next null byte
for (; *iter && iter < end; ++iter);

// Ensure we found a null
if (*iter)
return;

const char *value = start;
start = ++iter;

if (NS_FAILED(visitor->VisitMetaDataElement(name, value, &keep_going)))
keep_going = PR_FALSE;
}
}


//-----------------------------------------------------------------------------
// ReplayInputStream
//-----------------------------------------------------------------------------
class ReplayInputStream : public nsIInputStream {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIINPUTSTREAM

ReplayInputStream(nsISupports *entry,
std::vector<unsigned char> *data,
PRUint32 offset)
: entry_(entry),
data_(data),
offset_(offset) {
}

private:
~ReplayInputStream() { Close(); }

nsCOMPtr<nsISupports> entry_;
std::vector<unsigned char> *data_;
PRUint32 offset_;
};

NS_IMPL_THREADSAFE_ISUPPORTS1(ReplayInputStream, nsIInputStream)

NS_IMETHODIMP ReplayInputStream::Close() {
entry_ = nsnull;
data_ = nsnull;
return NS_OK;
}

NS_IMETHODIMP ReplayInputStream::Available(PRUint32 *avail) {
if (!entry_)
return NS_BASE_STREAM_CLOSED;

*avail = data_ ? (data_->size() - offset_) : 0;
return NS_OK;
}

NS_IMETHODIMP ReplayInputStream::Read(char *buf, PRUint32 count,
PRUint32 *result) {
if (!entry_ || !data_) {
*result = 0;
return NS_OK;
}

PRUint32 avail = data_->size() - offset_;
if (count > avail)
count = avail;

if (count) {
memcpy(buf, &(*data_)[0] + offset_, count);
}

offset_ += count;
*result = count;
return NS_OK;
}

NS_IMETHODIMP ReplayInputStream::ReadSegments(nsWriteSegmentFun callback,
void *closure,
PRUint32 count,
PRUint32 *result) {
return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP ReplayInputStream::IsNonBlocking(PRBool *result) {
*result = PR_FALSE;
return NS_OK;
}

//-----------------------------------------------------------------------------
// ReplayCacheEntry
//-----------------------------------------------------------------------------
class ReplayCacheEntry : public nsICacheEntryDescriptor {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSICACHEENTRYINFO
NS_DECL_NSICACHEENTRYDESCRIPTOR

protected:
~ReplayCacheEntry() { Close(); }

private:
friend class CacheSession;

ReplayCacheEntry(const nsACString &client_id, const nsACString &key)
: client_id_(client_id), key_(key) {
}

static bool AttemptToReplay(const nsACString &key,
nsICacheEntryDescriptor **result);
bool LoadFromCache();

nsCString client_id_;
nsCString key_;
nsCString meta_data_;
WebCacheDB::PayloadInfo payload_;
};

// static
bool ReplayCacheEntry::AttemptToReplay(const nsACString &key,
nsICacheEntryDescriptor **result) {
// TODO(michaeln): support HEAD requests
// TODO(darin): use proper client_id
NS_NAMED_LITERAL_CSTRING(client_id, "HTTP");
nsCOMPtr<ReplayCacheEntry> entry = new ReplayCacheEntry(client_id, key);
if (!entry->LoadFromCache()) {
return false;
}
NS_ADDREF(*result = entry);
return true;
}

bool ReplayCacheEntry::LoadFromCache() {
WebCacheDB* db = WebCacheDB::GetDB();
if (!db) {
return false;
}

std::string16 url;
if (!UTF8ToString16(key_.get(), key_.Length(), &url)) {
return false;
}

if (!db->Service(url.c_str(), NULL, false, &payload_)) {
return false; // we have no response for this url
}

// synthesize cache meta_data_ based on our cached response

// Values are stored as a wide character string in the webcache database,
// we convert to ASCII here to satisfy what firefox wants to see in its
// meta data. The conversion to UTF8 == a converstion to ASCII since the
// source data for the string was ASCII to begin with.
std::string headers_utf8;
std::string status_line_utf8;
if (!String16ToUTF8(payload_.status_line.c_str(),
payload_.status_line.length(),
&status_line_utf8) ||
!String16ToUTF8(payload_.headers.c_str(),
payload_.headers.length(),
&headers_utf8)) {
return false;
}

const char *kRequestMethod = "request-method";
const char *kResponseHead = "response-head";
const char kNullChar = '\0';
meta_data_.Append(kRequestMethod);
meta_data_.Append(kNullChar);
meta_data_.Append(HttpConstants::kHttpGETAscii);
meta_data_.Append(kNullChar);
meta_data_.Append(kResponseHead);
meta_data_.Append(kNullChar);
meta_data_.Append(status_line_utf8.c_str());
meta_data_.Append(HttpConstants::kCrLfAscii);
meta_data_.Append(headers_utf8.c_str());
meta_data_.Append(kNullChar);

return true;
}

NS_IMPL_THREADSAFE_ISUPPORTS2(ReplayCacheEntry,
nsICacheEntryDescriptor,
nsICacheEntryInfo)

NS_IMETHODIMP ReplayCacheEntry::GetClientID(char **value) {
*value = NS_CStringCloneData(client_id_);
return *value ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}

NS_IMETHODIMP ReplayCacheEntry::GetDeviceID(char **value) {
*value = NS_CStringCloneData(NS_LITERAL_CSTRING(PRODUCT_SHORT_NAME_ASCII));
return *value ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}

NS_IMETHODIMP ReplayCacheEntry::GetKey(nsACString &value) {
value = key_;
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::GetFetchCount(PRInt32 *value) {
*value = 1; // fudge
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::GetLastFetched(PRUint32 *value) {
*value = NowInSeconds() - 10; // fudge
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::GetLastModified(PRUint32 *value) {
*value = NowInSeconds() - 10; // fudge
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::GetExpirationTime(PRUint32 *value) {
*value = NowInSeconds() + 10; // fudge
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::GetDataSize(PRUint32 *value) {
*value = payload_.data.get() ? payload_.data->size() : 0;
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::IsStreamBased(PRBool *value) {
*value = PR_TRUE;
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::SetExpirationTime(PRUint32 value) {
// Ignore
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::SetDataSize(PRUint32 value) {
// Ignore
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::OpenInputStream(PRUint32 offset,
nsIInputStream **result) {
LOG(("ReplayCacheEntry::OpenInputStream\n"));

size_t payload_size = payload_.data.get() ? payload_.data->size() : 0;
NS_ENSURE_ARG(offset <= payload_size);

*result = new ReplayInputStream(this, payload_.data.get(), offset);
if (!*result)
return NS_ERROR_OUT_OF_MEMORY;

NS_ADDREF(*result);
return NS_OK;
}

NS_IMETHODIMP
ReplayCacheEntry::OpenOutputStream(PRUint32 offset,
nsIOutputStream **result) {
// We're not open for writing.
return NS_ERROR_UNEXPECTED;
}

NS_IMETHODIMP ReplayCacheEntry::GetCacheElement(nsISupports **value) {
*value = nsnull;
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::SetCacheElement(nsISupports *value) {
// We're not open for writing.
return NS_ERROR_UNEXPECTED;
}

NS_IMETHODIMP
ReplayCacheEntry::GetAccessGranted(nsCacheAccessMode *value) {
*value = nsICache::ACCESS_READ;
return NS_OK;
}

NS_IMETHODIMP
ReplayCacheEntry::GetStoragePolicy(nsCacheStoragePolicy *value) {
// TODO(darin): make this depend on client_id_
*value = nsICache::STORE_ON_DISK;
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::SetStoragePolicy(nsCacheStoragePolicy) {
// We're not open for writing.
return NS_ERROR_UNEXPECTED;
}

NS_IMETHODIMP ReplayCacheEntry::GetFile(nsIFile **value) {
*value = nsnull;
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::GetSecurityInfo(nsISupports **value) {
// TODO(darin): HTTPS caching needs to store a value here.
*value = nsnull;
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::SetSecurityInfo(nsISupports *value) {
// We're not open for writing.
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::Doom() {
// Ignore
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::DoomAndFailPendingRequests(nsresult) {
// Ignore
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::MarkValid() {
// Ignore
return NS_OK;
}

NS_IMETHODIMP ReplayCacheEntry::Close() {
// TODO(darin): Make us act like we've been closed?
return NS_OK;
}


//-----------------------------------------------------------------------------
// FindMetaDataElement
//-----------------------------------------------------------------------------
class FindMetaDataElement : public AbstractCacheMetaDataVisitor {
public:
FindMetaDataElement(const char *name, char **value)
: name_(name),
value_(value) {
}

NS_IMETHODIMP VisitMetaDataElement(const char *name,
const char *value,
PRBool *keep_going) {
*keep_going = (strcmp(name, name_) != 0);

if (!*keep_going) // found it
*value_ = NS_CStringCloneData(nsDependentCString(value));

return NS_OK;
}

private:
const char *name_;
char **value_;
};

NS_IMETHODIMP ReplayCacheEntry::GetMetaDataElement(const char *name,
char **value) {
FindMetaDataElement finder(name, value);
VisitSerializedCacheEntryMetaData(meta_data_, &finder);
return *value ? NS_OK : NS_ERROR_NOT_AVAILABLE;
}

NS_IMETHODIMP ReplayCacheEntry::SetMetaDataElement(const char *name,
const char *value) {
// We're not open for writing.
return NS_ERROR_UNEXPECTED;
}

NS_IMETHODIMP
ReplayCacheEntry::VisitMetaData(nsICacheMetaDataVisitor *visitor) {
VisitSerializedCacheEntryMetaData(meta_data_, visitor);
return NS_OK;
}



//-----------------------------------------------------------------------------
// CacheSession
//-----------------------------------------------------------------------------
class CacheSession : public nsICacheSession {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSICACHESESSION

CacheSession(nsICacheSession *inner) : inner_(inner) {
}

private:
nsCOMPtr<nsICacheSession> inner_;
};

NS_IMPL_THREADSAFE_ISUPPORTS1(CacheSession, nsICacheSession)

NS_IMETHODIMP CacheSession::GetDoomEntriesIfExpired(PRBool *value) {
return inner_->GetDoomEntriesIfExpired(value);
}

NS_IMETHODIMP CacheSession::SetDoomEntriesIfExpired(PRBool value) {
return inner_->SetDoomEntriesIfExpired(value);
}

NS_IMETHODIMP
CacheSession::OpenCacheEntry(const nsACString &key,
nsCacheAccessMode access_req,
PRBool blockingMode,
nsICacheEntryDescriptor **result) {
// NOTE: force reloads (shift-reload) bypass this intercept layer
LOG(("CacheSession::OpenCacheEntry( %s )\n",
nsCString(key).get()));

if (!(access_req & nsICache::ACCESS_READ)) {
LOG(("CacheSession: not a read request, using default handling\n"));
return inner_->OpenCacheEntry(key, access_req, blockingMode, result);
}

if (ReplayCacheEntry::AttemptToReplay(key, result)) {
LOG(("CacheSession: cache hit\n"));
return NS_OK;
} else {
LOG(("CacheSession: cache miss, using default handling\n"));
return inner_->OpenCacheEntry(key, access_req, blockingMode, result);
}
}

NS_IMETHODIMP
CacheSession::AsyncOpenCacheEntry(const nsACString &key,
nsCacheAccessMode access_req,
nsICacheListener *listener) {
// No need to lookup in our cache here.
return inner_->AsyncOpenCacheEntry(key, access_req, listener);
}

NS_IMETHODIMP CacheSession::EvictEntries() {
return inner_->EvictEntries();
}

NS_IMETHODIMP CacheSession::IsStorageEnabled(PRBool *result) {
return inner_->IsStorageEnabled(result);
}

//-----------------------------------------------------------------------------
// CacheIntercept
//-----------------------------------------------------------------------------
NS_IMPL_THREADSAFE_ISUPPORTS2(CacheIntercept,
nsICacheService,
nsIObserver)

NS_IMETHODIMP CacheIntercept::CreateSession(const char *client_id,
nsCacheStoragePolicy policy,
PRBool stream_based,
nsICacheSession **result) {
LOG(("CacheIntercept::CreateSession [%s]\n", client_id));
NS_ENSURE_TRUE(default_cache_, NS_ERROR_NOT_INITIALIZED);

nsCOMPtr<nsICacheSession> inner_session;
nsresult rv = default_cache_->CreateSession(client_id, policy, stream_based,
getter_AddRefs(inner_session));
if (NS_FAILED(rv))
return rv;

if (!MayIntercept(client_id) ||
!(*result = new CacheSession(inner_session))) {
NS_ADDREF(*result = inner_session);
} else {
NS_ADDREF(*result);
}
return NS_OK;
}

#if BROWSER_FF3
NS_IMETHODIMP CacheIntercept::CreateTemporaryClientID(
nsCacheStoragePolicy policy,
nsACString &retval) {
LOG(("CacheIntercept::CreateTemporaryClientID\n"));
NS_ENSURE_TRUE(default_cache_, NS_ERROR_NOT_INITIALIZED);
return default_cache_->CreateTemporaryClientID(policy, retval);
}
#endif

NS_IMETHODIMP CacheIntercept::VisitEntries(nsICacheVisitor *visitor) {
LOG(("CacheIntercept::VisitEntries\n"));
NS_ENSURE_TRUE(default_cache_, NS_ERROR_NOT_INITIALIZED);
return default_cache_->VisitEntries(visitor);
}

NS_IMETHODIMP CacheIntercept::EvictEntries(nsCacheStoragePolicy policy) {
LOG(("CacheIntercept::EvictEntries\n"));
NS_ENSURE_TRUE(default_cache_, NS_ERROR_NOT_INITIALIZED);
return default_cache_->EvictEntries(policy);
}

NS_IMETHODIMP CacheIntercept::Observe(nsISupports *subject,
const char *topic,
const PRUnichar *data) {
if (0 == strcmp(topic, NS_XPCOM_STARTUP_CATEGORY)) {
Init();
} else if (0 == strcmp(topic, kOnModifyRequestTopic)) {
MaybeForceToCache(subject);
} else {
// We didn't sign up for any other topics, so we should not get here.
assert(false);
LOG(("CacheIntercept: Unexpected observer topic: %s", topic));
}

return NS_OK;
}

void CacheIntercept::Init() {
#if defined(WIN32) && !defined(WINCE)
// Only send crash reports for offical builds. Crashes on an engineer's machine
// during internal development are confusing false alarms.
#ifdef OFFICIAL_BUILD
if (IsUsageReportingAllowed()) {
static ExceptionManager exception_manager(false); // false == only our DLL
exception_manager.StartMonitoring();
// Trace buffers only exist in dbg official builds.
#ifdef DEBUG
exception_manager.AddMemoryRange(g_trace_buffers,
sizeof(g_trace_buffers));
exception_manager.AddMemoryRange(g_trace_positions,
sizeof(g_trace_positions));
#endif // DEBUG
}
#endif // OFFICIAL_BUILD
#endif // WIN32 && !WINCE

std::string16 buildinfo;
AppendBuildInfo(&buildinfo);
std::string buildinfo_utf8;
String16ToUTF8(buildinfo.c_str(), &buildinfo_utf8);
LOG(("CacheIntercept::Init, Gears %s\n", buildinfo_utf8.c_str()));

// We're initialized on the main ui thread of which there is exactly one
// in firefox/mozila. Save this thread value so that we can easily
// determine when we're executing on this thread of control.
g_ui_thread = ThreadMessageQueue::GetInstance()->GetCurrentThreadId();

const nsCID kCacheCID = NS_CACHESERVICE_CID;
default_cache_ = do_GetService(kCacheCID);
if (!default_cache_) {
LOG(("CacheIntercept: no cache service\n"));
return;
}

nsCOMPtr<nsIFactory> factory =
do_GetClassObject(kCacheInterceptContractId);
if (!factory) {
LOG(("CacheIntercept: no factory\n"));
return;
}

// Override the default cache.
nsCOMPtr<nsIComponentRegistrar> reg;
NS_GetComponentRegistrar(getter_AddRefs(reg));
if (!reg) {
LOG(("CacheIntercept: no compreg\n"));
return;
}

nsresult rv = reg->RegisterFactory(kCacheCID,
kCacheInterceptClassName,
NS_CACHESERVICE_CONTRACTID,
factory);
if (NS_FAILED(rv)) {
LOG(("CacheIntercept: failed to register factory\n"));
}

nsCOMPtr<nsIObserverService> observer_service(
do_GetService(kObserverServiceContractId));
if (!observer_service) {
LOG(("CacheIntercept: Could not get observer service"));
}

// We let the observer service hold a strong reference to us since we are
// alive for the life of the application anyway.
observer_service->AddObserver(this, kOnModifyRequestTopic, false);

LOG(("CacheIntercept: registration complete\n"));
}

void CacheIntercept::MaybeForceToCache(nsISupports *subject) {
nsCOMPtr<nsIRequest> request(do_QueryInterface(subject));
if (!request) {
LOG(("CacheIntercept: could not get request"));
return;
}

// If the request is already going to the cache do nothing.
nsLoadFlags flags(0);
request->GetLoadFlags(&flags);
if (0 == (flags & nsIRequest::LOAD_BYPASS_CACHE)) {
return;
}

nsCOMPtr<nsIChannel> channel(do_QueryInterface(request));
if (!channel) {
LOG(("CacheIntercept: could not get channel"));
return;
}

// Gears requests can intentionally bypass caches.
FFHttpRequest *gears_http_request = GetGearsHttpRequest(channel);
if (gears_http_request &&
gears_http_request->GetCachingBehavior() ==
HttpRequest::BYPASS_ALL_CACHES) {
return;
}

// Only drive GET and HEAD requests to our cache.
nsCOMPtr<nsIHttpChannel> http_channel(do_QueryInterface(channel));
if (!http_channel) {
LOG(("CacheIntercept: could not get http channel"));
return;
}

nsCString method;
http_channel->GetRequestMethod(method);
if (!method.Equals(NS_LITERAL_CSTRING("GET")) &&
!method.Equals(NS_LITERAL_CSTRING("HEAD"))) {
return;
}

// If the URL can be served locally, force it to go to the cache.
nsCOMPtr<nsIURI> uri;
channel->GetURI(getter_AddRefs(uri));
if (!uri) {
LOG(("CacheIntercept: could not get uri"));
return;
}

WebCacheDB *db = WebCacheDB::GetDB();
if (!db) {
LOG(("CacheIntercept: Could not get WebCacheDB"));
return;
}

nsCString spec;
uri->GetSpec(spec);

// TODO(aa): It would be nice to not have to hit the database twice for these
// requests. Perhaps cache the result of this query somewhere?
if (db->CanService(NS_ConvertUTF8toUTF16(spec).get(), NULL)) {
LOG(("CacheIntercept: Caught ctrl+refresh, removing LOAD_BYPASS_CACHE"));
request->SetLoadFlags(flags & ~nsIRequest::LOAD_BYPASS_CACHE);
}
}

FFHttpRequest *CacheIntercept::GetGearsHttpRequest(nsIChannel *channel) {
// TODO(michaeln): perhaps we should use Get/SetOwner() for this purpose
// instead. If some other OnModifyRequest listener injects their own
// listener which chains to the existing listener, we could get cut
// out of the loop?
nsCOMPtr<nsIInterfaceRequestor> listener;
channel->GetNotificationCallbacks(getter_AddRefs(listener));
if (!listener) return NULL;

nsCOMPtr<SpecialHttpRequestInterface> gears_request(
do_QueryInterface(listener));
if (!gears_request) return NULL;

FFHttpRequest *http_request = NULL;
gears_request->GetNativeHttpRequest(&http_request);
return http_request;
}

// See ui_thread.h for declaration.
ThreadId GetUiThread() {
assert(g_ui_thread);
return g_ui_thread;
}

// See ui_thread.h for declaration.
bool IsUiThread() {
assert(g_ui_thread);
return g_ui_thread == ThreadMessageQueue::GetInstance()->GetCurrentThreadId();
}
Show details Hide details

Change log

r2769 by gears.daemon on Aug 27, 2008   Diff
[Author: aa]

Check registry before sending crash
reports.

PRESUBMIT=passed
R=cprince
CC=gears-internal
DELTA=106  (92 added, 0 deleted, 14
changed)
OCL=8105765
SCL=8117115
Go to: 
Project members, sign in to write a code review

Older revisions

r2584 by gears.daemon on Aug 04, 2008   Diff
[Author: nigeltao]

Clean up
base/common/base_interface_ff.idl,
since there no longer is an XPCOM
...
r2554 by gears.daemon on Jul 30, 2008   Diff
[Author: nigeltao]

Flatten the gears/factory directory
hierarchy.
For example,
...
r2284 by gears.daemon on Jul 08, 2008   Diff
[Author: nigeltao]

LocalServer and ResourceStore are now
Dispatcher-based. In doing so,
ResourceStore::CaptureBlob is now
...
All revisions of this file

File info

Size: 25722 bytes, 810 lines