Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / pepper_flash_content_settings_utils.cc
blob7f18b9ed93d1a302d0a6febcadda7248880bae41
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 } // namespace
25 MediaException::MediaException(const ContentSettingsPattern& in_pattern,
26 ContentSetting in_setting)
27 : pattern(in_pattern),
28 setting(in_setting) {
31 MediaException::~MediaException() {
34 bool MediaException::operator==(const MediaException& other) const {
35 return pattern == other.pattern && setting == other.setting;
38 // static
39 ContentSetting PepperFlashContentSettingsUtils::FlashPermissionToContentSetting(
40 PP_Flash_BrowserOperations_Permission permission) {
41 switch (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;
55 // static
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())
70 continue;
72 ContentSetting setting = FlashPermissionToContentSetting(iter->permission);
74 media_exceptions->push_back(MediaException(pattern, setting));
78 // static
79 void PepperFlashContentSettingsUtils::SortMediaExceptions(
80 MediaExceptions* media_exceptions) {
81 std::sort(media_exceptions->begin(), media_exceptions->end(),
82 MediaExceptionSortFunc);
85 // static
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(),
95 default_setting_1);
96 MediaException default_exception_2(ContentSettingsPattern(),
97 default_setting_2);
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)
103 return false;
104 ++iter_1;
105 } else if (compare_result > 0) {
106 if (iter_2->setting != default_exception_1.setting) {
107 return false;
109 ++iter_2;
110 } else {
111 if (iter_1->setting != iter_2->setting)
112 return false;
113 ++iter_1;
114 ++iter_2;
118 while (iter_1 != exceptions_1.end()) {
119 if (iter_1->setting != default_exception_2.setting)
120 return false;
121 ++iter_1;
123 while (iter_2 != exceptions_2.end()) {
124 if (iter_2->setting != default_exception_1.setting)
125 return false;
126 ++iter_2;
128 return true;
131 } // namespace options