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..f7c20de31101088004d4cbde190830e7abe6686c |
--- /dev/null |
+++ b/chrome/browser/permissions/chooser_permission_context_base.cc |
@@ -0,0 +1,89 @@ |
+// 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" |
+ |
+namespace { |
+const char* const kObjectListKey = "chosen-objects"; |
+} |
+ |
+ChooserPermissionContextBase::ChooserPermissionContextBase( |
+ Profile* profile, |
+ const ContentSettingsType permission_type) |
+ : PermissionContextBase(profile, permission_type) {} |
+ |
+ChooserPermissionContextBase::~ChooserPermissionContextBase() {} |
+ |
+scoped_ptr<base::ListValue> ChooserPermissionContextBase::GetChosenObjects( |
+ const GURL& origin) { |
raymes
2015/10/06 05:28:20
I think this is probably where we want to check th
Reilly Grant (use Gerrit)
2015/10/06 20:45:30
If this class is created for CONTENT_SETTING_TYPE_
|
+ scoped_ptr<base::DictionaryValue> setting = GetWebsiteSetting(origin); |
+ scoped_ptr<base::Value> object_list; |
+ if (!setting->Remove(kObjectListKey, &object_list)) |
+ return make_scoped_ptr(new base::ListValue()); |
+ |
+ if (object_list->GetType() != base::Value::TYPE_LIST) |
+ return make_scoped_ptr(new base::ListValue()); |
+ return base::ListValue::From(object_list.Pass()); |
+} |
+ |
+void ChooserPermissionContextBase::GrantObjectPermission( |
+ const GURL& origin, |
+ scoped_ptr<base::Value> object) { |
+ scoped_ptr<base::DictionaryValue> setting = GetWebsiteSetting(origin); |
+ base::ListValue* object_list; |
+ if (!setting->GetList(kObjectListKey, &object_list)) { |
+ object_list = new base::ListValue(); |
+ setting->Set(kObjectListKey, object_list); |
+ } |
+ object_list->Append(object.Pass()); |
+ SetWebsiteSetting(origin, setting.Pass()); |
+} |
+ |
+void ChooserPermissionContextBase::RevokeObjectPermission( |
+ const GURL& origin, |
+ const base::Value& object) { |
+ scoped_ptr<base::DictionaryValue> setting = GetWebsiteSetting(origin); |
+ base::ListValue* object_list; |
+ if (!setting->GetList(kObjectListKey, &object_list)) |
+ return; |
+ object_list->Remove(object, nullptr); |
+ SetWebsiteSetting(origin, setting.Pass()); |
+} |
+ |
+scoped_ptr<base::DictionaryValue> |
+ChooserPermissionContextBase::GetWebsiteSetting(const GURL& origin) { |
+ HostContentSettingsMap* settings = |
+ HostContentSettingsMapFactory::GetForProfile(profile()); |
+ DCHECK(settings); |
+ |
+ scoped_ptr<base::DictionaryValue> value = |
+ base::DictionaryValue::From(settings->GetWebsiteSetting( |
+ origin, origin, type(), std::string(), nullptr)); |
+ if (!value) |
+ return make_scoped_ptr(new base::DictionaryValue()); |
+ |
+ return value.Pass(); |
+} |
+ |
+void ChooserPermissionContextBase::SetWebsiteSetting( |
+ const GURL& origin, |
+ scoped_ptr<base::Value> value) { |
+ HostContentSettingsMap* settings = |
+ HostContentSettingsMapFactory::GetForProfile(profile()); |
+ DCHECK(settings); |
+ |
+ ContentSettingsPattern pattern( |
+ ContentSettingsPattern::FromURLNoWildcard(origin)); |
+ if (!pattern.IsValid()) |
+ return; |
+ |
+ settings->SetWebsiteSetting(pattern, ContentSettingsPattern::Wildcard(), |
+ type(), std::string(), value.release()); |
+} |