1 // Copyright 2019 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_UPDATE_CLIENT_NETWORK_H_
6 #define COMPONENTS_UPDATE_CLIENT_NETWORK_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <string>
12 
13 #include "base/callback_forward.h"
14 #include "base/containers/flat_map.h"
15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h"
17 
18 class GURL;
19 
20 namespace base {
21 class FilePath;
22 }  // namespace base
23 
24 namespace update_client {
25 
26 class NetworkFetcher {
27  public:
28   // If the request does not have an X-Retry-After header, implementations
29   // should pass -1 for |xheader_retry_after_sec|.
30   using PostRequestCompleteCallback =
31       base::OnceCallback<void(std::unique_ptr<std::string> response_body,
32                               int net_error,
33                               const std::string& header_etag,
34                               int64_t xheader_retry_after_sec)>;
35   using DownloadToFileCompleteCallback =
36       base::OnceCallback<void(int net_error, int64_t content_size)>;
37   using ResponseStartedCallback =
38       base::OnceCallback<void(int response_code, int64_t content_length)>;
39   using ProgressCallback = base::RepeatingCallback<void(int64_t current)>;
40 
41   // The ETag header carries the ECSDA signature of the POST response, if
42   // signing has been used.
43   static constexpr char kHeaderEtag[] = "ETag";
44 
45   // The server uses the optional X-Retry-After header to indicate that the
46   // current request should not be attempted again.
47   //
48   // The value of the header is the number of seconds to wait before trying to
49   // do a subsequent update check. Only the values retrieved over HTTPS are
50   // trusted.
51   static constexpr char kHeaderXRetryAfter[] = "X-Retry-After";
52 
53   virtual ~NetworkFetcher() = default;
54 
55   virtual void PostRequest(
56       const GURL& url,
57       const std::string& post_data,
58       const base::flat_map<std::string, std::string>& post_additional_headers,
59       ResponseStartedCallback response_started_callback,
60       ProgressCallback progress_callback,
61       PostRequestCompleteCallback post_request_complete_callback) = 0;
62   virtual void DownloadToFile(
63       const GURL& url,
64       const base::FilePath& file_path,
65       ResponseStartedCallback response_started_callback,
66       ProgressCallback progress_callback,
67       DownloadToFileCompleteCallback download_to_file_complete_callback) = 0;
68 
69  protected:
70   NetworkFetcher() = default;
71 
72  private:
73   DISALLOW_COPY_AND_ASSIGN(NetworkFetcher);
74 };
75 
76 class NetworkFetcherFactory : public base::RefCounted<NetworkFetcherFactory> {
77  public:
78   virtual std::unique_ptr<NetworkFetcher> Create() const = 0;
79 
80  protected:
81   friend class base::RefCounted<NetworkFetcherFactory>;
82   NetworkFetcherFactory() = default;
83   virtual ~NetworkFetcherFactory() = default;
84 
85  private:
86   DISALLOW_COPY_AND_ASSIGN(NetworkFetcherFactory);
87 };
88 
89 }  // namespace update_client
90 
91 #endif  // COMPONENTS_UPDATE_CLIENT_NETWORK_H_
92