diff options
author | joi@chromium.org <joi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-16 12:59:18 +0000 |
---|---|---|
committer | joi@chromium.org <joi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-16 12:59:18 +0000 |
commit | 6b618e62ab66de3e8a923368d2a41b078218d0ed (patch) | |
tree | 42145ad803d108116aee4915853d88334392a594 /content | |
parent | 414c90e876a361a7233afe03b1cebf208a733a74 (diff) | |
download | chromium_src-6b618e62ab66de3e8a923368d2a41b078218d0ed.zip chromium_src-6b618e62ab66de3e8a923368d2a41b078218d0ed.tar.gz chromium_src-6b618e62ab66de3e8a923368d2a41b078218d0ed.tar.bz2 |
Add IPC::Sender and GetRoutingID() to WebContents for convenience and safety.
Code that is or has a WebContentsObserver can already use safe
versions of Send() and GetRoutingID() that perform NULL checks and
delete the Message object and return MSG_ROUTING_NONE when
appropriate. This brings the same thing to code that has just a
WebContents.
BUG=none
Review URL: https://chromiumcodereview.appspot.com/10830337
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@151880 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
-rw-r--r-- | content/browser/web_contents/web_contents_impl.cc | 40 | ||||
-rw-r--r-- | content/browser/web_contents/web_contents_impl.h | 4 | ||||
-rw-r--r-- | content/public/browser/web_contents.h | 7 | ||||
-rw-r--r-- | content/public/browser/web_contents_observer.cc | 8 |
4 files changed, 40 insertions, 19 deletions
diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc index 32ff95d..cee3043 100644 --- a/content/browser/web_contents/web_contents_impl.cc +++ b/content/browser/web_contents/web_contents_impl.cc @@ -751,6 +751,13 @@ RenderViewHost* WebContentsImpl::GetRenderViewHost() const { return render_manager_.current_host(); } +int WebContentsImpl::GetRoutingID() const { + if (!GetRenderViewHost()) + return MSG_ROUTING_NONE; + + return GetRenderViewHost()->GetRoutingID(); +} + RenderWidgetHostView* WebContentsImpl::GetRenderWidgetHostView() const { return render_manager_.GetRenderWidgetHostView(); } @@ -1449,6 +1456,15 @@ WebContents* WebContentsImpl::OpenURL(const OpenURLParams& params) { return new_contents; } +bool WebContentsImpl::Send(IPC::Message* message) { + if (!GetRenderViewHost()) { + delete message; + return false; + } + + return GetRenderViewHost()->Send(message); +} + bool WebContentsImpl::NavigateToPendingEntry( NavigationController::ReloadType reload_type) { return NavigateToEntry( @@ -1559,9 +1575,9 @@ void WebContentsImpl::SetHistoryLengthAndPrune( NOTREACHED(); return; } - rvh->Send(new ViewMsg_SetHistoryLengthAndPrune(rvh->GetRoutingID(), - history_length, - minimum_page_id)); + Send(new ViewMsg_SetHistoryLengthAndPrune(GetRoutingID(), + history_length, + minimum_page_id)); } void WebContentsImpl::FocusThroughTabTraversal(bool reverse) { @@ -1646,14 +1662,12 @@ bool WebContentsImpl::WillNotifyDisconnection() const { void WebContentsImpl::SetOverrideEncoding(const std::string& encoding) { SetEncoding(encoding); - GetRenderViewHostImpl()->Send(new ViewMsg_SetPageEncoding( - GetRenderViewHost()->GetRoutingID(), encoding)); + Send(new ViewMsg_SetPageEncoding(GetRoutingID(), encoding)); } void WebContentsImpl::ResetOverrideEncoding() { encoding_.clear(); - GetRenderViewHostImpl()->Send(new ViewMsg_ResetPageEncodingToDefault( - GetRenderViewHost()->GetRoutingID())); + Send(new ViewMsg_ResetPageEncodingToDefault(GetRoutingID())); } content::RendererPreferences* WebContentsImpl::GetMutableRendererPrefs() { @@ -1835,13 +1849,12 @@ bool WebContentsImpl::HasOpener() const { void WebContentsImpl::DidChooseColorInColorChooser(int color_chooser_id, SkColor color) { - GetRenderViewHost()->Send(new ViewMsg_DidChooseColorResponse( - GetRenderViewHost()->GetRoutingID(), color_chooser_id, color)); + Send(new ViewMsg_DidChooseColorResponse( + GetRoutingID(), color_chooser_id, color)); } void WebContentsImpl::DidEndColorChooser(int color_chooser_id) { - GetRenderViewHost()->Send(new ViewMsg_DidEndColorChooser( - GetRenderViewHost()->GetRoutingID(), color_chooser_id)); + Send(new ViewMsg_DidEndColorChooser(GetRoutingID(), color_chooser_id)); if (delegate_) delegate_->DidEndColorChooser(); color_chooser_ = NULL; @@ -2452,7 +2465,7 @@ void WebContentsImpl::RenderViewCreated(RenderViewHost* render_view_host) { if (entry->IsViewSourceMode()) { // Put the renderer in view source mode. - static_cast<RenderViewHostImpl*>(render_view_host)->Send( + render_view_host->Send( new ViewMsg_EnableViewSourceMode(render_view_host->GetRoutingID())); } @@ -2846,8 +2859,7 @@ void WebContentsImpl::RouteMessageEvent( // In most cases, we receive this from a swapped out RenderViewHost. // It is possible to receive it from one that has just been swapped in, // in which case we might as well deliver the message anyway. - GetRenderViewHost()->Send(new ViewMsg_PostMessageEvent( - GetRenderViewHost()->GetRoutingID(), new_params)); + Send(new ViewMsg_PostMessageEvent(GetRoutingID(), new_params)); } void WebContentsImpl::RunJavaScriptMessage( diff --git a/content/browser/web_contents/web_contents_impl.h b/content/browser/web_contents/web_contents_impl.h index 8d55043..b9320ae 100644 --- a/content/browser/web_contents/web_contents_impl.h +++ b/content/browser/web_contents/web_contents_impl.h @@ -172,6 +172,7 @@ class CONTENT_EXPORT WebContentsImpl virtual content::BrowserContext* GetBrowserContext() const OVERRIDE; virtual content::RenderProcessHost* GetRenderProcessHost() const OVERRIDE; virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE; + virtual int GetRoutingID() const OVERRIDE; virtual content::RenderWidgetHostView* GetRenderWidgetHostView() const OVERRIDE; virtual content::WebContentsView* GetView() const OVERRIDE; @@ -264,6 +265,9 @@ class CONTENT_EXPORT WebContentsImpl virtual content::WebContents* OpenURL( const content::OpenURLParams& params) OVERRIDE; + // Implementation of IPC::Sender. + virtual bool Send(IPC::Message* message) OVERRIDE; + // RenderViewHostDelegate ---------------------------------------------------- virtual content::RenderViewHostDelegateView* GetDelegateView() OVERRIDE; diff --git a/content/public/browser/web_contents.h b/content/public/browser/web_contents.h index c6f55f5..d68e148 100644 --- a/content/public/browser/web_contents.h +++ b/content/public/browser/web_contents.h @@ -14,6 +14,7 @@ #include "content/public/browser/page_navigator.h" #include "content/public/browser/save_page_type.h" #include "content/public/browser/web_ui.h" +#include "ipc/ipc_sender.h" #include "third_party/skia/include/core/SkColor.h" #include "ui/gfx/native_widget_types.h" #include "webkit/glue/window_open_disposition.h" @@ -45,7 +46,7 @@ class WebContentsView; struct RendererPreferences; // Describes what goes in the main content area of a tab. -class WebContents : public PageNavigator { +class WebContents : public PageNavigator, public IPC::Sender { public: // |base_web_contents| is used if we want to size the new WebContents's view // based on the view of an existing WebContents. This can be NULL if not @@ -110,6 +111,10 @@ class WebContents : public PageNavigator { // Gets the current RenderViewHost for this tab. virtual RenderViewHost* GetRenderViewHost() const = 0; + // Gets the current RenderViewHost's routing id. Returns + // MSG_ROUTING_NONE when there is no RenderViewHost. + virtual int GetRoutingID() const = 0; + // Returns the currently active RenderWidgetHostView. This may change over // time and can be NULL (during setup and teardown). virtual content::RenderWidgetHostView* GetRenderWidgetHostView() const = 0; diff --git a/content/public/browser/web_contents_observer.cc b/content/public/browser/web_contents_observer.cc index 07b4592..1479029 100644 --- a/content/public/browser/web_contents_observer.cc +++ b/content/public/browser/web_contents_observer.cc @@ -42,19 +42,19 @@ bool WebContentsObserver::OnMessageReceived(const IPC::Message& message) { } bool WebContentsObserver::Send(IPC::Message* message) { - if (!web_contents_ || !web_contents_->GetRenderViewHost()) { + if (!web_contents_) { delete message; return false; } - return web_contents_->GetRenderViewHost()->Send(message); + return web_contents_->Send(message); } int WebContentsObserver::routing_id() const { - if (!web_contents_ || !web_contents_->GetRenderViewHost()) + if (!web_contents_) return MSG_ROUTING_NONE; - return web_contents_->GetRenderViewHost()->GetRoutingID(); + return web_contents_->GetRoutingID(); } void WebContentsObserver::WebContentsImplDestroyed() { |