Chromium Code Reviews| 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_permission_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 const char kObjectListKey[] = "chosen-objects"; | |
| 14 | |
| 15 ChooserPermissionContextBase::ChooserPermissionContextBase( | |
| 16 Profile* profile, | |
| 17 const ContentSettingsType permission_type, | |
| 18 const ContentSettingsType chooser_settings_type) | |
| 19 : host_content_settings_map_( | |
| 20 HostContentSettingsMapFactory::GetForProfile(profile)), | |
| 21 permission_type_(permission_type), | |
| 22 chooser_settings_type_(chooser_settings_type) { | |
| 23 DCHECK(host_content_settings_map_); | |
| 24 } | |
| 25 | |
| 26 ChooserPermissionContextBase::~ChooserPermissionContextBase() {} | |
| 27 | |
| 28 ChooserPermissionContextBase::ObjectList | |
| 29 ChooserPermissionContextBase::GetPreviouslyChosenObjects( | |
| 30 const GURL& requesting_origin, | |
| 31 const GURL& embedding_origin) { | |
| 32 ContentSetting content_setting = | |
| 33 host_content_settings_map_->GetContentSetting( | |
| 34 requesting_origin, embedding_origin, permission_type_, std::string()); | |
| 35 if (content_setting != CONTENT_SETTING_ALLOW && | |
| 36 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.
| |
| 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)) | |
|
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
| |
| 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 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
| |
| 54 continue; | |
| 55 | |
| 56 scoped_ptr<base::DictionaryValue> object( | |
| 57 static_cast<base::DictionaryValue*>(*it)); | |
| 58 *it = nullptr; | |
| 59 | |
| 60 if (IsValidObject(*object)) | |
| 61 results.push_back(object.Pass()); | |
| 62 } | |
| 63 return results.Pass(); | |
| 64 } | |
| 65 | |
| 66 void ChooserPermissionContextBase::GrantObjectPermission( | |
| 67 const GURL& requesting_origin, | |
| 68 const GURL& embedding_origin, | |
| 69 scoped_ptr<base::DictionaryValue> object) { | |
| 70 DCHECK(object && IsValidObject(*object)); | |
| 71 scoped_ptr<base::DictionaryValue> setting = | |
| 72 GetWebsiteSetting(requesting_origin, embedding_origin); | |
| 73 base::ListValue* object_list; | |
| 74 if (!setting->GetList(kObjectListKey, &object_list)) { | |
| 75 object_list = new base::ListValue(); | |
| 76 setting->Set(kObjectListKey, object_list); | |
| 77 } | |
| 78 object_list->AppendIfNotPresent(object.release()); | |
| 79 SetWebsiteSetting(requesting_origin, embedding_origin, setting.Pass()); | |
| 80 } | |
| 81 | |
| 82 void ChooserPermissionContextBase::RevokeObjectPermission( | |
| 83 const GURL& requesting_origin, | |
| 84 const GURL& embedding_origin, | |
| 85 const base::DictionaryValue& object) { | |
|
raymes
2015/10/14 02:07:04
optional: Maybe just passing a pointer here for co
| |
| 86 DCHECK(IsValidObject(object)); | |
| 87 scoped_ptr<base::DictionaryValue> setting = | |
| 88 GetWebsiteSetting(requesting_origin, embedding_origin); | |
| 89 base::ListValue* object_list; | |
| 90 if (!setting->GetList(kObjectListKey, &object_list)) | |
| 91 return; | |
| 92 object_list->Remove(object, nullptr); | |
| 93 SetWebsiteSetting(requesting_origin, embedding_origin, setting.Pass()); | |
| 94 } | |
| 95 | |
| 96 scoped_ptr<base::DictionaryValue> | |
| 97 ChooserPermissionContextBase::GetWebsiteSetting(const GURL& requesting_origin, | |
| 98 const GURL& embedding_origin) { | |
| 99 scoped_ptr<base::DictionaryValue> value = | |
| 100 base::DictionaryValue::From(host_content_settings_map_->GetWebsiteSetting( | |
| 101 requesting_origin, embedding_origin, chooser_settings_type_, | |
| 102 std::string(), nullptr)); | |
| 103 if (!value) | |
| 104 return make_scoped_ptr(new base::DictionaryValue()); | |
| 105 | |
| 106 return value.Pass(); | |
| 107 } | |
| 108 | |
| 109 void ChooserPermissionContextBase::SetWebsiteSetting( | |
| 110 const GURL& requesting_origin, | |
| 111 const GURL& embedding_origin, | |
| 112 scoped_ptr<base::Value> value) { | |
| 113 ContentSettingsPattern primary_pattern( | |
| 114 ContentSettingsPattern::FromURLNoWildcard(requesting_origin)); | |
| 115 ContentSettingsPattern secondary_pattern( | |
| 116 ContentSettingsPattern::FromURLNoWildcard(embedding_origin)); | |
| 117 if (!primary_pattern.IsValid() || !secondary_pattern.IsValid()) | |
| 118 return; | |
| 119 | |
| 120 host_content_settings_map_->SetWebsiteSetting( | |
| 121 primary_pattern, secondary_pattern, chooser_settings_type_, std::string(), | |
| 122 value.release()); | |
| 123 } | |
| OLD | NEW |