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 data_content_settings_type) |
| 20 : host_content_settings_map_( |
| 21 HostContentSettingsMapFactory::GetForProfile(profile)), |
| 22 data_content_settings_type_(data_content_settings_type) { |
| 23 DCHECK(host_content_settings_map_); |
| 24 } |
| 25 |
| 26 ChooserContextBase::~ChooserContextBase() {} |
| 27 |
| 28 ObjectList ChooserContextBase::GetGrantedObjects(const GURL& requesting_origin, |
| 29 const GURL& embedding_origin) { |
| 30 scoped_ptr<base::DictionaryValue> setting = |
| 31 GetWebsiteSetting(requesting_origin, embedding_origin); |
| 32 scoped_ptr<base::Value> objects; |
| 33 if (!setting->Remove(kObjectListKey, &objects)) |
| 34 return ObjectList(); |
| 35 |
| 36 scoped_ptr<base::ListValue> object_list = |
| 37 base::ListValue::From(objects.Pass()); |
| 38 if (!object_list) |
| 39 return ObjectList(); |
| 40 |
| 41 ObjectList results; |
| 42 for (base::ListValue::iterator it = object_list->begin(); |
| 43 it != object_list->end(); ++it) { |
| 44 // Steal ownership of |object| from |object_list|. |
| 45 scoped_ptr<base::Value> object(*it); |
| 46 *it = nullptr; |
| 47 |
| 48 scoped_ptr<base::DictionaryValue> object_dict = |
| 49 base::DictionaryValue::From(object.Pass()); |
| 50 if (object_dict && IsValidObject(*object_dict)) |
| 51 results.push_back(object_dict.Pass()); |
| 52 } |
| 53 return results.Pass(); |
| 54 } |
| 55 |
| 56 void ChooserContextBase::GrantObjectPermission( |
| 57 const GURL& requesting_origin, |
| 58 const GURL& embedding_origin, |
| 59 scoped_ptr<base::DictionaryValue> object) { |
| 60 DCHECK(object && IsValidObject(*object)); |
| 61 scoped_ptr<base::DictionaryValue> setting = |
| 62 GetWebsiteSetting(requesting_origin, embedding_origin); |
| 63 base::ListValue* object_list; |
| 64 if (!setting->GetList(kObjectListKey, &object_list)) { |
| 65 object_list = new base::ListValue(); |
| 66 setting->Set(kObjectListKey, object_list); |
| 67 } |
| 68 object_list->AppendIfNotPresent(object.release()); |
| 69 SetWebsiteSetting(requesting_origin, embedding_origin, setting.Pass()); |
| 70 } |
| 71 |
| 72 void ChooserContextBase::RevokeObjectPermission( |
| 73 const GURL& requesting_origin, |
| 74 const GURL& embedding_origin, |
| 75 const base::DictionaryValue& object) { |
| 76 DCHECK(IsValidObject(object)); |
| 77 scoped_ptr<base::DictionaryValue> setting = |
| 78 GetWebsiteSetting(requesting_origin, embedding_origin); |
| 79 base::ListValue* object_list; |
| 80 if (!setting->GetList(kObjectListKey, &object_list)) |
| 81 return; |
| 82 object_list->Remove(object, nullptr); |
| 83 SetWebsiteSetting(requesting_origin, embedding_origin, setting.Pass()); |
| 84 } |
| 85 |
| 86 scoped_ptr<base::DictionaryValue> ChooserContextBase::GetWebsiteSetting( |
| 87 const GURL& requesting_origin, |
| 88 const GURL& embedding_origin) { |
| 89 scoped_ptr<base::DictionaryValue> value = |
| 90 base::DictionaryValue::From(host_content_settings_map_->GetWebsiteSetting( |
| 91 requesting_origin, embedding_origin, data_content_settings_type_, |
| 92 std::string(), nullptr)); |
| 93 if (!value) |
| 94 value.reset(new base::DictionaryValue()); |
| 95 |
| 96 return value.Pass(); |
| 97 } |
| 98 |
| 99 void ChooserContextBase::SetWebsiteSetting(const GURL& requesting_origin, |
| 100 const GURL& embedding_origin, |
| 101 scoped_ptr<base::Value> value) { |
| 102 ContentSettingsPattern primary_pattern( |
| 103 ContentSettingsPattern::FromURLNoWildcard(requesting_origin)); |
| 104 ContentSettingsPattern secondary_pattern( |
| 105 ContentSettingsPattern::FromURLNoWildcard(embedding_origin)); |
| 106 if (!primary_pattern.IsValid() || !secondary_pattern.IsValid()) |
| 107 return; |
| 108 |
| 109 host_content_settings_map_->SetWebsiteSetting( |
| 110 primary_pattern, secondary_pattern, data_content_settings_type_, |
| 111 std::string(), value.release()); |
| 112 } |
OLD | NEW |