diff options
author | avi@google.com <avi@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-06 20:12:07 +0000 |
---|---|---|
committer | avi@google.com <avi@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-06 20:12:07 +0000 |
commit | b8615376ec35c6b328ec67325289e0b126f5f4cd (patch) | |
tree | d93d9ab0f28bdb48abfcdfbc0500b75d20369367 /chrome/browser/tab_contents | |
parent | 3ddb7b91615c7c81d6723c061c106b430294b05f (diff) | |
download | chromium_src-b8615376ec35c6b328ec67325289e0b126f5f4cd.zip chromium_src-b8615376ec35c6b328ec67325289e0b126f5f4cd.tar.gz chromium_src-b8615376ec35c6b328ec67325289e0b126f5f4cd.tar.bz2 |
Initial Mac port of web_contents_view and render_widget_host_view.
Review URL: http://codereview.chromium.org/20124
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9330 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/tab_contents')
10 files changed, 341 insertions, 13 deletions
diff --git a/chrome/browser/tab_contents/render_view_host_manager.cc b/chrome/browser/tab_contents/render_view_host_manager.cc index b7655a5..c512b9a 100644 --- a/chrome/browser/tab_contents/render_view_host_manager.cc +++ b/chrome/browser/tab_contents/render_view_host_manager.cc @@ -4,16 +4,11 @@ #include "chrome/browser/tab_contents/render_view_host_manager.h" -#if defined(OS_MACOSX) -#include "chrome/common/temp_scaffolding_stubs.h" -#else -#include "chrome/browser/renderer_host/render_widget_host_view.h" -#endif - #include "base/command_line.h" #include "base/logging.h" #include "chrome/browser/renderer_host/render_view_host.h" #include "chrome/browser/renderer_host/render_view_host_delegate.h" +#include "chrome/browser/renderer_host/render_widget_host_view.h" #include "chrome/browser/tab_contents/navigation_controller.h" #include "chrome/browser/tab_contents/navigation_entry.h" #include "chrome/browser/tab_contents/site_instance.h" diff --git a/chrome/browser/tab_contents/web_contents.cc b/chrome/browser/tab_contents/web_contents.cc index b152a28..f0f39bf 100644 --- a/chrome/browser/tab_contents/web_contents.cc +++ b/chrome/browser/tab_contents/web_contents.cc @@ -20,8 +20,10 @@ #include "chrome/browser/profile.h" #include "chrome/browser/renderer_host/render_process_host.h" #include "chrome/browser/renderer_host/render_view_host.h" +#include "chrome/browser/renderer_host/render_widget_host_view.h" #include "chrome/browser/search_engines/template_url_model.h" #include "chrome/browser/tab_contents/provisional_load_details.h" +#include "chrome/browser/tab_contents/web_contents_view.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/l10n_util.h" #include "chrome/common/notification_service.h" @@ -490,11 +492,12 @@ void WebContents::PopupNotificationVisibilityChanged(bool visible) { render_view_host()->PopupNotificationVisibilityChanged(visible); } -#if defined(OS_WIN) // Stupid view pass-throughs void WebContents::CreateView() { view_->CreateView(); } + +#if defined(OS_WIN) HWND WebContents::GetContainerHWND() const { return view_->GetNativeView(); } diff --git a/chrome/browser/tab_contents/web_contents.h b/chrome/browser/tab_contents/web_contents.h index ccd1e2e..3326cb1 100644 --- a/chrome/browser/tab_contents/web_contents.h +++ b/chrome/browser/tab_contents/web_contents.h @@ -128,11 +128,11 @@ class WebContents : public TabContents, virtual void SetDownloadShelfVisible(bool visible); virtual void PopupNotificationVisibilityChanged(bool visible); -#if defined(OS_WIN) // Retarded pass-throughs to the view. // TODO(brettw) fix this, tab contents shouldn't have these methods, probably // it should be killed altogether. virtual void CreateView(); +#if defined(OS_WIN) virtual HWND GetContainerHWND() const; virtual HWND GetContentHWND(); virtual void GetContainerBounds(gfx::Rect *out) const; @@ -390,7 +390,11 @@ class WebContents : public TabContents, friend class TestWebContents; // Temporary until the view/contents separation is complete. +#if defined(OS_WIN) friend class WebContentsViewWin; +#elif defined(OS_MACOSX) + friend class WebContentsViewMac; +#endif // So InterstitialPage can access SetIsLoading. friend class InterstitialPage; diff --git a/chrome/browser/tab_contents/web_contents_view.h b/chrome/browser/tab_contents/web_contents_view.h index 7c8a4e0..4bf8dca 100644 --- a/chrome/browser/tab_contents/web_contents_view.h +++ b/chrome/browser/tab_contents/web_contents_view.h @@ -61,7 +61,7 @@ class WebContentsView : public RenderViewHostDelegate::View { // Returns the outermost native view. This will be used as the parent for // dialog boxes. - virtual gfx::NativeView GetTopLevelNativeView() const = 0; + virtual gfx::NativeWindow GetTopLevelNativeView() const = 0; // Computes the rectangle for the native widget that contains the contents of // the tab relative to its parent. @@ -130,6 +130,7 @@ class WebContentsView : public RenderViewHostDelegate::View { // when the tab comes back. virtual void HideFindBar(bool end_session) = 0; +#if defined(OS_WIN) // Called when the tab is reparented to a new browser window. On MS Windows, // we have to change the parent of our find bar to go with the new window. // @@ -137,6 +138,7 @@ class WebContentsView : public RenderViewHostDelegate::View { // around the tab like this, the download bar etc. should be managed by the // BrowserView2 object. virtual void ReparentFindWindow(Browser* new_browser) const = 0; +#endif // Computes the location of the find bar and whether it is fully visible in // its parent window. The return value indicates if the window is visible at diff --git a/chrome/browser/tab_contents/web_contents_view_gtk.cc b/chrome/browser/tab_contents/web_contents_view_gtk.cc index c5ff575..cd0d09a 100644 --- a/chrome/browser/tab_contents/web_contents_view_gtk.cc +++ b/chrome/browser/tab_contents/web_contents_view_gtk.cc @@ -49,7 +49,7 @@ gfx::NativeView WebContentsViewGtk::GetContentNativeView() const { return NULL; } -gfx::NativeView WebContentsViewGtk::GetTopLevelNativeView() const { +gfx::NativeWindow WebContentsViewGtk::GetTopLevelNativeView() const { NOTIMPLEMENTED(); return NULL; } diff --git a/chrome/browser/tab_contents/web_contents_view_gtk.h b/chrome/browser/tab_contents/web_contents_view_gtk.h index b00a520..f10df5c 100644 --- a/chrome/browser/tab_contents/web_contents_view_gtk.h +++ b/chrome/browser/tab_contents/web_contents_view_gtk.h @@ -24,7 +24,7 @@ class WebContentsViewGtk : public WebContentsView { virtual gfx::NativeView GetNativeView() const; virtual gfx::NativeView GetContentNativeView() const; - virtual gfx::NativeView GetTopLevelNativeView() const; + virtual gfx::NativeWindow GetTopLevelNativeView() const; virtual void GetContainerBounds(gfx::Rect* out) const; virtual void OnContentsDestroy(); virtual void SetPageTitle(const std::wstring& title); diff --git a/chrome/browser/tab_contents/web_contents_view_mac.h b/chrome/browser/tab_contents/web_contents_view_mac.h new file mode 100644 index 0000000..ec3b613 --- /dev/null +++ b/chrome/browser/tab_contents/web_contents_view_mac.h @@ -0,0 +1,88 @@ +// 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_TAB_CONTENTS_WEB_CONTENTS_VIEW_MAC_H_ +#define CHROME_BROWSER_TAB_CONTENTS_WEB_CONTENTS_VIEW_MAC_H_ + +#import <Cocoa/Cocoa.h> + +#include "base/gfx/size.h" +#include "base/scoped_cftyperef.h" +#include "chrome/browser/tab_contents/web_contents_view.h" + +class FindBarMac; + +@interface WebContentsViewCocoa : NSView { +} + +@end + +// Mac-specific implementation of the WebContentsView. It owns an NSView that +// contains all of the contents of the tab and associated child views. +class WebContentsViewMac : public WebContentsView { + public: + // The corresponding WebContents is passed in the constructor, and manages our + // lifetime. This doesn't need to be the case, but is this way currently + // because that's what was easiest when they were split. + explicit WebContentsViewMac(WebContents* web_contents); + virtual ~WebContentsViewMac(); + + // WebContentsView implementation -------------------------------------------- + + virtual WebContents* GetWebContents(); + virtual void CreateView(); + virtual RenderWidgetHostView* CreateViewForWidget( + RenderWidgetHost* render_widget_host); + virtual gfx::NativeView GetNativeView() const; + virtual gfx::NativeView GetContentNativeView() const; + virtual gfx::NativeWindow GetTopLevelNativeView() const; + virtual void GetContainerBounds(gfx::Rect* out) const; + virtual void OnContentsDestroy(); + virtual void SetPageTitle(const std::wstring& title); + virtual void Invalidate(); + virtual void SizeContents(const gfx::Size& size); + virtual void FindInPage(const Browser& browser, + bool find_next, bool forward_direction); + virtual void HideFindBar(bool end_session); + virtual bool GetFindBarWindowInfo(gfx::Point* position, + bool* fully_visible) const; + + // Backend implementation of RenderViewHostDelegate::View. + virtual WebContents* CreateNewWindowInternal( + int route_id, base::WaitableEvent* modal_dialog_event); + virtual RenderWidgetHostView* CreateNewWidgetInternal(int route_id, + bool activatable); + virtual void ShowCreatedWindowInternal(WebContents* new_web_contents, + WindowOpenDisposition disposition, + const gfx::Rect& initial_pos, + bool user_gesture); + virtual void ShowCreatedWidgetInternal(RenderWidgetHostView* widget_host_view, + const gfx::Rect& initial_pos); + virtual void ShowContextMenu(const ContextMenuParams& params); + virtual void StartDragging(const WebDropData& drop_data); + virtual void UpdateDragCursor(bool is_drop_target); + virtual void TakeFocus(bool reverse); + virtual void HandleKeyboardEvent(const WebKeyboardEvent& event); + virtual void OnFindReply(int request_id, + int number_of_matches, + const gfx::Rect& selection_rect, + int active_match_ordinal, + bool final_update); + + private: + // --------------------------------------------------------------------------- + + WebContents* web_contents_; + + // The Cocoa NSView that lives in the view hierarchy. + scoped_cftyperef<WebContentsViewCocoa*> cocoa_view_; + + // For find in page. This may be NULL if there is no find bar, and if it is + // non-NULL, it may or may not be visible. + scoped_ptr<FindBarMac> find_bar_; + + DISALLOW_COPY_AND_ASSIGN(WebContentsViewMac); +}; + +#endif // CHROME_BROWSER_TAB_CONTENTS_WEB_CONTENTS_VIEW_MAC_H_ diff --git a/chrome/browser/tab_contents/web_contents_view_mac.mm b/chrome/browser/tab_contents/web_contents_view_mac.mm new file mode 100644 index 0000000..4218eb2 --- /dev/null +++ b/chrome/browser/tab_contents/web_contents_view_mac.mm @@ -0,0 +1,236 @@ +// Copyright (c) 2006-2008 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/tab_contents/web_contents_view_mac.h" + +#include "chrome/browser/browser.h" // TODO(beng): this dependency is awful. +#include "chrome/browser/renderer_host/render_widget_host.h" +#include "chrome/browser/renderer_host/render_widget_host_view_mac.h" +#include "chrome/browser/tab_contents/web_contents.h" + +#include "chrome/common/temp_scaffolding_stubs.h" + +// static +WebContentsView* WebContentsView::Create(WebContents* web_contents) { + return new WebContentsViewMac(web_contents); +} + +WebContentsViewMac::WebContentsViewMac(WebContents* web_contents) + : web_contents_(web_contents) { +} + +WebContentsViewMac::~WebContentsViewMac() { +} + +WebContents* WebContentsViewMac::GetWebContents() { + return web_contents_; +} + +void WebContentsViewMac::CreateView() { + WebContentsViewCocoa* view = + [[WebContentsViewCocoa alloc] initWithFrame:NSZeroRect]; + // Under GC, ObjC and CF retains/releases are no longer equivalent. So we + // change our ObjC retain to a CF retain se we can use a scoped_cftyperef. + CFRetain(view); + [view release]; + cocoa_view_.reset(view); +} + +RenderWidgetHostView* WebContentsViewMac::CreateViewForWidget( + RenderWidgetHost* render_widget_host) { + DCHECK(!render_widget_host->view()); + RenderWidgetHostViewMac* view = + new RenderWidgetHostViewMac(render_widget_host); + + NSView* view_view = view->GetNativeView(); + [cocoa_view_.get() addSubview:view_view]; + + return view; +} + +gfx::NativeView WebContentsViewMac::GetNativeView() const { + return cocoa_view_.get(); +} + +gfx::NativeView WebContentsViewMac::GetContentNativeView() const { + if (!web_contents_->render_widget_host_view()) + return NULL; + return web_contents_->render_widget_host_view()->GetPluginNativeView(); +} + +gfx::NativeWindow WebContentsViewMac::GetTopLevelNativeView() const { + return [cocoa_view_.get() window]; +} + +void WebContentsViewMac::GetContainerBounds(gfx::Rect* out) const { + *out = gfx::Rect(NSRectToCGRect([cocoa_view_.get() frame])); +} + +void WebContentsViewMac::StartDragging(const WebDropData& drop_data) { + NOTIMPLEMENTED(); +} + +void WebContentsViewMac::OnContentsDestroy() { + // TODO(avi):Close the find bar if any. + if (find_bar_.get()) + find_bar_->Close(); +} + +void WebContentsViewMac::SetPageTitle(const std::wstring& title) { + // Meaningless on the Mac; widgets don't have a "title" attribute +} + +void WebContentsViewMac::Invalidate() { + [cocoa_view_.get() setNeedsDisplay:YES]; +} + +void WebContentsViewMac::SizeContents(const gfx::Size& size) { + // TODO(brettw) this is a hack and should be removed. See web_contents_view.h. + NSRect rect = [cocoa_view_.get() frame]; + rect.size = NSSizeFromCGSize(size.ToCGSize()); + [cocoa_view_.get() setBounds:rect]; +} + +void WebContentsViewMac::FindInPage(const Browser& browser, + bool find_next, bool forward_direction) { + if (!find_bar_.get()) { + // We want the Chrome top-level (Frame) window. + NSWindow* window = + static_cast<NSWindow*>(browser.window()->GetNativeHandle()); + find_bar_.reset(new FindBarMac(this, window)); + } else { + find_bar_->Show(); + } + + if (find_next && !find_bar_->find_string().empty()) + find_bar_->StartFinding(forward_direction); +} + +void WebContentsViewMac::HideFindBar(bool end_session) { + if (find_bar_.get()) { + if (end_session) + find_bar_->EndFindSession(); + else + find_bar_->DidBecomeUnselected(); + } +} + +bool WebContentsViewMac::GetFindBarWindowInfo(gfx::Point* position, + bool* fully_visible) const { + if (!find_bar_.get() || + [find_bar_->GetView() isHidden]) { + *position = gfx::Point(0, 0); + *fully_visible = false; + return false; + } + + NSRect frame = [find_bar_->GetView() frame]; + *position = gfx::Point(frame.origin.x, frame.origin.y); + *fully_visible = find_bar_->IsVisible() && !find_bar_->IsAnimating(); + return true; +} + +void WebContentsViewMac::UpdateDragCursor(bool is_drop_target) { + NOTIMPLEMENTED(); +} + +void WebContentsViewMac::TakeFocus(bool reverse) { + [cocoa_view_.get() becomeFirstResponder]; +} + +void WebContentsViewMac::HandleKeyboardEvent(const WebKeyboardEvent& event) { + // The renderer returned a keyboard event it did not process. TODO(avi): + // reconstruct an NSEvent and feed it to the view. + NOTIMPLEMENTED(); +} + +void WebContentsViewMac::OnFindReply(int request_id, + int number_of_matches, + const gfx::Rect& selection_rect, + int active_match_ordinal, + bool final_update) { + if (find_bar_.get()) { + find_bar_->OnFindReply(request_id, number_of_matches, selection_rect, + active_match_ordinal, final_update); + } +} + +void WebContentsViewMac::ShowContextMenu(const ContextMenuParams& params) { + NOTIMPLEMENTED(); +} + +WebContents* WebContentsViewMac::CreateNewWindowInternal( + int route_id, + base::WaitableEvent* modal_dialog_event) { + // Create the new web contents. This will automatically create the new + // WebContentsView. In the future, we may want to create the view separately. + WebContents* new_contents = + new WebContents(web_contents_->profile(), + web_contents_->GetSiteInstance(), + web_contents_->render_view_factory_, + route_id, + modal_dialog_event); + new_contents->SetupController(web_contents_->profile()); + WebContentsView* new_view = new_contents->view(); + + new_view->CreateView(); + + // TODO(brettw) it seems bogus that we have to call this function on the + // newly created object and give it one of its own member variables. + new_view->CreateViewForWidget(new_contents->render_view_host()); + return new_contents; +} + +RenderWidgetHostView* WebContentsViewMac::CreateNewWidgetInternal( + int route_id, + bool activatable) { + // Create the widget and its associated view. + // TODO(brettw) can widget creation be cross-platform? + RenderWidgetHost* widget_host = + new RenderWidgetHost(web_contents_->process(), route_id); + RenderWidgetHostViewMac* widget_view = + new RenderWidgetHostViewMac(widget_host); + + // Some weird reparenting stuff happens here. TODO(avi): figure that out + + return widget_view; +} + +void WebContentsViewMac::ShowCreatedWindowInternal( + WebContents* new_web_contents, + WindowOpenDisposition disposition, + const gfx::Rect& initial_pos, + bool user_gesture) { + if (!new_web_contents->render_widget_host_view() || + !new_web_contents->process()->channel()) { + // The view has gone away or the renderer crashed. Nothing to do. + return; + } + + // TODO(brettw) this seems bogus to reach into here and initialize the host. + new_web_contents->render_view_host()->Init(); + web_contents_->AddNewContents(new_web_contents, disposition, initial_pos, + user_gesture); +} + +void WebContentsViewMac::ShowCreatedWidgetInternal( + RenderWidgetHostView* widget_host_view, + const gfx::Rect& initial_pos) { + RenderWidgetHost* widget_host = widget_host_view->GetRenderWidgetHost(); + if (!widget_host->process()->channel()) { + // The view has gone away or the renderer crashed. Nothing to do. + return; + } + + // Reparenting magic goes here. TODO(avi): fix + + widget_host->Init(); +} + +@implementation WebContentsViewCocoa + +// Tons of stuff goes here, where we grab events going on in Cocoaland and send +// them into the C++ system. TODO(avi): all that jazz + +@end diff --git a/chrome/browser/tab_contents/web_contents_view_win.cc b/chrome/browser/tab_contents/web_contents_view_win.cc index 522f8a1..4e56727 100644 --- a/chrome/browser/tab_contents/web_contents_view_win.cc +++ b/chrome/browser/tab_contents/web_contents_view_win.cc @@ -91,7 +91,7 @@ gfx::NativeView WebContentsViewWin::GetContentNativeView() const { return web_contents_->render_widget_host_view()->GetPluginNativeView(); } -gfx::NativeView WebContentsViewWin::GetTopLevelNativeView() const { +gfx::NativeWindow WebContentsViewWin::GetTopLevelNativeView() const { return ::GetAncestor(GetNativeView(), GA_ROOT); } diff --git a/chrome/browser/tab_contents/web_contents_view_win.h b/chrome/browser/tab_contents/web_contents_view_win.h index 4edb6f5..a8649d7 100644 --- a/chrome/browser/tab_contents/web_contents_view_win.h +++ b/chrome/browser/tab_contents/web_contents_view_win.h @@ -34,7 +34,7 @@ class WebContentsViewWin : public WebContentsView, RenderWidgetHost* render_widget_host); virtual gfx::NativeView GetNativeView() const; virtual gfx::NativeView GetContentNativeView() const; - virtual gfx::NativeView GetTopLevelNativeView() const; + virtual gfx::NativeWindow GetTopLevelNativeView() const; virtual void GetContainerBounds(gfx::Rect* out) const; virtual void OnContentsDestroy(); virtual void SetPageTitle(const std::wstring& title); |