• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 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 #ifndef COMPONENTS_POLICY_CORE_COMMON_CLOUD_EXTERNAL_POLICY_DATA_UPDATER_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_CLOUD_EXTERNAL_POLICY_DATA_UPDATER_H_
7 
8 #include <map>
9 #include <queue>
10 #include <string>
11 
12 #include "base/basictypes.h"
13 #include "base/callback_forward.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "components/policy/policy_export.h"
18 
19 namespace base {
20 class SequencedTaskRunner;
21 }
22 
23 namespace policy {
24 
25 class ExternalPolicyDataFetcher;
26 
27 // This class downloads external policy data. Given a |Request|, data is fetched
28 // from the |url|, verified to not exceed |max_size| and to match the expected
29 // |hash| and then handed to a callback that can do further verification before
30 // finally deciding whether the fetched data is valid.
31 // If a fetch is not successful or retrieves invalid data, retries are scheduled
32 // with exponential backoff.
33 // The actual fetching is handled by an ExternalPolicyDataFetcher, allowing this
34 // class to run on a background thread where network I/O is not possible.
35 class POLICY_EXPORT ExternalPolicyDataUpdater {
36  public:
37   struct POLICY_EXPORT Request {
38    public:
39     Request();
40     Request(const std::string& url, const std::string& hash, int64 max_size);
41 
42     bool operator==(const Request& other) const;
43 
44     std::string url;
45     std::string hash;
46     int64 max_size;
47   };
48 
49   // This callback is invoked when a fetch has successfully retrieved |data|
50   // that does not exceed |max_size| and matches the expected |hash|. The
51   // callback can do further verification to decide whether the fetched data is
52   // valid.
53   // If the callback returns |true|, the data is accepted and the |Request| is
54   // finished. If the callback returns |false|, the data is rejected and the
55   // fetch is retried after a long backoff. Note that in this case, the callback
56   // may be invoked multiple times as the fetch is repeated. Make sure to not
57   // bind base::Passed() scoped_ptrs to the callback in such cases as these
58   // become invalid after a callback has been run once. base::Owned() can be
59   // used in all cases.
60   typedef base::Callback<bool(const std::string&)> FetchSuccessCallback;
61 
62   // This class runs on the background thread represented by |task_runner|,
63   // which must support file I/O. All network I/O is forwarded to a different
64   // thread by the |external_policy_data_fetcher|.
65   ExternalPolicyDataUpdater(
66       scoped_refptr<base::SequencedTaskRunner> task_runner,
67       scoped_ptr<ExternalPolicyDataFetcher> external_policy_data_fetcher,
68       size_t max_parallel_fetches);
69   ~ExternalPolicyDataUpdater();
70 
71   // Fetches the external data specified in the |request|. The |key| is an
72   // opaque identifier. If another request for the same |key| is still pending,
73   // it will be canceled and replaced with the new |request|. The callback will
74   // be invoked after a successful fetch. See the documentation of
75   // |FetchSuccessCallback| for more details.
76   void FetchExternalData(const std::string key,
77                          const Request& request,
78                          const FetchSuccessCallback& callback);
79 
80   // Cancels the pending request identified by |key|. If no such request is
81   // pending, does nothing.
82   void CancelExternalDataFetch(const std::string& key);
83 
84  private:
85   class FetchJob;
86 
87   // Starts jobs from the |job_queue_| until |max_parallel_jobs_| are running or
88   // the queue is depleted.
89   void StartNextJobs();
90 
91   // Appends |job| to the |job_queue_| and starts it immediately if less than
92   // |max_parallel_jobs_| are running.
93   void ScheduleJob(FetchJob* job);
94 
95   // Callback for jobs that succeeded.
96   void OnJobSucceeded(FetchJob* job);
97 
98   // Callback for jobs that failed.
99   void OnJobFailed(FetchJob* job);
100 
101   scoped_refptr<base::SequencedTaskRunner> task_runner_;
102   scoped_ptr<ExternalPolicyDataFetcher> external_policy_data_fetcher_;
103 
104   // The maximum number of jobs to run in parallel.
105   size_t max_parallel_jobs_;
106 
107   // The number of jobs currently running.
108   size_t running_jobs_;
109 
110   // Queue of jobs waiting to be run. Jobs are taken off the queue and started
111   // by StartNextJobs().
112   std::queue<base::WeakPtr<FetchJob> > job_queue_;
113 
114   // Map that owns all existing jobs, regardless of whether they are currently
115   // queued, running or waiting for a retry.
116   std::map<std::string, FetchJob*> job_map_;
117 
118   // |True| once the destructor starts. Prevents jobs from being started during
119   // shutdown.
120   bool shutting_down_;
121 
122   DISALLOW_COPY_AND_ASSIGN(ExternalPolicyDataUpdater);
123 };
124 
125 }  // namespace policy
126 
127 #endif  // COMPONENTS_POLICY_CORE_COMMON_CLOUD_EXTERNAL_POLICY_DATA_UPDATER_H_
128