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