OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ | 5 #ifndef COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ |
6 #define COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ | 6 #define COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
| 10 #include <memory> |
10 #include <string> | 11 #include <string> |
11 | 12 |
12 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
17 #include "components/web_resource/resource_request_allowed_notifier.h" | 17 #include "components/web_resource/resource_request_allowed_notifier.h" |
18 #include "net/url_request/url_fetcher_delegate.h" | 18 #include "net/url_request/url_fetcher_delegate.h" |
19 #include "url/gurl.h" | 19 #include "url/gurl.h" |
20 | 20 |
21 class PrefService; | 21 class PrefService; |
22 | 22 |
23 namespace base { | 23 namespace base { |
24 class DictionaryValue; | 24 class DictionaryValue; |
25 class Value; | 25 class Value; |
26 } | 26 } |
27 | 27 |
28 namespace net { | 28 namespace net { |
29 class URLFetcher; | 29 class URLFetcher; |
30 class URLRequestContextGetter; | 30 class URLRequestContextGetter; |
31 } | 31 } |
32 | 32 |
33 namespace web_resource { | 33 namespace web_resource { |
34 | 34 |
35 // A WebResourceService fetches JSON data from a web server and periodically | 35 // A WebResourceService fetches JSON data from a web server and periodically |
36 // refreshes it. | 36 // refreshes it. |
37 class WebResourceService | 37 class WebResourceService |
38 : public net::URLFetcherDelegate, | 38 : public net::URLFetcherDelegate, |
39 public ResourceRequestAllowedNotifier::Observer { | 39 public ResourceRequestAllowedNotifier::Observer { |
40 public: | 40 public: |
41 // Callbacks for JSON parsing. | 41 // Callbacks for JSON parsing. |
42 using SuccessCallback = base::Callback<void(scoped_ptr<base::Value>)>; | 42 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; |
43 using ErrorCallback = base::Callback<void(const std::string&)>; | 43 using ErrorCallback = base::Callback<void(const std::string&)>; |
44 using ParseJSONCallback = base::Callback< | 44 using ParseJSONCallback = base::Callback< |
45 void(const std::string&, const SuccessCallback&, const ErrorCallback&)>; | 45 void(const std::string&, const SuccessCallback&, const ErrorCallback&)>; |
46 | 46 |
47 // Creates a new WebResourceService. | 47 // Creates a new WebResourceService. |
48 // If |application_locale| is not empty, it will be appended as a locale | 48 // If |application_locale| is not empty, it will be appended as a locale |
49 // parameter to the resource URL. | 49 // parameter to the resource URL. |
50 WebResourceService(PrefService* prefs, | 50 WebResourceService(PrefService* prefs, |
51 const GURL& web_resource_server, | 51 const GURL& web_resource_server, |
52 const std::string& application_locale, // May be empty | 52 const std::string& application_locale, // May be empty |
(...skipping 24 matching lines...) Expand all Loading... |
77 // Schedules a fetch after |delay_ms| milliseconds. | 77 // Schedules a fetch after |delay_ms| milliseconds. |
78 void ScheduleFetch(int64_t delay_ms); | 78 void ScheduleFetch(int64_t delay_ms); |
79 | 79 |
80 // Starts fetching data from the server. | 80 // Starts fetching data from the server. |
81 void StartFetch(); | 81 void StartFetch(); |
82 | 82 |
83 // Set |in_fetch_| to false, clean up temp directories (in the future). | 83 // Set |in_fetch_| to false, clean up temp directories (in the future). |
84 void EndFetch(); | 84 void EndFetch(); |
85 | 85 |
86 // Callbacks from the JSON parser. | 86 // Callbacks from the JSON parser. |
87 void OnUnpackFinished(scoped_ptr<base::Value> value); | 87 void OnUnpackFinished(std::unique_ptr<base::Value> value); |
88 void OnUnpackError(const std::string& error_message); | 88 void OnUnpackError(const std::string& error_message); |
89 | 89 |
90 // Implements ResourceRequestAllowedNotifier::Observer. | 90 // Implements ResourceRequestAllowedNotifier::Observer. |
91 void OnResourceRequestsAllowed() override; | 91 void OnResourceRequestsAllowed() override; |
92 | 92 |
93 // Helper class used to tell this service if it's allowed to make network | 93 // Helper class used to tell this service if it's allowed to make network |
94 // resource requests. | 94 // resource requests. |
95 ResourceRequestAllowedNotifier resource_request_allowed_notifier_; | 95 ResourceRequestAllowedNotifier resource_request_allowed_notifier_; |
96 | 96 |
97 // The tool that fetches the url data from the server. | 97 // The tool that fetches the url data from the server. |
98 scoped_ptr<net::URLFetcher> url_fetcher_; | 98 std::unique_ptr<net::URLFetcher> url_fetcher_; |
99 | 99 |
100 // True if we are currently fetching or unpacking data. If we are asked to | 100 // True if we are currently fetching or unpacking data. If we are asked to |
101 // start a fetch when we are still fetching resource data, schedule another | 101 // start a fetch when we are still fetching resource data, schedule another |
102 // one in |cache_update_delay_ms_| time, and silently exit. | 102 // one in |cache_update_delay_ms_| time, and silently exit. |
103 bool in_fetch_; | 103 bool in_fetch_; |
104 | 104 |
105 // URL that hosts the web resource. | 105 // URL that hosts the web resource. |
106 GURL web_resource_server_; | 106 GURL web_resource_server_; |
107 | 107 |
108 // Application locale, appended to the URL if not empty. | 108 // Application locale, appended to the URL if not empty. |
(...skipping 18 matching lines...) Expand all Loading... |
127 // So that we can delay our start so as not to affect start-up time; also, | 127 // So that we can delay our start so as not to affect start-up time; also, |
128 // so that we can schedule future cache updates. | 128 // so that we can schedule future cache updates. |
129 base::WeakPtrFactory<WebResourceService> weak_ptr_factory_; | 129 base::WeakPtrFactory<WebResourceService> weak_ptr_factory_; |
130 | 130 |
131 DISALLOW_COPY_AND_ASSIGN(WebResourceService); | 131 DISALLOW_COPY_AND_ASSIGN(WebResourceService); |
132 }; | 132 }; |
133 | 133 |
134 } // namespace web_resource | 134 } // namespace web_resource |
135 | 135 |
136 #endif // COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ | 136 #endif // COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ |
OLD | NEW |