Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: chrome/browser/ui/webui/site_settings_helper.cc

Issue 2237823003: Site Settings Desktop: Implement USB devices section. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: One more fix Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/site_settings_helper.h" 5 #include "chrome/browser/ui/webui/site_settings_helper.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "chrome/browser/permissions/chooser_context_base.h"
9 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/usb/usb_chooser_context_factory.h"
10 #include "chrome/common/pref_names.h" 12 #include "chrome/common/pref_names.h"
11 #include "components/content_settings/core/browser/host_content_settings_map.h" 13 #include "components/content_settings/core/browser/host_content_settings_map.h"
12 #include "components/prefs/pref_service.h" 14 #include "components/prefs/pref_service.h"
13 15
14 namespace site_settings { 16 namespace site_settings {
15 17
16 const char kSetting[] = "setting"; 18 const char kSetting[] = "setting";
17 const char kOrigin[] = "origin"; 19 const char kOrigin[] = "origin";
18 const char kPolicyProviderId[] = "policy"; 20 const char kPolicyProviderId[] = "policy";
19 const char kSource[] = "source"; 21 const char kSource[] = "source";
20 const char kEmbeddingOrigin[] = "embeddingOrigin"; 22 const char kEmbeddingOrigin[] = "embeddingOrigin";
21 const char kPreferencesSource[] = "preference"; 23 const char kPreferencesSource[] = "preference";
24 const char kObject[] = "object";
25 const char kObjectName[] = "objectName";
22 26
23 struct ContentSettingsTypeNameEntry { 27 const char kGroupTypeUsb[] = "usb-devices";
24 ContentSettingsType type;
25 const char* name;
26 };
27 28
28 const ContentSettingsTypeNameEntry kContentSettingsTypeGroupNames[] = { 29 ChooserContextBase* GetUsbChooserContext(Profile* profile) {
29 {CONTENT_SETTINGS_TYPE_COOKIES, "cookies"}, 30 return reinterpret_cast<ChooserContextBase*>(
30 {CONTENT_SETTINGS_TYPE_IMAGES, "images"}, 31 UsbChooserContextFactory::GetForProfile(profile));
31 {CONTENT_SETTINGS_TYPE_JAVASCRIPT, "javascript"}, 32 }
32 {CONTENT_SETTINGS_TYPE_PLUGINS, "plugins"}, 33
33 {CONTENT_SETTINGS_TYPE_POPUPS, "popups"}, 34 namespace {
34 {CONTENT_SETTINGS_TYPE_GEOLOCATION, "location"}, 35
35 {CONTENT_SETTINGS_TYPE_NOTIFICATIONS, "notifications"}, 36 // Maps from the UI string to the object it represents (for sorting purposes).
36 {CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, "auto-select-certificate"}, 37 typedef std::multimap<std::string, const base::DictionaryValue*> SortedObjects;
37 {CONTENT_SETTINGS_TYPE_FULLSCREEN, "fullscreen"}, 38
38 {CONTENT_SETTINGS_TYPE_MOUSELOCK, "mouselock"}, 39 // Maps from a secondary URL to the set of objects it has permission to access.
39 {CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS, "register-protocol-handler"}, 40 typedef std::map<GURL, SortedObjects> OneOriginObjects;
40 {CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC, "media-stream-mic"}, 41
41 {CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA, "media-stream-camera"}, 42 // Maps from a primary URL/source pair to a OneOriginObjects. All the mappings
42 {CONTENT_SETTINGS_TYPE_PPAPI_BROKER, "ppapi-broker"}, 43 // in OneOriginObjects share the given primary URL and source.
43 {CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, "multiple-automatic-downloads"}, 44 typedef std::map<std::pair<GURL, std::string>, OneOriginObjects>
44 {CONTENT_SETTINGS_TYPE_MIDI_SYSEX, "midi-sysex"}, 45 AllOriginObjects;
45 {CONTENT_SETTINGS_TYPE_SSL_CERT_DECISIONS, "ssl-cert-decisions"}, 46
46 #if defined(OS_CHROMEOS) 47 } // namespace
47 {CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER, "protectedContent"},
48 #endif
49 {CONTENT_SETTINGS_TYPE_KEYGEN, "keygen"},
50 {CONTENT_SETTINGS_TYPE_BACKGROUND_SYNC, "background-sync"},
51 };
52 48
53 bool HasRegisteredGroupName(ContentSettingsType type) { 49 bool HasRegisteredGroupName(ContentSettingsType type) {
54 for (size_t i = 0; i < arraysize(kContentSettingsTypeGroupNames); ++i) { 50 for (size_t i = 0; i < arraysize(kContentSettingsTypeGroupNames); ++i) {
55 if (type == kContentSettingsTypeGroupNames[i].type) 51 if (type == kContentSettingsTypeGroupNames[i].type)
56 return true; 52 return true;
57 } 53 }
58 return false; 54 return false;
59 } 55 }
60 56
61 ContentSettingsType ContentSettingsTypeFromGroupName(const std::string& name) { 57 ContentSettingsType ContentSettingsTypeFromGroupName(const std::string& name) {
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 std::sort( 221 std::sort(
226 patterns.begin(), patterns.end(), std::greater<ContentSettingsPattern>()); 222 patterns.begin(), patterns.end(), std::greater<ContentSettingsPattern>());
227 223
228 for (const ContentSettingsPattern& pattern : patterns) { 224 for (const ContentSettingsPattern& pattern : patterns) {
229 exceptions->push_back(GetExceptionForPage(pattern, ContentSettingsPattern(), 225 exceptions->push_back(GetExceptionForPage(pattern, ContentSettingsPattern(),
230 CONTENT_SETTING_ALLOW, 226 CONTENT_SETTING_ALLOW,
231 kPolicyProviderId)); 227 kPolicyProviderId));
232 } 228 }
233 } 229 }
234 230
231 const ChooserTypeNameEntry* ChooserTypeFromGroupName(const std::string& name) {
232 for (const auto& chooser_type : kChooserTypeGroupNames) {
233 if (chooser_type.name == name)
234 return &chooser_type;
235 }
236 return nullptr;
237 }
238
239 // Create a DictionaryValue* that will act as a data source for a single row
240 // in a chooser permission exceptions table.
241 std::unique_ptr<base::DictionaryValue> GetChooserExceptionForPage(
242 const GURL& requesting_origin,
243 const GURL& embedding_origin,
244 const std::string& provider_name,
245 const std::string& name,
246 const base::DictionaryValue* object) {
247 std::unique_ptr<base::DictionaryValue> exception(new base::DictionaryValue());
248
249 std::string setting_string =
250 content_settings::ContentSettingToString(CONTENT_SETTING_DEFAULT);
251 DCHECK(!setting_string.empty());
252
253 exception->SetString(site_settings::kSetting, setting_string);
254 exception->SetString(site_settings::kOrigin, requesting_origin.spec());
255 exception->SetString(
256 site_settings::kEmbeddingOrigin, embedding_origin.spec());
257 exception->SetString(site_settings::kSource, provider_name);
258 if (object) {
259 exception->SetString(kObjectName, name);
260 exception->Set(kObject, object->CreateDeepCopy());
261 }
262 return exception;
263 }
264
265 void GetChooserExceptionsFromProfile(
266 Profile* profile,
267 bool incognito,
268 const ChooserTypeNameEntry& chooser_type,
269 base::ListValue* exceptions) {
270 if (incognito) {
271 if (profile->HasOffTheRecordProfile())
Bernhard Bauer 2016/08/15 14:14:54 Invert the condition and return early?
Finnur 2016/08/16 10:13:55 Done.
272 profile = profile->GetOffTheRecordProfile();
273 else
274 return;
275 }
276
277 ChooserContextBase* chooser_context = chooser_type.get_context(profile);
278 std::vector<std::unique_ptr<ChooserContextBase::Object>> objects =
279 chooser_context->GetAllGrantedObjects();
280 AllOriginObjects all_origin_objects;
281 for (const auto& object : objects) {
282 std::string name;
283 bool found = object->object.GetString(chooser_type.ui_name_key, &name);
284 DCHECK(found);
285 // It is safe for this structure to hold references into |objects| because
286 // they are both destroyed at the end of this function.
287 all_origin_objects[make_pair(object->requesting_origin,
288 object->source)][object->embedding_origin]
289 .insert(make_pair(name, &object->object));
290 }
291
292 // Keep the exceptions sorted by provider so they will be displayed in
293 // precedence order.
294 std::vector<std::unique_ptr<base::DictionaryValue>>
295 all_provider_exceptions[HostContentSettingsMap::NUM_PROVIDER_TYPES];
296
297 for (const auto& all_origin_objects_entry : all_origin_objects) {
298 const GURL& requesting_origin = all_origin_objects_entry.first.first;
299 const std::string& source = all_origin_objects_entry.first.second;
300 const OneOriginObjects& one_origin_objects =
301 all_origin_objects_entry.second;
302
303 auto& this_provider_exceptions = all_provider_exceptions
304 [HostContentSettingsMap::GetProviderTypeFromSource(source)];
305
306 // Add entries for any non-embedded origins.
307 bool has_embedded_entries = false;
308 for (const auto& one_origin_objects_entry : one_origin_objects) {
309 const GURL& embedding_origin = one_origin_objects_entry.first;
310 const SortedObjects& sorted_objects = one_origin_objects_entry.second;
311
312 // Skip the embedded settings which will be added below.
313 if (requesting_origin != embedding_origin) {
314 has_embedded_entries = true;
315 continue;
316 }
317
318 for (const auto& sorted_objects_entry : sorted_objects) {
319 this_provider_exceptions.push_back(GetChooserExceptionForPage(
320 requesting_origin, embedding_origin, source,
321 sorted_objects_entry.first, sorted_objects_entry.second));
322 }
323 }
324
325 if (has_embedded_entries) {
326 // Add a "parent" entry that simply acts as a heading for all entries
327 // where |requesting_origin| has been embedded.
328 this_provider_exceptions.push_back(
329 GetChooserExceptionForPage(requesting_origin, requesting_origin,
330 source, std::string(), nullptr));
331
332 // Add the "children" for any embedded settings.
333 for (const auto& one_origin_objects_entry : one_origin_objects) {
334 const GURL& embedding_origin = one_origin_objects_entry.first;
335 const SortedObjects& sorted_objects = one_origin_objects_entry.second;
336
337 // Skip the non-embedded setting which we already added above.
338 if (requesting_origin == embedding_origin)
339 continue;
340
341 for (const auto& sorted_objects_entry : sorted_objects) {
342 this_provider_exceptions.push_back(GetChooserExceptionForPage(
343 requesting_origin, embedding_origin, source,
344 sorted_objects_entry.first, sorted_objects_entry.second));
345 }
346 }
347 }
348 }
349
350 for (auto& one_provider_exceptions : all_provider_exceptions) {
351 for (auto& exception : one_provider_exceptions)
352 exceptions->Append(std::move(exception));
353 }
354 }
355
235 } // namespace site_settings 356 } // namespace site_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698