Chromium Code Reviews| Index: chrome/browser/permissions/chooser_permission_context_base.cc |
| diff --git a/chrome/browser/permissions/chooser_permission_context_base.cc b/chrome/browser/permissions/chooser_permission_context_base.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1052b414f67fbd00b847b3f8304dfdc22249d073 |
| --- /dev/null |
| +++ b/chrome/browser/permissions/chooser_permission_context_base.cc |
| @@ -0,0 +1,123 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/permissions/chooser_permission_context_base.h" |
| + |
| +#include <string> |
| + |
| +#include "base/values.h" |
| +#include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| +#include "components/content_settings/core/browser/host_content_settings_map.h" |
| + |
| +const char kObjectListKey[] = "chosen-objects"; |
| + |
| +ChooserPermissionContextBase::ChooserPermissionContextBase( |
| + Profile* profile, |
| + const ContentSettingsType permission_type, |
| + const ContentSettingsType chooser_settings_type) |
| + : host_content_settings_map_( |
| + HostContentSettingsMapFactory::GetForProfile(profile)), |
| + permission_type_(permission_type), |
| + chooser_settings_type_(chooser_settings_type) { |
| + DCHECK(host_content_settings_map_); |
| +} |
| + |
| +ChooserPermissionContextBase::~ChooserPermissionContextBase() {} |
| + |
| +ChooserPermissionContextBase::ObjectList |
| +ChooserPermissionContextBase::GetPreviouslyChosenObjects( |
| + const GURL& requesting_origin, |
| + const GURL& embedding_origin) { |
| + ContentSetting content_setting = |
| + host_content_settings_map_->GetContentSetting( |
| + requesting_origin, embedding_origin, permission_type_, std::string()); |
| + if (content_setting != CONTENT_SETTING_ALLOW && |
| + content_setting != CONTENT_SETTING_SESSION_ONLY) |
|
raymes
2015/10/14 02:07:04
nit: SESSION_ONLY isn't a possible value for this
Reilly Grant (use Gerrit)
2015/10/22 00:57:02
Done.
|
| + return ObjectList(); |
| + |
| + scoped_ptr<base::DictionaryValue> setting = |
| + GetWebsiteSetting(requesting_origin, embedding_origin); |
| + scoped_ptr<base::Value> objects; |
| + if (!setting->Remove(kObjectListKey, &objects)) |
|
raymes
2015/10/14 02:07:04
Hmm maybe I'm misreading but it looks like this di
Reilly Grant (use Gerrit)
2015/10/22 00:57:02
I need to get the dictionary so that I can replace
|
| + return ObjectList(); |
| + |
| + scoped_ptr<base::ListValue> object_list = |
| + base::ListValue::From(objects.Pass()); |
| + if (!object_list) |
| + return ObjectList(); |
| + |
| + ObjectList results; |
| + for (base::ListValue::iterator it = object_list->begin(); |
| + it != object_list->end(); ++it) { |
| + if ((*it)->GetType() != base::Value::TYPE_DICTIONARY) |
|
raymes
2015/10/14 02:07:04
Should there be a DCHECK here?
Reilly Grant (use Gerrit)
2015/10/22 00:57:02
I would rather not DCHECK on invalid data in user
|
| + continue; |
| + |
| + scoped_ptr<base::DictionaryValue> object( |
| + static_cast<base::DictionaryValue*>(*it)); |
| + *it = nullptr; |
| + |
| + if (IsValidObject(*object)) |
| + results.push_back(object.Pass()); |
| + } |
| + return results.Pass(); |
| +} |
| + |
| +void ChooserPermissionContextBase::GrantObjectPermission( |
| + const GURL& requesting_origin, |
| + const GURL& embedding_origin, |
| + scoped_ptr<base::DictionaryValue> object) { |
| + DCHECK(object && IsValidObject(*object)); |
| + scoped_ptr<base::DictionaryValue> setting = |
| + GetWebsiteSetting(requesting_origin, embedding_origin); |
| + base::ListValue* object_list; |
| + if (!setting->GetList(kObjectListKey, &object_list)) { |
| + object_list = new base::ListValue(); |
| + setting->Set(kObjectListKey, object_list); |
| + } |
| + object_list->AppendIfNotPresent(object.release()); |
| + SetWebsiteSetting(requesting_origin, embedding_origin, setting.Pass()); |
| +} |
| + |
| +void ChooserPermissionContextBase::RevokeObjectPermission( |
| + const GURL& requesting_origin, |
| + const GURL& embedding_origin, |
| + const base::DictionaryValue& object) { |
|
raymes
2015/10/14 02:07:04
optional: Maybe just passing a pointer here for co
|
| + DCHECK(IsValidObject(object)); |
| + scoped_ptr<base::DictionaryValue> setting = |
| + GetWebsiteSetting(requesting_origin, embedding_origin); |
| + base::ListValue* object_list; |
| + if (!setting->GetList(kObjectListKey, &object_list)) |
| + return; |
| + object_list->Remove(object, nullptr); |
| + SetWebsiteSetting(requesting_origin, embedding_origin, setting.Pass()); |
| +} |
| + |
| +scoped_ptr<base::DictionaryValue> |
| +ChooserPermissionContextBase::GetWebsiteSetting(const GURL& requesting_origin, |
| + const GURL& embedding_origin) { |
| + scoped_ptr<base::DictionaryValue> value = |
| + base::DictionaryValue::From(host_content_settings_map_->GetWebsiteSetting( |
| + requesting_origin, embedding_origin, chooser_settings_type_, |
| + std::string(), nullptr)); |
| + if (!value) |
| + return make_scoped_ptr(new base::DictionaryValue()); |
| + |
| + return value.Pass(); |
| +} |
| + |
| +void ChooserPermissionContextBase::SetWebsiteSetting( |
| + const GURL& requesting_origin, |
| + const GURL& embedding_origin, |
| + scoped_ptr<base::Value> value) { |
| + ContentSettingsPattern primary_pattern( |
| + ContentSettingsPattern::FromURLNoWildcard(requesting_origin)); |
| + ContentSettingsPattern secondary_pattern( |
| + ContentSettingsPattern::FromURLNoWildcard(embedding_origin)); |
| + if (!primary_pattern.IsValid() || !secondary_pattern.IsValid()) |
| + return; |
| + |
| + host_content_settings_map_->SetWebsiteSetting( |
| + primary_pattern, secondary_pattern, chooser_settings_type_, std::string(), |
| + value.release()); |
| +} |