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 // TODO(rdsmith): This class needs to be moved out to the net/ embedder and
6 // hooked into whatever mechanisms the embedder uses for authentication.
7 // Specifically, this class needs methods overriding
8 // URLRequest::Delegate::{OnAuthRequired,OnCertificateRequested} and can't
9 // implement them at the net/ layer.
11 #ifndef NET_BASE_SDCH_DICTIONARY_FETCHER_H_
12 #define NET_BASE_SDCH_DICTIONARY_FETCHER_H_
18 #include "base/memory/scoped_ptr.h"
19 #include "base/memory/weak_ptr.h"
20 #include "base/threading/non_thread_safe.h"
21 #include "net/base/sdch_manager.h"
22 #include "net/url_request/url_fetcher_delegate.h"
23 #include "net/url_request/url_request.h"
28 class URLRequestThrottlerEntryInterface
;
30 // This class implements the SdchFetcher interface. It queues requests
31 // for dictionaries and dispatches them serially, implementing
32 // the URLRequest::Delegate interface to handle callbacks (but see above
33 // TODO). It tracks all requests, only attempting to fetch each dictionary
35 class NET_EXPORT SdchDictionaryFetcher
37 public URLRequest::Delegate
,
38 public base::NonThreadSafe
{
40 // The consumer must guarantee that |*consumer| and |*context| outlive
42 SdchDictionaryFetcher(SdchFetcher::Delegate
* consumer
,
43 URLRequestContext
* context
);
44 virtual ~SdchDictionaryFetcher();
46 // Implementation of SdchFetcher methods.
47 virtual void Schedule(const GURL
& dictionary_url
) OVERRIDE
;
48 virtual void Cancel() OVERRIDE
;
50 // Implementation of URLRequest::Delegate methods.
51 virtual void OnResponseStarted(URLRequest
* request
) OVERRIDE
;
52 virtual void OnReadCompleted(URLRequest
* request
, int bytes_read
) OVERRIDE
;
58 STATE_REQUEST_STARTED
,
59 STATE_REQUEST_READING
,
60 STATE_REQUEST_COMPLETE
,
63 // State machine implementation.
65 int DoDispatchRequest(int rv
);
66 int DoRequestStarted(int rv
);
68 int DoCompleteRequest(int rv
);
73 SdchFetcher::Delegate
* const consumer_
;
75 // A queue of URLs that are being used to download dictionaries.
76 std::queue
<GURL
> fetch_queue_
;
78 // The request and buffer used for getting the current dictionary
79 // Both are null when a fetch is not in progress.
80 scoped_ptr
<URLRequest
> current_request_
;
81 scoped_refptr
<IOBuffer
> buffer_
;
83 // The currently accumulating dictionary.
84 std::string dictionary_
;
86 // Althought the SDCH spec does not preclude a server from using a single URL
87 // to load several distinct dictionaries (by telling a client to load a
88 // dictionary from an URL several times), current implementations seem to have
89 // that 1-1 relationship (i.e., each URL points at a single dictionary, and
90 // the dictionary content does not change over time, and hence is not worth
91 // trying to load more than once). In addition, some dictionaries prove
92 // unloadable only after downloading them (because they are too large? ...or
93 // malformed?). As a protective element, Chromium will *only* load a
94 // dictionary at most once from a given URL (so that it doesn't waste
95 // bandwidth trying repeatedly).
96 // The following set lists all the dictionary URLs that we've tried to load,
97 // so that we won't try to load from an URL more than once.
98 // TODO(jar): Try to augment the SDCH proposal to include this restiction.
99 std::set
<GURL
> attempted_load_
;
101 // Store the URLRequestContext associated with the owning SdchManager for
102 // use while fetching.
103 URLRequestContext
* context_
;
105 base::WeakPtrFactory
<SdchDictionaryFetcher
> weak_factory_
;
107 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher
);
112 #endif // NET_BASE_SDCH_DICTIONARY_FETCHER_H_