| OLD | NEW |
| 1 // Copyright (c) 2010 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 "base/file_path.h" | |
| 12 #include "chrome/browser/prefs/pref_service.h" | |
| 13 #include "chrome/browser/utility_process_host.h" | 11 #include "chrome/browser/utility_process_host.h" |
| 14 #include "chrome/common/web_resource/web_resource_unpacker.h" | 12 #include "chrome/common/notification_type.h" |
| 15 | 13 |
| 14 class PrefService; |
| 16 class Profile; | 15 class Profile; |
| 17 | 16 |
| 18 namespace WebResourceServiceUtil { | 17 // A WebResourceService fetches data from a web resource server and store |
| 19 | 18 // locally as user preference. |
| 20 // Certain promotions should only be shown to certain classes of users. This | |
| 21 // function will change to reflect each kind of promotion. | |
| 22 bool CanShowPromo(Profile* profile); | |
| 23 | |
| 24 } // namespace WebResourceService | |
| 25 | |
| 26 // A WebResourceService fetches data from a web resource server to be used to | |
| 27 // dynamically change the appearance of the New Tab Page. For example, it has | |
| 28 // been used to fetch "tips" to be displayed on the NTP, or to display | |
| 29 // promotional messages to certain groups of Chrome users. | |
| 30 // | |
| 31 // TODO(mirandac): Arrange for a server to be set up specifically for promo | |
| 32 // messages, which have until now been piggybacked onto the old tips server | |
| 33 // structure. (see http://crbug.com/70634 for details.) | |
| 34 class WebResourceService | 19 class WebResourceService |
| 35 : public UtilityProcessHost::Client { | 20 : public UtilityProcessHost::Client { |
| 36 public: | 21 public: |
| 37 explicit WebResourceService(Profile* profile); | 22 WebResourceService(Profile* profile, |
| 23 const char* web_resource_server, |
| 24 bool apply_locale_to_url_, |
| 25 NotificationType::Type notification_type, |
| 26 const char* last_update_time_pref_name, |
| 27 int start_fetch_delay, |
| 28 int cache_update_delay); |
| 38 | 29 |
| 39 // Sleep until cache needs to be updated, but always for at least 5 seconds | 30 // Sleep until cache needs to be updated, but always for at least 5 seconds |
| 40 // so we don't interfere with startup. Then begin updating resources. | 31 // so we don't interfere with startup. Then begin updating resources. |
| 41 void StartAfterDelay(); | 32 void StartAfterDelay(); |
| 42 | 33 |
| 43 // We have successfully pulled data from a resource server; now launch | 34 // We have successfully pulled data from a resource server; now launch |
| 44 // the process that will parse the JSON, and then update the cache. | 35 // the process that will parse the JSON, and then update the cache. |
| 45 void UpdateResourceCache(const std::string& json_data); | 36 void UpdateResourceCache(const std::string& json_data); |
| 46 | 37 |
| 47 // Unpack the web resource as a set of tips. Expects json in the form of: | 38 protected: |
| 48 // { | 39 virtual ~WebResourceService(); |
| 49 // "lang": "en", | |
| 50 // "topic": { | |
| 51 // "topid_id": "24013", | |
| 52 // "topics": [ | |
| 53 // ], | |
| 54 // "answers": [ | |
| 55 // { | |
| 56 // "answer_id": "18625", | |
| 57 // "inproduct": "Text here will be shown as a tip", | |
| 58 // }, | |
| 59 // ... | |
| 60 // ] | |
| 61 // } | |
| 62 // } | |
| 63 // | |
| 64 // Public for unit testing. | |
| 65 void UnpackTips(const DictionaryValue& parsed_json); | |
| 66 | 40 |
| 67 // Unpack the web resource as a custom promo signal. Expects a start and end | 41 virtual void Unpack(const DictionaryValue& parsed_json) = 0; |
| 68 // signal, with the promo to be shown in the tooltip of the start signal | |
| 69 // field. Delivery will be in json in the form of: | |
| 70 // { | |
| 71 // "topic": { | |
| 72 // "answers": [ | |
| 73 // { | |
| 74 // "answer_id": "1067976", | |
| 75 // "name": "promo_start", | |
| 76 // "question": "1:24", | |
| 77 // "tooltip": | |
| 78 // "Click \u003ca href=http://www.google.com\u003ehere\u003c/a\u003e!", | |
| 79 // "inproduct": "10/8/09 12:00", | |
| 80 // "inproduct_target": null | |
| 81 // }, | |
| 82 // { | |
| 83 // "answer_id": "1067976", | |
| 84 // "name": "promo_end", | |
| 85 // "question": "", | |
| 86 // "tooltip": "", | |
| 87 // "inproduct": "10/8/11 12:00", | |
| 88 // "inproduct_target": null | |
| 89 // }, | |
| 90 // ... | |
| 91 // ] | |
| 92 // } | |
| 93 // } | |
| 94 // | |
| 95 // Because the promo signal data is piggybacked onto the tip server, the | |
| 96 // values don't exactly correspond with the field names: | |
| 97 // | |
| 98 // For "promo_start" or "promo_end", the date to start or stop showing the | |
| 99 // promotional line is given by the "inproduct" line. | |
| 100 // For "promo_start", the promotional line itself is given in the "tooltip" | |
| 101 // field. The "question" field gives the type of builds that should be shown | |
| 102 // this promo (see the BuildType enum in web_resource_service.cc) and the | |
| 103 // number of hours that each promo group should see it, separated by ":". | |
| 104 // For example, "7:24" would indicate that all builds should see the promo, | |
| 105 // and each group should see it for 24 hours. | |
| 106 // | |
| 107 // Public for unit testing. | |
| 108 void UnpackPromoSignal(const DictionaryValue& parsed_json); | |
| 109 | 42 |
| 110 // Unpack the web resource as a custom logo signal. Expects a start and end | 43 // If delay_ms is positive, schedule notification with the delay. |
| 111 // signal. Delivery will be in json in the form of: | 44 // If delay_ms is 0, notify immediately by calling WebResourceStateChange(). |
| 112 // { | 45 // If delay_ms is negative, do nothing. |
| 113 // "topic": { | 46 void PostNotification(int64 delay_ms); |
| 114 // "answers": [ | |
| 115 // { | |
| 116 // "answer_id": "107366", | |
| 117 // "name": "custom_logo_start", | |
| 118 // "question": "", | |
| 119 // "tooltip": "", | |
| 120 // "inproduct": "10/8/09 12:00", | |
| 121 // "inproduct_target": null | |
| 122 // }, | |
| 123 // { | |
| 124 // "answer_id": "107366", | |
| 125 // "name": "custom_logo_end", | |
| 126 // "question": "", | |
| 127 // "tooltip": "", | |
| 128 // "inproduct": "10/8/09 12:00", | |
| 129 // "inproduct_target": null | |
| 130 // }, | |
| 131 // ... | |
| 132 // ] | |
| 133 // } | |
| 134 // } | |
| 135 // | |
| 136 // Public for unit testing. | |
| 137 void UnpackLogoSignal(const DictionaryValue& parsed_json); | |
| 138 | 47 |
| 139 int cache_update_delay() const { return cache_update_delay_; } | 48 // We need to be able to load parsed resource data into preferences file, |
| 140 | 49 // and get proper install directory. |
| 141 Profile* profile() const { return profile_; } | 50 PrefService* prefs_; |
| 142 | |
| 143 static const char* kCurrentTipPrefName; | |
| 144 static const char* kTipCachePrefName; | |
| 145 | |
| 146 // Default server of dynamically loaded NTP HTML elements (promotions, tips): | |
| 147 static const char* kDefaultWebResourceServer; | |
| 148 | 51 |
| 149 private: | 52 private: |
| 150 class WebResourceFetcher; | 53 class WebResourceFetcher; |
| 151 friend class WebResourceFetcher; | 54 friend class WebResourceFetcher; |
| 152 | 55 |
| 153 class UnpackerClient; | 56 class UnpackerClient; |
| 154 | 57 |
| 155 ~WebResourceService(); | |
| 156 | |
| 157 void Init(); | |
| 158 | |
| 159 // Set in_fetch_ to false, clean up temp directories (in the future). | 58 // Set in_fetch_ to false, clean up temp directories (in the future). |
| 160 void EndFetch(); | 59 void EndFetch(); |
| 161 | 60 |
| 162 // Puts parsed json data in the right places, and writes to prefs file. | 61 // Puts parsed json data in the right places, and writes to prefs file. |
| 163 void OnWebResourceUnpacked(const DictionaryValue& parsed_json); | 62 void OnWebResourceUnpacked(const DictionaryValue& parsed_json); |
| 164 | 63 |
| 165 // Notify listeners that the state of a web resource has changed. | 64 // Notify listeners that the state of a web resource has changed. |
| 166 void WebResourceStateChange(); | 65 void WebResourceStateChange(); |
| 167 | 66 |
| 168 // Schedule a notification that a web resource is either going to become | |
| 169 // available or be no longer valid. | |
| 170 void ScheduleNotification(double ms_start_time, double ms_end_time); | |
| 171 | |
| 172 // We need to be able to load parsed resource data into preferences file, | |
| 173 // and get proper install directory. | |
| 174 PrefService* prefs_; | |
| 175 | |
| 176 // Display and fetch of promo lines depends on data associated with a user's | |
| 177 // profile. | |
| 178 Profile* profile_; | 67 Profile* profile_; |
| 179 | 68 |
| 180 scoped_ptr<WebResourceFetcher> web_resource_fetcher_; | 69 scoped_ptr<WebResourceFetcher> web_resource_fetcher_; |
| 181 | 70 |
| 182 ResourceDispatcherHost* resource_dispatcher_host_; | 71 ResourceDispatcherHost* resource_dispatcher_host_; |
| 183 | 72 |
| 184 // 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 |
| 185 // notification. This allows the WebResourceService to notify the New Tab | 74 // notification. This allows the WebResourceService to notify the New Tab |
| 186 // 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. |
| 187 ScopedRunnableMethodFactory<WebResourceService> service_factory_; | 76 ScopedRunnableMethodFactory<WebResourceService> service_factory_; |
| 188 | 77 |
| 189 // Gets mutable dictionary attached to user's preferences, so that we | |
| 190 // can write resource data back to user's pref file. | |
| 191 DictionaryValue* web_resource_cache_; | |
| 192 | |
| 193 // 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 |
| 194 // when we are still fetching resource data, schedule another one in | 79 // when we are still fetching resource data, schedule another one in |
| 195 // kCacheUpdateDelay time, and silently exit. | 80 // kCacheUpdateDelay time, and silently exit. |
| 196 bool in_fetch_; | 81 bool in_fetch_; |
| 197 | 82 |
| 83 // URL that hosts the web resource. |
| 84 const char* web_resource_server_; |
| 85 |
| 86 // Indicates whether we should append locale to the web resource server URL. |
| 87 bool apply_locale_to_url_; |
| 88 |
| 89 // Notification type when an update is done. |
| 90 NotificationType::Type notification_type_; |
| 91 |
| 92 // Pref name to store the last update's time. |
| 93 const char* last_update_time_pref_name_; |
| 94 |
| 95 // Delay on first fetch so we don't interfere with startup. |
| 96 int start_fetch_delay_; |
| 97 |
| 198 // Delay between calls to update the web resource cache. This delay may be | 98 // Delay between calls to update the web resource cache. This delay may be |
| 199 // different for different builds of Chrome. | 99 // different for different builds of Chrome. |
| 200 int cache_update_delay_; | 100 int cache_update_delay_; |
| 201 | 101 |
| 202 // True if a task has been set to update the cache when a new web resource | 102 // True if a task has been set to update the cache when a new web resource |
| 203 // becomes available. | 103 // becomes available. |
| 204 bool web_resource_update_scheduled_; | 104 bool web_resource_update_scheduled_; |
| 205 | 105 |
| 206 DISALLOW_COPY_AND_ASSIGN(WebResourceService); | 106 DISALLOW_COPY_AND_ASSIGN(WebResourceService); |
| 207 }; | 107 }; |
| 208 | 108 |
| 209 #endif // CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ | 109 #endif // CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ |
| OLD | NEW |