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