diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-08 21:03:10 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-08 21:03:10 +0000 |
commit | f364d139a49933c7212d521801fec68d1b229487 (patch) | |
tree | 7c4fe037e5a083276962d668369dff201eeaf612 /content | |
parent | 5bba4dd3a7ad928f6c8a58aea8d716605eec285a (diff) | |
download | chromium_src-f364d139a49933c7212d521801fec68d1b229487.zip chromium_src-f364d139a49933c7212d521801fec68d1b229487.tar.gz chromium_src-f364d139a49933c7212d521801fec68d1b229487.tar.bz2 |
Relanding http://src.chromium.org/viewvc/chrome?view=rev&revision=80938.
Add a new RenderViewHostObserver interface for filtering IPCs send to RenderViewHost. This is needed because with TabContentsObserver, an observer might not know which RenderViewHost a message came from duing a pending navigation. A side-benefit is that we only have to add these observers once, instead of remembering/knowing all the delegates that might want to filter these messages.
BUG=78629
Review URL: http://codereview.chromium.org/6813046
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@80984 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
-rw-r--r-- | content/browser/content_browser_client.cc | 9 | ||||
-rw-r--r-- | content/browser/content_browser_client.h | 5 | ||||
-rw-r--r-- | content/browser/renderer_host/render_view_host.cc | 21 | ||||
-rw-r--r-- | content/browser/renderer_host/render_view_host.h | 12 | ||||
-rw-r--r-- | content/browser/renderer_host/render_view_host_delegate.cc | 5 | ||||
-rw-r--r-- | content/browser/renderer_host/render_view_host_delegate.h | 6 | ||||
-rw-r--r-- | content/browser/renderer_host/render_view_host_observer.cc | 41 | ||||
-rw-r--r-- | content/browser/renderer_host/render_view_host_observer.h | 51 | ||||
-rw-r--r-- | content/browser/tab_contents/tab_contents.cc | 13 | ||||
-rw-r--r-- | content/browser/tab_contents/tab_contents.h | 6 | ||||
-rw-r--r-- | content/content_browser.gypi | 2 |
11 files changed, 163 insertions, 8 deletions
diff --git a/content/browser/content_browser_client.cc b/content/browser/content_browser_client.cc index 5b763c2b..57392a1 100644 --- a/content/browser/content_browser_client.cc +++ b/content/browser/content_browser_client.cc @@ -10,6 +10,15 @@ namespace content { +void ContentBrowserClient::RenderViewHostCreated( + RenderViewHost* render_view_host) { +} + +void ContentBrowserClient::PreCreateRenderView(RenderViewHost* render_view_host, + Profile* profile, + const GURL& url) { +} + WebUIFactory* ContentBrowserClient::GetWebUIFactory() { // Return an empty factory so callsites don't have to check for NULL. return EmptyWebUIFactory::Get(); diff --git a/content/browser/content_browser_client.h b/content/browser/content_browser_client.h index e8350e6..1f3c7ca 100644 --- a/content/browser/content_browser_client.h +++ b/content/browser/content_browser_client.h @@ -19,10 +19,13 @@ class WebUIFactory; // Embedder API for participating in browser logic. class ContentBrowserClient { public: + // Notifies that a new RenderHostView has been created. + virtual void RenderViewHostCreated(RenderViewHost* render_view_host); + // Initialize a RenderViewHost before its CreateRenderView method is called. virtual void PreCreateRenderView(RenderViewHost* render_view_host, Profile* profile, - const GURL& url) {} + const GURL& url); // Gets the WebUIFactory which will be responsible for generating WebUIs. virtual WebUIFactory* GetWebUIFactory(); diff --git a/content/browser/renderer_host/render_view_host.cc b/content/browser/renderer_host/render_view_host.cc index 3e6ef59..17c5338 100644 --- a/content/browser/renderer_host/render_view_host.cc +++ b/content/browser/renderer_host/render_view_host.cc @@ -30,10 +30,12 @@ #include "chrome/common/translate_errors.h" #include "chrome/common/url_constants.h" #include "content/browser/child_process_security_policy.h" +#include "content/browser/content_browser_client.h" #include "content/browser/cross_site_request_manager.h" #include "content/browser/in_process_webkit/session_storage_namespace.h" #include "content/browser/renderer_host/render_process_host.h" #include "content/browser/renderer_host/render_view_host_delegate.h" +#include "content/browser/renderer_host/render_view_host_observer.h" #include "content/browser/renderer_host/render_widget_host.h" #include "content/browser/renderer_host/render_widget_host_view.h" #include "content/browser/site_instance.h" @@ -115,9 +117,14 @@ RenderViewHost::RenderViewHost(SiteInstance* instance, DCHECK(instance_); DCHECK(delegate_); + + content::GetContentClient()->browser()->RenderViewHostCreated(this); } RenderViewHost::~RenderViewHost() { + FOR_EACH_OBSERVER( + RenderViewHostObserver, observers_, RenderViewHostDestruction()); + NotificationService::current()->Notify( NotificationType::RENDER_VIEW_HOST_DELETED, Source<RenderViewHost>(this), @@ -712,6 +719,12 @@ bool RenderViewHost::OnMessageReceived(const IPC::Message& msg) { } #endif + ObserverListBase<RenderViewHostObserver>::Iterator it(observers_); + RenderViewHostObserver* observer; + while ((observer = it.GetNext()) != NULL) + if (observer->OnMessageReceived(msg)) + return true; + if (delegate_->OnMessageReceived(msg)) return true; @@ -1225,6 +1238,14 @@ void RenderViewHost::OnAddMessageToConsole(const std::wstring& message, << "\", source: " << source_id << " (" << line_no << ")"; } +void RenderViewHost::AddObserver(RenderViewHostObserver* observer) { + observers_.AddObserver(observer); +} + +void RenderViewHost::RemoveObserver(RenderViewHostObserver* observer) { + observers_.RemoveObserver(observer); +} + bool RenderViewHost::PreHandleKeyboardEvent( const NativeWebKeyboardEvent& event, bool* is_keyboard_shortcut) { RenderViewHostDelegate::View* view = delegate_->GetViewDelegate(); diff --git a/content/browser/renderer_host/render_view_host.h b/content/browser/renderer_host/render_view_host.h index 0304ec9..88dac2a 100644 --- a/content/browser/renderer_host/render_view_host.h +++ b/content/browser/renderer_host/render_view_host.h @@ -10,6 +10,7 @@ #include <vector> #include "base/memory/scoped_ptr.h" +#include "base/observer_list.h" #include "base/process_util.h" #include "chrome/browser/ui/find_bar/find_bar_controller.h" #include "chrome/common/content_settings_types.h" @@ -32,6 +33,7 @@ class FilePath; class GURL; class ListValue; class RenderViewHostDelegate; +class RenderViewHostObserver; class SessionStorageNamespace; class SiteInstance; class SkBitmap; @@ -499,6 +501,13 @@ class RenderViewHost : public RenderWidgetHost { GURL* url); protected: + friend class RenderViewHostObserver; + + // Add and remove observers for filtering IPC messages. Clients must be sure + // to remove the observer before they go away. + void AddObserver(RenderViewHostObserver* observer); + void RemoveObserver(RenderViewHostObserver* observer); + // RenderWidgetHost protected overrides. virtual bool PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event, bool* is_keyboard_shortcut); @@ -682,6 +691,9 @@ class RenderViewHost : public RenderWidgetHost { // The enabled/disabled states of various commands. std::map<RenderViewCommand, CommandState> command_states_; + // A list of observers that filter messages. Weak references. + ObserverList<RenderViewHostObserver> observers_; + DISALLOW_COPY_AND_ASSIGN(RenderViewHost); }; diff --git a/content/browser/renderer_host/render_view_host_delegate.cc b/content/browser/renderer_host/render_view_host_delegate.cc index 2d56531..bd1f5bd 100644 --- a/content/browser/renderer_host/render_view_host_delegate.cc +++ b/content/browser/renderer_host/render_view_host_delegate.cc @@ -71,3 +71,8 @@ WebPreferences RenderViewHostDelegate::GetWebkitPrefs() { bool RenderViewHostDelegate::IsExternalTabContainer() const { return false; } + +bool RenderViewHostDelegate::RequestDesktopNotificationPermission( + const GURL& source_origin, int callback_context) { + return false; +} diff --git a/content/browser/renderer_host/render_view_host_delegate.h b/content/browser/renderer_host/render_view_host_delegate.h index 3fcc384..f4f0783 100644 --- a/content/browser/renderer_host/render_view_host_delegate.h +++ b/content/browser/renderer_host/render_view_host_delegate.h @@ -567,6 +567,12 @@ class RenderViewHostDelegate : public IPC::Channel::Listener { // Notification that a worker process has crashed. void WorkerCrashed() {} + // Ask the user if they want to allow the view to show desktop notifications. + // Returns true if the delegate will take care of asking the user, otherwise + // the caller will do the default behavior. + bool RequestDesktopNotificationPermission(const GURL& source_origin, + int callback_context); + protected: virtual ~RenderViewHostDelegate() {} }; diff --git a/content/browser/renderer_host/render_view_host_observer.cc b/content/browser/renderer_host/render_view_host_observer.cc new file mode 100644 index 0000000..e60e2bb --- /dev/null +++ b/content/browser/renderer_host/render_view_host_observer.cc @@ -0,0 +1,41 @@ +// Copyright (c) 2011 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 "content/browser/renderer_host/render_view_host_observer.h" + +#include "content/browser/renderer_host/render_view_host.h" + +RenderViewHostObserver::RenderViewHostObserver(RenderViewHost* render_view_host) + : render_view_host_(render_view_host), + routing_id_(render_view_host->routing_id()) { + render_view_host_->AddObserver(this); +} + +RenderViewHostObserver::~RenderViewHostObserver() { + if (render_view_host_) + render_view_host_->RemoveObserver(this); +} + +void RenderViewHostObserver::RenderViewHostDestroyed() { + delete this; +} + +bool RenderViewHostObserver::OnMessageReceived(const IPC::Message& message) { + return false; +} + +bool RenderViewHostObserver::Send(IPC::Message* message) { + if (!render_view_host_) { + delete message; + return false; + } + + return render_view_host_->Send(message); +} + +void RenderViewHostObserver::RenderViewHostDestruction() { + render_view_host_->RemoveObserver(this); + RenderViewHostDestroyed(); + render_view_host_ = NULL; +} diff --git a/content/browser/renderer_host/render_view_host_observer.h b/content/browser/renderer_host/render_view_host_observer.h new file mode 100644 index 0000000..2f63935 --- /dev/null +++ b/content/browser/renderer_host/render_view_host_observer.h @@ -0,0 +1,51 @@ +// Copyright (c) 2011 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 CONTENT_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_OBSERVER_H_ +#define CONTENT_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_OBSERVER_H_ + +#include "ipc/ipc_channel.h" + +class RenderViewHost; + +// An observer API implemented by classes which want to filter IPC messages from +// RenderViewHost. +class RenderViewHostObserver : public IPC::Channel::Listener, + public IPC::Message::Sender { + public: + + protected: + explicit RenderViewHostObserver(RenderViewHost* render_view_host); + + virtual ~RenderViewHostObserver(); + + // Invoked when the RenderViewHost is being destroyed. Gives subclasses a + // chance to cleanup. The base implementation will delete the object. + virtual void RenderViewHostDestroyed(); + + // IPC::Channel::Listener implementation. + virtual bool OnMessageReceived(const IPC::Message& message); + + // IPC::Message::Sender implementation. + virtual bool Send(IPC::Message* message); + + RenderViewHost* render_view_host() const { return render_view_host_; } + int routing_id() { return routing_id_; } + + private: + friend class RenderViewHost; + + // Invoked from RenderViewHost. Invokes RenderViewHostDestroyed and NULL out + // |render_view_host_|. + void RenderViewHostDestruction(); + + RenderViewHost* render_view_host_; + + // The routing ID of the associated RenderViewHost. + int routing_id_; + + DISALLOW_COPY_AND_ASSIGN(RenderViewHostObserver); +}; + +#endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_OBSERVER_H_ diff --git a/content/browser/tab_contents/tab_contents.cc b/content/browser/tab_contents/tab_contents.cc index 110acf5..a15e6fe 100644 --- a/content/browser/tab_contents/tab_contents.cc +++ b/content/browser/tab_contents/tab_contents.cc @@ -22,7 +22,6 @@ #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/debugger/devtools_manager.h" #include "chrome/browser/defaults.h" -#include "chrome/browser/desktop_notification_handler.h" #include "chrome/browser/dom_operation_notification_details.h" #include "chrome/browser/download/download_item_model.h" #include "chrome/browser/download/download_manager.h" @@ -36,6 +35,7 @@ #include "chrome/browser/load_notification_details.h" #include "chrome/browser/metrics/metric_event_duration_details.h" #include "chrome/browser/metrics/user_metrics.h" +#include "chrome/browser/notifications/desktop_notification_service.h" #include "chrome/browser/omnibox_search_hint.h" #include "chrome/browser/pdf_unsupported_feature.h" #include "chrome/browser/platform_util.h" @@ -372,8 +372,6 @@ TabContents::~TabContents() { void TabContents::AddObservers() { favicon_helper_.reset(new FaviconHelper(this)); - desktop_notification_handler_.reset( - new DesktopNotificationHandlerForTC(this, GetRenderProcessHost())); plugin_observer_.reset(new PluginObserver(this)); safebrowsing_detection_host_.reset(new safe_browsing::ClientSideDetectionHost( this)); @@ -2395,6 +2393,15 @@ void TabContents::WorkerCrashed() { delegate()->WorkerCrashed(); } +void TabContents::RequestDesktopNotificationPermission( + const GURL& source_origin, int callback_context) { + DesktopNotificationService* service = + profile()->GetDesktopNotificationService(); + service->RequestPermission( + source_origin, GetRenderProcessHost()->id(), + render_view_host()->routing_id(), callback_context, this); +} + void TabContents::BeforeUnloadFiredFromRenderManager( bool proceed, bool* proceed_to_fire_unload) { diff --git a/content/browser/tab_contents/tab_contents.h b/content/browser/tab_contents/tab_contents.h index d1ac0b9..4f62795d 100644 --- a/content/browser/tab_contents/tab_contents.h +++ b/content/browser/tab_contents/tab_contents.h @@ -59,7 +59,6 @@ class ClientSideDetectionHost; class BlockedContentContainer; class WebUI; -class DesktopNotificationHandlerForTC; class DownloadItem; class Extension; class InfoBarDelegate; @@ -881,6 +880,8 @@ class TabContents : public PageNavigator, int maximum_percent, bool remember); virtual void WorkerCrashed(); + virtual void RequestDesktopNotificationPermission(const GURL& source_origin, + int callback_context); // RenderViewHostManager::Delegate ------------------------------------------- @@ -964,9 +965,6 @@ class TabContents : public PageNavigator, // RenderViewHost::ContentSettingsDelegate. scoped_ptr<TabSpecificContentSettings> content_settings_delegate_; - // Handles desktop notification IPCs. - scoped_ptr<DesktopNotificationHandlerForTC> desktop_notification_handler_; - // Handles IPCs related to SafeBrowsing client-side phishing detection. scoped_ptr<safe_browsing::ClientSideDetectionHost> safebrowsing_detection_host_; diff --git a/content/content_browser.gypi b/content/content_browser.gypi index de3cf83..f1df8aa 100644 --- a/content/content_browser.gypi +++ b/content/content_browser.gypi @@ -234,6 +234,8 @@ 'browser/renderer_host/render_view_host_factory.cc', 'browser/renderer_host/render_view_host_factory.h', 'browser/renderer_host/render_view_host_notification_task.h', + 'browser/renderer_host/render_view_host_observer.cc', + 'browser/renderer_host/render_view_host_observer.h', 'browser/renderer_host/render_widget_fullscreen_host.cc', 'browser/renderer_host/render_widget_fullscreen_host.h', 'browser/renderer_host/render_widget_helper.cc', |