Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / pepper_flash_content_settings_utils.cc
blob7f05fcdeb0aab5a68d54653e705f9c53daf5fb63
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/ui/webui/options/pepper_flash_content_settings_utils.h"
7 #include <algorithm>
9 #include "base/memory/scoped_ptr.h"
11 namespace options {
13 namespace {
15 int CompareMediaException(const MediaException& i, const MediaException& j) {
16 return i.pattern.Compare(j.pattern);
19 bool MediaExceptionSortFunc(const MediaException& i, const MediaException& j) {
20 return CompareMediaException(i, j) < 0;
23 bool AreSettingsEqualOrIgnored(const MediaException& i,
24 const MediaException& j,
25 bool ignore_audio_setting,
26 bool ignore_video_setting) {
27 return (ignore_audio_setting || i.audio_setting == j.audio_setting) &&
28 (ignore_video_setting || i.video_setting == j.video_setting);
31 } // namespace
33 MediaException::MediaException(const ContentSettingsPattern& in_pattern,
34 ContentSetting in_audio_setting,
35 ContentSetting in_video_setting)
36 : pattern(in_pattern),
37 audio_setting(in_audio_setting),
38 video_setting(in_video_setting) {
41 MediaException::~MediaException() {
44 bool MediaException::operator==(const MediaException& other) const {
45 return pattern == other.pattern &&
46 audio_setting == other.audio_setting &&
47 video_setting == other.video_setting;
50 // static
51 ContentSetting PepperFlashContentSettingsUtils::FlashPermissionToContentSetting(
52 PP_Flash_BrowserOperations_Permission permission) {
53 switch (permission) {
54 case PP_FLASH_BROWSEROPERATIONS_PERMISSION_DEFAULT:
55 return CONTENT_SETTING_DEFAULT;
56 case PP_FLASH_BROWSEROPERATIONS_PERMISSION_ALLOW:
57 return CONTENT_SETTING_ALLOW;
58 case PP_FLASH_BROWSEROPERATIONS_PERMISSION_BLOCK:
59 return CONTENT_SETTING_BLOCK;
60 case PP_FLASH_BROWSEROPERATIONS_PERMISSION_ASK:
61 return CONTENT_SETTING_ASK;
62 // No default so the compiler will warn us if a new type is added.
64 return CONTENT_SETTING_DEFAULT;
67 // static
68 void PepperFlashContentSettingsUtils::FlashSiteSettingsToMediaExceptions(
69 const ppapi::FlashSiteSettings& site_settings,
70 MediaExceptions* media_exceptions) {
71 media_exceptions->clear();
73 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder(
74 ContentSettingsPattern::CreateBuilder(false));
75 builder->WithSchemeWildcard()->WithPortWildcard();
76 for (ppapi::FlashSiteSettings::const_iterator iter = site_settings.begin();
77 iter != site_settings.end(); ++iter) {
78 builder->WithHost(iter->site);
80 ContentSettingsPattern pattern = builder->Build();
81 if (!pattern.IsValid())
82 continue;
84 ContentSetting setting = FlashPermissionToContentSetting(iter->permission);
86 media_exceptions->push_back(MediaException(pattern, setting, setting));
90 // static
91 void PepperFlashContentSettingsUtils::SortMediaExceptions(
92 MediaExceptions* media_exceptions) {
93 std::sort(media_exceptions->begin(), media_exceptions->end(),
94 MediaExceptionSortFunc);
97 // static
98 bool PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
99 ContentSetting default_audio_setting_1,
100 ContentSetting default_video_setting_1,
101 const MediaExceptions& exceptions_1,
102 ContentSetting default_audio_setting_2,
103 ContentSetting default_video_setting_2,
104 const MediaExceptions& exceptions_2,
105 bool ignore_audio_setting,
106 bool ignore_video_setting) {
107 MediaExceptions::const_iterator iter_1 = exceptions_1.begin();
108 MediaExceptions::const_iterator iter_2 = exceptions_2.begin();
110 MediaException default_exception_1(ContentSettingsPattern(),
111 default_audio_setting_1,
112 default_video_setting_1);
113 MediaException default_exception_2(ContentSettingsPattern(),
114 default_audio_setting_2,
115 default_video_setting_2);
117 while (iter_1 != exceptions_1.end() && iter_2 != exceptions_2.end()) {
118 int compare_result = CompareMediaException(*iter_1, *iter_2);
119 if (compare_result < 0) {
120 if (!AreSettingsEqualOrIgnored(*iter_1, default_exception_2,
121 ignore_audio_setting,
122 ignore_video_setting)) {
123 return false;
125 ++iter_1;
126 } else if (compare_result > 0) {
127 if (!AreSettingsEqualOrIgnored(*iter_2, default_exception_1,
128 ignore_audio_setting,
129 ignore_video_setting)) {
130 return false;
132 ++iter_2;
133 } else {
134 if (!AreSettingsEqualOrIgnored(*iter_1, *iter_2, ignore_audio_setting,
135 ignore_video_setting)) {
136 return false;
138 ++iter_1;
139 ++iter_2;
143 while (iter_1 != exceptions_1.end()) {
144 if (!AreSettingsEqualOrIgnored(*iter_1, default_exception_2,
145 ignore_audio_setting,
146 ignore_video_setting)) {
147 return false;
149 ++iter_1;
151 while (iter_2 != exceptions_2.end()) {
152 if (!AreSettingsEqualOrIgnored(*iter_2, default_exception_1,
153 ignore_audio_setting,
154 ignore_video_setting)) {
155 return false;
157 ++iter_2;
159 return true;
162 } // namespace options