OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/content_settings/permission_observer.h" |
| 6 |
| 7 #include "chrome/browser/content_settings/permission_context.h" |
| 8 #include "chrome/browser/content_settings/permission_context_base.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 11 #include "url/gurl.h" |
| 12 |
| 13 struct PermissionObserver::Subscription { |
| 14 ContentSettingsType content_type; |
| 15 GURL requesting_origin; |
| 16 GURL embedding_origin; |
| 17 PermissionChangeCallback callback; |
| 18 ContentSetting current_value; |
| 19 }; |
| 20 |
| 21 PermissionObserver::PermissionObserver(Profile* profile) |
| 22 : profile_(profile) { |
| 23 } |
| 24 |
| 25 PermissionObserver::~PermissionObserver() { |
| 26 if (!subscriptions_.IsEmpty()) |
| 27 profile_->GetHostContentSettingsMap()->RemoveObserver(this); |
| 28 } |
| 29 |
| 30 int PermissionObserver::Subscribe(ContentSettingsType content_type, |
| 31 const GURL& requesting_origin, |
| 32 const GURL& embedding_origin, |
| 33 const PermissionChangeCallback& callback) { |
| 34 if (subscriptions_.IsEmpty()) |
| 35 profile_->GetHostContentSettingsMap()->AddObserver(this); |
| 36 |
| 37 Subscription* subscription = new Subscription(); |
| 38 subscription->content_type = content_type; |
| 39 subscription->requesting_origin = requesting_origin; |
| 40 subscription->embedding_origin = embedding_origin; |
| 41 subscription->callback = callback; |
| 42 subscription->current_value = PermissionContext::Get(profile_, content_type) |
| 43 ->GetPermissionStatus(subscription->requesting_origin, |
| 44 subscription->embedding_origin); |
| 45 |
| 46 return subscriptions_.Add(subscription); |
| 47 } |
| 48 |
| 49 void PermissionObserver::Unsubscribe(int subscription_id) { |
| 50 // Whether |subscription_id| is known will be checked by the Remove() call. |
| 51 subscriptions_.Remove(subscription_id); |
| 52 |
| 53 if (subscriptions_.IsEmpty()) |
| 54 profile_->GetHostContentSettingsMap()->RemoveObserver(this); |
| 55 } |
| 56 |
| 57 void PermissionObserver::OnContentSettingChanged( |
| 58 const ContentSettingsPattern& primary_pattern, |
| 59 const ContentSettingsPattern& secondary_pattern, |
| 60 ContentSettingsType content_type, |
| 61 std::string resource_identifier) { |
| 62 for (SubscriptionsMap::iterator iter(&subscriptions_); |
| 63 !iter.IsAtEnd(); iter.Advance()) { |
| 64 Subscription* subscription = iter.GetCurrentValue(); |
| 65 if (subscription->content_type != content_type) |
| 66 continue; |
| 67 |
| 68 if (primary_pattern.IsValid() && |
| 69 !primary_pattern.Matches(subscription->requesting_origin)) |
| 70 continue; |
| 71 if (secondary_pattern.IsValid() && |
| 72 !secondary_pattern.Matches(subscription->embedding_origin)) |
| 73 continue; |
| 74 |
| 75 ContentSetting new_value = PermissionContext::Get(profile_, content_type) |
| 76 ->GetPermissionStatus(subscription->requesting_origin, |
| 77 subscription->embedding_origin); |
| 78 if (subscription->current_value == new_value) |
| 79 continue; |
| 80 |
| 81 subscription->current_value = new_value; |
| 82 subscription->callback.Run(new_value); |
| 83 } |
| 84 } |
OLD | NEW |