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_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "components/web_resource/resource_request_allowed_notifier.h"
14 #include "net/url_request/url_fetcher_delegate.h"
20 class DictionaryValue
;
26 class URLRequestContextGetter
;
29 namespace web_resource
{
31 // A WebResourceService fetches JSON data from a web server and periodically
33 class WebResourceService
34 : public net::URLFetcherDelegate
,
35 public ResourceRequestAllowedNotifier::Observer
{
37 // Creates a new WebResourceService.
38 // If |application_locale| is not empty, it will be appended as a locale
39 // parameter to the resource URL.
40 WebResourceService(PrefService
* prefs
,
41 const GURL
& web_resource_server
,
42 const std::string
& application_locale
, // May be empty
43 const char* last_update_time_pref_name
,
44 int start_fetch_delay_ms
,
45 int cache_update_delay_ms
,
46 net::URLRequestContextGetter
* request_context
,
47 const char* disable_network_switch
);
49 ~WebResourceService() override
;
51 // Sleep until cache needs to be updated, but always for at least
52 // |start_fetch_delay_ms| so we don't interfere with startup.
53 // Then begin updating resources.
54 void StartAfterDelay();
57 // Callbacks for JSON parsing.
58 using SuccessCallback
= base::Callback
<void(scoped_ptr
<base::Value
>)>;
59 using ErrorCallback
= base::Callback
<void(const std::string
&)>;
64 // For the subclasses to process the result of a fetch.
65 virtual void Unpack(const base::DictionaryValue
& parsed_json
) = 0;
67 // Parses a JSON string |data| and invokes |success_callback| or
69 virtual void ParseJSON(const std::string
& data
,
70 const SuccessCallback
& success_callback
,
71 const ErrorCallback
& error_callback
) = 0;
73 // net::URLFetcherDelegate implementation:
74 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
76 // Schedules a fetch after |delay_ms| milliseconds.
77 void ScheduleFetch(int64 delay_ms
);
79 // Starts fetching data from the server.
82 // Set |in_fetch_| to false, clean up temp directories (in the future).
85 // Callbacks from the JSON parser.
86 void OnUnpackFinished(scoped_ptr
<base::Value
> value
);
87 void OnUnpackError(const std::string
& error_message
);
89 // Implements ResourceRequestAllowedNotifier::Observer.
90 void OnResourceRequestsAllowed() override
;
92 // Helper class used to tell this service if it's allowed to make network
94 ResourceRequestAllowedNotifier resource_request_allowed_notifier_
;
96 // The tool that fetches the url data from the server.
97 scoped_ptr
<net::URLFetcher
> url_fetcher_
;
99 // True if we are currently fetching or unpacking data. If we are asked to
100 // start a fetch when we are still fetching resource data, schedule another
101 // one in |cache_update_delay_ms_| time, and silently exit.
104 // URL that hosts the web resource.
105 GURL web_resource_server_
;
107 // Application locale, appended to the URL if not empty.
108 std::string application_locale_
;
110 // Pref name to store the last update's time.
111 const char* last_update_time_pref_name_
;
113 // Delay on first fetch so we don't interfere with startup.
114 int start_fetch_delay_ms_
;
116 // Delay between calls to update the web resource cache. This delay may be
117 // different for different builds of Chrome.
118 int cache_update_delay_ms_
;
120 // The request context for the resource fetch.
121 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
123 // So that we can delay our start so as not to affect start-up time; also,
124 // so that we can schedule future cache updates.
125 base::WeakPtrFactory
<WebResourceService
> weak_ptr_factory_
;
127 DISALLOW_COPY_AND_ASSIGN(WebResourceService
);
130 } // namespace web_resource
132 #endif // COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_