OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 #import "chrome/browser/cocoa/tab_contents_controller.h" | 5 #import "chrome/browser/cocoa/tab_contents_controller.h" |
6 | 6 |
7 #include "base/mac_util.h" | 7 #include "base/mac_util.h" |
8 #include "base/scoped_nsobject.h" | 8 #include "base/scoped_nsobject.h" |
9 #include "chrome/browser/renderer_host/render_view_host.h" | 9 #include "chrome/browser/renderer_host/render_view_host.h" |
10 #include "chrome/browser/renderer_host/render_widget_host_view.h" | 10 #include "chrome/browser/renderer_host/render_widget_host_view.h" |
11 #include "chrome/browser/tab_contents/navigation_controller.h" | |
11 #include "chrome/browser/tab_contents/tab_contents.h" | 12 #include "chrome/browser/tab_contents/tab_contents.h" |
13 #include "chrome/common/notification_details.h" | |
14 #include "chrome/common/notification_observer.h" | |
15 #include "chrome/common/notification_registrar.h" | |
16 #include "chrome/common/notification_source.h" | |
17 #include "chrome/common/notification_type.h" | |
18 | |
19 | |
20 @interface TabContentsController(Private) | |
21 // Forwards frame update to |delegate_| (TabContentsControllerView calls it). | |
22 - (void)tabContentsViewFrameWillChange:(NSRect)frameRect; | |
23 // Notification from TabContents (forwarded by TabContentsNotificationBridge). | |
24 - (void)tabContentsRenderViewHostChanged:(RenderViewHost*)oldHost | |
25 newHost:(RenderViewHost*)newHost; | |
26 @end | |
27 | |
28 | |
29 // A supporting C++ bridge object to register for TabContents notifications. | |
30 | |
31 class TabContentsNotificationBridge : public NotificationObserver { | |
32 public: | |
33 explicit TabContentsNotificationBridge(TabContentsController* controller); | |
34 | |
35 // Overriden from NotificationObserver. | |
36 virtual void Observe(NotificationType type, | |
37 const NotificationSource& source, | |
38 const NotificationDetails& details); | |
39 // Register for |contents|'s notifications, remove all prior registrations. | |
40 void ObserveNotifications(TabContents* contents); | |
41 private: | |
42 NotificationRegistrar registrar_; | |
43 TabContentsController* controller_; // weak, owns us | |
44 }; | |
45 | |
46 TabContentsNotificationBridge::TabContentsNotificationBridge( | |
47 TabContentsController* controller) | |
48 : controller_(controller) { | |
49 } | |
50 | |
51 void TabContentsNotificationBridge::Observe( | |
52 NotificationType type, | |
53 const NotificationSource& source, | |
54 const NotificationDetails& details) { | |
55 if (type == NotificationType::RENDER_VIEW_HOST_CHANGED) { | |
56 RenderViewHostSwitchedDetails* switched_details = | |
57 Details<RenderViewHostSwitchedDetails>(details).ptr(); | |
58 [controller_ tabContentsRenderViewHostChanged:switched_details->old_host | |
59 newHost:switched_details->new_host]; | |
60 } else { | |
61 NOTREACHED(); | |
62 } | |
63 } | |
64 | |
65 void TabContentsNotificationBridge::ObserveNotifications( | |
rohitrao (ping after 24h)
2010/11/15 23:46:58
Maybe call this method "RegisterForTabContents"?
Aleksey Shlyapnikov
2010/11/16 02:07:37
Done.
| |
66 TabContents* contents) { | |
67 registrar_.RemoveAll(); | |
68 if (contents) { | |
69 registrar_.Add(this, | |
70 NotificationType::RENDER_VIEW_HOST_CHANGED, | |
71 Source<NavigationController>(&contents->controller())); | |
72 } | |
73 } | |
74 | |
75 | |
76 // A custom view that notifies |controller| that view's frame is changing. | |
77 | |
78 @interface TabContentsControllerView : NSView { | |
rohitrao (ping after 24h)
2010/11/15 23:46:58
This isn't a great name, especially since we alrea
Aleksey Shlyapnikov
2010/11/16 02:07:37
Cannot think of anything better than this. Done.
| |
79 TabContentsController* controller_; | |
80 } | |
81 - (id)initWithController:(TabContentsController*)controller; | |
82 @end | |
83 | |
84 @implementation TabContentsControllerView | |
85 | |
86 - (id)initWithController:(TabContentsController*)controller { | |
87 if ((self = [super initWithFrame:NSZeroRect])) { | |
88 controller_ = controller; | |
89 } | |
90 return self; | |
91 } | |
92 | |
93 - (void)setFrame:(NSRect)frameRect { | |
94 [controller_ tabContentsViewFrameWillChange:frameRect]; | |
95 [super setFrame:frameRect]; | |
96 } | |
97 | |
98 @end | |
12 | 99 |
13 | 100 |
14 @implementation TabContentsController | 101 @implementation TabContentsController |
15 @synthesize tabContents = contents_; | 102 @synthesize tabContents = contents_; |
16 | 103 |
17 - (id)initWithContents:(TabContents*)contents { | 104 - (id)initWithContents:(TabContents*)contents |
105 delegate:(id<TabContentsControllerDelegate>)delegate { | |
18 if ((self = [super initWithNibName:nil bundle:nil])) { | 106 if ((self = [super initWithNibName:nil bundle:nil])) { |
19 contents_ = contents; | 107 contents_ = contents; |
108 delegate_ = delegate; | |
109 tabContentsBridge_.reset(new TabContentsNotificationBridge(self)); | |
rohitrao (ping after 24h)
2010/11/15 23:46:58
I really dislike listening for this notification h
| |
110 tabContentsBridge_->ObserveNotifications(contents); | |
20 } | 111 } |
21 return self; | 112 return self; |
22 } | 113 } |
23 | 114 |
24 - (void)dealloc { | 115 - (void)dealloc { |
25 // make sure our contents have been removed from the window | 116 // make sure our contents have been removed from the window |
26 [[self view] removeFromSuperview]; | 117 [[self view] removeFromSuperview]; |
27 [super dealloc]; | 118 [super dealloc]; |
28 } | 119 } |
29 | 120 |
30 - (void)loadView { | 121 - (void)loadView { |
31 scoped_nsobject<NSView> view([[NSView alloc] initWithFrame:NSZeroRect]); | 122 scoped_nsobject<TabContentsControllerView> view( |
123 [[TabContentsControllerView alloc] initWithController:self]); | |
32 [view setAutoresizingMask:NSViewHeightSizable|NSViewWidthSizable]; | 124 [view setAutoresizingMask:NSViewHeightSizable|NSViewWidthSizable]; |
33 [self setView:view]; | 125 [self setView:view]; |
34 } | 126 } |
35 | 127 |
36 - (void)ensureContentsSizeDoesNotChange { | 128 - (void)ensureContentsSizeDoesNotChange { |
37 NSView* contentsContainer = [self view]; | 129 if (contents_) { |
38 NSArray* subviews = [contentsContainer subviews]; | 130 NSView* contentsContainer = [self view]; |
39 if ([subviews count] > 0) | 131 NSArray* subviews = [contentsContainer subviews]; |
40 [contents_->GetNativeView() setAutoresizingMask:NSViewNotSizable]; | 132 if ([subviews count] > 0) |
133 [contents_->GetNativeView() setAutoresizingMask:NSViewNotSizable]; | |
134 } | |
41 } | 135 } |
42 | 136 |
43 // Call when the tab view is properly sized and the render widget host view | 137 // Call when the tab view is properly sized and the render widget host view |
44 // should be put into the view hierarchy. | 138 // should be put into the view hierarchy. |
45 - (void)ensureContentsVisible { | 139 - (void)ensureContentsVisible { |
140 if (!contents_) | |
141 return; | |
46 NSView* contentsContainer = [self view]; | 142 NSView* contentsContainer = [self view]; |
47 NSArray* subviews = [contentsContainer subviews]; | 143 NSArray* subviews = [contentsContainer subviews]; |
48 NSView* contentsNativeView = contents_->GetNativeView(); | 144 NSView* contentsNativeView = contents_->GetNativeView(); |
49 [contentsNativeView setFrame:[contentsContainer frame]]; | 145 |
146 NSRect contentsNativeViewFrame = [contentsContainer frame]; | |
147 contentsNativeViewFrame.origin = NSZeroPoint; | |
148 | |
149 [delegate_ tabContentsViewFrameWillChange:self | |
150 frameRect:contentsNativeViewFrame]; | |
151 | |
152 // Native view is resized to the actual size before it becomes visible | |
153 // to avoid flickering. | |
154 [contentsNativeView setFrame:contentsNativeViewFrame]; | |
50 if ([subviews count] == 0) { | 155 if ([subviews count] == 0) { |
51 [contentsContainer addSubview:contentsNativeView]; | 156 [contentsContainer addSubview:contentsNativeView]; |
52 } else if ([subviews objectAtIndex:0] != contentsNativeView) { | 157 } else if ([subviews objectAtIndex:0] != contentsNativeView) { |
53 [contentsContainer replaceSubview:[subviews objectAtIndex:0] | 158 [contentsContainer replaceSubview:[subviews objectAtIndex:0] |
54 with:contentsNativeView]; | 159 with:contentsNativeView]; |
55 } | 160 } |
161 // Restore autoresizing properties possibly stripped by | |
162 // ensureContentsSizeDoesNotChange call. | |
56 [contentsNativeView setAutoresizingMask:NSViewWidthSizable| | 163 [contentsNativeView setAutoresizingMask:NSViewWidthSizable| |
57 NSViewHeightSizable]; | 164 NSViewHeightSizable]; |
58 } | 165 } |
59 | 166 |
60 // Returns YES if the tab represented by this controller is the front-most. | 167 - (void)changeTabContents:(TabContents*)newContents { |
61 - (BOOL)isCurrentTab { | 168 contents_ = newContents; |
62 // We're the current tab if we're in the view hierarchy, otherwise some other | 169 tabContentsBridge_->ObserveNotifications(contents_); |
63 // tab is. | 170 } |
64 return [[self view] superview] ? YES : NO; | 171 |
172 - (void)tabContentsViewFrameWillChange:(NSRect)frameRect { | |
173 [delegate_ tabContentsViewFrameWillChange:self frameRect:frameRect]; | |
174 } | |
175 | |
176 - (void)tabContentsRenderViewHostChanged:(RenderViewHost*)oldHost | |
177 newHost:(RenderViewHost*)newHost { | |
178 if (oldHost && newHost && oldHost->view() && newHost->view()) { | |
179 newHost->view()->set_reserved_contents_rect( | |
180 oldHost->view()->reserved_contents_rect()); | |
181 } else { | |
182 [delegate_ tabContentsViewFrameWillChange:self | |
183 frameRect:[[self view] frame]]; | |
184 } | |
65 } | 185 } |
66 | 186 |
67 - (void)willBecomeUnselectedTab { | 187 - (void)willBecomeUnselectedTab { |
68 // The RWHV is ripped out of the view hierarchy on tab switches, so it never | 188 // The RWHV is ripped out of the view hierarchy on tab switches, so it never |
69 // formally resigns first responder status. Handle this by explicitly sending | 189 // formally resigns first responder status. Handle this by explicitly sending |
70 // a Blur() message to the renderer, but only if the RWHV currently has focus. | 190 // a Blur() message to the renderer, but only if the RWHV currently has focus. |
71 RenderViewHost* rvh = contents_->render_view_host(); | 191 RenderViewHost* rvh = [self tabContents]->render_view_host(); |
72 if (rvh && rvh->view() && rvh->view()->HasFocus()) | 192 if (rvh && rvh->view() && rvh->view()->HasFocus()) |
73 rvh->Blur(); | 193 rvh->Blur(); |
74 } | 194 } |
75 | 195 |
76 - (void)willBecomeSelectedTab { | 196 - (void)willBecomeSelectedTab { |
77 // Do not explicitly call Focus() here, as the RWHV may not actually have | 197 // Do not explicitly call Focus() here, as the RWHV may not actually have |
78 // focus (for example, if the omnibox has focus instead). The TabContents | 198 // focus (for example, if the omnibox has focus instead). The TabContents |
79 // logic will restore focus to the appropriate view. | 199 // logic will restore focus to the appropriate view. |
80 } | 200 } |
81 | 201 |
82 - (void)tabDidChange:(TabContents*)updatedContents { | 202 - (void)tabDidChange:(TabContents*)updatedContents { |
83 // Calling setContentView: here removes any first responder status | 203 // Calling setContentView: here removes any first responder status |
84 // the view may have, so avoid changing the view hierarchy unless | 204 // the view may have, so avoid changing the view hierarchy unless |
85 // the view is different. | 205 // the view is different. |
86 if (contents_ != updatedContents) { | 206 if ([self tabContents] != updatedContents) { |
87 contents_ = updatedContents; | 207 [self changeTabContents:updatedContents]; |
88 [self ensureContentsVisible]; | 208 if ([self tabContents]) |
209 [self ensureContentsVisible]; | |
89 } | 210 } |
90 } | 211 } |
91 | 212 |
92 @end | 213 @end |
OLD | NEW |