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