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 COMPONENTS_POLICY_CORE_COMMON_EXTERNAL_DATA_FETCHER_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_EXTERNAL_DATA_FETCHER_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/callback_forward.h"
12 #include "base/memory/weak_ptr.h"
13 #include "components/policy/policy_export.h"
14 
15 namespace policy {
16 
17 class ExternalDataManager;
18 
19 // A helper that encapsulates the parameters required to retrieve the external
20 // data for a policy.
21 class POLICY_EXPORT ExternalDataFetcher {
22  public:
23   typedef base::Callback<void(std::unique_ptr<std::string>)> FetchCallback;
24 
25   // This instance's Fetch() method will instruct the |manager| to retrieve the
26   // external data referenced by the given |policy|.
27   ExternalDataFetcher(base::WeakPtr<ExternalDataManager> manager,
28                       const std::string& policy);
29   ExternalDataFetcher(const ExternalDataFetcher& other);
30 
31   ~ExternalDataFetcher();
32 
33   static bool Equals(const ExternalDataFetcher* first,
34                      const ExternalDataFetcher* second);
35 
36   // Retrieves the external data referenced by |policy_| and invokes |callback|
37   // with the result. If |policy_| does not reference any external data, the
38   // |callback| is invoked with a NULL pointer. Otherwise, the |callback| is
39   // invoked with the referenced data once it has been successfully retrieved.
40   // If retrieval is temporarily impossible (e.g. no network connectivity), the
41   // |callback| will be invoked when the temporary hindrance is resolved. If
42   // retrieval is permanently impossible (e.g. |policy_| references data that
43   // does not exist on the server), the |callback| will never be invoked.
44   void Fetch(const FetchCallback& callback) const;
45 
46  private:
47   base::WeakPtr<ExternalDataManager> manager_;
48   const std::string policy_;
49 };
50 
51 }  // namespace policy
52 
53 #endif  // COMPONENTS_POLICY_CORE_COMMON_EXTERNAL_DATA_FETCHER_H_
54