| 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 "chrome/browser/utility_process_host.h" | 11 #include "chrome/browser/utility_process_host.h" |
| 12 #include "content/common/notification_type.h" | 12 #include "content/common/notification_type.h" |
| 13 | 13 |
| 14 class PrefService; | 14 class PrefService; |
| 15 class Profile; |
| 15 class ResourceDispatcherHost; | 16 class ResourceDispatcherHost; |
| 16 | 17 |
| 17 // A WebResourceService fetches data from a web resource server and store | 18 // A WebResourceService fetches data from a web resource server and store |
| 18 // locally as user preference. | 19 // locally as user preference. |
| 19 class WebResourceService | 20 class WebResourceService |
| 20 : public UtilityProcessHost::Client { | 21 : public UtilityProcessHost::Client { |
| 21 public: | 22 public: |
| 22 // Pass notification_type = NOTIFICATION_TYPE_COUNT if notification is not | 23 // Pass notification_type = NOTIFICATION_TYPE_COUNT if notification is not |
| 23 // required. | 24 // required. |
| 24 WebResourceService(PrefService* prefs, | 25 WebResourceService(Profile* profile, |
| 26 PrefService* prefs, |
| 25 const char* web_resource_server, | 27 const char* web_resource_server, |
| 26 bool apply_locale_to_url_, | 28 bool apply_locale_to_url_, |
| 27 NotificationType::Type notification_type, | 29 NotificationType::Type notification_type, |
| 28 const char* last_update_time_pref_name, | 30 const char* last_update_time_pref_name, |
| 29 int start_fetch_delay, | 31 int start_fetch_delay, |
| 30 int cache_update_delay); | 32 int cache_update_delay); |
| 31 | 33 |
| 32 // Sleep until cache needs to be updated, but always for at least 5 seconds | 34 // Sleep until cache needs to be updated, but always for at least 5 seconds |
| 33 // so we don't interfere with startup. Then begin updating resources. | 35 // so we don't interfere with startup. Then begin updating resources. |
| 34 void StartAfterDelay(); | 36 void StartAfterDelay(); |
| 35 | 37 |
| 36 // We have successfully pulled data from a resource server; now launch | 38 // We have successfully pulled data from a resource server; now launch |
| 37 // the process that will parse the JSON, and then update the cache. | 39 // the process that will parse the JSON, and then update the cache. |
| 38 void UpdateResourceCache(const std::string& json_data); | 40 void UpdateResourceCache(const std::string& json_data); |
| 39 | 41 |
| 40 protected: | 42 protected: |
| 41 virtual ~WebResourceService(); | 43 virtual ~WebResourceService(); |
| 42 | 44 |
| 43 virtual void Unpack(const DictionaryValue& parsed_json) = 0; | 45 virtual void Unpack(const DictionaryValue& parsed_json) = 0; |
| 44 | 46 |
| 45 // If delay_ms is positive, schedule notification with the delay. | 47 // If delay_ms is positive, schedule notification with the delay. |
| 46 // If delay_ms is 0, notify immediately by calling WebResourceStateChange(). | 48 // If delay_ms is 0, notify immediately by calling WebResourceStateChange(). |
| 47 // If delay_ms is negative, do nothing. | 49 // If delay_ms is negative, do nothing. |
| 48 void PostNotification(int64 delay_ms); | 50 void PostNotification(int64 delay_ms); |
| 49 | 51 |
| 50 // 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, |
| 51 // and get proper install directory. | 53 // and get proper install directory. |
| 52 PrefService* prefs_; | 54 PrefService* prefs_; |
| 53 | 55 |
| 56 Profile* profile_; |
| 57 |
| 54 private: | 58 private: |
| 55 class WebResourceFetcher; | 59 class WebResourceFetcher; |
| 56 friend class WebResourceFetcher; | 60 friend class WebResourceFetcher; |
| 57 | 61 |
| 58 class UnpackerClient; | 62 class UnpackerClient; |
| 59 | 63 |
| 60 // Set in_fetch_ to false, clean up temp directories (in the future). | 64 // Set in_fetch_ to false, clean up temp directories (in the future). |
| 61 void EndFetch(); | 65 void EndFetch(); |
| 62 | 66 |
| 63 // Puts parsed json data in the right places, and writes to prefs file. | 67 // Puts parsed json data in the right places, and writes to prefs file. |
| 64 void OnWebResourceUnpacked(const DictionaryValue& parsed_json); | 68 void OnWebResourceUnpacked(const DictionaryValue& parsed_json); |
| 65 | 69 |
| 66 // Notify listeners that the state of a web resource has changed. | 70 // Notify listeners that the state of a web resource has changed. |
| 67 void WebResourceStateChange(); | 71 void WebResourceStateChange(); |
| 68 | 72 |
| 69 scoped_ptr<WebResourceFetcher> web_resource_fetcher_; | 73 scoped_ptr<WebResourceFetcher> web_resource_fetcher_; |
| 70 | 74 |
| 71 ResourceDispatcherHost* resource_dispatcher_host_; | 75 ResourceDispatcherHost* resource_dispatcher_host_; |
| 72 | 76 |
| 73 // Allows the creation of tasks to send a WEB_RESOURCE_STATE_CHANGED | 77 // Allows the creation of tasks to send a WEB_RESOURCE_STATE_CHANGED |
| 74 // notification. This allows the WebResourceService to notify the New Tab | 78 // notification. This allows the WebResourceService to notify the New Tab |
| 75 // Page immediately when a new web resource should be shown or removed. | 79 // Page immediately when a new web resource should be shown or removed. |
| 76 ScopedRunnableMethodFactory<WebResourceService> service_factory_; | 80 ScopedRunnableMethodFactory<WebResourceService> service_factory_; |
| 77 | 81 |
| 78 // True if we are currently mid-fetch. If we are asked to start a fetch | 82 // True if we are currently mid-fetch. If we are asked to start a fetch |
| 79 // when we are still fetching resource data, schedule another one in | 83 // when we are still fetching resource data, schedule another one in |
| 80 // kCacheUpdateDelay time, and silently exit. | 84 // kCacheUpdateDelay time, and silently exit. |
| 81 bool in_fetch_; | 85 bool in_fetch_; |
| 82 | 86 |
| 83 // URL that hosts the web resource. This URL will be loaded with a | 87 // URL that hosts the web resource. |
| 84 // SystemURLRequestContext, so should not depend on Profile request context | |
| 85 // data (see http://codereview.chromium.org/7099004/). | |
| 86 const char* web_resource_server_; | 88 const char* web_resource_server_; |
| 87 | 89 |
| 88 // Indicates whether we should append locale to the web resource server URL. | 90 // Indicates whether we should append locale to the web resource server URL. |
| 89 bool apply_locale_to_url_; | 91 bool apply_locale_to_url_; |
| 90 | 92 |
| 91 // Notification type when an update is done. | 93 // Notification type when an update is done. |
| 92 NotificationType::Type notification_type_; | 94 NotificationType::Type notification_type_; |
| 93 | 95 |
| 94 // Pref name to store the last update's time. | 96 // Pref name to store the last update's time. |
| 95 const char* last_update_time_pref_name_; | 97 const char* last_update_time_pref_name_; |
| 96 | 98 |
| 97 // Delay on first fetch so we don't interfere with startup. | 99 // Delay on first fetch so we don't interfere with startup. |
| 98 int start_fetch_delay_; | 100 int start_fetch_delay_; |
| 99 | 101 |
| 100 // Delay between calls to update the web resource cache. This delay may be | 102 // Delay between calls to update the web resource cache. This delay may be |
| 101 // different for different builds of Chrome. | 103 // different for different builds of Chrome. |
| 102 int cache_update_delay_; | 104 int cache_update_delay_; |
| 103 | 105 |
| 104 // True if a task has been set to update the cache when a new web resource | 106 // True if a task has been set to update the cache when a new web resource |
| 105 // becomes available. | 107 // becomes available. |
| 106 bool web_resource_update_scheduled_; | 108 bool web_resource_update_scheduled_; |
| 107 | 109 |
| 108 DISALLOW_COPY_AND_ASSIGN(WebResourceService); | 110 DISALLOW_COPY_AND_ASSIGN(WebResourceService); |
| 109 }; | 111 }; |
| 110 | 112 |
| 111 #endif // CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ | 113 #endif // CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ |
| OLD | NEW |