OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_EXTENSIONS_APPS_PROMO_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_APPS_PROMO_H_ | |
7 #pragma once | |
8 | |
9 #include <set> | |
10 #include <string> | |
11 | |
12 #include "base/gtest_prod_util.h" | |
13 #include "chrome/common/extensions/extension.h" | |
14 #include "net/url_request/url_fetcher_delegate.h" | |
15 | |
16 class PrefService; | |
17 class Profile; | |
18 | |
19 namespace net { | |
20 class URLFetcher; | |
21 } // namespace net | |
22 | |
23 // This encapsulates business logic for: | |
24 // - Whether to show the apps promo in the launcher | |
25 // - Whether to expire existing default apps | |
26 class AppsPromo { | |
27 public: | |
28 // Groups users by whether they have seen a web store promo before. This is | |
29 // used for deciding to maximize the promo and apps section on the NTP. | |
30 enum UserGroup { | |
31 // Matches no users. | |
32 USERS_NONE = 0, | |
33 | |
34 // Users who have not seen a promo (last promo id is default value). | |
35 USERS_NEW = 1, | |
36 | |
37 // Users who have already seen a promo (last promo id is non-default). | |
38 USERS_EXISTING = 1 << 1, | |
39 }; | |
40 | |
41 // Holds all the data that specifies a promo for the apps section of the NTP. | |
42 struct PromoData { | |
43 PromoData(); | |
44 PromoData(const std::string& id, | |
45 const std::string& header, | |
46 const std::string& button, | |
47 const GURL& link, | |
48 const std::string& expire, | |
49 const GURL& logo, | |
50 int user_group); | |
51 ~PromoData(); | |
52 | |
53 // See PromoResourceService::UnpackWebStoreSignal for descriptions of these | |
54 // fields. | |
55 std::string id; | |
56 std::string header; | |
57 std::string button; | |
58 GURL link; | |
59 std::string expire; | |
60 GURL logo; | |
61 int user_group; | |
62 }; | |
63 | |
64 // Register our preferences. Parts of the promo content are stored in Local | |
65 // State since they're independent of the user profile. | |
66 static void RegisterPrefs(PrefService* local_state); | |
67 static void RegisterUserPrefs(PrefService* prefs); | |
68 | |
69 // Returns true if a promo is available for the current locale. | |
70 static bool IsPromoSupportedForLocale(); | |
71 | |
72 // Returns true if the web store is active for the existing locale. | |
73 static bool IsWebStoreSupportedForLocale(); | |
74 | |
75 // Sets whether the web store and apps section is supported for the current | |
76 // locale. | |
77 static void SetWebStoreSupportedForLocale(bool supported); | |
78 | |
79 // Accesses the current promo data. The default logo will be used if | |
80 // |promo_data.logo| is empty or not a valid 'data' URL. | |
81 static void ClearPromo(); | |
82 static PromoData GetPromo(); | |
83 static void SetPromo(const PromoData& promo_data); | |
84 | |
85 // Gets the original URL of the logo. This should only be set when the logo | |
86 // was served over HTTPS. | |
87 static GURL GetSourcePromoLogoURL(); | |
88 static void SetSourcePromoLogoURL(const GURL& original_url); | |
89 | |
90 explicit AppsPromo(PrefService* prefs); | |
91 ~AppsPromo(); | |
92 | |
93 // Gets the set of old default apps that may have been installed by previous | |
94 // versions of Chrome. | |
95 const extensions::ExtensionIdSet& old_default_apps() const { | |
96 return old_default_app_ids_; | |
97 } | |
98 | |
99 // Halts the special treatment of the default apps. The default apps may be | |
100 // removed by the caller after calling this method. If the apps remain | |
101 // installed, AppsPromo will no longer consider the apps "default". | |
102 void ExpireDefaultApps(); | |
103 | |
104 // Called to hide the promo from the apps section. | |
105 void HidePromo(); | |
106 | |
107 // Returns true if the app launcher should be displayed on the NTP. | |
108 bool ShouldShowAppLauncher(const extensions::ExtensionIdSet& installed_ids); | |
109 | |
110 // Returns true if the apps promo should be displayed in the launcher. | |
111 bool ShouldShowPromo(const extensions::ExtensionIdSet& installed_ids, | |
112 bool* just_expired); | |
113 | |
114 private: | |
115 FRIEND_TEST_ALL_PREFIXES(ExtensionAppsPromo, HappyPath); | |
116 FRIEND_TEST_ALL_PREFIXES(ExtensionAppsPromo, PromoPrefs); | |
117 FRIEND_TEST_ALL_PREFIXES(ExtensionAppsPromo, UpdatePromoFocus); | |
118 | |
119 // The maximum number of times to show the apps promo. The promo counter | |
120 // actually goes up to this number + 1 because we need to differentiate | |
121 // between the first time we overflow and subsequent times. | |
122 static const int kDefaultAppsCounterMax; | |
123 | |
124 bool GetDefaultAppsInstalled() const; | |
125 | |
126 // Gets the UserGroup classification of the current user. | |
127 UserGroup GetCurrentUserGroup() const; | |
128 | |
129 // Gets/sets the ID of the last promo shown. | |
130 std::string GetLastPromoId(); | |
131 void SetLastPromoId(const std::string& id); | |
132 | |
133 // Gets/sets the number of times the promo has been viewed. Promo views are | |
134 // only counted when the default apps are installed. | |
135 int GetPromoCounter() const; | |
136 void SetPromoCounter(int val); | |
137 | |
138 // Our permanent state is stored in this PrefService instance. | |
139 PrefService* prefs_; | |
140 | |
141 // The set of default extensions. Initialized to a static list in the | |
142 // constructor. | |
143 extensions::ExtensionIdSet old_default_app_ids_; | |
144 | |
145 DISALLOW_COPY_AND_ASSIGN(AppsPromo); | |
146 }; | |
147 | |
148 // Fetches logos over HTTPS, making sure we don't send cookies and that we | |
149 // cache the image until its source URL changes. | |
150 class AppsPromoLogoFetcher : public net::URLFetcherDelegate { | |
151 public: | |
152 AppsPromoLogoFetcher(Profile* profile, | |
153 const AppsPromo::PromoData& promo_data); | |
154 virtual ~AppsPromoLogoFetcher(); | |
155 | |
156 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
157 | |
158 private: | |
159 // Fetches the logo and stores the result as a data URL. | |
160 void FetchLogo(); | |
161 | |
162 // Checks if the logo was downloaded previously. | |
163 bool HaveCachedLogo(); | |
164 | |
165 // Sets the apps promo based on the current data and then issues the | |
166 // WEB_STORE_PROMO_LOADED notification so open NTPs can inject the promo. | |
167 void SavePromo(); | |
168 | |
169 // Checks if the promo logo matches https://*.google.com/*.png. | |
170 bool SupportsLogoURL(); | |
171 | |
172 Profile* profile_; | |
173 AppsPromo::PromoData promo_data_; | |
174 scoped_ptr<net::URLFetcher> url_fetcher_; | |
175 }; | |
176 | |
177 #endif // CHROME_BROWSER_EXTENSIONS_APPS_PROMO_H_ | |
OLD | NEW |