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"
9 #include "base/memory/scoped_ptr.h"
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
);
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
;
51 ContentSetting
PepperFlashContentSettingsUtils::FlashPermissionToContentSetting(
52 PP_Flash_BrowserOperations_Permission 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
;
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())
84 ContentSetting setting
= FlashPermissionToContentSetting(iter
->permission
);
86 media_exceptions
->push_back(MediaException(pattern
, setting
, setting
));
91 void PepperFlashContentSettingsUtils::SortMediaExceptions(
92 MediaExceptions
* media_exceptions
) {
93 std::sort(media_exceptions
->begin(), media_exceptions
->end(),
94 MediaExceptionSortFunc
);
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
)) {
126 } else if (compare_result
> 0) {
127 if (!AreSettingsEqualOrIgnored(*iter_2
, default_exception_1
,
128 ignore_audio_setting
,
129 ignore_video_setting
)) {
134 if (!AreSettingsEqualOrIgnored(*iter_1
, *iter_2
, ignore_audio_setting
,
135 ignore_video_setting
)) {
143 while (iter_1
!= exceptions_1
.end()) {
144 if (!AreSettingsEqualOrIgnored(*iter_1
, default_exception_2
,
145 ignore_audio_setting
,
146 ignore_video_setting
)) {
151 while (iter_2
!= exceptions_2
.end()) {
152 if (!AreSettingsEqualOrIgnored(*iter_2
, default_exception_1
,
153 ignore_audio_setting
,
154 ignore_video_setting
)) {
162 } // namespace options