From 3e7cf136004dd8f5a8e521360b5d602e7b1af0c2 Mon Sep 17 00:00:00 2001 From: "idanan@chromium.org" Date: Fri, 21 Aug 2009 17:47:08 +0000 Subject: Send notice for blackisted non-visual resources This is just the piping to call into the blocked_popup_container (which shall be later renamed and expanded to a generalized blocked resource container) from the browser's request context. BUG=16932 TEST=none Review URL: http://codereview.chromium.org/171109 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@23973 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/app/generated_resources.grd | 6 ++ chrome/browser/blocked_popup_container.cc | 5 ++ chrome/browser/blocked_popup_container.h | 6 ++ chrome/browser/net/chrome_url_request_context.cc | 13 +++- .../privacy_blacklist/blacklist_observer.cc | 73 ++++++++++++++++++++++ .../browser/privacy_blacklist/blacklist_observer.h | 21 +++++++ .../renderer_host/render_view_host_delegate.cc | 4 ++ .../renderer_host/render_view_host_delegate.h | 3 + .../renderer_host/resource_message_filter.cc | 5 ++ chrome/browser/tab_contents/tab_contents.cc | 5 ++ chrome/browser/tab_contents/tab_contents.h | 1 + chrome/chrome.gyp | 2 + chrome/common/notification_type.h | 5 ++ 13 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 chrome/browser/privacy_blacklist/blacklist_observer.cc create mode 100644 chrome/browser/privacy_blacklist/blacklist_observer.h diff --git a/chrome/app/generated_resources.grd b/chrome/app/generated_resources.grd index 06b308b..d9354ca 100644 --- a/chrome/app/generated_resources.grd +++ b/chrome/app/generated_resources.grd @@ -4817,6 +4817,12 @@ each locale. --> Click to unblock + + Cookies not sent + + + Referrer not sent + diff --git a/chrome/browser/blocked_popup_container.cc b/chrome/browser/blocked_popup_container.cc index 13bd6a7..4df5bef 100644 --- a/chrome/browser/blocked_popup_container.cc +++ b/chrome/browser/blocked_popup_container.cc @@ -105,6 +105,11 @@ void BlockedPopupContainer::LaunchPopupAtIndex(size_t index) { EraseDataForPopupAndUpdateUI(i); } +void BlockedPopupContainer::AddBlockedNotice(const GURL& url, + const string16& reason) { + // TODO(idanan): Implement this. See header for description. +} + size_t BlockedPopupContainer::GetBlockedPopupCount() const { return blocked_popups_.size(); } diff --git a/chrome/browser/blocked_popup_container.h b/chrome/browser/blocked_popup_container.h index ebffd23..d7e5d0e 100644 --- a/chrome/browser/blocked_popup_container.h +++ b/chrome/browser/blocked_popup_container.h @@ -7,6 +7,8 @@ // TabContents should use the appropriate methods on TabContents to access // information about blocked popups. +// TODO(idanan): Rename class to BlockedContentContainer. + #ifndef CHROME_BROWSER_BLOCKED_POPUP_CONTAINER_H_ #define CHROME_BROWSER_BLOCKED_POPUP_CONTAINER_H_ @@ -17,6 +19,7 @@ #include "base/gfx/native_widget_types.h" #include "base/gfx/rect.h" +#include "base/string16.h" #include "chrome/browser/tab_contents/constrained_window.h" #include "chrome/browser/tab_contents/tab_contents_delegate.h" #include "chrome/common/notification_registrar.h" @@ -100,6 +103,9 @@ class BlockedPopupContainer : public TabContentsDelegate, // Returns the number of blocked popups size_t GetBlockedPopupCount() const; + // Adds a blocked notice if one is not already there for the same host. + void AddBlockedNotice(const GURL& url, const string16& reason); + // Returns true if host |index| is whitelisted. Returns false if |index| is // invalid. bool IsHostWhitelisted(size_t index) const; diff --git a/chrome/browser/net/chrome_url_request_context.cc b/chrome/browser/net/chrome_url_request_context.cc index 0aa4fc8..a36887c 100644 --- a/chrome/browser/net/chrome_url_request_context.cc +++ b/chrome/browser/net/chrome_url_request_context.cc @@ -450,6 +450,11 @@ bool ChromeURLRequestContext::InterceptCookie(const URLRequest* request, if (d) { const Blacklist::Match* match = static_cast(d); if (match->attributes() & Blacklist::kDontStoreCookies) { + NotificationService::current()->Notify( + NotificationType::BLACKLIST_BLOCKED_RESOURCE, + Source(this), + Details(request)); + cookie->clear(); return false; } @@ -466,8 +471,14 @@ bool ChromeURLRequestContext::AllowSendingCookies(const URLRequest* request) request->GetUserData(&Blacklist::kRequestDataKey); if (d) { const Blacklist::Match* match = static_cast(d); - if (match->attributes() & Blacklist::kDontSendCookies) + if (match->attributes() & Blacklist::kDontSendCookies) { + NotificationService::current()->Notify( + NotificationType::BLACKLIST_BLOCKED_RESOURCE, + Source(this), + Details(request)); + return false; + } } return true; } diff --git a/chrome/browser/privacy_blacklist/blacklist_observer.cc b/chrome/browser/privacy_blacklist/blacklist_observer.cc new file mode 100644 index 0000000..d93dbfe --- /dev/null +++ b/chrome/browser/privacy_blacklist/blacklist_observer.cc @@ -0,0 +1,73 @@ +// Copyright (c) 2009 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/privacy_blacklist/blacklist_observer.h" + +#include "app/l10n_util.h" +#include "app/resource_bundle.h" +#include "base/string16.h" +#include "chrome/browser/blocked_popup_container.h" +#include "chrome/browser/privacy_blacklist/blacklist.h" +#include "chrome/browser/renderer_host/render_view_host.h" +#include "chrome/browser/renderer_host/resource_dispatcher_host.h" +#include "chrome/browser/tab_contents/tab_contents.h" +#include "chrome/common/notification_details.h" +#include "chrome/common/notification_service.h" +#include "chrome/common/notification_type.h" +#include "grit/generated_resources.h" + +class BlockedContentNotice : public Task { + public: + BlockedContentNotice(const GURL& gurl, + const Blacklist::Match* match, + const ResourceDispatcherHost::ExtraRequestInfo* info) + : gurl_(gurl), + match_(match), + process_id_(info->process_id), + route_id_(info->route_id) { + if (match_->attributes() & Blacklist::kDontStoreCookies) { + // No cookies stored. + reason_ = l10n_util::GetStringUTF16(IDS_BLACKLIST_BLOCKED_COOKIES); + } else if (match_->attributes() & Blacklist::kDontSendCookies) { + // No cookies sent. + reason_ = l10n_util::GetStringUTF16(IDS_BLACKLIST_BLOCKED_COOKIES); + } else if (match_->attributes() & Blacklist::kDontSendReferrer) { + // No referrer sent. + reason_ = l10n_util::GetStringUTF16(IDS_BLACKLIST_BLOCKED_REFERRER); + } + } + + virtual void Run() { + RenderViewHost* view = RenderViewHost::FromID(process_id_, route_id_); + if (!view) + return; // The view may be gone by the time we get here. + + view->delegate()->AddBlockedNotice(gurl_, reason_); + } + + private: + const GURL gurl_; + const Blacklist::Match* match_; + const int process_id_; + const int route_id_; + + string16 reason_; +}; + +void BlacklistObserver::ContentBlocked(const URLRequest* request) { + const URLRequest::UserData* d = + request->GetUserData(&Blacklist::kRequestDataKey); + const Blacklist::Match* match = static_cast(d); + const ResourceDispatcherHost::ExtraRequestInfo* info = + static_cast( + request->GetUserData(NULL)); + const GURL& gurl = request->url(); + BlockedContentNotice* task = new BlockedContentNotice(gurl, match, info); + + // Notify the UI that something non-visual has been blocked. We can + // safely cast the delegate to the ResourceDispatherHost because it + // is the only place where Blacklist::Match data is added to requests. + static_cast(request->delegate())-> + ui_loop()->PostTask(FROM_HERE, task); +} diff --git a/chrome/browser/privacy_blacklist/blacklist_observer.h b/chrome/browser/privacy_blacklist/blacklist_observer.h new file mode 100644 index 0000000..196c658 --- /dev/null +++ b/chrome/browser/privacy_blacklist/blacklist_observer.h @@ -0,0 +1,21 @@ +// Copyright (c) 2009 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. + +#ifndef CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_OBSERVER_H_ +#define CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_OBSERVER_H_ + +#include "chrome/common/notification_details.h" +#include "chrome/common/notification_observer.h" +#include "chrome/common/notification_service.h" +#include "chrome/common/notification_type.h" + +class URLRequest; + +class BlacklistObserver { + public: + // Called when non-visual content is blocked by the privacy blacklist. + static void ContentBlocked(const URLRequest* request); +}; + +#endif // CHROME_BROWSER_PRIVACY_BLACKLIST_BLACKLIST_OBSERVER_H_ diff --git a/chrome/browser/renderer_host/render_view_host_delegate.cc b/chrome/browser/renderer_host/render_view_host_delegate.cc index 1afbdac..a97431c 100644 --- a/chrome/browser/renderer_host/render_view_host_delegate.cc +++ b/chrome/browser/renderer_host/render_view_host_delegate.cc @@ -55,6 +55,10 @@ TabContents* RenderViewHostDelegate::GetAsTabContents() { return NULL; } +void RenderViewHostDelegate::AddBlockedNotice(const GURL& url, + const string16& reason) { +} + GURL RenderViewHostDelegate::GetAlternateErrorPageURL() const { return GURL(); } diff --git a/chrome/browser/renderer_host/render_view_host_delegate.h b/chrome/browser/renderer_host/render_view_host_delegate.h index d9a7d9c..2727879 100644 --- a/chrome/browser/renderer_host/render_view_host_delegate.h +++ b/chrome/browser/renderer_host/render_view_host_delegate.h @@ -374,6 +374,9 @@ class RenderViewHostDelegate { // not a TabContents, returns NULL. virtual TabContents* GetAsTabContents(); + // Adds a notice that something was blocked. + virtual void AddBlockedNotice(const GURL& url, const string16& reason); + // Return id number of browser window which this object is attached to. If no // browser window is attached to, just return -1. virtual int GetBrowserWindowID() const = 0; diff --git a/chrome/browser/renderer_host/resource_message_filter.cc b/chrome/browser/renderer_host/resource_message_filter.cc index 7ba1a47..4ab489f 100644 --- a/chrome/browser/renderer_host/resource_message_filter.cc +++ b/chrome/browser/renderer_host/resource_message_filter.cc @@ -20,6 +20,7 @@ #include "chrome/browser/plugin_service.h" #include "chrome/browser/profile.h" #include "chrome/browser/privacy_blacklist/blacklist.h" +#include "chrome/browser/privacy_blacklist/blacklist_observer.h" #include "chrome/browser/renderer_host/audio_renderer_host.h" #include "chrome/browser/renderer_host/browser_render_process_host.h" #include "chrome/browser/renderer_host/database_dispatcher_host.h" @@ -203,6 +204,8 @@ void ResourceMessageFilter::OnFilterAdded(IPC::Channel* channel) { // Add the observers to intercept. registrar_.Add(this, NotificationType::SPELLCHECKER_REINITIALIZED, Source(static_cast(profile_))); + registrar_.Add(this, NotificationType::BLACKLIST_BLOCKED_RESOURCE, + NotificationService::AllSources()); } // Called on the IPC thread: @@ -880,6 +883,8 @@ void ResourceMessageFilter::Observe(NotificationType type, if (type == NotificationType::SPELLCHECKER_REINITIALIZED) { spellchecker_ = Details (details).ptr()->spellchecker; + } else if (type == NotificationType::BLACKLIST_BLOCKED_RESOURCE) { + BlacklistObserver::ContentBlocked(Details(details).ptr()); } } diff --git a/chrome/browser/tab_contents/tab_contents.cc b/chrome/browser/tab_contents/tab_contents.cc index 18880c1..3fdce1d 100644 --- a/chrome/browser/tab_contents/tab_contents.cc +++ b/chrome/browser/tab_contents/tab_contents.cc @@ -1805,6 +1805,11 @@ TabContents* TabContents::GetAsTabContents() { return this; } +void TabContents::AddBlockedNotice(const GURL& url, const string16& reason) { + CreateBlockedPopupContainerIfNecessary(); + blocked_popups_->AddBlockedNotice(url, reason); +} + ViewType::Type TabContents::GetRenderViewType() const { return ViewType::TAB_CONTENTS; } diff --git a/chrome/browser/tab_contents/tab_contents.h b/chrome/browser/tab_contents/tab_contents.h index 22a51c5..7e86dc4 100644 --- a/chrome/browser/tab_contents/tab_contents.h +++ b/chrome/browser/tab_contents/tab_contents.h @@ -806,6 +806,7 @@ class TabContents : public PageNavigator, virtual RenderViewHostDelegate::FavIcon* GetFavIconDelegate(); virtual RenderViewHostDelegate::Autofill* GetAutofillDelegate(); virtual TabContents* GetAsTabContents(); + virtual void AddBlockedNotice(const GURL& url, const string16& reason); virtual ViewType::Type GetRenderViewType() const; virtual int GetBrowserWindowID() const; virtual void RenderViewCreated(RenderViewHost* render_view_host); diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index a833360..0da33bb 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -1533,6 +1533,8 @@ 'browser/privacy_blacklist/blacklist.cc', 'browser/privacy_blacklist/blacklist_io.h', 'browser/privacy_blacklist/blacklist_io.cc', + 'browser/privacy_blacklist/blacklist_observer.h', + 'browser/privacy_blacklist/blacklist_observer.cc', 'browser/privacy_blacklist/blacklist_store.h', 'browser/privacy_blacklist/blacklist_store.cc', 'browser/privacy_blacklist/blocked_response.h', diff --git a/chrome/common/notification_type.h b/chrome/common/notification_type.h index 8d98578..8cde6bf 100644 --- a/chrome/common/notification_type.h +++ b/chrome/common/notification_type.h @@ -650,6 +650,11 @@ class NotificationType { EXTENSION_TEST_PASSED, EXTENSION_TEST_FAILED, + // Privacy Blacklist ------------------------------------------------------- + + // Sent by the resource dispatcher host when a resource is blocked. + BLACKLIST_BLOCKED_RESOURCE, + // Debugging --------------------------------------------------------------- // Count (must be last) ---------------------------------------------------- -- cgit v1.1