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. 4 5 #ifndef CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ 6 #define CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ 7 8 #include <string> 9 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" 16 #include "url/gurl.h" 17 18 class PrefService; 19 20 namespace base { 21 class DictionaryValue; 22 } 23 24 namespace net { 25 class URLFetcher; 26 } 27 28 // A WebResourceService fetches JSON data from a web server and periodically 29 // refreshes it. 30 class WebResourceService 31 : public net::URLFetcherDelegate, 32 public JSONAsynchronousUnpackerDelegate, 33 public base::RefCountedThreadSafe<WebResourceService>, 34 public ResourceRequestAllowedNotifier::Observer { 35 public: 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); 42 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(); 47 48 // JSONAsynchronousUnpackerDelegate methods. 49 virtual void OnUnpackFinished(const DictionaryValue& parsed_json) OVERRIDE; 50 virtual void OnUnpackError(const std::string& error_message) OVERRIDE; 51 52 protected: 53 virtual ~WebResourceService(); 54 55 // For the subclasses to process the result of a fetch. 56 virtual void Unpack(const base::DictionaryValue& parsed_json) = 0; 57 58 PrefService* prefs_; 59 60 private: 61 class UnpackerClient; 62 friend class base::RefCountedThreadSafe<WebResourceService>; 63 64 // net::URLFetcherDelegate implementation: 65 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 66 67 // Schedules a fetch after |delay_ms| milliseconds. 68 void ScheduleFetch(int64 delay_ms); 69 70 // Starts fetching data from the server. 71 void StartFetch(); 72 73 // Set |in_fetch_| to false, clean up temp directories (in the future). 74 void EndFetch(); 75 76 // Implements ResourceRequestAllowedNotifier::Observer. 77 virtual void OnResourceRequestsAllowed() OVERRIDE; 78 79 // Helper class used to tell this service if it's allowed to make network 80 // resource requests. 81 ResourceRequestAllowedNotifier resource_request_allowed_notifier_; 82 83 // The tool that fetches the url data from the server. 84 scoped_ptr<net::URLFetcher> url_fetcher_; 85 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_; 89 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. 93 bool in_fetch_; 94 95 // URL that hosts the web resource. 96 GURL web_resource_server_; 97 98 // Indicates whether we should append locale to the web resource server URL. 99 bool apply_locale_to_url_; 100 101 // Pref name to store the last update's time. 102 const char* last_update_time_pref_name_; 103 104 // Delay on first fetch so we don't interfere with startup. 105 int start_fetch_delay_ms_; 106 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_; 110 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_; 114 115 DISALLOW_COPY_AND_ASSIGN(WebResourceService); 116 }; 117 118 #endif // CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ 119