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