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/weak_ptr.h" 16 #include "base/threading/non_thread_safe.h" 17 18 namespace base { 19 class SequencedTaskRunner; 20 } 21 22 namespace net { 23 class URLRequestContextGetter; 24 } 25 26 namespace policy { 27 28 // This class downloads external policy data. Given a |Request|, data is fetched 29 // from the |url|, verified to not exceed |max_size| and to match the expected 30 // |hash| and then handed to a callback that can do further verification before 31 // finally deciding whether the fetched data is valid. 32 // If a fetch is not successful or retrieves invalid data, retries are scheduled 33 // with exponential backoff. 34 class ExternalPolicyDataUpdater : public base::NonThreadSafe { 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 // |task_runner| must support file I/O, and is used to post delayed retry 62 // tasks. 63 // |request_context| will be used for the download fetchers. 64 ExternalPolicyDataUpdater( 65 scoped_refptr<base::SequencedTaskRunner> task_runner, 66 scoped_refptr<net::URLRequestContextGetter> request_context, 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_refptr<net::URLRequestContextGetter> request_context_; 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 // A monotonically increasing job ID. Used to identify jobs in tests. 110 int next_job_id_; 111 112 // Queue of jobs waiting to be run. Jobs are taken off the queue and started 113 // by StartNextJobs(). 114 std::queue<base::WeakPtr<FetchJob> > job_queue_; 115 116 // Map that owns all existing jobs, regardless of whether they are currently 117 // queued, running or waiting for a retry. 118 std::map<std::string, FetchJob*> job_map_; 119 120 // |True| once the destructor starts. Prevents jobs from being started during 121 // shutdown. 122 bool shutting_down_; 123 124 DISALLOW_COPY_AND_ASSIGN(ExternalPolicyDataUpdater); 125 }; 126 127 } // namespace policy 128 129 #endif // CHROME_BROWSER_POLICY_CLOUD_EXTERNAL_POLICY_DATA_UPDATER_H_ 130