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 delegate URLRequest::Delegate methods
6 // to the net/ embedder for correct implementation of authentication.
7 // Specifically, this class needs the embedder to provide functionality
9 // URLRequest::Delegate::{OnAuthRequired,OnCertificateRequested}.
11 #ifndef NET_URL_REQUEST_SDCH_DICTIONARY_FETCHER_H_
12 #define NET_URL_REQUEST_SDCH_DICTIONARY_FETCHER_H_
16 #include "base/macros.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/threading/non_thread_safe.h"
20 #include "net/base/sdch_manager.h"
21 #include "net/url_request/url_fetcher_delegate.h"
22 #include "net/url_request/url_request.h"
29 class URLRequestThrottlerEntryInterface
;
31 // This class is used by embedder SDCH policy object to fetch
32 // dictionaries. It queues requests for dictionaries and dispatches
33 // them serially, implementing the URLRequest::Delegate interface to
34 // handle callbacks (but see above TODO). It tracks all requests, only
35 // attempting to fetch each dictionary once.
36 class NET_EXPORT SdchDictionaryFetcher
: public URLRequest::Delegate
,
37 public base::NonThreadSafe
{
39 typedef base::Callback
<void(const std::string
& dictionary_text
,
40 const GURL
& dictionary_url
,
41 const BoundNetLog
& net_log
,
43 OnDictionaryFetchedCallback
;
45 // The consumer must guarantee that |*context| outlives this object.
46 explicit SdchDictionaryFetcher(URLRequestContext
* context
);
47 ~SdchDictionaryFetcher() override
;
49 // Request a new dictionary fetch. The callback will be called
50 // only if the dictionary is successfully fetched. Returns true if a
51 // request for |dictionary_url| has been scheduled, and false otherwise.
52 virtual bool Schedule(const GURL
& dictionary_url
,
53 const OnDictionaryFetchedCallback
& callback
);
55 // Request a dictionary fetch from cache only. The callback will be called
56 // only if the dictionary is successfully fetched. Returns true if a request
57 // for |dictionary_url| has been scheduled, and false otherwise.
58 virtual bool ScheduleReload(const GURL
& dictionary_url
,
59 const OnDictionaryFetchedCallback
& callback
);
61 // Cancel any in-progress requests.
62 virtual void Cancel();
64 // Implementation of URLRequest::Delegate methods.
65 void OnResponseStarted(URLRequest
* request
) override
;
66 void OnReadCompleted(URLRequest
* request
, int bytes_read
) override
;
72 STATE_SEND_REQUEST_COMPLETE
,
74 STATE_READ_BODY_COMPLETE
,
75 STATE_REQUEST_COMPLETE
,
78 class UniqueFetchQueue
;
80 // Schedule implementation. Returns true if a request for |dictionary_url| has
81 // been added to the queue, and false otherwise.
82 bool ScheduleInternal(const GURL
& dictionary_url
,
84 const OnDictionaryFetchedCallback
& callback
);
86 // Null out the current request and push the state machine to the
87 // next request, if any.
90 // State machine implementation.
92 int DoSendRequest(int rv
);
93 int DoSendRequestComplete(int rv
);
94 int DoReadBody(int rv
);
95 int DoReadBodyComplete(int rv
);
96 int DoCompleteRequest(int rv
);
101 // A queue of URLs that are being used to download dictionaries.
102 scoped_ptr
<UniqueFetchQueue
> fetch_queue_
;
104 // The request, buffer, and consumer supplied data used for getting
105 // the current dictionary. All are null when a fetch is not in progress.
106 scoped_ptr
<URLRequest
> current_request_
;
107 scoped_refptr
<IOBuffer
> buffer_
;
108 OnDictionaryFetchedCallback current_callback_
;
110 // The currently accumulating dictionary.
111 std::string dictionary_
;
113 // Store the URLRequestContext associated with the owning SdchManager for
114 // use while fetching.
115 URLRequestContext
* const context_
;
117 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher
);
122 #endif // NET_URL_REQUEST_SDCH_DICTIONARY_FETCHER_H_