Chromium Code Reviews| 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_PROMO_RESOURCE_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_WEB_RESOURCE_PROMO_RESOURCE_SERVICE_H_ |
| 6 #define CHROME_BROWSER_WEB_RESOURCE_PROMO_RESOURCE_SERVICE_H_ | 6 #define CHROME_BROWSER_WEB_RESOURCE_PROMO_RESOURCE_SERVICE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "chrome/browser/web_resource/web_resource_service.h" | 11 #include "chrome/browser/web_resource/web_resource_service.h" |
| 12 #include "chrome/common/chrome_version_info.h" | 12 #include "chrome/common/chrome_version_info.h" |
| 13 | 13 |
| 14 namespace base { | |
| 15 class DictionaryValue; | |
| 16 } | |
| 17 | |
| 14 class Profile; | 18 class Profile; |
| 15 | |
| 16 namespace PromoResourceServiceUtil { | |
| 17 | |
| 18 // Certain promotions should only be shown to certain classes of users. This | |
| 19 // function will change to reflect each kind of promotion. | |
| 20 bool CanShowPromo(Profile* profile); | |
| 21 | |
| 22 } // namespace PromoResourceServiceUtil | |
| 23 | |
| 24 class PrefService; | 19 class PrefService; |
| 25 | 20 |
| 26 // A PromoResourceService fetches data from a web resource server to be used to | 21 // A PromoResourceService 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 | 22 // 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 | 23 // been used to fetch "tips" to be displayed on the NTP, or to display |
| 29 // promotional messages to certain groups of Chrome users. | 24 // promotional messages to certain groups of Chrome users. |
| 30 // | 25 // |
| 31 // TODO(mirandac): Arrange for a server to be set up specifically for promo | 26 // 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 | 27 // messages, which have until now been piggybacked onto the old tips server |
| 33 // structure. (see http://crbug.com/70634 for details.) | 28 // structure. (see http://crbug.com/70634 for details.) |
| 34 class PromoResourceService | 29 class PromoResourceService |
| 35 : public WebResourceService { | 30 : public WebResourceService { |
| 36 public: | 31 public: |
| 37 static bool IsBuildTargeted(chrome::VersionInfo::Channel channel, | 32 // Checks for conditions to show promo: start/end times, channel, etc. |
| 38 int builds_targeted); | 33 static bool CanShowPromo(Profile* profile); |
| 39 | 34 |
| 40 static void RegisterPrefs(PrefService* local_state); | 35 static void RegisterPrefs(PrefService* local_state); |
| 41 | 36 |
| 42 static void RegisterUserPrefs(PrefService* prefs); | 37 static void RegisterUserPrefs(PrefService* prefs); |
| 43 | 38 |
| 44 explicit PromoResourceService(Profile* profile); | 39 explicit PromoResourceService(Profile* profile); |
| 45 | 40 |
| 46 // Default server of dynamically loaded NTP HTML elements. | 41 // Default server of dynamically loaded NTP HTML elements. |
| 47 static const char* kDefaultPromoResourceServer; | 42 static const char* kDefaultPromoResourceServer; |
| 48 | 43 |
| 49 private: | 44 private: |
| 45 FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, IsBuildTargeted); | |
| 50 FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, UnpackLogoSignal); | 46 FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, UnpackLogoSignal); |
| 51 FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, UnpackPromoSignal); | 47 FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, UnpackPromoSignal); |
| 52 FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, UnpackWebStoreSignal); | 48 FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, UnpackWebStoreSignal); |
| 53 FRIEND_TEST_ALL_PREFIXES( | 49 FRIEND_TEST_ALL_PREFIXES( |
| 54 PromoResourceServiceTest, UnpackPartialWebStoreSignal); | 50 PromoResourceServiceTest, UnpackPartialWebStoreSignal); |
| 55 | 51 |
| 56 // Identifies types of Chrome builds for promo targeting. | 52 // Identifies types of Chrome builds for promo targeting. |
| 57 enum BuildType { | 53 enum BuildType { |
| 58 NO_BUILD = 0, | 54 NO_BUILD = 0, |
| 59 DEV_BUILD = 1, | 55 DEV_BUILD = 1, |
| 60 BETA_BUILD = 1 << 1, | 56 BETA_BUILD = 1 << 1, |
| 61 STABLE_BUILD = 1 << 2, | 57 STABLE_BUILD = 1 << 2, |
| 62 CANARY_BUILD = 1 << 3, | 58 CANARY_BUILD = 1 << 3, |
| 63 }; | 59 }; |
| 64 | 60 |
| 65 virtual ~PromoResourceService(); | 61 virtual ~PromoResourceService(); |
| 66 | 62 |
| 67 int GetPromoServiceVersion(); | 63 int GetPromoServiceVersion(); |
| 68 | 64 |
| 69 // Gets the locale of the last promos fetched from the server. This is saved | 65 // Gets the locale of the last promos fetched from the server. This is saved |
| 70 // so we can fetch new data if the locale changes. | 66 // so we can fetch new data if the locale changes. |
| 71 std::string GetPromoLocale(); | 67 std::string GetPromoLocale(); |
| 72 | 68 |
| 73 void Init(); | 69 void Init(); |
| 74 | 70 |
| 71 static bool IsBuildTargeted(chrome::VersionInfo::Channel channel, | |
| 72 int builds_targeted); | |
| 73 | |
| 75 // Returns true if |builds_targeted| includes the release channel Chrome | 74 // Returns true if |builds_targeted| includes the release channel Chrome |
| 76 // belongs to. For testing purposes, you can override the current channel | 75 // belongs to. For testing purposes, you can override the current channel |
| 77 // with set_channel. | 76 // with set_channel. |
| 78 bool IsThisBuildTargeted(int builds_targeted); | 77 bool IsThisBuildTargeted(int builds_targeted); |
| 79 | 78 |
| 80 // Schedule a notification that a web resource is either going to become | 79 // Schedule a notification that a web resource is either going to become |
| 81 // available or be no longer valid. | 80 // available or be no longer valid. |
| 82 void ScheduleNotification(double ms_start_time, double ms_end_time); | 81 void ScheduleNotification(double ms_start_time, double ms_end_time); |
| 83 | 82 |
| 84 // Schedules the initial notification for when the web resource is going | 83 // Schedules the initial notification for when the web resource is going |
| 85 // to become available or no longer valid. This performs a few additional | 84 // to become available or no longer valid. This performs a few additional |
| 86 // checks than ScheduleNotification, namely it schedules updates immediately | 85 // checks than ScheduleNotification, namely it schedules updates immediately |
| 87 // if the promo service or Chrome locale has changed. | 86 // if the promo service or Chrome locale has changed. |
| 88 void ScheduleNotificationOnInit(); | 87 void ScheduleNotificationOnInit(); |
| 89 | 88 |
| 90 // Overrides the current Chrome release channel for testing purposes. | 89 // Overrides the current Chrome release channel for testing purposes. |
| 91 void set_channel(chrome::VersionInfo::Channel channel) { channel_ = channel; } | 90 void set_channel(chrome::VersionInfo::Channel channel) { channel_ = channel; } |
| 92 | 91 |
| 93 virtual void Unpack(const base::DictionaryValue& parsed_json); | 92 virtual void Unpack(const base::DictionaryValue& parsed_json); |
| 94 | 93 |
| 95 // Unpack the web resource as a custom promo signal. Expects a start and end | 94 // Unpack the web resource as a custom promo signal. Expects a start and end |
| 96 // signal, with the promo to be shown in the tooltip of the start signal | 95 // signal, with the promo to be shown in the tooltip of the start signal |
| 97 // field. Delivery will be in json in the form of: | 96 // field. Delivery will be in json in the form of: |
| 98 // { | 97 // { |
| 99 // "topic": { | 98 // "topic": { |
| 100 // "answers": [ | 99 // "answers": [ |
| 101 // { | 100 // { |
| 102 // "answer_id": "1067976", | 101 // "answer_id": "1067976", |
| 103 // "name": "promo_start", | 102 // "name": "promo_start", |
| 104 // "question": "1:24", | 103 // "question": "1:24", |
|
Miranda Callahan
2011/08/23 08:33:31
Could you add the new max group element to this ex
achuithb
2011/08/23 21:38:55
Done.
| |
| 105 // "tooltip": | 104 // "tooltip": |
| 106 // "Click \u003ca href=http://www.google.com\u003ehere\u003c/a\u003e!", | 105 // "Click \u003ca href=http://www.google.com\u003ehere\u003c/a\u003e!", |
| 107 // "inproduct": "10/8/09 12:00", | 106 // "inproduct": "10/8/09 12:00", |
| 108 // "inproduct_target": null | 107 // "inproduct_target": null |
| 109 // }, | 108 // }, |
| 110 // { | 109 // { |
| 111 // "answer_id": "1067976", | 110 // "answer_id": "1067976", |
| 112 // "name": "promo_end", | 111 // "name": "promo_end", |
| 113 // "question": "", | 112 // "question": "", |
| 114 // "tooltip": "", | 113 // "tooltip": "", |
| 115 // "inproduct": "10/8/11 12:00", | 114 // "inproduct": "10/8/11 12:00", |
| 116 // "inproduct_target": null | 115 // "inproduct_target": null |
| 117 // }, | 116 // }, |
| 118 // ... | 117 // ... |
| 119 // ] | 118 // ] |
| 120 // } | 119 // } |
| 121 // } | 120 // } |
| 122 // | 121 // |
| 123 // Because the promo signal data is piggybacked onto the tip server, the | 122 // Because the promo signal data is piggybacked onto the tip server, the |
| 124 // values don't exactly correspond with the field names: | 123 // values don't exactly correspond with the field names: |
| 125 // | 124 // |
| 126 // For "promo_start" or "promo_end", the date to start or stop showing the | 125 // For "promo_start" or "promo_end", the date to start or stop showing the |
| 127 // promotional line is given by the "inproduct" line. | 126 // promotional line is given by the "inproduct" line. |
| 128 // For "promo_start", the promotional line itself is given in the "tooltip" | 127 // For "promo_start", the promotional line itself is given in the "tooltip" |
| 129 // field. The "question" field gives the type of builds that should be shown | 128 // field. The "question" field gives the type of builds that should be shown |
| 130 // this promo (see the BuildType enum in web_resource_service.cc) and the | 129 // this promo (see the BuildType enum in web_resource_service.cc) and the |
| 131 // number of hours that each promo group should see it, separated by ":". | 130 // number of hours that each promo group should see it, separated by ":". |
| 132 // For example, "7:24" would indicate that all builds should see the promo, | 131 // For example, "7:24" would indicate that all builds should see the promo, |
| 133 // and each group should see it for 24 hours. | 132 // and each group should see it for 24 hours. |
|
Miranda Callahan
2011/08/23 08:33:31
Here, too, a comment explaining the third field?
achuithb
2011/08/23 21:38:55
Done.
| |
| 134 // | 133 // |
| 135 void UnpackPromoSignal(const base::DictionaryValue& parsed_json); | 134 void UnpackPromoSignal(const base::DictionaryValue& parsed_json); |
| 136 | 135 |
| 137 // Unpack the promo resource as a custom logo signal. Expects a start and end | 136 // Unpack the promo resource as a custom logo signal. Expects a start and end |
| 138 // signal. Delivery will be in json in the form of: | 137 // signal. Delivery will be in json in the form of: |
| 139 // { | 138 // { |
| 140 // "topic": { | 139 // "topic": { |
| 141 // "answers": [ | 140 // "answers": [ |
| 142 // { | 141 // { |
| 143 // "answer_id": "107366", | 142 // "answer_id": "107366", |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 185 // inproduct: the promo button link | 184 // inproduct: the promo button link |
| 186 // tooltip: the text for the "hide this" link on the promo | 185 // tooltip: the text for the "hide this" link on the promo |
| 187 // name: starts with "webstore_promo" to identify the signal. the second | 186 // name: starts with "webstore_promo" to identify the signal. the second |
| 188 // part contains the release channels targeted (bitwise or of | 187 // part contains the release channels targeted (bitwise or of |
| 189 // BuildTypes). The third part is optional and specifies the URL of | 188 // BuildTypes). The third part is optional and specifies the URL of |
| 190 // the logo image. In the example above, the URL is empty so the | 189 // the logo image. In the example above, the URL is empty so the |
| 191 // default webstore logo will be used. | 190 // default webstore logo will be used. |
| 192 // answer_id: the promo's id | 191 // answer_id: the promo's id |
| 193 void UnpackWebStoreSignal(const base::DictionaryValue& parsed_json); | 192 void UnpackWebStoreSignal(const base::DictionaryValue& parsed_json); |
| 194 | 193 |
| 195 // Gets mutable dictionary attached to user's preferences, so that we | 194 // Parse the answers array element. |
| 196 // can write resource data back to user's pref file. | 195 void ParsePromo(base::DictionaryValue* a_dic, |
| 197 base::DictionaryValue* web_resource_cache_; | 196 std::string* promo_start_string, |
| 197 std::string* promo_end_string); | |
| 198 | |
| 199 // Set promo params from a question string, which is of the form | |
| 200 // <build_type>:<time_slice>:<max_group>. | |
| 201 void SetPromoParams(base::DictionaryValue* a_dic); | |
| 202 | |
| 203 // Extract the promo text from the tooltip string. | |
| 204 void SetPromoLine(base::DictionaryValue* a_dic); | |
| 205 | |
| 206 // Check if this promo is new based on start/end times, | |
| 207 // and trigger events accordingly. | |
| 208 void CheckForNewPromo(const std::string& promo_start_string, | |
| 209 const std::string& promo_end_string); | |
| 210 | |
| 211 // Calculate the promo times, taking into account our group, and the | |
| 212 // group time slice. | |
| 213 void CalculateNewPromoTimes(const std::string& promo_start_string, | |
| 214 const std::string& promo_end_string, | |
| 215 double* promo_start, | |
| 216 double* promo_end); | |
| 217 | |
| 218 // Get saved promo times. | |
| 219 void GetCurrentPromoTimes(double* old_promo_start, double* old_promo_end); | |
| 220 | |
| 221 // Create a new promo group. | |
| 222 int ResetPromoGroup(); | |
| 223 | |
| 224 // Actions on receiving a new promo. | |
| 225 void OnNewPromo(double promo_start, double promo_end); | |
| 198 | 226 |
| 199 // Overrides the current Chrome release channel for testing purposes. | 227 // Overrides the current Chrome release channel for testing purposes. |
| 200 chrome::VersionInfo::Channel channel_; | 228 chrome::VersionInfo::Channel channel_; |
| 201 | 229 |
| 202 DISALLOW_COPY_AND_ASSIGN(PromoResourceService); | 230 DISALLOW_COPY_AND_ASSIGN(PromoResourceService); |
| 203 }; | 231 }; |
| 204 | 232 |
| 205 #endif // CHROME_BROWSER_WEB_RESOURCE_PROMO_RESOURCE_SERVICE_H_ | 233 #endif // CHROME_BROWSER_WEB_RESOURCE_PROMO_RESOURCE_SERVICE_H_ |
| OLD | NEW |