1 // Copyright (c) 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 CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_
6 #define CHROME_BROWSER_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 "chrome/browser/web_resource/json_asynchronous_unpacker.h"
14 #include "chrome/browser/web_resource/resource_request_allowed_notifier.h"
15 #include "net/url_request/url_fetcher_delegate.h"
21 class DictionaryValue;
28 // A WebResourceService fetches JSON data from a web server and periodically
30 class WebResourceService
31 : public net::URLFetcherDelegate,
32 public JSONAsynchronousUnpackerDelegate,
33 public base::RefCountedThreadSafe<WebResourceService>,
34 public ResourceRequestAllowedNotifier::Observer {
36 WebResourceService(PrefService* prefs,
37 const GURL& web_resource_server,
38 bool apply_locale_to_url_,
39 const char* last_update_time_pref_name,
40 int start_fetch_delay_ms,
41 int cache_update_delay_ms);
43 // Sleep until cache needs to be updated, but always for at least
44 // |start_fetch_delay_ms| so we don't interfere with startup.
45 // Then begin updating resources.
46 void StartAfterDelay();
48 // JSONAsynchronousUnpackerDelegate methods.
49 void OnUnpackFinished(const base::DictionaryValue& parsed_json) override;
50 void OnUnpackError(const std::string& error_message) override;
53 ~WebResourceService() override;
55 // For the subclasses to process the result of a fetch.
56 virtual void Unpack(const base::DictionaryValue& parsed_json) = 0;
62 friend class base::RefCountedThreadSafe<WebResourceService>;
64 // net::URLFetcherDelegate implementation:
65 void OnURLFetchComplete(const net::URLFetcher* source) override;
67 // Schedules a fetch after |delay_ms| milliseconds.
68 void ScheduleFetch(int64 delay_ms);
70 // Starts fetching data from the server.
73 // Set |in_fetch_| to false, clean up temp directories (in the future).
76 // Implements ResourceRequestAllowedNotifier::Observer.
77 void OnResourceRequestsAllowed() override;
79 // Helper class used to tell this service if it's allowed to make network
81 ResourceRequestAllowedNotifier resource_request_allowed_notifier_;
83 // The tool that fetches the url data from the server.
84 scoped_ptr<net::URLFetcher> url_fetcher_;
86 // The tool that parses and transforms the json data. Weak reference as it
87 // deletes itself once the unpack is done.
88 JSONAsynchronousUnpacker* json_unpacker_;
90 // True if we are currently fetching or unpacking data. If we are asked to
91 // start a fetch when we are still fetching resource data, schedule another
92 // one in |cache_update_delay_ms_| time, and silently exit.
95 // URL that hosts the web resource.
96 GURL web_resource_server_;
98 // Indicates whether we should append locale to the web resource server URL.
99 bool apply_locale_to_url_;
101 // Pref name to store the last update's time.
102 const char* last_update_time_pref_name_;
104 // Delay on first fetch so we don't interfere with startup.
105 int start_fetch_delay_ms_;
107 // Delay between calls to update the web resource cache. This delay may be
108 // different for different builds of Chrome.
109 int cache_update_delay_ms_;
111 // So that we can delay our start so as not to affect start-up time; also,
112 // so that we can schedule future cache updates.
113 base::WeakPtrFactory<WebResourceService> weak_ptr_factory_;
115 DISALLOW_COPY_AND_ASSIGN(WebResourceService);
118 #endif // CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_