| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/cocoa/extensions/extension_view_mac.h" | 5 #include "chrome/browser/ui/cocoa/extensions/extension_view_mac.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/extension_host.h" | 7 #include "chrome/browser/extensions/extension_host.h" |
| 8 #include "chrome/browser/tab_contents/tab_contents_view_mac.h" | |
| 9 #include "content/browser/renderer_host/render_view_host.h" | 8 #include "content/browser/renderer_host/render_view_host.h" |
| 10 #include "content/browser/renderer_host/render_widget_host_view.h" | |
| 11 #include "content/browser/renderer_host/render_widget_host_view_mac.h" | 9 #include "content/browser/renderer_host/render_widget_host_view_mac.h" |
| 12 #include "content/browser/tab_contents/tab_contents.h" | |
| 13 #include "content/browser/tab_contents/tab_contents_view.h" | |
| 14 | 10 |
| 15 // The minimum/maximum dimensions of the popup. | 11 // The minimum/maximum dimensions of the popup. |
| 16 const CGFloat ExtensionViewMac::kMinWidth = 25.0; | 12 const CGFloat ExtensionViewMac::kMinWidth = 25.0; |
| 17 const CGFloat ExtensionViewMac::kMinHeight = 25.0; | 13 const CGFloat ExtensionViewMac::kMinHeight = 25.0; |
| 18 const CGFloat ExtensionViewMac::kMaxWidth = 800.0; | 14 const CGFloat ExtensionViewMac::kMaxWidth = 800.0; |
| 19 const CGFloat ExtensionViewMac::kMaxHeight = 600.0; | 15 const CGFloat ExtensionViewMac::kMaxHeight = 600.0; |
| 20 | 16 |
| 21 ExtensionViewMac::ExtensionViewMac(ExtensionHost* extension_host, | 17 ExtensionViewMac::ExtensionViewMac(ExtensionHost* extension_host, |
| 22 Browser* browser) | 18 Browser* browser) |
| 23 : browser_(browser), | 19 : browser_(browser), |
| 24 extension_host_(extension_host) { | 20 extension_host_(extension_host), |
| 21 render_widget_host_view_(NULL) { |
| 25 DCHECK(extension_host_); | 22 DCHECK(extension_host_); |
| 26 [native_view() setHidden:YES]; | |
| 27 } | 23 } |
| 28 | 24 |
| 29 ExtensionViewMac::~ExtensionViewMac() { | 25 ExtensionViewMac::~ExtensionViewMac() { |
| 26 if (render_widget_host_view_) |
| 27 [render_widget_host_view_->native_view() release]; |
| 30 } | 28 } |
| 31 | 29 |
| 32 void ExtensionViewMac::Init() { | 30 void ExtensionViewMac::Init() { |
| 33 CreateWidgetHostView(); | 31 CreateWidgetHostView(); |
| 34 } | 32 } |
| 35 | 33 |
| 36 gfx::NativeView ExtensionViewMac::native_view() { | 34 gfx::NativeView ExtensionViewMac::native_view() { |
| 37 return extension_host_->host_contents()->view()->GetNativeView(); | 35 DCHECK(render_widget_host_view_); |
| 36 return render_widget_host_view_->native_view(); |
| 38 } | 37 } |
| 39 | 38 |
| 40 RenderViewHost* ExtensionViewMac::render_view_host() const { | 39 RenderViewHost* ExtensionViewMac::render_view_host() const { |
| 41 return extension_host_->render_view_host(); | 40 return extension_host_->render_view_host(); |
| 42 } | 41 } |
| 43 | 42 |
| 44 void ExtensionViewMac::DidStopLoading() { | |
| 45 ShowIfCompletelyLoaded(); | |
| 46 } | |
| 47 | |
| 48 void ExtensionViewMac::SetBackground(const SkBitmap& background) { | 43 void ExtensionViewMac::SetBackground(const SkBitmap& background) { |
| 49 if (!pending_background_.empty() && render_view_host()->view()) { | 44 DCHECK(render_widget_host_view_); |
| 50 render_view_host()->view()->SetBackground(background); | 45 if (render_view_host()->IsRenderViewLive()) { |
| 46 render_widget_host_view_->SetBackground(background); |
| 51 } else { | 47 } else { |
| 52 pending_background_ = background; | 48 pending_background_ = background; |
| 53 } | 49 } |
| 54 ShowIfCompletelyLoaded(); | |
| 55 } | 50 } |
| 56 | 51 |
| 57 void ExtensionViewMac::UpdatePreferredSize(const gfx::Size& new_size) { | 52 void ExtensionViewMac::UpdatePreferredSize(const gfx::Size& new_size) { |
| 58 // When we update the size, our container becomes visible. Stay hidden until | 53 // TODO(thakis, erikkay): Windows does some tricks to resize the extension |
| 59 // the host is loaded. | 54 // view not before it's visible. Do something similar here. |
| 60 if (!extension_host_->did_stop_loading()) { | |
| 61 pending_preferred_size_ = new_size; | |
| 62 return; | |
| 63 } | |
| 64 | 55 |
| 65 // No need to use CA here, our caller calls us repeatedly to animate the | 56 // No need to use CA here, our caller calls us repeatedly to animate the |
| 66 // resizing. | 57 // resizing. |
| 67 NSView* view = native_view(); | 58 NSView* view = native_view(); |
| 68 NSRect frame = [view frame]; | 59 NSRect frame = [view frame]; |
| 69 frame.size.width = new_size.width(); | 60 frame.size.width = new_size.width(); |
| 70 frame.size.height = new_size.height(); | 61 frame.size.height = new_size.height(); |
| 71 | 62 |
| 72 // |new_size| is in pixels. Convert to view units. | 63 // |new_size| is in pixels. Convert to view units. |
| 73 frame.size = [view convertSize:frame.size fromView:nil]; | 64 frame.size = [view convertSize:frame.size fromView:nil]; |
| 74 | 65 |
| 75 // On first display of some extensions, this function is called with zero | 66 // On first display of some extensions, this function is called with zero |
| 76 // width after the correct size has been set. Bail if zero is seen, assuming | 67 // width after the correct size has been set. Bail if zero is seen, assuming |
| 77 // that an extension's view doesn't want any dimensions to ever be zero. | 68 // that an extension's view doesn't want any dimensions to ever be zero. |
| 78 // TODO(andybons): Verify this assumption and look into WebCore's | 69 // TODO(andybons): Verify this assumption and look into WebCore's |
| 79 // |contentesPreferredWidth| to see why this is occurring. | 70 // |contentesPreferredWidth| to see why this is occurring. |
| 80 if (NSIsEmptyRect(frame)) | 71 if (NSIsEmptyRect(frame)) |
| 81 return; | 72 return; |
| 82 | 73 |
| 83 DCHECK([view isKindOfClass:[TabContentsViewCocoa class]]); | 74 DCHECK([view isKindOfClass:[RenderWidgetHostViewCocoa class]]); |
| 84 TabContentsViewCocoa* hostView = (TabContentsViewCocoa*)view; | 75 RenderWidgetHostViewCocoa* hostView = (RenderWidgetHostViewCocoa*)view; |
| 85 | 76 |
| 86 // TabContentsViewCocoa overrides setFrame but not setFrameSize. | 77 // RenderWidgetHostViewCocoa overrides setFrame but not setFrameSize. |
| 87 // We need to defer the update back to the RenderWidgetHost so we don't | 78 // We need to defer the update back to the RenderWidgetHost so we don't |
| 88 // get the flickering effect on 10.5 of http://crbug.com/31970 | 79 // get the flickering effect on 10.5 of http://crbug.com/31970 |
| 89 [hostView setFrameWithDeferredUpdate:frame]; | 80 [hostView setFrameWithDeferredUpdate:frame]; |
| 90 [hostView setNeedsDisplay:YES]; | 81 [hostView setNeedsDisplay:YES]; |
| 91 } | 82 } |
| 92 | 83 |
| 93 void ExtensionViewMac::RenderViewCreated() { | 84 void ExtensionViewMac::RenderViewCreated() { |
| 94 // Do not allow webkit to draw scroll bars on views smaller than | 85 // Do not allow webkit to draw scroll bars on views smaller than |
| 95 // the largest size view allowed. The view will be resized to make | 86 // the largest size view allowed. The view will be resized to make |
| 96 // scroll bars unnecessary. Scroll bars change the height of the | 87 // scroll bars unnecessary. Scroll bars change the height of the |
| 97 // view, so not drawing them is necessary to avoid infinite resizing. | 88 // view, so not drawing them is necessary to avoid infinite resizing. |
| 98 gfx::Size largest_popup_size( | 89 gfx::Size largest_popup_size( |
| 99 CGSizeMake(ExtensionViewMac::kMaxWidth, ExtensionViewMac::kMaxHeight)); | 90 CGSizeMake(ExtensionViewMac::kMaxWidth, ExtensionViewMac::kMaxHeight)); |
| 100 extension_host_->DisableScrollbarsForSmallWindows(largest_popup_size); | 91 extension_host_->DisableScrollbarsForSmallWindows(largest_popup_size); |
| 101 | 92 |
| 102 if (!pending_background_.empty() && render_view_host()->view()) { | 93 if (!pending_background_.empty() && render_view_host()->view()) { |
| 103 render_view_host()->view()->SetBackground(pending_background_); | 94 render_widget_host_view_->SetBackground(pending_background_); |
| 104 pending_background_.reset(); | 95 pending_background_.reset(); |
| 105 } | 96 } |
| 106 } | 97 } |
| 107 | 98 |
| 108 void ExtensionViewMac::WindowFrameChanged() { | 99 void ExtensionViewMac::WindowFrameChanged() { |
| 109 if (render_view_host()->view()) | 100 if (render_widget_host_view_) |
| 110 render_view_host()->view()->WindowFrameChanged(); | 101 render_widget_host_view_->WindowFrameChanged(); |
| 111 } | 102 } |
| 112 | 103 |
| 113 void ExtensionViewMac::CreateWidgetHostView() { | 104 void ExtensionViewMac::CreateWidgetHostView() { |
| 114 extension_host_->CreateRenderViewSoon(); | 105 DCHECK(!render_widget_host_view_); |
| 106 render_widget_host_view_ = new RenderWidgetHostViewMac(render_view_host()); |
| 107 |
| 108 // The RenderWidgetHostViewMac is owned by its native view, which is created |
| 109 // in an autoreleased state. retain it, so that it doesn't immediately |
| 110 // disappear. |
| 111 [render_widget_host_view_->native_view() retain]; |
| 112 |
| 113 extension_host_->CreateRenderViewSoon(render_widget_host_view_); |
| 115 } | 114 } |
| 116 | |
| 117 void ExtensionViewMac::ShowIfCompletelyLoaded() { | |
| 118 // We wait to show the ExtensionView until it has loaded, and the view has | |
| 119 // actually been created. These can happen in different orders. | |
| 120 if (extension_host_->did_stop_loading()) { | |
| 121 [native_view() setHidden:NO]; | |
| 122 UpdatePreferredSize(pending_preferred_size_); | |
| 123 } | |
| 124 } | |
| OLD | NEW |