summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/sdch_dictionary_fetcher.h
blob: 10cfdbe14c7375fd04e7c180ef12d523ba386e6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Support modularity by calling to load a new SDCH filter dictionary.
// Note that this sort of calling can't be done in the /net directory, as it has
// no concept of the HTTP cache (which is only visible at the browser level).

#ifndef CHROME_BROWSER_NET_SDCH_DICTIONARY_FETCHER_H_
#define CHROME_BROWSER_NET_SDCH_DICTIONARY_FETCHER_H_
#pragma once

#include <queue>
#include <set>
#include <string>

#include "base/memory/scoped_ptr.h"
#include "base/task.h"
#include "content/public/common/url_fetcher_delegate.h"
#include "net/base/sdch_manager.h"

class SdchDictionaryFetcher : public content::URLFetcherDelegate,
                              public net::SdchFetcher {
 public:
  SdchDictionaryFetcher();
  virtual ~SdchDictionaryFetcher();

  // Stop fetching dictionaries, and abandon any current URLFetcheer operations
  // so that the IO thread can be stopped.
  static void Shutdown();

  // Implementation of SdchFetcher class.
  // This method gets the requested dictionary, and then calls back into the
  // SdchManager class with the dictionary's text.
  virtual void Schedule(const GURL& dictionary_url);

 private:
  // Delay in ms between Schedule and actual download.
  // This leaves the URL in a queue, which is de-duped, so that there is less
  // chance we'll try to load the same URL multiple times when a pile of
  // page subresources (or tabs opened in parallel) all suggest the dictionary.
  static const int kMsDelayFromRequestTillDownload = 100;

  // Ensure the download after the above delay.
  void ScheduleDelayedRun();

  // Make sure we're processing (or waiting for) the the arrival of the next URL
  // in the  |fetch_queue_|.
  void StartFetching();

  // Implementation of content::URLFetcherDelegate. Called after transmission
  // completes (either successfully or with failure).
  virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE;

  // A queue of URLs that are being used to download dictionaries.
  std::queue<GURL> fetch_queue_;
  // The currently outstanding URL fetch of a dicitonary.
  // If this is null, then there is no outstanding request.
  scoped_ptr<content::URLFetcher> current_fetch_;

  // Always spread out the dictionary fetches, so that they don't steal
  // bandwidth from the actual page load.  Create delayed tasks to spread out
  // the download.
  ScopedRunnableMethodFactory<SdchDictionaryFetcher> method_factory_;
  bool task_is_pending_;

  // Althought the SDCH spec does not preclude a server from using a single URL
  // to load several distinct dictionaries (by telling a client to load a
  // dictionary from an URL several times), current implementations seem to have
  // that 1-1 relationship (i.e., each URL points at a single dictionary, and
  // the dictionary content does not change over time, and hence is not worth
  // trying to load more than once).  In addition, some dictionaries prove
  // unloadable only after downloading them (because they are too large?  ...or
  // malformed?). As a protective element, Chromium will *only* load a
  // dictionary at most once from a given URL (so that it doesn't waste
  // bandwidth trying repeatedly).
  // The following set lists all the dictionary URLs that we've tried to load,
  // so that we won't try to load from an URL more than once.
  // TODO(jar): Try to augment the SDCH proposal to include this restiction.
  std::set<GURL> attempted_load_;

  DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher);
};

#endif  // CHROME_BROWSER_NET_SDCH_DICTIONARY_FETCHER_H_