| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ |
| 6 #define CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ | 6 #define CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "content/browser/utility_process_host.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "content/public/browser/notification_types.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "content/public/common/url_fetcher_delegate.h" |
| 13 | 15 |
| 14 class PrefService; | 16 class PrefService; |
| 15 class ResourceDispatcherHost; | 17 class ResourceDispatcherHost; |
| 16 | 18 |
| 17 namespace base { | 19 namespace base { |
| 18 class DictionaryValue; | 20 class DictionaryValue; |
| 19 } | 21 } |
| 20 | 22 |
| 21 // A WebResourceService fetches data from a web resource server and store | 23 // A WebResourceService fetches JSON data from a web server and periodically |
| 22 // locally as user preference. | 24 // refreshes it. |
| 23 class WebResourceService : public UtilityProcessHost::Client { | 25 class WebResourceService |
| 26 : public base::RefCountedThreadSafe<WebResourceService>, |
| 27 public content::URLFetcherDelegate { |
| 24 public: | 28 public: |
| 25 // Pass notification_type = NOTIFICATION_TYPE_COUNT if notification is not | |
| 26 // required. | |
| 27 WebResourceService(PrefService* prefs, | 29 WebResourceService(PrefService* prefs, |
| 28 const char* web_resource_server, | 30 const char* web_resource_server, |
| 29 bool apply_locale_to_url_, | 31 bool apply_locale_to_url_, |
| 30 int notification_type, | |
| 31 const char* last_update_time_pref_name, | 32 const char* last_update_time_pref_name, |
| 32 int start_fetch_delay, | 33 int start_fetch_delay_ms, |
| 33 int cache_update_delay); | 34 int cache_update_delay_ms); |
| 34 | 35 |
| 35 // Sleep until cache needs to be updated, but always for at least 5 seconds | 36 // Sleep until cache needs to be updated, but always for at least |
| 36 // so we don't interfere with startup. Then begin updating resources. | 37 // |start_fetch_delay_ms| so we don't interfere with startup. |
| 37 void StartAfterDelay(); | 38 // Then begin updating resources. |
| 38 | 39 // TODO(bauerb): Comment |
| 39 // We have successfully pulled data from a resource server; now launch | 40 bool StartAfterDelay(bool only_if_necessary); |
| 40 // the process that will parse the JSON, and then update the cache. | |
| 41 void UpdateResourceCache(const std::string& json_data); | |
| 42 | 41 |
| 43 protected: | 42 protected: |
| 44 virtual ~WebResourceService(); | 43 virtual ~WebResourceService(); |
| 44 friend class base::RefCountedThreadSafe<WebResourceService>; |
| 45 |
| 46 // TODO(bauerb): comment |
| 47 static void RegisterLastUpdateTimePref(PrefService* prefs, |
| 48 const char* pref_name); |
| 45 | 49 |
| 46 virtual void Unpack(const base::DictionaryValue& parsed_json) = 0; | 50 virtual void Unpack(const base::DictionaryValue& parsed_json) = 0; |
| 47 | 51 |
| 48 // If delay_ms is positive, schedule notification with the delay. | |
| 49 // If delay_ms is 0, notify immediately by calling WebResourceStateChange(). | |
| 50 // If delay_ms is negative, do nothing. | |
| 51 void PostNotification(int64 delay_ms); | |
| 52 | |
| 53 // We need to be able to load parsed resource data into preferences file, | 52 // We need to be able to load parsed resource data into preferences file, |
| 54 // and get proper install directory. | 53 // and get proper install directory. |
| 55 PrefService* prefs_; | 54 PrefService* prefs_; |
| 56 | 55 |
| 57 private: | 56 private: |
| 58 class WebResourceFetcher; | |
| 59 friend class WebResourceFetcher; | |
| 60 | |
| 61 class UnpackerClient; | 57 class UnpackerClient; |
| 62 | 58 |
| 63 // Set in_fetch_ to false, clean up temp directories (in the future). | 59 // content::URLFetcherDelegate implementation: |
| 60 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 61 |
| 62 // Schedules a fetch after |delay_ms| milliseconds. |
| 63 void ScheduleFetch(int64 delay_ms); |
| 64 |
| 65 // Starts fetching data from the server. |
| 66 void StartFetch(); |
| 67 |
| 68 // Set |in_fetch_| to false, clean up temp directories (in the future). |
| 64 void EndFetch(); | 69 void EndFetch(); |
| 65 | 70 |
| 66 // Puts parsed json data in the right places, and writes to prefs file. | 71 // So that we can delay our start so as not to affect start-up time; also, |
| 67 void OnWebResourceUnpacked(const base::DictionaryValue& parsed_json); | 72 // so that we can schedule future cache updates. |
| 73 base::WeakPtrFactory<WebResourceService> weak_ptr_factory_; |
| 68 | 74 |
| 69 // Notify listeners that the state of a web resource has changed. | 75 // The tool that fetches the url data from the server. |
| 70 void WebResourceStateChange(); | 76 scoped_ptr<content::URLFetcher> url_fetcher_; |
| 71 | 77 |
| 72 scoped_ptr<WebResourceFetcher> web_resource_fetcher_; | 78 // True if we are currently fetching or unpacking data. If we are asked to |
| 73 | 79 // start a fetch when we are still fetching resource data, schedule another |
| 74 ResourceDispatcherHost* resource_dispatcher_host_; | 80 // one in |cache_update_delay_ms_| time, and silently exit. |
| 75 | |
| 76 // Allows the creation of tasks to send a WEB_RESOURCE_STATE_CHANGED | |
| 77 // notification. This allows the WebResourceService to notify the New Tab | |
| 78 // Page immediately when a new web resource should be shown or removed. | |
| 79 ScopedRunnableMethodFactory<WebResourceService> service_factory_; | |
| 80 | |
| 81 // True if we are currently mid-fetch. If we are asked to start a fetch | |
| 82 // when we are still fetching resource data, schedule another one in | |
| 83 // kCacheUpdateDelay time, and silently exit. | |
| 84 bool in_fetch_; | 81 bool in_fetch_; |
| 85 | 82 |
| 86 // URL that hosts the web resource. | 83 // URL that hosts the web resource. |
| 87 const char* web_resource_server_; | 84 const char* web_resource_server_; |
| 88 | 85 |
| 89 // Indicates whether we should append locale to the web resource server URL. | 86 // Indicates whether we should append locale to the web resource server URL. |
| 90 bool apply_locale_to_url_; | 87 bool apply_locale_to_url_; |
| 91 | 88 |
| 92 // Notification type when an update is done. | |
| 93 int notification_type_; | |
| 94 | |
| 95 // Pref name to store the last update's time. | 89 // Pref name to store the last update's time. |
| 96 const char* last_update_time_pref_name_; | 90 const char* last_update_time_pref_name_; |
| 97 | 91 |
| 98 // Delay on first fetch so we don't interfere with startup. | 92 // Delay on first fetch so we don't interfere with startup. |
| 99 int start_fetch_delay_; | 93 int start_fetch_delay_ms_; |
| 100 | 94 |
| 101 // Delay between calls to update the web resource cache. This delay may be | 95 // Delay between calls to update the web resource cache. This delay may be |
| 102 // different for different builds of Chrome. | 96 // different for different builds of Chrome. |
| 103 int cache_update_delay_; | 97 int cache_update_delay_ms_; |
| 104 | |
| 105 // True if a task has been set to update the cache when a new web resource | |
| 106 // becomes available. | |
| 107 bool web_resource_update_scheduled_; | |
| 108 | 98 |
| 109 DISALLOW_COPY_AND_ASSIGN(WebResourceService); | 99 DISALLOW_COPY_AND_ASSIGN(WebResourceService); |
| 110 }; | 100 }; |
| 111 | 101 |
| 112 #endif // CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ | 102 #endif // CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ |
| OLD | NEW |