1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Support modularity by calling to load a new SDCH filter dictionary.
6 // Note that this sort of calling can't be done in the /net directory, as it has
7 // no concept of the HTTP cache (which is only visible at the browser level).
9 #ifndef NET_BASE_SDCH_DICTIONARY_FETCHER_H_
10 #define NET_BASE_SDCH_DICTIONARY_FETCHER_H_
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/threading/non_thread_safe.h"
19 #include "net/base/sdch_manager.h"
20 #include "net/url_request/url_fetcher_delegate.h"
25 class URLRequestContextGetter
;
27 class NET_EXPORT SdchDictionaryFetcher
28 : public URLFetcherDelegate
,
30 public base::NonThreadSafe
{
32 // The consumer must guarantee that |*manager| outlives
33 // this object. The current implementation guarantees this by
34 // the SdchManager owning this object.
35 SdchDictionaryFetcher(SdchManager
* manager
,
36 scoped_refptr
<URLRequestContextGetter
> context
);
37 virtual ~SdchDictionaryFetcher();
39 // Implementation of SdchFetcher class.
40 virtual void Schedule(const GURL
& dictionary_url
) OVERRIDE
;
41 virtual void Cancel() OVERRIDE
;
44 // Delay in ms between Schedule and actual download.
45 // This leaves the URL in a queue, which is de-duped, so that there is less
46 // chance we'll try to load the same URL multiple times when a pile of
47 // page subresources (or tabs opened in parallel) all suggest the dictionary.
48 static const int kMsDelayFromRequestTillDownload
= 100;
50 // Ensure the download after the above delay.
51 void ScheduleDelayedRun();
53 // Make sure we're processing (or waiting for) the the arrival of the next URL
54 // in the |fetch_queue_|.
57 // Implementation of URLFetcherDelegate. Called after transmission
58 // completes (either successfully or with failure).
59 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
;
61 SdchManager
* const manager_
;
63 // A queue of URLs that are being used to download dictionaries.
64 std::queue
<GURL
> fetch_queue_
;
65 // The currently outstanding URL fetch of a dicitonary.
66 // If this is null, then there is no outstanding request.
67 scoped_ptr
<URLFetcher
> current_fetch_
;
69 bool task_is_pending_
;
71 // Althought the SDCH spec does not preclude a server from using a single URL
72 // to load several distinct dictionaries (by telling a client to load a
73 // dictionary from an URL several times), current implementations seem to have
74 // that 1-1 relationship (i.e., each URL points at a single dictionary, and
75 // the dictionary content does not change over time, and hence is not worth
76 // trying to load more than once). In addition, some dictionaries prove
77 // unloadable only after downloading them (because they are too large? ...or
78 // malformed?). As a protective element, Chromium will *only* load a
79 // dictionary at most once from a given URL (so that it doesn't waste
80 // bandwidth trying repeatedly).
81 // The following set lists all the dictionary URLs that we've tried to load,
82 // so that we won't try to load from an URL more than once.
83 // TODO(jar): Try to augment the SDCH proposal to include this restiction.
84 std::set
<GURL
> attempted_load_
;
86 // Store the system_url_request_context_getter to use it when we start
88 scoped_refptr
<URLRequestContextGetter
> context_
;
90 // Always spread out the dictionary fetches, so that they don't steal
91 // bandwidth from the actual page load. Create delayed tasks to spread out
93 base::WeakPtrFactory
<SdchDictionaryFetcher
> weak_factory_
;
95 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher
);
100 #endif // NET_BASE_SDCH_DICTIONARY_FETCHER_H_