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