| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_PREFETCH_REQUEST_FETCHER_H_ | 5 #ifndef COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_PREFETCH_REQUEST_FETCHER_H_ |
| 6 #define COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_PREFETCH_REQUEST_FETCHER_H_ | 6 #define COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_PREFETCH_REQUEST_FETCHER_H_ |
| 7 | 7 |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "components/offline_pages/core/prefetch/prefetch_types.h" | 11 #include "components/offline_pages/core/prefetch/prefetch_types.h" |
| 12 #include "net/url_request/url_fetcher_delegate.h" | 12 #include "net/url_request/url_fetcher_delegate.h" |
| 13 | 13 |
| 14 class GURL; | |
| 15 namespace net { | 14 namespace net { |
| 16 class URLRequestContextGetter; | 15 class URLRequestContextGetter; |
| 17 } | 16 } |
| 18 | 17 |
| 19 namespace offline_pages { | 18 namespace offline_pages { |
| 20 | 19 |
| 20 // Asynchronously fetches the offline prefetch related data from the server. |
| 21 class PrefetchRequestFetcher : public net::URLFetcherDelegate { | 21 class PrefetchRequestFetcher : public net::URLFetcherDelegate { |
| 22 public: | 22 public: |
| 23 using FinishedCallback = base::Callback<void(PrefetchRequestStatus status, | 23 using FinishedCallback = base::Callback<void(PrefetchRequestStatus status, |
| 24 const std::string& data)>; | 24 const std::string& data)>; |
| 25 | 25 |
| 26 PrefetchRequestFetcher(const GURL& url, | 26 // Creates a fetcher that will sends a GET request to the server. |
| 27 const std::string& message, | 27 static std::unique_ptr<PrefetchRequestFetcher> CreateForGet( |
| 28 net::URLRequestContextGetter* request_context_getter, | 28 const std::string& url_path, |
| 29 const FinishedCallback& callback); | 29 net::URLRequestContextGetter* request_context_getter, |
| 30 const FinishedCallback& callback); |
| 31 |
| 32 // Creates a fetcher that will sends a POST request to the server. |
| 33 static std::unique_ptr<PrefetchRequestFetcher> CreateForPost( |
| 34 const std::string& url_path, |
| 35 const std::string& message, |
| 36 net::URLRequestContextGetter* request_context_getter, |
| 37 const FinishedCallback& callback); |
| 38 |
| 30 ~PrefetchRequestFetcher() override; | 39 ~PrefetchRequestFetcher() override; |
| 31 | 40 |
| 32 // URLFetcherDelegate implementation. | 41 // URLFetcherDelegate implementation. |
| 33 void OnURLFetchComplete(const net::URLFetcher* source) override; | 42 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 34 | 43 |
| 35 private: | 44 private: |
| 45 // If |message| is empty, the GET request is sent. Otherwise, the POST request |
| 46 // is sent with |message| as post data. |
| 47 PrefetchRequestFetcher(const std::string& url_path, |
| 48 const std::string& message, |
| 49 net::URLRequestContextGetter* request_context_getter, |
| 50 const FinishedCallback& callback); |
| 51 |
| 36 PrefetchRequestStatus ParseResponse(const net::URLFetcher* source, | 52 PrefetchRequestStatus ParseResponse(const net::URLFetcher* source, |
| 37 std::string* data); | 53 std::string* data); |
| 38 | 54 |
| 39 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 55 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 40 std::unique_ptr<net::URLFetcher> url_fetcher_; | 56 std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 41 FinishedCallback callback_; | 57 FinishedCallback callback_; |
| 42 | 58 |
| 43 DISALLOW_COPY_AND_ASSIGN(PrefetchRequestFetcher); | 59 DISALLOW_COPY_AND_ASSIGN(PrefetchRequestFetcher); |
| 44 }; | 60 }; |
| 45 | 61 |
| 46 } // namespace offline_pages | 62 } // namespace offline_pages |
| 47 | 63 |
| 48 #endif // COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_PREFETCH_REQUEST_FETCHER_H_ | 64 #endif // COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_PREFETCH_REQUEST_FETCHER_H_ |
| OLD | NEW |