diff options
author | johnnyg@chromium.org <johnnyg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-23 23:39:09 +0000 |
---|---|---|
committer | johnnyg@chromium.org <johnnyg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-23 23:39:09 +0000 |
commit | 9e9e55c35954a06b68d5e62a27fa1ea60675d6f2 (patch) | |
tree | aa031fb90182ccd30df76f9d7947c36799ec611d /chrome/browser/notifications | |
parent | 475b996b15bd1010869876e6d70e4c79bc1fea05 (diff) | |
download | chromium_src-9e9e55c35954a06b68d5e62a27fa1ea60675d6f2.zip chromium_src-9e9e55c35954a06b68d5e62a27fa1ea60675d6f2.tar.gz chromium_src-9e9e55c35954a06b68d5e62a27fa1ea60675d6f2.tar.bz2 |
Refactor BalloonViewHost, removing a lot of duplicate code that crept into notifications during the port to mac & linux and making behavior more consistent.
BUG=34826
TEST=notifications on each platform.
Review URL: http://codereview.chromium.org/1055005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42404 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/notifications')
-rw-r--r-- | chrome/browser/notifications/balloon_host.cc | 131 | ||||
-rw-r--r-- | chrome/browser/notifications/balloon_host.h | 116 |
2 files changed, 247 insertions, 0 deletions
diff --git a/chrome/browser/notifications/balloon_host.cc b/chrome/browser/notifications/balloon_host.cc new file mode 100644 index 0000000..3b4a832 --- /dev/null +++ b/chrome/browser/notifications/balloon_host.cc @@ -0,0 +1,131 @@ +// Copyright (c) 2010 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/notifications/balloon_host.h" + +#include "chrome/browser/browser_list.h" +#include "chrome/browser/in_process_webkit/webkit_context.h" +#include "chrome/browser/extensions/extension_process_manager.h" +#include "chrome/browser/notifications/balloon.h" +#include "chrome/browser/profile.h" +#include "chrome/browser/renderer_host/render_view_host.h" +#include "chrome/browser/renderer_host/site_instance.h" +#include "chrome/browser/renderer_preferences_util.h" +#include "chrome/common/bindings_policy.h" +#include "chrome/common/notification_service.h" +#include "chrome/common/notification_type.h" +#include "chrome/common/render_messages.h" +#include "chrome/common/renderer_preferences.h" +#include "chrome/common/url_constants.h" + +BalloonHost::BalloonHost(Balloon* balloon) + : render_view_host_(NULL), + balloon_(balloon), + initialized_(false), + should_notify_on_disconnect_(false), + is_extension_page_(false) { + DCHECK(balloon_); + + // If the notification is for an extension URL, make sure to use the extension + // process to render it, so that it can communicate with other views in the + // extension. + const GURL& balloon_url = balloon_->notification().content_url(); + if (balloon_url.SchemeIs(chrome::kExtensionScheme)) { + is_extension_page_ = true; + site_instance_ = + balloon_->profile()->GetExtensionProcessManager()->GetSiteInstanceForURL( + balloon_url); + } else { + site_instance_ = SiteInstance::CreateSiteInstance(balloon_->profile()); + } +} + +void BalloonHost::Shutdown() { + if (render_view_host_) { + render_view_host_->Shutdown(); + render_view_host_ = NULL; + } +} + +WebPreferences BalloonHost::GetWebkitPrefs() { + WebPreferences prefs; + prefs.allow_scripts_to_close_windows = true; + return prefs; +} + +void BalloonHost::Close(RenderViewHost* render_view_host) { + balloon_->CloseByScript(); +} + + +void BalloonHost::RenderViewCreated(RenderViewHost* render_view_host) { + render_view_host->Send(new ViewMsg_EnablePreferredSizeChangedMode( + render_view_host->routing_id())); +} + +void BalloonHost::RendererReady(RenderViewHost* render_view_host) { + should_notify_on_disconnect_ = true; + NotificationService::current()->Notify( + NotificationType::NOTIFY_BALLOON_CONNECTED, + Source<Balloon>(balloon_), NotificationService::NoDetails()); +} + +void BalloonHost::RendererGone(RenderViewHost* render_view_host) { + if (!should_notify_on_disconnect_) + return; + + should_notify_on_disconnect_ = false; + NotificationService::current()->Notify( + NotificationType::NOTIFY_BALLOON_DISCONNECTED, + Source<Balloon>(balloon_), NotificationService::NoDetails()); +} + +// RenderViewHostDelegate::View methods implemented to allow links to +// open pages in new tabs. +void BalloonHost::CreateNewWindow(int route_id) { + delegate_view_helper_.CreateNewWindow( + route_id, balloon_->profile(), site_instance_.get(), + DOMUIFactory::GetDOMUIType(balloon_->notification().content_url()), NULL); +} + +void BalloonHost::ShowCreatedWindow(int route_id, + WindowOpenDisposition disposition, + const gfx::Rect& initial_pos, + bool user_gesture) { + // Don't allow pop-ups from notifications. + if (disposition == NEW_POPUP) + return; + + TabContents* contents = delegate_view_helper_.GetCreatedWindow(route_id); + if (contents) { + Browser* browser = BrowserList::GetLastActive(); + browser->AddTabContents(contents, disposition, initial_pos, user_gesture); + } +} + +void BalloonHost::UpdatePreferredSize(const gfx::Size& new_size) { + balloon_->SetContentPreferredSize(new_size); +} + +void BalloonHost::Init() { + DCHECK(!render_view_host_) << "BalloonViewHost already initialized."; + int64 session_storage_namespace_id = balloon_->profile()->GetWebKitContext()-> + dom_storage_context()->AllocateSessionStorageNamespaceId(); + RenderViewHost* rvh = new RenderViewHost(site_instance_.get(), + this, MSG_ROUTING_NONE, + session_storage_namespace_id); + if (is_extension_page_) + rvh->AllowBindings(BindingsPolicy::EXTENSION); + + // Do platform-specific initialization. + render_view_host_ = rvh; + InitRenderWidgetHostView(); + DCHECK(render_widget_host_view()); + + rvh->set_view(render_widget_host_view()); + rvh->CreateRenderView(GetProfile()->GetRequestContext()); + rvh->NavigateToURL(balloon_->notification().content_url()); + + initialized_ = true; +} diff --git a/chrome/browser/notifications/balloon_host.h b/chrome/browser/notifications/balloon_host.h new file mode 100644 index 0000000..b42ea46 --- /dev/null +++ b/chrome/browser/notifications/balloon_host.h @@ -0,0 +1,116 @@ +// Copyright (c) 2010 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_NOTIFICATIONS_BALLOON_HOST_H_ +#define CHROME_BROWSER_NOTIFICATIONS_BALLOON_HOST_H_ + +#include "chrome/browser/notifications/balloon.h" +#include "chrome/browser/notifications/notification.h" +#include "chrome/browser/renderer_host/render_view_host_delegate.h" +#include "chrome/browser/renderer_host/site_instance.h" +#include "chrome/browser/tab_contents/render_view_host_delegate_helper.h" +#include "chrome/common/renderer_preferences.h" +#include "webkit/glue/webpreferences.h" + +class Profile; + +class BalloonHost : public RenderViewHostDelegate, + public RenderViewHostDelegate::View { + public: + explicit BalloonHost(Balloon* balloon); + + // Initialize the view. + void Init(); + + // Stops showing the balloon. + void Shutdown(); + + RenderViewHost* render_view_host() const { return render_view_host_; } + + // RenderViewHostDelegate overrides. + virtual WebPreferences GetWebkitPrefs(); + virtual SiteInstance* GetSiteInstance() const { + return site_instance_.get(); + } + virtual Profile* GetProfile() const { return balloon_->profile(); } + virtual const GURL& GetURL() const { + return balloon_->notification().content_url(); + } + virtual void Close(RenderViewHost* render_view_host); + virtual void RenderViewCreated(RenderViewHost* render_view_host); + virtual void RendererReady(RenderViewHost* render_view_host); + virtual void RendererGone(RenderViewHost* render_view_host); + virtual void UpdateTitle(RenderViewHost* render_view_host, + int32 page_id, const std::wstring& title) {} + virtual int GetBrowserWindowID() const { return -1; } + virtual ViewType::Type GetRenderViewType() const { + return ViewType::TAB_CONTENTS; + } + virtual RenderViewHostDelegate::View* GetViewDelegate() { + return this; + } + + // RenderViewHostDelegate::View methods. Only the ones for opening new + // windows are currently implemented. + virtual void CreateNewWindow(int route_id); + virtual void CreateNewWidget(int route_id, bool activatable) {} + virtual void ShowCreatedWindow(int route_id, + WindowOpenDisposition disposition, + const gfx::Rect& initial_pos, + bool user_gesture); + virtual void ShowCreatedWidget(int route_id, + const gfx::Rect& initial_pos) {} + virtual void ShowContextMenu(const ContextMenuParams& params) {} + virtual void StartDragging(const WebDropData& drop_data, + WebKit::WebDragOperationsMask allowed_ops) {} + virtual void StartDragging(const WebDropData&, + WebKit::WebDragOperationsMask, + const SkBitmap&, + const gfx::Point&) {} + virtual void UpdateDragCursor(WebKit::WebDragOperation operation) {} + virtual void GotFocus() {} + virtual void TakeFocus(bool reverse) {} + virtual bool PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event, + bool* is_keyboard_shortcut) { + return false; + } + virtual void HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {} + virtual void HandleMouseEvent() {} + virtual void HandleMouseLeave() {} + virtual void UpdatePreferredSize(const gfx::Size& pref_size); + virtual RendererPreferences GetRendererPrefs(Profile* profile) const { + return RendererPreferences(); + } + + protected: + // Must override in platform specific implementations. + virtual void InitRenderWidgetHostView() = 0; + virtual RenderWidgetHostView* render_widget_host_view() const = 0; + + // Owned pointer to the host for the renderer process. + RenderViewHost* render_view_host_; + + private: + // Non-owned pointer to the associated balloon. + Balloon* balloon_; + + // True after Init() has completed. + bool initialized_; + + // Indicates whether we should notify about disconnection of this balloon. + // This is used to ensure disconnection notifications only happen if + // a connection notification has happened and that they happen only once. + bool should_notify_on_disconnect_; + + // Whether the page we are rendering is from an extension. + bool is_extension_page_; + + // Site instance for the balloon/profile, to be used for opening new links. + scoped_refptr<SiteInstance> site_instance_; + + // Common implementations of some RenderViewHostDelegate::View methods. + RenderViewHostDelegateViewHelper delegate_view_helper_; +}; + +#endif // CHROME_BROWSER_NOTIFICATIONS_BALLOON_HOST_H_ |