| OLD | NEW | 
|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "chrome/browser/banners/app_banner_settings_helper.h" | 5 #include "chrome/browser/banners/app_banner_settings_helper.h" | 
| 6 | 6 | 
| 7 #include <algorithm> | 7 #include <algorithm> | 
| 8 #include <string> | 8 #include <string> | 
| 9 | 9 | 
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" | 
| (...skipping 27 matching lines...) Expand all  Loading... | 
| 38 const unsigned int kMinimumBannerBlockedToBannerShown = 90; | 38 const unsigned int kMinimumBannerBlockedToBannerShown = 90; | 
| 39 | 39 | 
| 40 // Dictionary keys to use for the events. | 40 // Dictionary keys to use for the events. | 
| 41 const char* kBannerEventKeys[] = { | 41 const char* kBannerEventKeys[] = { | 
| 42     "couldShowBannerEvents", | 42     "couldShowBannerEvents", | 
| 43     "didShowBannerEvent", | 43     "didShowBannerEvent", | 
| 44     "didBlockBannerEvent", | 44     "didBlockBannerEvent", | 
| 45     "didAddToHomescreenEvent", | 45     "didAddToHomescreenEvent", | 
| 46 }; | 46 }; | 
| 47 | 47 | 
| 48 // Dictionary key to use whether the banner has been blocked. |  | 
| 49 const char kHasBlockedKey[] = "hasBlocked"; |  | 
| 50 |  | 
| 51 scoped_ptr<base::DictionaryValue> GetOriginDict( | 48 scoped_ptr<base::DictionaryValue> GetOriginDict( | 
| 52     HostContentSettingsMap* settings, | 49     HostContentSettingsMap* settings, | 
| 53     const GURL& origin_url) { | 50     const GURL& origin_url) { | 
| 54   if (!settings) | 51   if (!settings) | 
| 55     return scoped_ptr<base::DictionaryValue>(); | 52     return scoped_ptr<base::DictionaryValue>(); | 
| 56 | 53 | 
| 57   scoped_ptr<base::Value> value = settings->GetWebsiteSetting( | 54   scoped_ptr<base::Value> value = settings->GetWebsiteSetting( | 
| 58       origin_url, origin_url, CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), | 55       origin_url, origin_url, CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), | 
| 59       NULL); | 56       NULL); | 
| 60   if (!value.get()) | 57   if (!value.get()) | 
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 98 } | 95 } | 
| 99 | 96 | 
| 100 void AppBannerSettingsHelper::RecordBannerEvent( | 97 void AppBannerSettingsHelper::RecordBannerEvent( | 
| 101     content::WebContents* web_contents, | 98     content::WebContents* web_contents, | 
| 102     const GURL& origin_url, | 99     const GURL& origin_url, | 
| 103     const std::string& package_name_or_start_url, | 100     const std::string& package_name_or_start_url, | 
| 104     AppBannerEvent event, | 101     AppBannerEvent event, | 
| 105     base::Time time) { | 102     base::Time time) { | 
| 106   Profile* profile = | 103   Profile* profile = | 
| 107       Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 104       Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 
| 108   if (profile->IsOffTheRecord() || web_contents->GetURL() != origin_url || | 105   if (profile->IsOffTheRecord() || package_name_or_start_url.empty()) | 
| 109       package_name_or_start_url.empty()) { |  | 
| 110     return; | 106     return; | 
| 111   } |  | 
| 112 | 107 | 
| 113   ContentSettingsPattern pattern(ContentSettingsPattern::FromURL(origin_url)); | 108   ContentSettingsPattern pattern(ContentSettingsPattern::FromURL(origin_url)); | 
| 114   if (!pattern.IsValid()) | 109   if (!pattern.IsValid()) | 
| 115     return; | 110     return; | 
| 116 | 111 | 
| 117   HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); | 112   HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); | 
| 118   scoped_ptr<base::DictionaryValue> origin_dict = | 113   scoped_ptr<base::DictionaryValue> origin_dict = | 
| 119       GetOriginDict(settings, origin_url); | 114       GetOriginDict(settings, origin_url); | 
| 120   if (!origin_dict) | 115   if (!origin_dict) | 
| 121     return; | 116     return; | 
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 290   if (!app_dict) | 285   if (!app_dict) | 
| 291     return base::Time(); | 286     return base::Time(); | 
| 292 | 287 | 
| 293   std::string event_key(kBannerEventKeys[event]); | 288   std::string event_key(kBannerEventKeys[event]); | 
| 294   double internal_time; | 289   double internal_time; | 
| 295   if (!app_dict->GetDouble(event_key, &internal_time)) | 290   if (!app_dict->GetDouble(event_key, &internal_time)) | 
| 296     return base::Time(); | 291     return base::Time(); | 
| 297 | 292 | 
| 298   return base::Time::FromInternalValue(internal_time); | 293   return base::Time::FromInternalValue(internal_time); | 
| 299 } | 294 } | 
| 300 |  | 
| 301 bool AppBannerSettingsHelper::IsAllowed( |  | 
| 302     content::WebContents* web_contents, |  | 
| 303     const GURL& origin_url, |  | 
| 304     const std::string& package_name_or_start_url) { |  | 
| 305   Profile* profile = |  | 
| 306       Profile::FromBrowserContext(web_contents->GetBrowserContext()); |  | 
| 307   if (profile->IsOffTheRecord() || web_contents->GetURL() != origin_url || |  | 
| 308       package_name_or_start_url.empty()) { |  | 
| 309     return false; |  | 
| 310   } |  | 
| 311 |  | 
| 312   HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); |  | 
| 313   scoped_ptr<base::DictionaryValue> origin_dict = |  | 
| 314       GetOriginDict(settings, origin_url); |  | 
| 315 |  | 
| 316   if (!origin_dict) |  | 
| 317     return true; |  | 
| 318 |  | 
| 319   base::DictionaryValue* app_dict = |  | 
| 320       GetAppDict(origin_dict.get(), package_name_or_start_url); |  | 
| 321   if (!app_dict) |  | 
| 322     return true; |  | 
| 323 |  | 
| 324   bool has_blocked; |  | 
| 325   if (!app_dict->GetBoolean(kHasBlockedKey, &has_blocked)) |  | 
| 326     return true; |  | 
| 327 |  | 
| 328   return !has_blocked; |  | 
| 329 } |  | 
| 330 |  | 
| 331 void AppBannerSettingsHelper::Block( |  | 
| 332     content::WebContents* web_contents, |  | 
| 333     const GURL& origin_url, |  | 
| 334     const std::string& package_name_or_start_url) { |  | 
| 335   Profile* profile = |  | 
| 336       Profile::FromBrowserContext(web_contents->GetBrowserContext()); |  | 
| 337   if (profile->IsOffTheRecord() || web_contents->GetURL() != origin_url || |  | 
| 338       package_name_or_start_url.empty()) { |  | 
| 339     return; |  | 
| 340   } |  | 
| 341 |  | 
| 342   ContentSettingsPattern pattern(ContentSettingsPattern::FromURL(origin_url)); |  | 
| 343   if (!pattern.IsValid()) |  | 
| 344     return; |  | 
| 345 |  | 
| 346   HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); |  | 
| 347   scoped_ptr<base::DictionaryValue> origin_dict = |  | 
| 348       GetOriginDict(settings, origin_url); |  | 
| 349 |  | 
| 350   if (!origin_dict) |  | 
| 351     return; |  | 
| 352 |  | 
| 353   base::DictionaryValue* app_dict = |  | 
| 354       GetAppDict(origin_dict.get(), package_name_or_start_url); |  | 
| 355   if (!app_dict) |  | 
| 356     return; |  | 
| 357 |  | 
| 358   // Update the setting and save it back. |  | 
| 359   app_dict->SetBoolean(kHasBlockedKey, true); |  | 
| 360   settings->SetWebsiteSetting(pattern, ContentSettingsPattern::Wildcard(), |  | 
| 361                               CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), |  | 
| 362                               origin_dict.release()); |  | 
| 363 } |  | 
| OLD | NEW | 
|---|