| 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 AppsPromoLogoFetcher; | 18 class AppsPromoLogoFetcher; |
| 15 class PrefService; | 19 class PrefService; |
| 16 class Profile; | 20 class Profile; |
| 17 | |
| 18 namespace PromoResourceServiceUtil { | |
| 19 | |
| 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 PromoResourceServiceUtil | |
| 25 | |
| 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 CanShowNotificationPromo(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, UnpackNotificationSignal); |
| 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 FRIEND_TEST_ALL_PREFIXES( | 51 FRIEND_TEST_ALL_PREFIXES( |
| 56 PromoResourceServiceTest, UnpackWebStoreSignalHttpsLogo); | 52 PromoResourceServiceTest, UnpackWebStoreSignalHttpsLogo); |
| 57 FRIEND_TEST_ALL_PREFIXES( | 53 FRIEND_TEST_ALL_PREFIXES( |
| 58 PromoResourceServiceTest, UnpackWebStoreSignalHttpsLogoError); | 54 PromoResourceServiceTest, UnpackWebStoreSignalHttpsLogoError); |
| 59 FRIEND_TEST_ALL_PREFIXES( | 55 FRIEND_TEST_ALL_PREFIXES( |
| 60 PromoResourceServiceTest, UnpackWebStoreSignalHttpLogo); | 56 PromoResourceServiceTest, UnpackWebStoreSignalHttpLogo); |
| 61 | 57 |
| 62 | |
| 63 // Identifies types of Chrome builds for promo targeting. | 58 // Identifies types of Chrome builds for promo targeting. |
| 64 enum BuildType { | 59 enum BuildType { |
| 65 NO_BUILD = 0, | 60 NO_BUILD = 0, |
| 66 DEV_BUILD = 1, | 61 DEV_BUILD = 1, |
| 67 BETA_BUILD = 1 << 1, | 62 BETA_BUILD = 1 << 1, |
| 68 STABLE_BUILD = 1 << 2, | 63 STABLE_BUILD = 1 << 2, |
| 69 CANARY_BUILD = 1 << 3, | 64 CANARY_BUILD = 1 << 3, |
| 65 ALL_BUILDS = (1 << 4) - 1, |
| 70 }; | 66 }; |
| 71 | 67 |
| 72 virtual ~PromoResourceService(); | 68 virtual ~PromoResourceService(); |
| 73 | 69 |
| 74 int GetPromoServiceVersion(); | 70 int GetPromoServiceVersion(); |
| 75 | 71 |
| 76 // Gets the locale of the last promos fetched from the server. This is saved | 72 // Gets the locale of the last promos fetched from the server. This is saved |
| 77 // so we can fetch new data if the locale changes. | 73 // so we can fetch new data if the locale changes. |
| 78 std::string GetPromoLocale(); | 74 std::string GetPromoLocale(); |
| 79 | 75 |
| 80 void Init(); | 76 void Init(); |
| 81 | 77 |
| 78 static bool IsBuildTargeted(chrome::VersionInfo::Channel channel, |
| 79 int builds_targeted); |
| 80 |
| 82 // Returns true if |builds_targeted| includes the release channel Chrome | 81 // Returns true if |builds_targeted| includes the release channel Chrome |
| 83 // belongs to. For testing purposes, you can override the current channel | 82 // belongs to. For testing purposes, you can override the current channel |
| 84 // with set_channel. | 83 // with set_channel. |
| 85 bool IsThisBuildTargeted(int builds_targeted); | 84 bool IsThisBuildTargeted(int builds_targeted); |
| 86 | 85 |
| 87 // Schedule a notification that a web resource is either going to become | 86 // Schedule a notification that a web resource is either going to become |
| 88 // available or be no longer valid. | 87 // available or be no longer valid. |
| 89 void ScheduleNotification(double ms_start_time, double ms_end_time); | 88 void ScheduleNotification(double ms_start_time, double ms_end_time); |
| 90 | 89 |
| 91 // Schedules the initial notification for when the web resource is going | 90 // Schedules the initial notification for when the web resource is going |
| 92 // to become available or no longer valid. This performs a few additional | 91 // to become available or no longer valid. This performs a few additional |
| 93 // checks than ScheduleNotification, namely it schedules updates immediately | 92 // checks than ScheduleNotification, namely it schedules updates immediately |
| 94 // if the promo service or Chrome locale has changed. | 93 // if the promo service or Chrome locale has changed. |
| 95 void ScheduleNotificationOnInit(); | 94 void ScheduleNotificationOnInit(); |
| 96 | 95 |
| 97 // Overrides the current Chrome release channel for testing purposes. | 96 // Overrides the current Chrome release channel for testing purposes. |
| 98 void set_channel(chrome::VersionInfo::Channel channel) { channel_ = channel; } | 97 void set_channel(chrome::VersionInfo::Channel channel) { channel_ = channel; } |
| 99 | 98 |
| 100 virtual void Unpack(const base::DictionaryValue& parsed_json); | 99 virtual void Unpack(const base::DictionaryValue& parsed_json); |
| 101 | 100 |
| 102 // Unpack the web resource as a custom promo signal. Expects a start and end | 101 // Unpack the web resource as a custom promo signal. Expects a start and end |
| 103 // signal, with the promo to be shown in the tooltip of the start signal | 102 // signal, with the promo to be shown in the tooltip of the start signal |
| 104 // field. Delivery will be in json in the form of: | 103 // field. Delivery will be in json in the form of: |
| 105 // { | 104 // { |
| 106 // "topic": { | 105 // "topic": { |
| 107 // "answers": [ | 106 // "answers": [ |
| 108 // { | 107 // { |
| 109 // "answer_id": "1067976", | 108 // "answer_id": "1067976", |
| 110 // "name": "promo_start", | 109 // "name": "promo_start", |
| 111 // "question": "1:24", | 110 // "question": "1:24:10", |
| 112 // "tooltip": | 111 // "tooltip": |
| 113 // "Click \u003ca href=http://www.google.com\u003ehere\u003c/a\u003e!", | 112 // "Click \u003ca href=http://www.google.com\u003ehere\u003c/a\u003e!", |
| 114 // "inproduct": "10/8/09 12:00", | 113 // "inproduct": "10/8/09 12:00", |
| 115 // "inproduct_target": null | 114 // "inproduct_target": null |
| 116 // }, | 115 // }, |
| 117 // { | 116 // { |
| 118 // "answer_id": "1067976", | 117 // "answer_id": "1067976", |
| 119 // "name": "promo_end", | 118 // "name": "promo_end", |
| 120 // "question": "", | 119 // "question": "", |
| 121 // "tooltip": "", | 120 // "tooltip": "", |
| 122 // "inproduct": "10/8/11 12:00", | 121 // "inproduct": "10/8/11 12:00", |
| 123 // "inproduct_target": null | 122 // "inproduct_target": null |
| 124 // }, | 123 // }, |
| 125 // ... | 124 // ... |
| 126 // ] | 125 // ] |
| 127 // } | 126 // } |
| 128 // } | 127 // } |
| 129 // | 128 // |
| 130 // Because the promo signal data is piggybacked onto the tip server, the | 129 // Because the promo signal data is piggybacked onto the tip server, the |
| 131 // values don't exactly correspond with the field names: | 130 // values don't exactly correspond with the field names: |
| 132 // | 131 // |
| 133 // For "promo_start" or "promo_end", the date to start or stop showing the | 132 // For "promo_start" or "promo_end", the date to start or stop showing the |
| 134 // promotional line is given by the "inproduct" line. | 133 // promotional line is given by the "inproduct" line. |
| 135 // For "promo_start", the promotional line itself is given in the "tooltip" | 134 // For "promo_start", the promotional line itself is given in the "tooltip" |
| 136 // field. The "question" field gives the type of builds that should be shown | 135 // field. The "question" field gives the type of builds that should be shown |
| 137 // this promo (see the BuildType enum in web_resource_service.cc) and the | 136 // this promo (see the BuildType enum in web_resource_service.cc), the |
| 138 // number of hours that each promo group should see it, separated by ":". | 137 // number of hours that each promo group should see it, and the maximum promo |
| 139 // For example, "7:24" would indicate that all builds should see the promo, | 138 // group that should see it, separated by ":". |
| 140 // and each group should see it for 24 hours. | 139 // For example, "7:24:5" would indicate that all groups with ids less than 5, |
| 140 // and with dev, beta and stable builds, should see the promo. The groups |
| 141 // ramp up so 1 additional group sees the promo every 24 hours. |
| 141 // | 142 // |
| 142 void UnpackPromoSignal(const base::DictionaryValue& parsed_json); | 143 void UnpackNotificationSignal(const base::DictionaryValue& parsed_json); |
| 143 | 144 |
| 144 // Unpack the promo resource as a custom logo signal. Expects a start and end | 145 // Unpack the promo resource as a custom logo signal. Expects a start and end |
| 145 // signal. Delivery will be in json in the form of: | 146 // signal. Delivery will be in json in the form of: |
| 146 // { | 147 // { |
| 147 // "topic": { | 148 // "topic": { |
| 148 // "answers": [ | 149 // "answers": [ |
| 149 // { | 150 // { |
| 150 // "answer_id": "107366", | 151 // "answer_id": "107366", |
| 151 // "name": "custom_logo_start", | 152 // "name": "custom_logo_start", |
| 152 // "question": "", | 153 // "question": "", |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 // name: starts with "webstore_promo" to identify the signal. The second | 195 // name: starts with "webstore_promo" to identify the signal. The second |
| 195 // part contains the release channels targeted (bitwise or of | 196 // part contains the release channels targeted (bitwise or of |
| 196 // BuildTypes). The third part specifies what users should maximize | 197 // BuildTypes). The third part specifies what users should maximize |
| 197 // the apps section of the NTP when first loading the promo (bitwise | 198 // the apps section of the NTP when first loading the promo (bitwise |
| 198 // or of AppsPromo::UserGroup). The forth part is optional and | 199 // or of AppsPromo::UserGroup). The forth part is optional and |
| 199 // specifies the URL of the logo image. If left out, the default | 200 // specifies the URL of the logo image. If left out, the default |
| 200 // webstore logo will be used. The logo can be an HTTPS or DATA URL. | 201 // webstore logo will be used. The logo can be an HTTPS or DATA URL. |
| 201 // answer_id: the promo's id | 202 // answer_id: the promo's id |
| 202 void UnpackWebStoreSignal(const base::DictionaryValue& parsed_json); | 203 void UnpackWebStoreSignal(const base::DictionaryValue& parsed_json); |
| 203 | 204 |
| 205 // Parse the answers array element. |
| 206 void ParseNotification(base::DictionaryValue* a_dic, |
| 207 std::string* promo_start_string, |
| 208 std::string* promo_end_string); |
| 209 |
| 210 // Set notification promo params from a question string, which is of the form |
| 211 // <build_type>:<time_slice>:<max_group>. |
| 212 void SetNotificationParams(base::DictionaryValue* a_dic); |
| 213 |
| 214 // Extract the notification promo text from the tooltip string. |
| 215 void SetNotificationLine(base::DictionaryValue* a_dic); |
| 216 |
| 217 // Check if this notification promo is new based on start/end times, |
| 218 // and trigger events accordingly. |
| 219 void CheckForNewNotification(const std::string& promo_start_string, |
| 220 const std::string& promo_end_string); |
| 221 |
| 222 // Calculate the notification promo times, taking into account our group, and |
| 223 // the group time slice. |
| 224 void ParseNewNotificationTimes(const std::string& promo_start_string, |
| 225 const std::string& promo_end_string, |
| 226 double* promo_start, |
| 227 double* promo_end); |
| 228 |
| 229 // Calculates notification promo start time with group-based time slice |
| 230 // offset. |
| 231 static double GetNotificationStartTime(PrefService* prefs); |
| 232 |
| 233 // Create a new notification promo group. |
| 234 int ResetNotificationGroup(); |
| 235 |
| 236 // Get saved notification promo times. |
| 237 void GetCurrentNotificationTimes(double* old_promo_start, |
| 238 double* old_promo_end); |
| 239 |
| 240 // Actions on receiving a new notification promo. |
| 241 void OnNewNotification(double promo_start, double promo_end); |
| 242 |
| 204 // The profile this service belongs to. | 243 // The profile this service belongs to. |
| 205 Profile* profile_; | 244 Profile* profile_; |
| 206 | 245 |
| 207 // Gets mutable dictionary attached to user's preferences, so that we | |
| 208 // can write resource data back to user's pref file. | |
| 209 base::DictionaryValue* web_resource_cache_; | |
| 210 | |
| 211 // Overrides the current Chrome release channel for testing purposes. | 246 // Overrides the current Chrome release channel for testing purposes. |
| 212 chrome::VersionInfo::Channel channel_; | 247 chrome::VersionInfo::Channel channel_; |
| 213 | 248 |
| 214 // A helper that downloads the promo logo. | 249 // A helper that downloads the promo logo. |
| 215 scoped_ptr<AppsPromoLogoFetcher> apps_promo_logo_fetcher_; | 250 scoped_ptr<AppsPromoLogoFetcher> apps_promo_logo_fetcher_; |
| 216 | 251 |
| 217 DISALLOW_COPY_AND_ASSIGN(PromoResourceService); | 252 DISALLOW_COPY_AND_ASSIGN(PromoResourceService); |
| 218 }; | 253 }; |
| 219 | 254 |
| 220 #endif // CHROME_BROWSER_WEB_RESOURCE_PROMO_RESOURCE_SERVICE_H_ | 255 #endif // CHROME_BROWSER_WEB_RESOURCE_PROMO_RESOURCE_SERVICE_H_ |
| OLD | NEW |