1// Copyright 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_CHROMEOS_POLICY_CLOUD_EXTERNAL_DATA_MANAGER_BASE_H_
6#define CHROME_BROWSER_CHROMEOS_POLICY_CLOUD_EXTERNAL_DATA_MANAGER_BASE_H_
7
8#include "base/basictypes.h"
9#include "base/compiler_specific.h"
10#include "base/memory/ref_counted.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/threading/non_thread_safe.h"
13#include "components/policy/core/common/cloud/cloud_external_data_manager.h"
14#include "components/policy/core/common/policy_details.h"
15
16namespace base {
17class SequencedTaskRunner;
18}
19
20namespace policy {
21
22class CloudExternalDataStore;
23class ExternalPolicyDataFetcherBackend;
24
25// Downloads, verifies, caches and retrieves external data referenced by
26// policies.
27// This is a common base class used by specializations for regular users and
28// device-local accounts.
29class CloudExternalDataManagerBase : public CloudExternalDataManager,
30                                     public base::NonThreadSafe {
31 public:
32  // |get_policy_details| is used to determine the maximum size that the
33  // data referenced by each policy can have. Download scheduling, verification,
34  // caching and retrieval tasks are done via the |backend_task_runner|, which
35  // must support file I/O. Network I/O is done via the |io_task_runner|.
36  CloudExternalDataManagerBase(
37      const GetChromePolicyDetailsCallback& get_policy_details,
38      scoped_refptr<base::SequencedTaskRunner> backend_task_runner,
39      scoped_refptr<base::SequencedTaskRunner> io_task_runner);
40  virtual ~CloudExternalDataManagerBase();
41
42  // Allows downloaded external data to be cached in |external_data_store|.
43  // Ownership of the store is taken. The store can be destroyed by calling
44  // SetExternalDataStore(scoped_ptr<CloudExternalDataStore>()). Accesses to the
45  // store are made via |backend_task_runner_| only.
46  void SetExternalDataStore(
47      scoped_ptr<CloudExternalDataStore> external_data_store);
48
49  // CloudExternalDataManager:
50  virtual void SetPolicyStore(CloudPolicyStore* policy_store) OVERRIDE;
51  virtual void OnPolicyStoreLoaded() OVERRIDE;
52  virtual void Connect(
53      scoped_refptr<net::URLRequestContextGetter> request_context) OVERRIDE;
54  virtual void Disconnect() OVERRIDE;
55  virtual void Fetch(
56      const std::string& policy,
57      const ExternalDataFetcher::FetchCallback& callback) OVERRIDE;
58
59  // Allows policies to reference |max_size| bytes of external data even if no
60  // |max_size| was specified in policy_templates.json.
61  // TODO(bartfab): This override is only needed because there are no policies
62  // that reference external data and have a |max_size| yet. Once the first such
63  // policy is added, use that policy in tests and remove the override.
64  static void SetMaxExternalDataSizeForTesting(int max_size);
65
66 protected:
67  friend class CloudExternalDataManagerBaseTest;
68
69  // Try to download and cache all external data referenced by policies in
70  // |policy_store_|.
71  void FetchAll();
72
73  scoped_refptr<base::SequencedTaskRunner> backend_task_runner_;
74  scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
75
76 private:
77  // The |external_policy_data_fetcher_backend_| handles network I/O for the
78  // |backend_| because URLRequestContextGetter and URLFetchers cannot be
79  // referenced from background threads. It is instantiated on the thread |this|
80  // runs on but after that, must only be accessed and eventually destroyed via
81  // the |io_task_runner_|.
82  scoped_ptr<ExternalPolicyDataFetcherBackend>
83      external_policy_data_fetcher_backend_;
84
85  // The |backend_| handles all data download scheduling, verification, caching
86  // and retrieval. It is instantiated on the thread |this| runs on but after
87  // that, must only be accessed and eventually destroyed via the
88  // |backend_task_runner_|.
89  class Backend;
90  scoped_ptr<Backend> backend_;
91
92  DISALLOW_COPY_AND_ASSIGN(CloudExternalDataManagerBase);
93};
94
95}  // namespace policy
96
97#endif  // CHROME_BROWSER_CHROMEOS_POLICY_CLOUD_EXTERNAL_DATA_MANAGER_BASE_H_
98