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 "chrome/browser/permissions/permission_manager.h" | |
12 #include "chrome/browser/permissions/permission_manager_factory.h" | |
13 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
14 #include "content/public/common/permission_status.mojom.h" | |
15 | |
16 namespace { | |
17 const char* const kObjectListKey = "chosen-objects"; | |
Bernhard Bauer
2015/10/12 11:03:48
Use char[] instead of char* (which I think also le
Reilly Grant (use Gerrit)
2015/10/13 01:32:24
Done.
| |
18 } | |
19 | |
20 ChooserPermissionContextBase::ChooserPermissionContextBase( | |
21 Profile* profile, | |
22 const content::PermissionType permission_type, | |
23 const ContentSettingsType chooser_settings_type) | |
24 : profile_(profile), | |
25 permission_type_(permission_type), | |
26 chooser_settings_type_(chooser_settings_type) {} | |
27 | |
28 ChooserPermissionContextBase::~ChooserPermissionContextBase() {} | |
29 | |
30 scoped_ptr<base::ListValue> | |
31 ChooserPermissionContextBase::GetPreviouslyChosenObjects( | |
32 const GURL& requesting_origin, | |
33 const GURL& embedding_origin) { | |
34 PermissionManager* permission_manager = | |
35 PermissionManagerFactory::GetForProfile(profile_); | |
36 content::PermissionStatus permission_status = | |
37 permission_manager->GetPermissionStatus( | |
38 permission_type_, requesting_origin, embedding_origin); | |
39 if (permission_status != content::PERMISSION_STATUS_GRANTED) | |
40 return make_scoped_ptr(new base::ListValue()); | |
41 | |
42 scoped_ptr<base::DictionaryValue> setting = | |
43 GetWebsiteSetting(requesting_origin, embedding_origin); | |
44 scoped_ptr<base::Value> object_list; | |
45 if (!setting->Remove(kObjectListKey, &object_list)) | |
46 return make_scoped_ptr(new base::ListValue()); | |
47 | |
48 if (object_list->GetType() != base::Value::TYPE_LIST) | |
49 return make_scoped_ptr(new base::ListValue()); | |
50 return base::ListValue::From(object_list.Pass()); | |
51 } | |
52 | |
53 void ChooserPermissionContextBase::GrantObjectPermission( | |
54 const GURL& requesting_origin, | |
55 const GURL& embedding_origin, | |
56 scoped_ptr<base::Value> object) { | |
57 scoped_ptr<base::DictionaryValue> setting = | |
58 GetWebsiteSetting(requesting_origin, embedding_origin); | |
59 base::ListValue* object_list; | |
60 if (!setting->GetList(kObjectListKey, &object_list)) { | |
61 object_list = new base::ListValue(); | |
62 setting->Set(kObjectListKey, object_list); | |
63 } | |
64 object_list->Append(object.Pass()); | |
65 SetWebsiteSetting(requesting_origin, embedding_origin, setting.Pass()); | |
66 } | |
67 | |
68 void ChooserPermissionContextBase::RevokeObjectPermission( | |
69 const GURL& requesting_origin, | |
70 const GURL& embedding_origin, | |
71 const base::Value& object) { | |
72 scoped_ptr<base::DictionaryValue> setting = | |
73 GetWebsiteSetting(requesting_origin, embedding_origin); | |
74 base::ListValue* object_list; | |
75 if (!setting->GetList(kObjectListKey, &object_list)) | |
76 return; | |
77 object_list->Remove(object, nullptr); | |
78 SetWebsiteSetting(requesting_origin, embedding_origin, setting.Pass()); | |
79 } | |
80 | |
81 scoped_ptr<base::DictionaryValue> | |
82 ChooserPermissionContextBase::GetWebsiteSetting(const GURL& requesting_origin, | |
83 const GURL& embedding_origin) { | |
84 HostContentSettingsMap* settings = | |
85 HostContentSettingsMapFactory::GetForProfile(profile_); | |
86 DCHECK(settings); | |
87 | |
88 scoped_ptr<base::DictionaryValue> value = | |
89 base::DictionaryValue::From(settings->GetWebsiteSetting( | |
90 requesting_origin, embedding_origin, chooser_settings_type_, | |
91 std::string(), nullptr)); | |
92 if (!value) | |
93 return make_scoped_ptr(new base::DictionaryValue()); | |
94 | |
95 return value.Pass(); | |
96 } | |
97 | |
98 void ChooserPermissionContextBase::SetWebsiteSetting( | |
99 const GURL& requesting_origin, | |
100 const GURL& embedding_origin, | |
101 scoped_ptr<base::Value> value) { | |
102 HostContentSettingsMap* settings = | |
103 HostContentSettingsMapFactory::GetForProfile(profile_); | |
104 DCHECK(settings); | |
105 | |
106 ContentSettingsPattern primary_pattern( | |
107 ContentSettingsPattern::FromURLNoWildcard(requesting_origin)); | |
108 ContentSettingsPattern secondary_pattern( | |
109 ContentSettingsPattern::FromURLNoWildcard(embedding_origin)); | |
110 if (!primary_pattern.IsValid() || !secondary_pattern.IsValid()) | |
111 return; | |
112 | |
113 settings->SetWebsiteSetting(primary_pattern, secondary_pattern, | |
114 chooser_settings_type_, std::string(), | |
115 value.release()); | |
116 } | |
OLD | NEW |