Index: chrome/browser/content_settings/permission_observer.cc |
diff --git a/chrome/browser/content_settings/permission_observer.cc b/chrome/browser/content_settings/permission_observer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..53d765e36941e4eac07efd33ccb9524a2e404965 |
--- /dev/null |
+++ b/chrome/browser/content_settings/permission_observer.cc |
@@ -0,0 +1,84 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/content_settings/permission_observer.h" |
+ |
+#include "chrome/browser/content_settings/permission_context.h" |
+#include "chrome/browser/content_settings/permission_context_base.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "components/content_settings/core/browser/host_content_settings_map.h" |
+#include "url/gurl.h" |
+ |
+struct PermissionObserver::Subscription { |
+ ContentSettingsType content_type; |
+ GURL requesting_origin; |
+ GURL embedding_origin; |
+ PermissionChangeCallback callback; |
+ ContentSetting current_value; |
+}; |
+ |
+PermissionObserver::PermissionObserver(Profile* profile) |
+ : profile_(profile) { |
+} |
+ |
+PermissionObserver::~PermissionObserver() { |
+ if (!subscriptions_.IsEmpty()) |
+ profile_->GetHostContentSettingsMap()->RemoveObserver(this); |
+} |
+ |
+int PermissionObserver::Subscribe(ContentSettingsType content_type, |
+ const GURL& requesting_origin, |
+ const GURL& embedding_origin, |
+ const PermissionChangeCallback& callback) { |
+ if (subscriptions_.IsEmpty()) |
+ profile_->GetHostContentSettingsMap()->AddObserver(this); |
+ |
+ Subscription* subscription = new Subscription(); |
+ subscription->content_type = content_type; |
+ subscription->requesting_origin = requesting_origin; |
+ subscription->embedding_origin = embedding_origin; |
+ subscription->callback = callback; |
+ subscription->current_value = PermissionContext::Get(profile_, content_type) |
+ ->GetPermissionStatus(subscription->requesting_origin, |
+ subscription->embedding_origin); |
+ |
+ return subscriptions_.Add(subscription); |
+} |
+ |
+void PermissionObserver::Unsubscribe(int subscription_id) { |
+ // Whether |subscription_id| is known will be checked by the Remove() call. |
+ subscriptions_.Remove(subscription_id); |
+ |
+ if (subscriptions_.IsEmpty()) |
+ profile_->GetHostContentSettingsMap()->RemoveObserver(this); |
+} |
+ |
+void PermissionObserver::OnContentSettingChanged( |
+ const ContentSettingsPattern& primary_pattern, |
+ const ContentSettingsPattern& secondary_pattern, |
+ ContentSettingsType content_type, |
+ std::string resource_identifier) { |
+ for (SubscriptionsMap::iterator iter(&subscriptions_); |
+ !iter.IsAtEnd(); iter.Advance()) { |
+ Subscription* subscription = iter.GetCurrentValue(); |
+ if (subscription->content_type != content_type) |
+ continue; |
+ |
+ if (primary_pattern.IsValid() && |
+ !primary_pattern.Matches(subscription->requesting_origin)) |
+ continue; |
+ if (secondary_pattern.IsValid() && |
+ !secondary_pattern.Matches(subscription->embedding_origin)) |
+ continue; |
+ |
+ ContentSetting new_value = PermissionContext::Get(profile_, content_type) |
+ ->GetPermissionStatus(subscription->requesting_origin, |
+ subscription->embedding_origin); |
+ if (subscription->current_value == new_value) |
+ continue; |
+ |
+ subscription->current_value = new_value; |
+ subscription->callback.Run(new_value); |
+ } |
+} |