- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / options / pepper_flash_content_settings_utils.cc
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.
4
5 #include "chrome/browser/ui/webui/options/pepper_flash_content_settings_utils.h"
6
7 #include <algorithm>
8
9 #include "base/memory/scoped_ptr.h"
10
11 namespace options {
12
13 namespace {
14
15 int CompareMediaException(const MediaException& i, const MediaException& j) {
16   return i.pattern.Compare(j.pattern);
17 }
18
19 bool MediaExceptionSortFunc(const MediaException& i, const MediaException& j) {
20   return CompareMediaException(i, j) < 0;
21 }
22
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);
29 }
30
31 }  // namespace
32
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) {
39 }
40
41 MediaException::~MediaException() {
42 }
43
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;
48 }
49
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.
63   }
64   return CONTENT_SETTING_DEFAULT;
65 }
66
67 // static
68 void PepperFlashContentSettingsUtils::FlashSiteSettingsToMediaExceptions(
69     const ppapi::FlashSiteSettings& site_settings,
70     MediaExceptions* media_exceptions) {
71   media_exceptions->clear();
72
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);
79
80     ContentSettingsPattern pattern = builder->Build();
81     if (!pattern.IsValid())
82       continue;
83
84     ContentSetting setting = FlashPermissionToContentSetting(iter->permission);
85
86     media_exceptions->push_back(MediaException(pattern, setting, setting));
87   }
88 }
89
90 // static
91 void PepperFlashContentSettingsUtils::SortMediaExceptions(
92     MediaExceptions* media_exceptions) {
93   std::sort(media_exceptions->begin(), media_exceptions->end(),
94             MediaExceptionSortFunc);
95 }
96
97 // static
98 bool PepperFlashContentSettingsUtils::AreMediaExceptionsEqual(
99     ContentSetting default_setting_1,
100     const MediaExceptions& exceptions_1,
101     ContentSetting default_setting_2,
102     const MediaExceptions& exceptions_2,
103     bool ignore_audio_setting,
104     bool ignore_video_setting) {
105   MediaExceptions::const_iterator iter_1 = exceptions_1.begin();
106   MediaExceptions::const_iterator iter_2 = exceptions_2.begin();
107
108   MediaException default_exception_1(ContentSettingsPattern(),
109                                      default_setting_1,
110                                      default_setting_1);
111   MediaException default_exception_2(ContentSettingsPattern(),
112                                      default_setting_2,
113                                      default_setting_2);
114
115   while (iter_1 != exceptions_1.end() && iter_2 != exceptions_2.end()) {
116     int compare_result = CompareMediaException(*iter_1, *iter_2);
117     if (compare_result < 0) {
118       if (!AreSettingsEqualOrIgnored(*iter_1, default_exception_2,
119                                      ignore_audio_setting,
120                                      ignore_video_setting)) {
121         return false;
122       }
123       ++iter_1;
124     } else if (compare_result > 0) {
125       if (!AreSettingsEqualOrIgnored(*iter_2, default_exception_1,
126                                      ignore_audio_setting,
127                                      ignore_video_setting)) {
128         return false;
129       }
130       ++iter_2;
131     } else {
132       if (!AreSettingsEqualOrIgnored(*iter_1, *iter_2, ignore_audio_setting,
133                                      ignore_video_setting)) {
134         return false;
135       }
136       ++iter_1;
137       ++iter_2;
138     }
139   }
140
141   while (iter_1 != exceptions_1.end()) {
142     if (!AreSettingsEqualOrIgnored(*iter_1, default_exception_2,
143                                    ignore_audio_setting,
144                                    ignore_video_setting)) {
145       return false;
146     }
147     ++iter_1;
148   }
149   while (iter_2 != exceptions_2.end()) {
150     if (!AreSettingsEqualOrIgnored(*iter_2, default_exception_1,
151                                    ignore_audio_setting,
152                                    ignore_video_setting)) {
153       return false;
154     }
155     ++iter_2;
156   }
157   return true;
158 }
159
160 }  // namespace options