OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 #include "chrome/browser/permissions/chooser_context_base.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 11 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 12 |
| 13 using ObjectList = ScopedVector<base::DictionaryValue>; |
| 14 |
| 15 const char kObjectListKey[] = "chosen-objects"; |
| 16 |
| 17 ChooserContextBase::ChooserContextBase( |
| 18 Profile* profile, |
| 19 const ContentSettingsType permission_type, |
| 20 const ContentSettingsType chooser_type) |
| 21 : host_content_settings_map_( |
| 22 HostContentSettingsMapFactory::GetForProfile(profile)), |
| 23 permission_type_(permission_type), |
| 24 chooser_type_(chooser_type) { |
| 25 DCHECK(host_content_settings_map_); |
| 26 } |
| 27 |
| 28 ChooserContextBase::~ChooserContextBase() {} |
| 29 |
| 30 ObjectList ChooserContextBase::GetPreviouslyChosenObjects( |
| 31 const GURL& requesting_origin, |
| 32 const GURL& embedding_origin) { |
| 33 ContentSetting content_setting = |
| 34 host_content_settings_map_->GetContentSetting( |
| 35 requesting_origin, embedding_origin, permission_type_, std::string()); |
| 36 if (content_setting != CONTENT_SETTING_ALLOW) |
| 37 return ObjectList(); |
| 38 |
| 39 scoped_ptr<base::DictionaryValue> setting = |
| 40 GetWebsiteSetting(requesting_origin, embedding_origin); |
| 41 scoped_ptr<base::Value> objects; |
| 42 if (!setting->Remove(kObjectListKey, &objects)) |
| 43 return ObjectList(); |
| 44 |
| 45 scoped_ptr<base::ListValue> object_list = |
| 46 base::ListValue::From(objects.Pass()); |
| 47 if (!object_list) |
| 48 return ObjectList(); |
| 49 |
| 50 ObjectList results; |
| 51 for (base::ListValue::iterator it = object_list->begin(); |
| 52 it != object_list->end(); ++it) { |
| 53 // Steal ownership of |object| from |object_list|. |
| 54 scoped_ptr<base::Value> object(*it); |
| 55 *it = nullptr; |
| 56 |
| 57 scoped_ptr<base::DictionaryValue> object_dict = |
| 58 base::DictionaryValue::From(object.Pass()); |
| 59 if (object_dict && IsValidObject(*object_dict)) |
| 60 results.push_back(object_dict.Pass()); |
| 61 } |
| 62 return results.Pass(); |
| 63 } |
| 64 |
| 65 void ChooserContextBase::GrantObjectPermission( |
| 66 const GURL& requesting_origin, |
| 67 const GURL& embedding_origin, |
| 68 scoped_ptr<base::DictionaryValue> object) { |
| 69 DCHECK(object && IsValidObject(*object)); |
| 70 scoped_ptr<base::DictionaryValue> setting = |
| 71 GetWebsiteSetting(requesting_origin, embedding_origin); |
| 72 base::ListValue* object_list; |
| 73 if (!setting->GetList(kObjectListKey, &object_list)) { |
| 74 object_list = new base::ListValue(); |
| 75 setting->Set(kObjectListKey, object_list); |
| 76 } |
| 77 object_list->AppendIfNotPresent(object.release()); |
| 78 SetWebsiteSetting(requesting_origin, embedding_origin, setting.Pass()); |
| 79 } |
| 80 |
| 81 void ChooserContextBase::RevokeObjectPermission( |
| 82 const GURL& requesting_origin, |
| 83 const GURL& embedding_origin, |
| 84 const base::DictionaryValue& object) { |
| 85 DCHECK(IsValidObject(object)); |
| 86 scoped_ptr<base::DictionaryValue> setting = |
| 87 GetWebsiteSetting(requesting_origin, embedding_origin); |
| 88 base::ListValue* object_list; |
| 89 if (!setting->GetList(kObjectListKey, &object_list)) |
| 90 return; |
| 91 object_list->Remove(object, nullptr); |
| 92 SetWebsiteSetting(requesting_origin, embedding_origin, setting.Pass()); |
| 93 } |
| 94 |
| 95 scoped_ptr<base::DictionaryValue> ChooserContextBase::GetWebsiteSetting( |
| 96 const GURL& requesting_origin, |
| 97 const GURL& embedding_origin) { |
| 98 scoped_ptr<base::DictionaryValue> value = |
| 99 base::DictionaryValue::From(host_content_settings_map_->GetWebsiteSetting( |
| 100 requesting_origin, embedding_origin, chooser_type_, std::string(), |
| 101 nullptr)); |
| 102 if (!value) |
| 103 return make_scoped_ptr(new base::DictionaryValue()); |
| 104 |
| 105 return value.Pass(); |
| 106 } |
| 107 |
| 108 void ChooserContextBase::SetWebsiteSetting(const GURL& requesting_origin, |
| 109 const GURL& embedding_origin, |
| 110 scoped_ptr<base::Value> value) { |
| 111 ContentSettingsPattern primary_pattern( |
| 112 ContentSettingsPattern::FromURLNoWildcard(requesting_origin)); |
| 113 ContentSettingsPattern secondary_pattern( |
| 114 ContentSettingsPattern::FromURLNoWildcard(embedding_origin)); |
| 115 if (!primary_pattern.IsValid() || !secondary_pattern.IsValid()) |
| 116 return; |
| 117 |
| 118 host_content_settings_map_->SetWebsiteSetting( |
| 119 primary_pattern, secondary_pattern, chooser_type_, std::string(), |
| 120 value.release()); |
| 121 } |
OLD | NEW |