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/permission_infobar_request.h" | |
6 | |
7 #include "base/prefs/pref_service.h" | |
8 #include "chrome/browser/geolocation/geolocation_infobar_delegate.h" | |
9 #include "chrome/browser/infobars/infobar_service.h" | |
10 #include "chrome/browser/media/midi_permission_infobar_delegate.h" | |
11 #include "chrome/browser/notifications/notification_permission_infobar_delegate. h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/browser/storage/durable_storage_permission_infobar_delegate.h" | |
14 #include "chrome/common/pref_names.h" | |
15 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
16 #include "components/infobars/core/infobar.h" | |
17 #include "content/public/browser/browser_thread.h" | |
18 | |
19 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | |
20 #include "chrome/browser/media/protected_media_identifier_infobar_delegate.h" | |
21 #endif | |
mlamouri (slow - plz ping)
2015/09/16 16:21:01
Do we expect to use this code in other platforms t
Lalit Maganti
2015/09/16 17:28:38
Interesting question. I'm not sure. I assume becau
| |
22 | |
23 PermissionInfoBarRequest::PermissionRequest::PermissionRequest( | |
24 ContentSettingsType type, | |
25 const PermissionDecidedCallback& user_callback, | |
26 const PermissionDecidedCallback& non_user_callback) | |
27 : type_(type), | |
28 user_callback_(user_callback), | |
29 non_user_callback_(non_user_callback) { | |
30 } | |
31 | |
32 PermissionInfoBarRequest::PermissionRequest::~PermissionRequest() { | |
33 } | |
34 | |
35 PermissionInfoBarRequest::PermissionInfoBarRequest( | |
36 const GURL& requesting_frame, | |
37 const GURL& embedding_frame) | |
38 : infobar_(nullptr), | |
39 requesting_frame_(requesting_frame), | |
40 embedding_frame_(embedding_frame), | |
41 weak_factory_(this) { | |
42 } | |
43 | |
44 PermissionInfoBarRequest::~PermissionInfoBarRequest() { | |
45 } | |
46 | |
47 void PermissionInfoBarRequest::AddPermission( | |
48 const ContentSettingsType type, | |
49 const PermissionDecidedCallback& user_callback, | |
50 const PermissionDecidedCallback& non_user_callback) { | |
51 requests_.emplace_back(type, user_callback, non_user_callback); | |
52 } | |
53 | |
54 bool PermissionInfoBarRequest::ShowInfobar( | |
55 InfoBarService* infobar_service, | |
56 Profile* profile, | |
57 const base::Closure& manager_callback) { | |
58 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
59 | |
60 // After pruning if there is nothing left then just ignore this request. | |
61 PruneAnsweredPermissions(profile); | |
62 if (requests_.size() == 0) | |
63 return false; | |
64 | |
65 std::string display_languages = | |
66 profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | |
67 if (requests_.size() == 1) { | |
68 CreateInfoBar( | |
69 infobar_service, | |
70 requests_[0], | |
71 display_languages, | |
72 base::Bind(&PermissionInfoBarRequest::OnPermissionSet, | |
73 weak_factory_.GetWeakPtr(), | |
74 manager_callback)); | |
75 return true; | |
76 } | |
77 | |
78 // TODO(lalitm) once multiple permissions is ready to land, this | |
79 // should be implemented properly. | |
80 NOTIMPLEMENTED(); | |
81 for (size_t i = 0; i < requests_.size(); ++i) | |
82 requests_[i].non_user_callback().Run(false, CONTENT_SETTING_DEFAULT); | |
83 return false; | |
84 } | |
85 | |
86 void PermissionInfoBarRequest::Cancel() { | |
87 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
88 DCHECK(infobar_); | |
89 | |
90 infobar_->RemoveSelf(); | |
91 } | |
92 | |
93 void PermissionInfoBarRequest::OnPermissionSet( | |
94 const base::Closure& manager_callback, | |
95 bool update_content_setting, | |
96 bool allowed) { | |
97 OnPermissionsSet(manager_callback, update_content_setting, | |
98 std::vector<ContentSetting>(1, | |
99 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK)); | |
100 } | |
101 | |
102 void PermissionInfoBarRequest::OnPermissionsSet( | |
103 const base::Closure& manager_callback, | |
104 bool update_content_setting, | |
105 const std::vector<ContentSetting>& allowed) { | |
106 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
107 | |
108 // Set the infobar to nullptr so there is no attempt to remove it later. | |
109 infobar_ = nullptr; | |
110 | |
111 for (size_t i = 0; i < requests_.size(); ++i) { | |
112 requests_[i].user_callback() | |
113 .Run(update_content_setting, | |
114 update_content_setting ? allowed[i] : CONTENT_SETTING_DEFAULT); | |
115 } | |
116 manager_callback.Run(); | |
117 } | |
118 | |
119 void PermissionInfoBarRequest::PruneAnsweredPermissions( | |
120 Profile* profile) { | |
121 for (auto it = requests_.begin(); it != requests_.end();) { | |
122 ContentSetting content_setting = profile-> | |
123 GetHostContentSettingsMap()->GetContentSettingAndMaybeUpdateLastUsage( | |
124 requesting_frame_, embedding_frame_, it->type(), std::string()); | |
125 if (content_setting == CONTENT_SETTING_ALLOW || | |
126 content_setting == CONTENT_SETTING_BLOCK) { | |
127 it->non_user_callback().Run(true, content_setting); | |
128 it = requests_.erase(it); | |
129 } else { | |
130 ++it; | |
131 } | |
132 } | |
133 } | |
134 | |
135 void PermissionInfoBarRequest::CreateInfoBar( | |
136 InfoBarService* infobar_service, | |
137 const PermissionRequest& request, | |
138 const std::string& display_languages, | |
139 const PermissionInfobarDelegate::PermissionSetCallback& callback) { | |
140 switch (request.type()) { | |
141 case CONTENT_SETTINGS_TYPE_GEOLOCATION: | |
142 infobar_ = GeolocationInfoBarDelegate::Create( | |
143 infobar_service, requesting_frame_, | |
144 display_languages, callback); | |
145 break; | |
146 #if defined(ENABLE_NOTIFICATIONS) | |
147 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS: | |
148 infobar_ = NotificationPermissionInfobarDelegate::Create( | |
149 infobar_service, requesting_frame_, | |
150 display_languages, callback); | |
151 break; | |
152 #endif // ENABLE_NOTIFICATIONS | |
153 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX: | |
154 infobar_ = MidiPermissionInfoBarDelegate::Create( | |
155 infobar_service, requesting_frame_, | |
156 display_languages, request.type(), callback); | |
157 break; | |
158 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | |
159 case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER: | |
160 infobar_ = ProtectedMediaIdentifierInfoBarDelegate::Create( | |
161 infobar_service, requesting_frame_, | |
162 display_languages, callback); | |
163 break; | |
164 #endif | |
165 case CONTENT_SETTINGS_TYPE_DURABLE_STORAGE: | |
166 infobar_ = DurableStoragePermissionInfoBarDelegate::Create( | |
167 infobar_service, requesting_frame_, | |
168 display_languages, request.type(), callback); | |
169 break; | |
170 default: | |
171 NOTREACHED(); | |
172 break; | |
173 } | |
174 } | |
OLD | NEW |