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;
25 MediaException::MediaException(const ContentSettingsPattern
& in_pattern
,
26 ContentSetting in_setting
)
27 : pattern(in_pattern
),
31 MediaException::~MediaException() {
34 bool MediaException::operator==(const MediaException
& other
) const {
35 return pattern
== other
.pattern
&& setting
== other
.setting
;
39 ContentSetting
PepperFlashContentSettingsUtils::FlashPermissionToContentSetting(
40 PP_Flash_BrowserOperations_Permission permission
) {
42 case PP_FLASH_BROWSEROPERATIONS_PERMISSION_DEFAULT
:
43 return CONTENT_SETTING_DEFAULT
;
44 case PP_FLASH_BROWSEROPERATIONS_PERMISSION_ALLOW
:
45 return CONTENT_SETTING_ALLOW
;
46 case PP_FLASH_BROWSEROPERATIONS_PERMISSION_BLOCK
:
47 return CONTENT_SETTING_BLOCK
;
48 case PP_FLASH_BROWSEROPERATIONS_PERMISSION_ASK
:
49 return CONTENT_SETTING_ASK
;
50 // No default so the compiler will warn us if a new type is added.
52 return CONTENT_SETTING_DEFAULT
;
56 void PepperFlashContentSettingsUtils::FlashSiteSettingsToMediaExceptions(
57 const ppapi::FlashSiteSettings
& site_settings
,
58 MediaExceptions
* media_exceptions
) {
59 media_exceptions
->clear();
61 scoped_ptr
<ContentSettingsPattern::BuilderInterface
> builder(
62 ContentSettingsPattern::CreateBuilder(false));
63 builder
->WithSchemeWildcard()->WithPortWildcard();
64 for (ppapi::FlashSiteSettings::const_iterator iter
= site_settings
.begin();
65 iter
!= site_settings
.end(); ++iter
) {
66 builder
->WithHost(iter
->site
);
68 ContentSettingsPattern pattern
= builder
->Build();
69 if (!pattern
.IsValid())
72 ContentSetting setting
= FlashPermissionToContentSetting(iter
->permission
);
74 media_exceptions
->push_back(MediaException(pattern
, setting
));
79 void PepperFlashContentSettingsUtils::SortMediaExceptions(
80 MediaExceptions
* media_exceptions
) {
81 std::sort(media_exceptions
->begin(), media_exceptions
->end(),
82 MediaExceptionSortFunc
);
86 bool PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
87 ContentSetting default_setting_1
,
88 const MediaExceptions
& exceptions_1
,
89 ContentSetting default_setting_2
,
90 const MediaExceptions
& exceptions_2
) {
91 MediaExceptions::const_iterator iter_1
= exceptions_1
.begin();
92 MediaExceptions::const_iterator iter_2
= exceptions_2
.begin();
94 MediaException
default_exception_1(ContentSettingsPattern(),
96 MediaException
default_exception_2(ContentSettingsPattern(),
99 while (iter_1
!= exceptions_1
.end() && iter_2
!= exceptions_2
.end()) {
100 int compare_result
= CompareMediaException(*iter_1
, *iter_2
);
101 if (compare_result
< 0) {
102 if (iter_1
->setting
!= default_exception_2
.setting
)
105 } else if (compare_result
> 0) {
106 if (iter_2
->setting
!= default_exception_1
.setting
) {
111 if (iter_1
->setting
!= iter_2
->setting
)
118 while (iter_1
!= exceptions_1
.end()) {
119 if (iter_1
->setting
!= default_exception_2
.setting
)
123 while (iter_2
!= exceptions_2
.end()) {
124 if (iter_2
->setting
!= default_exception_1
.setting
)
131 } // namespace options