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