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