• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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.
4 
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).
8 
9 #ifndef CHROME_BROWSER_NET_SDCH_DICTIONARY_FETCHER_H_
10 #define CHROME_BROWSER_NET_SDCH_DICTIONARY_FETCHER_H_
11 #pragma once
12 
13 #include <queue>
14 #include <set>
15 #include <string>
16 
17 #include "base/memory/scoped_ptr.h"
18 #include "base/task.h"
19 #include "chrome/common/net/url_fetcher.h"
20 #include "net/base/sdch_manager.h"
21 
22 class SdchDictionaryFetcher : public URLFetcher::Delegate,
23                               public net::SdchFetcher {
24  public:
25   SdchDictionaryFetcher();
26   virtual ~SdchDictionaryFetcher();
27 
28   // Stop fetching dictionaries, and abandon any current URLFetcheer operations
29   // so that the IO thread can be stopped.
30   static void Shutdown();
31 
32   // Implementation of SdchFetcher class.
33   // This method gets the requested dictionary, and then calls back into the
34   // SdchManager class with the dictionary's text.
35   virtual void Schedule(const GURL& dictionary_url);
36 
37  private:
38   // Delay in ms between Schedule and actual download.
39   // This leaves the URL in a queue, which is de-duped, so that there is less
40   // chance we'll try to load the same URL multiple times when a pile of
41   // page subresources (or tabs opened in parallel) all suggest the dictionary.
42   static const int kMsDelayFromRequestTillDownload = 100;
43 
44   // Ensure the download after the above delay.
45   void ScheduleDelayedRun();
46 
47   // Make sure we're processing (or waiting for) the the arrival of the next URL
48   // in the  |fetch_queue_|.
49   void StartFetching();
50 
51   // Implementation of URLFetcher::Delegate. Called after transmission
52   // completes (either successfully or with failure).
53   virtual void OnURLFetchComplete(const URLFetcher* source,
54                                   const GURL& url,
55                                   const net::URLRequestStatus& status,
56                                   int response_code,
57                                   const ResponseCookies& cookies,
58                                   const std::string& data);
59 
60   // A queue of URLs that are being used to download dictionaries.
61   std::queue<GURL> fetch_queue_;
62   // The currently outstanding URL fetch of a dicitonary.
63   // If this is null, then there is no outstanding request.
64   scoped_ptr<URLFetcher> current_fetch_;
65 
66   // Always spread out the dictionary fetches, so that they don't steal
67   // bandwidth from the actual page load.  Create delayed tasks to spread out
68   // the download.
69   ScopedRunnableMethodFactory<SdchDictionaryFetcher> method_factory_;
70   bool task_is_pending_;
71 
72   // Althought the SDCH spec does not preclude a server from using a single URL
73   // to load several distinct dictionaries (by telling a client to load a
74   // dictionary from an URL several times), current implementations seem to have
75   // that 1-1 relationship (i.e., each URL points at a single dictionary, and
76   // the dictionary content does not change over time, and hence is not worth
77   // trying to load more than once).  In addition, some dictionaries prove
78   // unloadable only after downloading them (because they are too large?  ...or
79   // malformed?). As a protective element, Chromium will *only* load a
80   // dictionary at most once from a given URL (so that it doesn't waste
81   // bandwidth trying repeatedly).
82   // The following set lists all the dictionary URLs that we've tried to load,
83   // so that we won't try to load from an URL more than once.
84   // TODO(jar): Try to augment the SDCH proposal to include this restiction.
85   std::set<GURL> attempted_load_;
86 
87   DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher);
88 };
89 
90 #endif  // CHROME_BROWSER_NET_SDCH_DICTIONARY_FETCHER_H_
91