This source file includes following definitions.
- SanitizePackageName
- IsAllowed
- Block
#include "chrome/browser/android/banners/app_banner_settings_helper.h"
#include <algorithm>
#include <string>
#include "chrome/browser/content_settings/host_content_settings_map.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/content_settings_pattern.h"
#include "content/public/browser/web_contents.h"
#include "url/gurl.h"
namespace {
std::string SanitizePackageName(std::string package_name) {
std::replace(package_name.begin(), package_name.end(), '.', ' ');
return package_name;
}
const size_t kMaxAppsPerSite = 3;
}
bool AppBannerSettingsHelper::IsAllowed(content::WebContents* web_contents,
const GURL& origin_url,
const std::string& package_name) {
std::string sanitized_package_name = SanitizePackageName(package_name);
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
if (profile->IsOffTheRecord() || web_contents->GetURL() != origin_url ||
sanitized_package_name.empty()) {
return false;
}
HostContentSettingsMap* settings = profile->GetHostContentSettingsMap();
if (!settings)
return false;
scoped_ptr<base::Value> value(
settings->GetWebsiteSetting(origin_url,
origin_url,
CONTENT_SETTINGS_TYPE_APP_BANNER,
std::string(),
NULL));
if (!value.get()) {
return true;
} else if (value->IsType(base::Value::TYPE_DICTIONARY)) {
base::DictionaryValue* banner_dict =
static_cast<base::DictionaryValue*>(value.get());
bool is_allowed = false;
if (banner_dict->GetBoolean(sanitized_package_name, &is_allowed)) {
return is_allowed;
} else {
return banner_dict->size() < ::kMaxAppsPerSite;
}
} else {
ContentSettingsPattern pattern(ContentSettingsPattern::FromURL(origin_url));
if (pattern.IsValid()) {
settings->SetWebsiteSetting(pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_APP_BANNER,
std::string(),
NULL);
return true;
}
}
return false;
}
void AppBannerSettingsHelper::Block(content::WebContents* web_contents,
const GURL& origin_url,
const std::string& package_name) {
std::string sanitized_package_name = SanitizePackageName(package_name);
DCHECK(!sanitized_package_name.empty());
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
HostContentSettingsMap* settings = profile->GetHostContentSettingsMap();
ContentSettingsPattern pattern(ContentSettingsPattern::FromURL(origin_url));
if (!settings || !pattern.IsValid())
return;
scoped_ptr<base::Value> value(
settings->GetWebsiteSetting(origin_url,
origin_url,
CONTENT_SETTINGS_TYPE_APP_BANNER,
std::string(),
NULL));
base::DictionaryValue* banner_dict = NULL;
if (value.get() && value->IsType(base::Value::TYPE_DICTIONARY)) {
banner_dict = static_cast<base::DictionaryValue*>(value.release());
} else {
banner_dict = new base::DictionaryValue();
}
banner_dict->SetBoolean(sanitized_package_name, false);
settings->SetWebsiteSetting(pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_APP_BANNER,
std::string(),
banner_dict);
}