Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / components / web_resource / web_resource_service.h
blob85cd899237a9ef23baac72706f1f637c126952c6
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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_
6 #define COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_
8 #include <string>
10 #include "base/callback_forward.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "components/web_resource/resource_request_allowed_notifier.h"
15 #include "net/url_request/url_fetcher_delegate.h"
16 #include "url/gurl.h"
18 class PrefService;
20 namespace base {
21 class DictionaryValue;
22 class Value;
25 namespace net {
26 class URLFetcher;
27 class URLRequestContextGetter;
30 namespace web_resource {
32 // A WebResourceService fetches JSON data from a web server and periodically
33 // refreshes it.
34 class WebResourceService
35 : public net::URLFetcherDelegate,
36 public ResourceRequestAllowedNotifier::Observer {
37 public:
38 // Callbacks for JSON parsing.
39 using SuccessCallback = base::Callback<void(scoped_ptr<base::Value>)>;
40 using ErrorCallback = base::Callback<void(const std::string&)>;
41 using ParseJSONCallback = base::Callback<
42 void(const std::string&, const SuccessCallback&, const ErrorCallback&)>;
44 // Creates a new WebResourceService.
45 // If |application_locale| is not empty, it will be appended as a locale
46 // parameter to the resource URL.
47 WebResourceService(PrefService* prefs,
48 const GURL& web_resource_server,
49 const std::string& application_locale, // May be empty
50 const char* last_update_time_pref_name,
51 int start_fetch_delay_ms,
52 int cache_update_delay_ms,
53 net::URLRequestContextGetter* request_context,
54 const char* disable_network_switch,
55 const ParseJSONCallback& parse_json_callback);
57 ~WebResourceService() override;
59 // Sleep until cache needs to be updated, but always for at least
60 // |start_fetch_delay_ms| so we don't interfere with startup.
61 // Then begin updating resources.
62 void StartAfterDelay();
64 protected:
65 PrefService* prefs_;
67 private:
68 // For the subclasses to process the result of a fetch.
69 virtual void Unpack(const base::DictionaryValue& parsed_json) = 0;
71 // net::URLFetcherDelegate implementation:
72 void OnURLFetchComplete(const net::URLFetcher* source) override;
74 // Schedules a fetch after |delay_ms| milliseconds.
75 void ScheduleFetch(int64 delay_ms);
77 // Starts fetching data from the server.
78 void StartFetch();
80 // Set |in_fetch_| to false, clean up temp directories (in the future).
81 void EndFetch();
83 // Callbacks from the JSON parser.
84 void OnUnpackFinished(scoped_ptr<base::Value> value);
85 void OnUnpackError(const std::string& error_message);
87 // Implements ResourceRequestAllowedNotifier::Observer.
88 void OnResourceRequestsAllowed() override;
90 // Helper class used to tell this service if it's allowed to make network
91 // resource requests.
92 ResourceRequestAllowedNotifier resource_request_allowed_notifier_;
94 // The tool that fetches the url data from the server.
95 scoped_ptr<net::URLFetcher> url_fetcher_;
97 // True if we are currently fetching or unpacking data. If we are asked to
98 // start a fetch when we are still fetching resource data, schedule another
99 // one in |cache_update_delay_ms_| time, and silently exit.
100 bool in_fetch_;
102 // URL that hosts the web resource.
103 GURL web_resource_server_;
105 // Application locale, appended to the URL if not empty.
106 std::string application_locale_;
108 // Pref name to store the last update's time.
109 const char* last_update_time_pref_name_;
111 // Delay on first fetch so we don't interfere with startup.
112 int start_fetch_delay_ms_;
114 // Delay between calls to update the web resource cache. This delay may be
115 // different for different builds of Chrome.
116 int cache_update_delay_ms_;
118 // The request context for the resource fetch.
119 scoped_refptr<net::URLRequestContextGetter> request_context_;
121 // Callback used to parse JSON.
122 ParseJSONCallback parse_json_callback_;
124 // So that we can delay our start so as not to affect start-up time; also,
125 // so that we can schedule future cache updates.
126 base::WeakPtrFactory<WebResourceService> weak_ptr_factory_;
128 DISALLOW_COPY_AND_ASSIGN(WebResourceService);
131 } // namespace web_resource
133 #endif // COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_