Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: chrome/browser/ui/webui/options2/content_settings_handler2.cc

Issue 10698140: Remove "2" suffixes from options2 code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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/options2/content_settings_handler2.h"
6
7 #include <vector>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/command_line.h"
12 #include "base/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/content_settings/content_settings_details.h"
16 #include "chrome/browser/content_settings/content_settings_utils.h"
17 #include "chrome/browser/content_settings/host_content_settings_map.h"
18 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
19 #include "chrome/browser/extensions/extension_service.h"
20 #include "chrome/browser/extensions/extension_special_storage_policy.h"
21 #include "chrome/browser/intents/web_intents_util.h"
22 #include "chrome/browser/notifications/desktop_notification_service.h"
23 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
24 #include "chrome/browser/prefs/pref_service.h"
25 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/browser/ui/browser_list.h"
27 #include "chrome/common/chrome_notification_types.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/content_settings.h"
30 #include "chrome/common/content_settings_pattern.h"
31 #include "chrome/common/extensions/permissions/api_permission.h"
32 #include "chrome/common/extensions/extension_set.h"
33 #include "chrome/common/pref_names.h"
34 #include "chrome/common/url_constants.h"
35 #include "content/public/browser/notification_service.h"
36 #include "content/public/browser/notification_source.h"
37 #include "content/public/browser/notification_types.h"
38 #include "content/public/browser/user_metrics.h"
39 #include "content/public/browser/web_ui.h"
40 #include "content/public/common/content_switches.h"
41 #include "grit/generated_resources.h"
42 #include "grit/locale_settings.h"
43 #include "net/base/net_util.h"
44 #include "ui/base/l10n/l10n_util.h"
45
46 #if defined(OS_CHROMEOS)
47 #include "chrome/browser/chromeos/login/user_manager.h"
48 #endif
49
50 using content::UserMetricsAction;
51 using extensions::APIPermission;
52
53 namespace {
54
55 enum ExContentSettingsTypeEnum {
56 EX_CONTENT_SETTINGS_TYPE_PEPPER_FLASH_CAMERAMIC =
57 CONTENT_SETTINGS_NUM_TYPES,
58 EX_CONTENT_SETTINGS_NUM_TYPES
59 };
60
61 typedef std::map<ContentSettingsPattern, ContentSetting> OnePatternSettings;
62 typedef std::map<ContentSettingsPattern, OnePatternSettings>
63 AllPatternsSettings;
64
65 // The AppFilter is used in AddExceptionsGrantedByHostedApps() to choose
66 // extensions which should have their extent displayed.
67 typedef bool (*AppFilter)(const extensions::Extension& app, Profile* profile);
68
69 const char* kDisplayPattern = "displayPattern";
70 const char* kSetting = "setting";
71 const char* kOrigin = "origin";
72 const char* kSource = "source";
73 const char* kAppName = "appName";
74 const char* kAppId = "appId";
75 const char* kEmbeddingOrigin = "embeddingOrigin";
76 const char* kDefaultProviderID = "default";
77 const char* kPreferencesSource = "preferences";
78
79 std::string ContentSettingToString(ContentSetting setting) {
80 switch (setting) {
81 case CONTENT_SETTING_ALLOW:
82 return "allow";
83 case CONTENT_SETTING_ASK:
84 return "ask";
85 case CONTENT_SETTING_BLOCK:
86 return "block";
87 case CONTENT_SETTING_SESSION_ONLY:
88 return "session";
89 case CONTENT_SETTING_DEFAULT:
90 return "default";
91 case CONTENT_SETTING_NUM_SETTINGS:
92 NOTREACHED();
93 }
94
95 return "";
96 }
97
98 ContentSetting ContentSettingFromString(const std::string& name) {
99 if (name == "allow")
100 return CONTENT_SETTING_ALLOW;
101 if (name == "ask")
102 return CONTENT_SETTING_ASK;
103 if (name == "block")
104 return CONTENT_SETTING_BLOCK;
105 if (name == "session")
106 return CONTENT_SETTING_SESSION_ONLY;
107
108 NOTREACHED() << name << " is not a recognized content setting.";
109 return CONTENT_SETTING_DEFAULT;
110 }
111
112 std::string GeolocationExceptionToString(
113 const ContentSettingsPattern& origin,
114 const ContentSettingsPattern& embedding_origin) {
115 if (origin == embedding_origin)
116 return origin.ToString();
117
118 // TODO(estade): the page needs to use CSS to indent the string.
119 std::string indent(" ");
120
121 if (embedding_origin == ContentSettingsPattern::Wildcard()) {
122 // NOTE: As long as the user cannot add/edit entries from the exceptions
123 // dialog, it's impossible to actually have a non-default setting for some
124 // origin "embedded on any other site", so this row will never appear. If
125 // we add the ability to add/edit exceptions, we'll need to decide when to
126 // display this and how "removing" it will function.
127 return indent +
128 l10n_util::GetStringUTF8(IDS_EXCEPTIONS_GEOLOCATION_EMBEDDED_ANY_OTHER);
129 }
130
131 return indent + l10n_util::GetStringFUTF8(
132 IDS_EXCEPTIONS_GEOLOCATION_EMBEDDED_ON_HOST,
133 UTF8ToUTF16(embedding_origin.ToString()));
134 }
135
136 // Create a DictionaryValue* that will act as a data source for a single row
137 // in a HostContentSettingsMap-controlled exceptions table (e.g., cookies).
138 // Ownership of the pointer is passed to the caller.
139 DictionaryValue* GetExceptionForPage(
140 const ContentSettingsPattern& pattern,
141 const ContentSetting& setting,
142 const std::string& provider_name) {
143 DictionaryValue* exception = new DictionaryValue();
144 exception->SetString(kDisplayPattern, pattern.ToString());
145 exception->SetString(kSetting, ContentSettingToString(setting));
146 exception->SetString(kSource, provider_name);
147 return exception;
148 }
149
150 // Create a DictionaryValue* that will act as a data source for a single row
151 // in the Geolocation exceptions table. Ownership of the pointer is passed to
152 // the caller.
153 DictionaryValue* GetGeolocationExceptionForPage(
154 const ContentSettingsPattern& origin,
155 const ContentSettingsPattern& embedding_origin,
156 ContentSetting setting) {
157 DictionaryValue* exception = new DictionaryValue();
158 exception->SetString(kDisplayPattern,
159 GeolocationExceptionToString(origin, embedding_origin));
160 exception->SetString(kSetting, ContentSettingToString(setting));
161 exception->SetString(kOrigin, origin.ToString());
162 exception->SetString(kEmbeddingOrigin, embedding_origin.ToString());
163 return exception;
164 }
165
166 // Create a DictionaryValue* that will act as a data source for a single row
167 // in the desktop notifications exceptions table. Ownership of the pointer is
168 // passed to the caller.
169 DictionaryValue* GetNotificationExceptionForPage(
170 const ContentSettingsPattern& pattern,
171 ContentSetting setting,
172 const std::string& provider_name) {
173 DictionaryValue* exception = new DictionaryValue();
174 exception->SetString(kDisplayPattern, pattern.ToString());
175 exception->SetString(kSetting, ContentSettingToString(setting));
176 exception->SetString(kOrigin, pattern.ToString());
177 exception->SetString(kSource, provider_name);
178 return exception;
179 }
180
181 // Returns true whenever the hosted |app|'s extent enjoys protected storage
182 // under the current |profile|.
183 // Must have the AppFilter signature.
184 bool HasProtectedStorage(const extensions::Extension& app, Profile* profile) {
185 ExtensionSpecialStoragePolicy* policy =
186 profile->GetExtensionSpecialStoragePolicy();
187 return policy->NeedsProtection(&app);
188 }
189
190 // Returns true whenever the |extension| is hosted and has |permission|.
191 // Must have the AppFilter signature.
192 template <APIPermission::ID permission>
193 bool HostedAppHasPermission(
194 const extensions::Extension& extension, Profile* /*profile*/) {
195 return extension.is_hosted_app() && extension.HasAPIPermission(permission);
196 }
197
198 // Add an "Allow"-entry to the list of |exceptions| for a |url_pattern| from
199 // the web extent of a hosted |app|.
200 void AddExceptionForHostedApp(const std::string& url_pattern,
201 const extensions::Extension& app, ListValue* exceptions) {
202 DictionaryValue* exception = new DictionaryValue();
203 exception->SetString(kDisplayPattern, url_pattern);
204 exception->SetString(kSetting, ContentSettingToString(CONTENT_SETTING_ALLOW));
205 exception->SetString(kOrigin, url_pattern);
206 exception->SetString(kEmbeddingOrigin, url_pattern);
207 exception->SetString(kSource, "HostedApp");
208 exception->SetString(kAppName, app.name());
209 exception->SetString(kAppId, app.id());
210 exceptions->Append(exception);
211 }
212
213 // Asks the |profile| for hosted apps which have the |permission| set, and
214 // adds their web extent and launch URL to the |exceptions| list.
215 void AddExceptionsGrantedByHostedApps(
216 Profile* profile, AppFilter app_filter, ListValue* exceptions) {
217 const ExtensionService* extension_service = profile->GetExtensionService();
218 // After ExtensionSystem::Init has been called at the browser's start,
219 // GetExtensionService() should not return NULL, so this is safe:
220 const ExtensionSet* extensions = extension_service->extensions();
221
222 for (ExtensionSet::const_iterator extension = extensions->begin();
223 extension != extensions->end(); ++extension) {
224 if (!app_filter(**extension, profile)) continue;
225
226 URLPatternSet web_extent = (*extension)->web_extent();
227 // Add patterns from web extent.
228 for (URLPatternSet::const_iterator pattern = web_extent.begin();
229 pattern != web_extent.end(); ++pattern) {
230 std::string url_pattern = pattern->GetAsString();
231 AddExceptionForHostedApp(url_pattern, **extension, exceptions);
232 }
233 // Retrieve the launch URL.
234 std::string launch_url_string = (*extension)->launch_web_url();
235 GURL launch_url(launch_url_string);
236 // Skip adding the launch URL if it is part of the web extent.
237 if (web_extent.MatchesURL(launch_url)) continue;
238 AddExceptionForHostedApp(launch_url_string, **extension, exceptions);
239 }
240 }
241
242 ContentSetting FlashPermissionToContentSetting(
243 PP_Flash_BrowserOperations_Permission permission) {
244 switch (permission) {
245 case PP_FLASH_BROWSEROPERATIONS_PERMISSION_DEFAULT:
246 return CONTENT_SETTING_DEFAULT;
247 case PP_FLASH_BROWSEROPERATIONS_PERMISSION_ALLOW:
248 return CONTENT_SETTING_ALLOW;
249 case PP_FLASH_BROWSEROPERATIONS_PERMISSION_BLOCK:
250 return CONTENT_SETTING_BLOCK;
251 case PP_FLASH_BROWSEROPERATIONS_PERMISSION_ASK:
252 return CONTENT_SETTING_ASK;
253 default:
254 NOTREACHED();
255 return CONTENT_SETTING_DEFAULT;
256 }
257 }
258
259 PP_Flash_BrowserOperations_Permission FlashPermissionFromContentSetting(
260 ContentSetting setting) {
261 switch (setting) {
262 case CONTENT_SETTING_DEFAULT:
263 return PP_FLASH_BROWSEROPERATIONS_PERMISSION_DEFAULT;
264 case CONTENT_SETTING_ALLOW:
265 return PP_FLASH_BROWSEROPERATIONS_PERMISSION_ALLOW;
266 case CONTENT_SETTING_BLOCK:
267 return PP_FLASH_BROWSEROPERATIONS_PERMISSION_BLOCK;
268 case CONTENT_SETTING_ASK:
269 return PP_FLASH_BROWSEROPERATIONS_PERMISSION_ASK;
270 default:
271 NOTREACHED();
272 return PP_FLASH_BROWSEROPERATIONS_PERMISSION_DEFAULT;
273 }
274 }
275
276 std::string CanonicalizeHost(const std::string& host) {
277 url_canon::CanonHostInfo info;
278 return net::CanonicalizeHost(host, &info);
279 }
280
281 bool IsValidHost(const std::string& host) {
282 std::string canonicalized_host = CanonicalizeHost(host);
283 return !canonicalized_host.empty();
284 }
285
286 } // namespace
287
288 namespace options2 {
289
290 class ContentSettingsHandler::ExContentSettingsType {
291 public:
292 explicit ExContentSettingsType(int value) : value_(value) {
293 DCHECK(value_ < EX_CONTENT_SETTINGS_NUM_TYPES);
294 }
295 explicit ExContentSettingsType(ContentSettingsType type) : value_(type) {}
296 explicit ExContentSettingsType(ExContentSettingsTypeEnum type)
297 : value_(type) {}
298
299 bool IsExtraContentSettingsType() const {
300 return value_ >= CONTENT_SETTINGS_NUM_TYPES;
301 }
302
303 operator int() const { return value_; }
304
305 ContentSettingsType ToContentSettingsType() const {
306 DCHECK(value_ < CONTENT_SETTINGS_NUM_TYPES);
307 return static_cast<ContentSettingsType>(value_);
308 }
309
310 private:
311 int value_;
312 };
313
314 ContentSettingsHandler::CachedPepperFlashSettings::CachedPepperFlashSettings()
315 : default_permission(PP_FLASH_BROWSEROPERATIONS_PERMISSION_BLOCK),
316 initialized(false),
317 last_refresh_request_id(0) {
318 }
319
320 ContentSettingsHandler::CachedPepperFlashSettings::~CachedPepperFlashSettings() {
321 }
322
323 struct ContentSettingsHandler::ExContentSettingsTypeNameEntry {
324 int type;
325 const char* name;
326 };
327
328 const ContentSettingsHandler::ExContentSettingsTypeNameEntry
329 ContentSettingsHandler::kExContentSettingsTypeGroupNames[] = {
330 {CONTENT_SETTINGS_TYPE_COOKIES, "cookies"},
331 {CONTENT_SETTINGS_TYPE_IMAGES, "images"},
332 {CONTENT_SETTINGS_TYPE_JAVASCRIPT, "javascript"},
333 {CONTENT_SETTINGS_TYPE_PLUGINS, "plugins"},
334 {CONTENT_SETTINGS_TYPE_POPUPS, "popups"},
335 {CONTENT_SETTINGS_TYPE_GEOLOCATION, "location"},
336 {CONTENT_SETTINGS_TYPE_NOTIFICATIONS, "notifications"},
337 {CONTENT_SETTINGS_TYPE_INTENTS, "intents"},
338 {CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, "auto-select-certificate"},
339 {CONTENT_SETTINGS_TYPE_FULLSCREEN, "fullscreen"},
340 {CONTENT_SETTINGS_TYPE_MOUSELOCK, "mouselock"},
341 {CONTENT_SETTINGS_TYPE_MIXEDSCRIPT, "mixed-script"},
342 {EX_CONTENT_SETTINGS_TYPE_PEPPER_FLASH_CAMERAMIC, "pepper-flash-cameramic"},
343 {CONTENT_SETTINGS_TYPE_MEDIASTREAM, "media-stream"},
344 };
345
346 ContentSettingsHandler::ContentSettingsHandler() {
347 }
348
349 ContentSettingsHandler::~ContentSettingsHandler() {
350 }
351
352 void ContentSettingsHandler::GetLocalizedValues(
353 DictionaryValue* localized_strings) {
354 DCHECK(localized_strings);
355
356 static OptionsStringResource resources[] = {
357 { "allowException", IDS_EXCEPTIONS_ALLOW_BUTTON },
358 { "blockException", IDS_EXCEPTIONS_BLOCK_BUTTON },
359 { "sessionException", IDS_EXCEPTIONS_SESSION_ONLY_BUTTON },
360 { "askException", IDS_EXCEPTIONS_ASK_BUTTON },
361 { "otr_exceptions_explanation", IDS_EXCEPTIONS_OTR_LABEL },
362 { "addNewExceptionInstructions", IDS_EXCEPTIONS_ADD_NEW_INSTRUCTIONS },
363 { "manage_exceptions", IDS_EXCEPTIONS_MANAGE },
364 { "manage_handlers", IDS_HANDLERS_MANAGE },
365 { "exceptionPatternHeader", IDS_EXCEPTIONS_PATTERN_HEADER },
366 { "exceptionBehaviorHeader", IDS_EXCEPTIONS_ACTION_HEADER },
367 // Cookies filter.
368 { "cookies_tab_label", IDS_COOKIES_TAB_LABEL },
369 { "cookies_header", IDS_COOKIES_HEADER },
370 { "cookies_allow", IDS_COOKIES_ALLOW_RADIO },
371 { "cookies_block", IDS_COOKIES_BLOCK_RADIO },
372 { "cookies_session_only", IDS_COOKIES_SESSION_ONLY_RADIO },
373 { "cookies_block_3rd_party", IDS_COOKIES_BLOCK_3RDPARTY_CHKBOX },
374 { "cookies_clear_when_close", IDS_COOKIES_CLEAR_WHEN_CLOSE_CHKBOX },
375 { "cookies_lso_clear_when_close", IDS_COOKIES_LSO_CLEAR_WHEN_CLOSE_CHKBOX },
376 { "cookies_show_cookies", IDS_COOKIES_SHOW_COOKIES_BUTTON },
377 { "flash_storage_settings", IDS_FLASH_STORAGE_SETTINGS },
378 { "flash_storage_url", IDS_FLASH_STORAGE_URL },
379 // Image filter.
380 { "images_tab_label", IDS_IMAGES_TAB_LABEL },
381 { "images_header", IDS_IMAGES_HEADER },
382 { "images_allow", IDS_IMAGES_LOAD_RADIO },
383 { "images_block", IDS_IMAGES_NOLOAD_RADIO },
384 // JavaScript filter.
385 { "javascript_tab_label", IDS_JAVASCRIPT_TAB_LABEL },
386 { "javascript_header", IDS_JAVASCRIPT_HEADER },
387 { "javascript_allow", IDS_JS_ALLOW_RADIO },
388 { "javascript_block", IDS_JS_DONOTALLOW_RADIO },
389 // Plug-ins filter.
390 { "plugins_tab_label", IDS_PLUGIN_TAB_LABEL },
391 { "plugins_header", IDS_PLUGIN_HEADER },
392 { "plugins_ask", IDS_PLUGIN_ASK_RADIO },
393 { "plugins_allow", IDS_PLUGIN_LOAD_RADIO },
394 { "plugins_block", IDS_PLUGIN_NOLOAD_RADIO },
395 { "disableIndividualPlugins", IDS_PLUGIN_SELECTIVE_DISABLE },
396 // Pop-ups filter.
397 { "popups_tab_label", IDS_POPUP_TAB_LABEL },
398 { "popups_header", IDS_POPUP_HEADER },
399 { "popups_allow", IDS_POPUP_ALLOW_RADIO },
400 { "popups_block", IDS_POPUP_BLOCK_RADIO },
401 // Location filter.
402 { "location_tab_label", IDS_GEOLOCATION_TAB_LABEL },
403 { "location_header", IDS_GEOLOCATION_HEADER },
404 { "location_allow", IDS_GEOLOCATION_ALLOW_RADIO },
405 { "location_ask", IDS_GEOLOCATION_ASK_RADIO },
406 { "location_block", IDS_GEOLOCATION_BLOCK_RADIO },
407 { "set_by", IDS_GEOLOCATION_SET_BY_HOVER },
408 // Notifications filter.
409 { "notifications_tab_label", IDS_NOTIFICATIONS_TAB_LABEL },
410 { "notifications_header", IDS_NOTIFICATIONS_HEADER },
411 { "notifications_allow", IDS_NOTIFICATIONS_ALLOW_RADIO },
412 { "notifications_ask", IDS_NOTIFICATIONS_ASK_RADIO },
413 { "notifications_block", IDS_NOTIFICATIONS_BLOCK_RADIO },
414 // Intents filter.
415 { "webIntentsTabLabel", IDS_WEB_INTENTS_TAB_LABEL },
416 { "allowWebIntents", IDS_ALLOW_WEB_INTENTS },
417 // Fullscreen filter.
418 { "fullscreen_tab_label", IDS_FULLSCREEN_TAB_LABEL },
419 { "fullscreen_header", IDS_FULLSCREEN_HEADER },
420 // Mouse Lock filter.
421 { "mouselock_tab_label", IDS_MOUSE_LOCK_TAB_LABEL },
422 { "mouselock_header", IDS_MOUSE_LOCK_HEADER },
423 { "mouselock_allow", IDS_MOUSE_LOCK_ALLOW_RADIO },
424 { "mouselock_ask", IDS_MOUSE_LOCK_ASK_RADIO },
425 { "mouselock_block", IDS_MOUSE_LOCK_BLOCK_RADIO },
426 // Pepper Flash camera and microphone filter.
427 { "pepperFlashCameramicTabLabel", IDS_PEPPER_FLASH_CAMERAMIC_TAB_LABEL },
428 // The header has to be named as <content_type_name>_header.
429 { "pepper-flash-cameramic_header", IDS_PEPPER_FLASH_CAMERAMIC_HEADER },
430 { "pepperFlashCameramicAsk", IDS_PEPPER_FLASH_CAMERAMIC_ASK_RADIO },
431 { "pepperFlashCameramicBlock", IDS_PEPPER_FLASH_CAMERAMIC_BLOCK_RADIO },
432 #if defined(OS_CHROMEOS)
433 // Protected Content filter
434 { "protectedContentTabLabel", IDS_PROTECTED_CONTENT_TAB_LABEL },
435 { "protectedContentInfo", IDS_PROTECTED_CONTENT_INFO },
436 { "protectedContentEnable", IDS_PROTECTED_CONTENT_ENABLE},
437 #endif // defined(OS_CHROMEOS)
438 // Media stream capture device filter.
439 { "mediaStreamTabLabel", IDS_MEDIA_STREAM_TAB_LABEL },
440 { "media-stream_header", IDS_MEDIA_STREAM_HEADER },
441 { "mediaStreamAsk", IDS_MEDIA_STREAM_ASK_RADIO },
442 { "mediaStreamBlock", IDS_MEDIA_STREAM_BLOCK_RADIO },
443 };
444
445 RegisterStrings(localized_strings, resources, arraysize(resources));
446 RegisterTitle(localized_strings, "contentSettingsPage",
447 IDS_CONTENT_SETTINGS_TITLE);
448
449 // Register titles for each of the individual settings whose exception
450 // dialogs will be processed by |ContentSettingsHandler|.
451 RegisterTitle(localized_strings, "cookies",
452 IDS_COOKIES_TAB_LABEL);
453 RegisterTitle(localized_strings, "images",
454 IDS_IMAGES_TAB_LABEL);
455 RegisterTitle(localized_strings, "javascript",
456 IDS_JAVASCRIPT_TAB_LABEL);
457 RegisterTitle(localized_strings, "plugins",
458 IDS_PLUGIN_TAB_LABEL);
459 RegisterTitle(localized_strings, "popups",
460 IDS_POPUP_TAB_LABEL);
461 RegisterTitle(localized_strings, "location",
462 IDS_GEOLOCATION_TAB_LABEL);
463 RegisterTitle(localized_strings, "notifications",
464 IDS_NOTIFICATIONS_TAB_LABEL);
465 RegisterTitle(localized_strings, "fullscreen",
466 IDS_FULLSCREEN_TAB_LABEL);
467 RegisterTitle(localized_strings, "mouselock",
468 IDS_MOUSE_LOCK_TAB_LABEL);
469 RegisterTitle(localized_strings, "pepper-flash-cameramic",
470 IDS_PEPPER_FLASH_CAMERAMIC_TAB_LABEL);
471 RegisterTitle(localized_strings, "media-stream",
472 IDS_MEDIA_STREAM_TAB_LABEL);
473
474 Profile* profile = Profile::FromWebUI(web_ui());
475 localized_strings->SetBoolean(
476 "enable_web_intents",
477 web_intents::IsWebIntentsEnabledForProfile(profile));
478 // TODO(marja): clean up the options UI after the decision on the session
479 // restore changes has stabilized.
480 localized_strings->SetBoolean(
481 "enable_restore_session_state", false);
482 }
483
484 void ContentSettingsHandler::InitializeHandler() {
485 notification_registrar_.Add(
486 this, chrome::NOTIFICATION_PROFILE_CREATED,
487 content::NotificationService::AllSources());
488 notification_registrar_.Add(
489 this, chrome::NOTIFICATION_PROFILE_DESTROYED,
490 content::NotificationService::AllSources());
491
492 notification_registrar_.Add(
493 this, chrome::NOTIFICATION_CONTENT_SETTINGS_CHANGED,
494 content::NotificationService::AllSources());
495 notification_registrar_.Add(
496 this, chrome::NOTIFICATION_DESKTOP_NOTIFICATION_SETTINGS_CHANGED,
497 content::NotificationService::AllSources());
498 Profile* profile = Profile::FromWebUI(web_ui());
499 notification_registrar_.Add(
500 this, chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED,
501 content::Source<Profile>(profile));
502
503 PrefService* prefs = profile->GetPrefs();
504 pref_change_registrar_.Init(prefs);
505 pref_change_registrar_.Add(prefs::kGeolocationContentSettings, this);
506 pref_change_registrar_.Add(prefs::kPepperFlashSettingsEnabled, this);
507
508 flash_settings_manager_.reset(new PepperFlashSettingsManager(this, profile));
509 }
510
511 void ContentSettingsHandler::InitializePage() {
512 UpdateHandlersEnabledRadios();
513 UpdateAllExceptionsViewsFromModel();
514
515 flash_cameramic_settings_ = CachedPepperFlashSettings();
516 RefreshFlashSettingsCache(true);
517 }
518
519 void ContentSettingsHandler::Observe(
520 int type,
521 const content::NotificationSource& source,
522 const content::NotificationDetails& details) {
523 switch (type) {
524 case chrome::NOTIFICATION_PROFILE_DESTROYED: {
525 if (content::Source<Profile>(source).ptr()->IsOffTheRecord()) {
526 web_ui()->CallJavascriptFunction(
527 "ContentSettingsExceptionsArea.OTRProfileDestroyed");
528 }
529 break;
530 }
531
532 case chrome::NOTIFICATION_PROFILE_CREATED: {
533 if (content::Source<Profile>(source).ptr()->IsOffTheRecord())
534 UpdateAllOTRExceptionsViewsFromModel();
535 break;
536 }
537
538 case chrome::NOTIFICATION_CONTENT_SETTINGS_CHANGED: {
539 // Filter out notifications from other profiles.
540 HostContentSettingsMap* map =
541 content::Source<HostContentSettingsMap>(source).ptr();
542 if (map != GetContentSettingsMap() &&
543 map != GetOTRContentSettingsMap())
544 break;
545
546 const ContentSettingsDetails* settings_details =
547 content::Details<const ContentSettingsDetails>(details).ptr();
548
549 // TODO(estade): we pretend update_all() is always true.
550 if (settings_details->update_all_types()) {
551 UpdateAllExceptionsViewsFromModel();
552 } else {
553 UpdateExceptionsViewFromModel(
554 ExContentSettingsType(settings_details->type()));
555 }
556 break;
557 }
558
559 case chrome::NOTIFICATION_PREF_CHANGED: {
560 const std::string& pref_name =
561 *content::Details<std::string>(details).ptr();
562 if (pref_name == prefs::kGeolocationContentSettings)
563 UpdateGeolocationExceptionsView();
564 else if (pref_name == prefs::kPepperFlashSettingsEnabled)
565 RefreshFlashSettingsCache(false);
566
567 break;
568 }
569
570 case chrome::NOTIFICATION_DESKTOP_NOTIFICATION_SETTINGS_CHANGED: {
571 UpdateNotificationExceptionsView();
572 break;
573 }
574
575 case chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED: {
576 UpdateHandlersEnabledRadios();
577 break;
578 }
579
580 default:
581 OptionsPageUIHandler::Observe(type, source, details);
582 }
583 }
584
585 void ContentSettingsHandler::OnGetPermissionSettingsCompleted(
586 uint32 request_id,
587 bool success,
588 PP_Flash_BrowserOperations_Permission default_permission,
589 const ppapi::FlashSiteSettings& sites) {
590 if (success &&
591 request_id == flash_cameramic_settings_.last_refresh_request_id) {
592 flash_cameramic_settings_.default_permission = default_permission;
593 flash_cameramic_settings_.sites.clear();
594 for (ppapi::FlashSiteSettings::const_iterator iter = sites.begin();
595 iter != sites.end(); ++iter) {
596 if (IsValidHost(iter->site))
597 flash_cameramic_settings_.sites[iter->site] = iter->permission;
598 }
599 UpdateExceptionsViewFromModel(
600 ExContentSettingsType(EX_CONTENT_SETTINGS_TYPE_PEPPER_FLASH_CAMERAMIC));
601
602 if (!flash_cameramic_settings_.initialized) {
603 web_ui()->CallJavascriptFunction(
604 "ContentSettings.enablePepperFlashCameraMicSettings");
605 flash_cameramic_settings_.initialized = true;
606 }
607 }
608 }
609
610 void ContentSettingsHandler::UpdateSettingDefaultFromModel(
611 const ExContentSettingsType& type) {
612 DictionaryValue filter_settings;
613 std::string provider_id;
614 filter_settings.SetString(
615 ExContentSettingsTypeToGroupName(type) + ".value",
616 GetSettingDefaultFromModel(type, &provider_id));
617 filter_settings.SetString(
618 ExContentSettingsTypeToGroupName(type) + ".managedBy", provider_id);
619
620 web_ui()->CallJavascriptFunction(
621 "ContentSettings.setContentFilterSettingsValue", filter_settings);
622 web_ui()->CallJavascriptFunction(
623 "BrowserOptions.setContentFilterSettingsValue", filter_settings);
624 }
625
626 std::string ContentSettingsHandler::GetSettingDefaultFromModel(
627 const ExContentSettingsType& type, std::string* provider_id) {
628 Profile* profile = Profile::FromWebUI(web_ui());
629 ContentSetting default_setting;
630 if (type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) {
631 default_setting =
632 DesktopNotificationServiceFactory::GetForProfile(profile)->
633 GetDefaultContentSetting(provider_id);
634 } else if (type == EX_CONTENT_SETTINGS_TYPE_PEPPER_FLASH_CAMERAMIC) {
635 default_setting = FlashPermissionToContentSetting(
636 flash_cameramic_settings_.default_permission);
637 *provider_id = kDefaultProviderID;
638 } else {
639 default_setting =
640 profile->GetHostContentSettingsMap()->
641 GetDefaultContentSetting(type.ToContentSettingsType(), provider_id);
642 }
643
644 return ContentSettingToString(default_setting);
645 }
646
647 void ContentSettingsHandler::UpdateHandlersEnabledRadios() {
648 base::FundamentalValue handlers_enabled(
649 GetProtocolHandlerRegistry()->enabled());
650
651 web_ui()->CallJavascriptFunction(
652 "ContentSettings.updateHandlersEnabledRadios",
653 handlers_enabled);
654 }
655
656 void ContentSettingsHandler::UpdateAllExceptionsViewsFromModel() {
657 for (int type = CONTENT_SETTINGS_TYPE_DEFAULT + 1;
658 type < EX_CONTENT_SETTINGS_NUM_TYPES; ++type) {
659 // The content settings type CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
660 // is supposed to be set by policy only. Hence there is no user facing UI
661 // for this content type and we skip it here.
662 if (type == CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE)
663 continue;
664 UpdateExceptionsViewFromModel(ExContentSettingsType(type));
665 }
666 }
667
668 void ContentSettingsHandler::UpdateAllOTRExceptionsViewsFromModel() {
669 for (int type = CONTENT_SETTINGS_TYPE_DEFAULT + 1;
670 type < EX_CONTENT_SETTINGS_NUM_TYPES; ++type) {
671 UpdateOTRExceptionsViewFromModel(ExContentSettingsType(type));
672 }
673 }
674
675 void ContentSettingsHandler::UpdateExceptionsViewFromModel(
676 const ExContentSettingsType& type) {
677 switch (type) {
678 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
679 UpdateGeolocationExceptionsView();
680 break;
681 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
682 UpdateNotificationExceptionsView();
683 break;
684 case EX_CONTENT_SETTINGS_TYPE_PEPPER_FLASH_CAMERAMIC:
685 UpdateFlashCameraMicExceptionsView();
686 break;
687 case CONTENT_SETTINGS_TYPE_INTENTS:
688 // Don't update intents settings at this point.
689 // Turn on when enable_web_intents_tag is enabled.
690 break;
691 case CONTENT_SETTINGS_TYPE_MIXEDSCRIPT:
692 // We don't yet support exceptions for mixed scripting.
693 break;
694 default:
695 UpdateExceptionsViewFromHostContentSettingsMap(
696 type.ToContentSettingsType());
697 break;
698 }
699 }
700
701 void ContentSettingsHandler::UpdateOTRExceptionsViewFromModel(
702 const ExContentSettingsType& type) {
703 switch (type) {
704 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
705 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
706 case CONTENT_SETTINGS_TYPE_INTENTS:
707 case CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE:
708 case CONTENT_SETTINGS_TYPE_MIXEDSCRIPT:
709 case EX_CONTENT_SETTINGS_TYPE_PEPPER_FLASH_CAMERAMIC:
710 break;
711 default:
712 UpdateExceptionsViewFromOTRHostContentSettingsMap(
713 type.ToContentSettingsType());
714 break;
715 }
716 }
717
718 void ContentSettingsHandler::UpdateGeolocationExceptionsView() {
719 Profile* profile = Profile::FromWebUI(web_ui());
720 HostContentSettingsMap* map = profile->GetHostContentSettingsMap();
721
722 ContentSettingsForOneType all_settings;
723 map->GetSettingsForOneType(
724 CONTENT_SETTINGS_TYPE_GEOLOCATION,
725 std::string(),
726 &all_settings);
727
728 // Group geolocation settings by primary_pattern.
729 AllPatternsSettings all_patterns_settings;
730 for (ContentSettingsForOneType::iterator i =
731 all_settings.begin();
732 i != all_settings.end();
733 ++i) {
734 // Don't add default settings.
735 if (i->primary_pattern == ContentSettingsPattern::Wildcard() &&
736 i->secondary_pattern == ContentSettingsPattern::Wildcard() &&
737 i->source != kPreferencesSource) {
738 continue;
739 }
740 all_patterns_settings[i->primary_pattern][i->secondary_pattern] =
741 i->setting;
742 }
743
744 ListValue exceptions;
745 AddExceptionsGrantedByHostedApps(
746 profile,
747 HostedAppHasPermission<APIPermission::kGeolocation>,
748 &exceptions);
749
750 for (AllPatternsSettings::iterator i = all_patterns_settings.begin();
751 i != all_patterns_settings.end();
752 ++i) {
753 const ContentSettingsPattern& primary_pattern = i->first;
754 const OnePatternSettings& one_settings = i->second;
755
756 OnePatternSettings::const_iterator parent =
757 one_settings.find(primary_pattern);
758
759 // Add the "parent" entry for the non-embedded setting.
760 ContentSetting parent_setting =
761 parent == one_settings.end() ? CONTENT_SETTING_DEFAULT : parent->second;
762 exceptions.Append(GetGeolocationExceptionForPage(primary_pattern,
763 primary_pattern,
764 parent_setting));
765
766 // Add the "children" for any embedded settings.
767 for (OnePatternSettings::const_iterator j = one_settings.begin();
768 j != one_settings.end();
769 ++j) {
770 // Skip the non-embedded setting which we already added above.
771 if (j == parent)
772 continue;
773
774 exceptions.Append(
775 GetGeolocationExceptionForPage(primary_pattern, j->first, j->second));
776 }
777 }
778
779 StringValue type_string(
780 ContentSettingsTypeToGroupName(CONTENT_SETTINGS_TYPE_GEOLOCATION));
781 web_ui()->CallJavascriptFunction("ContentSettings.setExceptions",
782 type_string, exceptions);
783
784 // This is mainly here to keep this function ideologically parallel to
785 // UpdateExceptionsViewFromHostContentSettingsMap().
786 UpdateSettingDefaultFromModel(
787 ExContentSettingsType(CONTENT_SETTINGS_TYPE_GEOLOCATION));
788 }
789
790 void ContentSettingsHandler::UpdateNotificationExceptionsView() {
791 Profile* profile = Profile::FromWebUI(web_ui());
792 DesktopNotificationService* service =
793 DesktopNotificationServiceFactory::GetForProfile(profile);
794
795 ContentSettingsForOneType settings;
796 service->GetNotificationsSettings(&settings);
797
798 ListValue exceptions;
799 AddExceptionsGrantedByHostedApps(profile,
800 HostedAppHasPermission<APIPermission::kNotification>,
801 &exceptions);
802
803 for (ContentSettingsForOneType::const_iterator i =
804 settings.begin();
805 i != settings.end();
806 ++i) {
807 // Don't add default settings.
808 if (i->primary_pattern == ContentSettingsPattern::Wildcard() &&
809 i->secondary_pattern == ContentSettingsPattern::Wildcard() &&
810 i->source != kPreferencesSource) {
811 continue;
812 }
813
814 exceptions.Append(
815 GetNotificationExceptionForPage(i->primary_pattern, i->setting,
816 i->source));
817 }
818
819 StringValue type_string(
820 ContentSettingsTypeToGroupName(CONTENT_SETTINGS_TYPE_NOTIFICATIONS));
821 web_ui()->CallJavascriptFunction("ContentSettings.setExceptions",
822 type_string, exceptions);
823
824 // This is mainly here to keep this function ideologically parallel to
825 // UpdateExceptionsViewFromHostContentSettingsMap().
826 UpdateSettingDefaultFromModel(
827 ExContentSettingsType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS));
828 }
829
830 void ContentSettingsHandler::UpdateFlashCameraMicExceptionsView() {
831 ListValue exceptions;
832 for (CachedPepperFlashSettings::SiteMap::iterator iter =
833 flash_cameramic_settings_.sites.begin();
834 iter != flash_cameramic_settings_.sites.end(); ++iter) {
835 DictionaryValue* exception = new DictionaryValue();
836 exception->SetString(kDisplayPattern, iter->first);
837 exception->SetString(
838 kSetting,
839 ContentSettingToString(FlashPermissionToContentSetting(iter->second)));
840 exception->SetString(kSource, "preference");
841 exceptions.Append(exception);
842 }
843
844 StringValue type_string(ExContentSettingsTypeToGroupName(
845 ExContentSettingsType(EX_CONTENT_SETTINGS_TYPE_PEPPER_FLASH_CAMERAMIC)));
846 web_ui()->CallJavascriptFunction("ContentSettings.setExceptions",
847 type_string, exceptions);
848
849 UpdateSettingDefaultFromModel(
850 ExContentSettingsType(EX_CONTENT_SETTINGS_TYPE_PEPPER_FLASH_CAMERAMIC));
851 }
852
853 void ContentSettingsHandler::UpdateExceptionsViewFromHostContentSettingsMap(
854 ContentSettingsType type) {
855 ContentSettingsForOneType entries;
856 GetContentSettingsMap()->GetSettingsForOneType(type, "", &entries);
857
858 ListValue exceptions;
859 for (size_t i = 0; i < entries.size(); ++i) {
860 // Skip default settings from extensions and policy, and the default content
861 // settings; all of them will affect the default setting UI.
862 if (entries[i].primary_pattern == ContentSettingsPattern::Wildcard() &&
863 entries[i].secondary_pattern == ContentSettingsPattern::Wildcard() &&
864 entries[i].source != "preference") {
865 continue;
866 }
867 // The content settings UI does not support secondary content settings
868 // pattern yet. For content settings set through the content settings UI the
869 // secondary pattern is by default a wildcard pattern. Hence users are not
870 // able to modify content settings with a secondary pattern other than the
871 // wildcard pattern. So only show settings that the user is able to modify.
872 // TODO(bauerb): Support a read-only view for those patterns.
873 if (entries[i].secondary_pattern == ContentSettingsPattern::Wildcard()) {
874 // Media Stream is using compound values for exceptions, which are
875 // granted as |CONTENT_SETTING_ALLOW|.
876 ContentSetting content_setting = CONTENT_SETTING_DEFAULT;
877 if (type == CONTENT_SETTINGS_TYPE_MEDIASTREAM)
878 content_setting = CONTENT_SETTING_ALLOW;
879 else
880 content_setting = entries[i].setting;
881
882 exceptions.Append(GetExceptionForPage(entries[i].primary_pattern,
883 content_setting,
884 entries[i].source));
885 } else {
886 LOG(ERROR) << "Secondary content settings patterns are not "
887 << "supported by the content settings UI";
888 }
889 }
890
891 StringValue type_string(ContentSettingsTypeToGroupName(type));
892 web_ui()->CallJavascriptFunction("ContentSettings.setExceptions", type_string,
893 exceptions);
894
895 UpdateExceptionsViewFromOTRHostContentSettingsMap(type);
896
897 // TODO(koz): The default for fullscreen is always 'ask'.
898 // http://crbug.com/104683
899 if (type == CONTENT_SETTINGS_TYPE_FULLSCREEN)
900 return;
901
902 // The default may also have changed (we won't get a separate notification).
903 // If it hasn't changed, this call will be harmless.
904 UpdateSettingDefaultFromModel(ExContentSettingsType(type));
905 }
906
907 void ContentSettingsHandler::UpdateExceptionsViewFromOTRHostContentSettingsMap(
908 ContentSettingsType type) {
909 const HostContentSettingsMap* otr_settings_map = GetOTRContentSettingsMap();
910 if (!otr_settings_map)
911 return;
912
913 ContentSettingsForOneType otr_entries;
914 otr_settings_map->GetSettingsForOneType(type, "", &otr_entries);
915
916 ListValue otr_exceptions;
917 for (size_t i = 0; i < otr_entries.size(); ++i) {
918 // Off-the-record HostContentSettingsMap contains incognito content settings
919 // as well as normal content settings. Here, we use the incongnito settings
920 // only.
921 if (!otr_entries[i].incognito)
922 continue;
923 // The content settings UI does not support secondary content settings
924 // pattern yet. For content settings set through the content settings UI the
925 // secondary pattern is by default a wildcard pattern. Hence users are not
926 // able to modify content settings with a secondary pattern other than the
927 // wildcard pattern. So only show settings that the user is able to modify.
928 // TODO(bauerb): Support a read-only view for those patterns.
929 if (otr_entries[i].secondary_pattern ==
930 ContentSettingsPattern::Wildcard()) {
931 ContentSetting content_setting = CONTENT_SETTING_DEFAULT;
932 // Media Stream is using compound values for its exceptions and arbitrary
933 // values for its default setting. And all the exceptions are granted as
934 // |CONTENT_SETTING_ALLOW|.
935 if (type == CONTENT_SETTINGS_TYPE_MEDIASTREAM &&
936 otr_entries[i].primary_pattern != ContentSettingsPattern::Wildcard())
937 content_setting = CONTENT_SETTING_ALLOW;
938 else
939 content_setting = otr_entries[i].setting;
940
941 otr_exceptions.Append(GetExceptionForPage(otr_entries[i].primary_pattern,
942 content_setting,
943 otr_entries[i].source));
944 } else {
945 LOG(ERROR) << "Secondary content settings patterns are not "
946 << "supported by the content settings UI";
947 }
948 }
949
950 StringValue type_string(ContentSettingsTypeToGroupName(type));
951 web_ui()->CallJavascriptFunction("ContentSettings.setOTRExceptions",
952 type_string, otr_exceptions);
953 }
954
955 void ContentSettingsHandler::RemoveGeolocationException(
956 const ListValue* args, size_t arg_index) {
957 Profile* profile = Profile::FromWebUI(web_ui());
958 std::string origin;
959 std::string embedding_origin;
960 bool rv = args->GetString(arg_index++, &origin);
961 DCHECK(rv);
962 rv = args->GetString(arg_index++, &embedding_origin);
963 DCHECK(rv);
964
965 profile->GetHostContentSettingsMap()->
966 SetContentSetting(ContentSettingsPattern::FromString(origin),
967 ContentSettingsPattern::FromString(embedding_origin),
968 CONTENT_SETTINGS_TYPE_GEOLOCATION,
969 std::string(),
970 CONTENT_SETTING_DEFAULT);
971 }
972
973 void ContentSettingsHandler::RemoveNotificationException(
974 const ListValue* args, size_t arg_index) {
975 Profile* profile = Profile::FromWebUI(web_ui());
976 std::string origin;
977 std::string setting;
978 bool rv = args->GetString(arg_index++, &origin);
979 DCHECK(rv);
980 rv = args->GetString(arg_index++, &setting);
981 DCHECK(rv);
982 ContentSetting content_setting = ContentSettingFromString(setting);
983
984 DCHECK(content_setting == CONTENT_SETTING_ALLOW ||
985 content_setting == CONTENT_SETTING_BLOCK);
986 DesktopNotificationServiceFactory::GetForProfile(profile)->
987 ClearSetting(ContentSettingsPattern::FromString(origin));
988 }
989
990 void ContentSettingsHandler::RemoveFlashCameraMicException(
991 const ListValue* args, size_t arg_index) {
992 std::string mode;
993 bool rv = args->GetString(arg_index++, &mode);
994 DCHECK(rv);
995 DCHECK_EQ(mode, "normal");
996
997 std::string pattern;
998 rv = args->GetString(arg_index++, &pattern);
999 DCHECK(rv);
1000
1001 CachedPepperFlashSettings::SiteMap::iterator iter =
1002 flash_cameramic_settings_.sites.find(pattern);
1003 if (iter != flash_cameramic_settings_.sites.end()) {
1004 flash_cameramic_settings_.sites.erase(iter);
1005 ppapi::FlashSiteSettings site_settings(
1006 1,
1007 ppapi::FlashSiteSetting(pattern,
1008 PP_FLASH_BROWSEROPERATIONS_PERMISSION_DEFAULT));
1009 flash_settings_manager_->SetSitePermission(
1010 PP_FLASH_BROWSEROPERATIONS_SETTINGTYPE_CAMERAMIC,
1011 site_settings);
1012 } else {
1013 NOTREACHED();
1014 }
1015
1016 UpdateFlashCameraMicExceptionsView();
1017 }
1018
1019 void ContentSettingsHandler::RemoveExceptionFromHostContentSettingsMap(
1020 const ListValue* args, size_t arg_index,
1021 const ExContentSettingsType& type) {
1022 std::string mode;
1023 bool rv = args->GetString(arg_index++, &mode);
1024 DCHECK(rv);
1025
1026 std::string pattern;
1027 rv = args->GetString(arg_index++, &pattern);
1028 DCHECK(rv);
1029
1030 HostContentSettingsMap* settings_map =
1031 mode == "normal" ? GetContentSettingsMap() :
1032 GetOTRContentSettingsMap();
1033 if (settings_map) {
1034 settings_map->SetWebsiteSetting(
1035 ContentSettingsPattern::FromString(pattern),
1036 ContentSettingsPattern::Wildcard(),
1037 type.ToContentSettingsType(),
1038 "",
1039 NULL);
1040 }
1041 }
1042
1043 void ContentSettingsHandler::RegisterMessages() {
1044 web_ui()->RegisterMessageCallback("setContentFilter",
1045 base::Bind(&ContentSettingsHandler::SetContentFilter,
1046 base::Unretained(this)));
1047 web_ui()->RegisterMessageCallback("removeException",
1048 base::Bind(&ContentSettingsHandler::RemoveException,
1049 base::Unretained(this)));
1050 web_ui()->RegisterMessageCallback("setException",
1051 base::Bind(&ContentSettingsHandler::SetException,
1052 base::Unretained(this)));
1053 web_ui()->RegisterMessageCallback("checkExceptionPatternValidity",
1054 base::Bind(&ContentSettingsHandler::CheckExceptionPatternValidity,
1055 base::Unretained(this)));
1056 }
1057
1058 void ContentSettingsHandler::ApplyWhitelist(ContentSettingsType content_type,
1059 ContentSetting default_setting) {
1060 Profile* profile = Profile::FromWebUI(web_ui());
1061 HostContentSettingsMap* map = GetContentSettingsMap();
1062 if (content_type != CONTENT_SETTINGS_TYPE_PLUGINS)
1063 return;
1064 const int kDefaultWhitelistVersion = 1;
1065 PrefService* prefs = profile->GetPrefs();
1066 int version = prefs->GetInteger(
1067 prefs::kContentSettingsDefaultWhitelistVersion);
1068 if (version >= kDefaultWhitelistVersion)
1069 return;
1070 ContentSetting old_setting =
1071 map->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS, NULL);
1072 // TODO(bauerb): Remove this once the Google Talk plug-in works nicely with
1073 // click-to-play (b/6090625).
1074 if (old_setting == CONTENT_SETTING_ALLOW &&
1075 default_setting == CONTENT_SETTING_ASK) {
1076 map->SetWebsiteSetting(
1077 ContentSettingsPattern::Wildcard(),
1078 ContentSettingsPattern::Wildcard(),
1079 CONTENT_SETTINGS_TYPE_PLUGINS,
1080 "google-talk",
1081 Value::CreateIntegerValue(CONTENT_SETTING_ALLOW));
1082 }
1083 prefs->SetInteger(prefs::kContentSettingsDefaultWhitelistVersion,
1084 kDefaultWhitelistVersion);
1085 }
1086
1087 void ContentSettingsHandler::SetContentFilter(const ListValue* args) {
1088 DCHECK_EQ(2U, args->GetSize());
1089 std::string group, setting;
1090 if (!(args->GetString(0, &group) &&
1091 args->GetString(1, &setting))) {
1092 NOTREACHED();
1093 return;
1094 }
1095
1096 ContentSetting default_setting = ContentSettingFromString(setting);
1097 ExContentSettingsType content_type =
1098 ExContentSettingsTypeFromGroupName(group);
1099 Profile* profile = Profile::FromWebUI(web_ui());
1100
1101 #if defined(OS_CHROMEOS)
1102 // ChromeOS special case : in Guest mode settings are opened in Incognito
1103 // mode, so we need original profile to actually modify settings.
1104 if (chromeos::UserManager::Get()->IsLoggedInAsGuest())
1105 profile = profile->GetOriginalProfile();
1106 #endif
1107
1108 if (content_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) {
1109 DesktopNotificationServiceFactory::GetForProfile(profile)->
1110 SetDefaultContentSetting(default_setting);
1111 } else if (content_type == EX_CONTENT_SETTINGS_TYPE_PEPPER_FLASH_CAMERAMIC) {
1112 flash_cameramic_settings_.default_permission =
1113 FlashPermissionFromContentSetting(default_setting);
1114 flash_settings_manager_->SetDefaultPermission(
1115 PP_FLASH_BROWSEROPERATIONS_SETTINGTYPE_CAMERAMIC,
1116 flash_cameramic_settings_.default_permission, false);
1117 RefreshFlashSettingsCache(true);
1118 } else {
1119 HostContentSettingsMap* map = profile->GetHostContentSettingsMap();
1120 ContentSettingsType converted_type = content_type.ToContentSettingsType();
1121 ApplyWhitelist(converted_type, default_setting);
1122 map->SetDefaultContentSetting(converted_type, default_setting);
1123 }
1124 switch (content_type) {
1125 case CONTENT_SETTINGS_TYPE_COOKIES:
1126 content::RecordAction(
1127 UserMetricsAction("Options_DefaultCookieSettingChanged"));
1128 break;
1129 case CONTENT_SETTINGS_TYPE_IMAGES:
1130 content::RecordAction(
1131 UserMetricsAction("Options_DefaultImagesSettingChanged"));
1132 break;
1133 case CONTENT_SETTINGS_TYPE_JAVASCRIPT:
1134 content::RecordAction(
1135 UserMetricsAction("Options_DefaultJavaScriptSettingChanged"));
1136 break;
1137 case CONTENT_SETTINGS_TYPE_PLUGINS:
1138 content::RecordAction(
1139 UserMetricsAction("Options_DefaultPluginsSettingChanged"));
1140 break;
1141 case CONTENT_SETTINGS_TYPE_POPUPS:
1142 content::RecordAction(
1143 UserMetricsAction("Options_DefaultPopupsSettingChanged"));
1144 break;
1145 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
1146 content::RecordAction(
1147 UserMetricsAction("Options_DefaultNotificationsSettingChanged"));
1148 break;
1149 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
1150 content::RecordAction(
1151 UserMetricsAction("Options_DefaultGeolocationSettingChanged"));
1152 break;
1153 case CONTENT_SETTINGS_TYPE_INTENTS:
1154 content::RecordAction(
1155 UserMetricsAction("Options_DefaultHandlersSettingChanged"));
1156 break;
1157 case CONTENT_SETTINGS_TYPE_MOUSELOCK:
1158 content::RecordAction(
1159 UserMetricsAction("Options_DefaultMouseLockSettingChanged"));
1160 break;
1161 case EX_CONTENT_SETTINGS_TYPE_PEPPER_FLASH_CAMERAMIC:
1162 content::RecordAction(
1163 UserMetricsAction("Options_DefaultFlashCameraMicSettingChanged"));
1164 break;
1165 case CONTENT_SETTINGS_TYPE_MEDIASTREAM:
1166 content::RecordAction(
1167 UserMetricsAction("Options_DefaultMediaStreamSettingChanged"));
1168 break;
1169 default:
1170 break;
1171 }
1172 }
1173
1174 void ContentSettingsHandler::RemoveException(const ListValue* args) {
1175 size_t arg_i = 0;
1176 std::string type_string;
1177 CHECK(args->GetString(arg_i++, &type_string));
1178
1179 ExContentSettingsType type = ExContentSettingsTypeFromGroupName(type_string);
1180 switch (type) {
1181 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
1182 RemoveGeolocationException(args, arg_i);
1183 break;
1184 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
1185 RemoveNotificationException(args, arg_i);
1186 break;
1187 case EX_CONTENT_SETTINGS_TYPE_PEPPER_FLASH_CAMERAMIC:
1188 RemoveFlashCameraMicException(args, arg_i);
1189 break;
1190 default:
1191 RemoveExceptionFromHostContentSettingsMap(args, arg_i, type);
1192 break;
1193 }
1194 }
1195
1196 void ContentSettingsHandler::SetException(const ListValue* args) {
1197 size_t arg_i = 0;
1198 std::string type_string;
1199 CHECK(args->GetString(arg_i++, &type_string));
1200 std::string mode;
1201 CHECK(args->GetString(arg_i++, &mode));
1202 std::string pattern;
1203 CHECK(args->GetString(arg_i++, &pattern));
1204 std::string setting;
1205 CHECK(args->GetString(arg_i++, &setting));
1206
1207 ExContentSettingsType type = ExContentSettingsTypeFromGroupName(type_string);
1208 if (type == CONTENT_SETTINGS_TYPE_GEOLOCATION ||
1209 type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS ||
1210 type == CONTENT_SETTINGS_TYPE_MEDIASTREAM) {
1211 NOTREACHED();
1212 } else if (type == EX_CONTENT_SETTINGS_TYPE_PEPPER_FLASH_CAMERAMIC) {
1213 DCHECK(IsValidHost(pattern));
1214
1215 if (flash_cameramic_settings_.sites.find(pattern) ==
1216 flash_cameramic_settings_.sites.end()) {
1217 pattern = CanonicalizeHost(pattern);
1218 }
1219 PP_Flash_BrowserOperations_Permission permission =
1220 FlashPermissionFromContentSetting(ContentSettingFromString(setting));
1221 flash_cameramic_settings_.sites[pattern] = permission;
1222 ppapi::FlashSiteSettings
1223 site_settings(1, ppapi::FlashSiteSetting(pattern, permission));
1224 flash_settings_manager_->SetSitePermission(
1225 PP_FLASH_BROWSEROPERATIONS_SETTINGTYPE_CAMERAMIC,
1226 site_settings);
1227 UpdateFlashCameraMicExceptionsView();
1228 } else {
1229 HostContentSettingsMap* settings_map =
1230 mode == "normal" ? GetContentSettingsMap() :
1231 GetOTRContentSettingsMap();
1232
1233 // The settings map could be null if the mode was OTR but the OTR profile
1234 // got destroyed before we received this message.
1235 if (!settings_map)
1236 return;
1237 settings_map->SetContentSetting(ContentSettingsPattern::FromString(pattern),
1238 ContentSettingsPattern::Wildcard(),
1239 type.ToContentSettingsType(),
1240 "",
1241 ContentSettingFromString(setting));
1242 }
1243 }
1244
1245 void ContentSettingsHandler::CheckExceptionPatternValidity(
1246 const ListValue* args) {
1247 size_t arg_i = 0;
1248 std::string type_string;
1249 CHECK(args->GetString(arg_i++, &type_string));
1250 std::string mode_string;
1251 CHECK(args->GetString(arg_i++, &mode_string));
1252 std::string pattern_string;
1253 CHECK(args->GetString(arg_i++, &pattern_string));
1254
1255 ExContentSettingsType type = ExContentSettingsTypeFromGroupName(type_string);
1256 bool is_valid = false;
1257 if (type == EX_CONTENT_SETTINGS_TYPE_PEPPER_FLASH_CAMERAMIC) {
1258 is_valid = IsValidHost(pattern_string);
1259 } else {
1260 ContentSettingsPattern pattern =
1261 ContentSettingsPattern::FromString(pattern_string);
1262 is_valid = pattern.IsValid();
1263 }
1264
1265 scoped_ptr<Value> type_value(Value::CreateStringValue(type_string));
1266 scoped_ptr<Value> mode_value(Value::CreateStringValue(mode_string));
1267 scoped_ptr<Value> pattern_value(Value::CreateStringValue(pattern_string));
1268 scoped_ptr<Value> valid_value(Value::CreateBooleanValue(is_valid));
1269
1270 web_ui()->CallJavascriptFunction(
1271 "ContentSettings.patternValidityCheckComplete",
1272 *type_value.get(),
1273 *mode_value.get(),
1274 *pattern_value.get(),
1275 *valid_value.get());
1276 }
1277
1278 // static
1279 std::string ContentSettingsHandler::ContentSettingsTypeToGroupName(
1280 ContentSettingsType type) {
1281 return ExContentSettingsTypeToGroupName(ExContentSettingsType(type));
1282 }
1283
1284 HostContentSettingsMap* ContentSettingsHandler::GetContentSettingsMap() {
1285 return Profile::FromWebUI(web_ui())->GetHostContentSettingsMap();
1286 }
1287
1288 ProtocolHandlerRegistry* ContentSettingsHandler::GetProtocolHandlerRegistry() {
1289 return Profile::FromWebUI(web_ui())->GetProtocolHandlerRegistry();
1290 }
1291
1292 HostContentSettingsMap*
1293 ContentSettingsHandler::GetOTRContentSettingsMap() {
1294 Profile* profile = Profile::FromWebUI(web_ui());
1295 if (profile->HasOffTheRecordProfile())
1296 return profile->GetOffTheRecordProfile()->GetHostContentSettingsMap();
1297 return NULL;
1298 }
1299
1300 void ContentSettingsHandler::RefreshFlashSettingsCache(bool force) {
1301 if (force || !flash_cameramic_settings_.initialized) {
1302 flash_cameramic_settings_.last_refresh_request_id =
1303 flash_settings_manager_->GetPermissionSettings(
1304 PP_FLASH_BROWSEROPERATIONS_SETTINGTYPE_CAMERAMIC);
1305 }
1306 }
1307
1308 // static
1309 ContentSettingsHandler::ExContentSettingsType
1310 ContentSettingsHandler::ExContentSettingsTypeFromGroupName(
1311 const std::string& name) {
1312 COMPILE_ASSERT(arraysize(kExContentSettingsTypeGroupNames) ==
1313 EX_CONTENT_SETTINGS_NUM_TYPES,
1314 MISSING_CONTENT_SETTINGS_TYPE);
1315
1316 for (size_t i = 0; i < arraysize(kExContentSettingsTypeGroupNames); ++i) {
1317 if (name == kExContentSettingsTypeGroupNames[i].name)
1318 return ExContentSettingsType(kExContentSettingsTypeGroupNames[i].type);
1319 }
1320
1321 NOTREACHED() << name << " is not a recognized content settings type.";
1322 return ExContentSettingsType(CONTENT_SETTINGS_TYPE_DEFAULT);
1323 }
1324
1325 // static
1326 std::string ContentSettingsHandler::ExContentSettingsTypeToGroupName(
1327 const ExContentSettingsType& type) {
1328 for (size_t i = 0; i < arraysize(kExContentSettingsTypeGroupNames); ++i) {
1329 if (type == kExContentSettingsTypeGroupNames[i].type)
1330 return kExContentSettingsTypeGroupNames[i].name;
1331 }
1332
1333 NOTREACHED();
1334 return std::string();
1335 }
1336
1337 } // namespace options2
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698