Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: chrome/browser/ui/panels/panel.cc

Issue 8539025: Fix Panel to always detect when a tab is added. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Test cleanup and fix Mac test hang Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/ui/panels/panel.h ('k') | chrome/browser/ui/panels/panel_browser_view.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/panels/panel.h" 5 #include "chrome/browser/ui/panels/panel.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "chrome/browser/extensions/extension_prefs.h" 8 #include "chrome/browser/extensions/extension_prefs.h"
9 #include "chrome/browser/extensions/extension_service.h" 9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
11 #include "content/browser/renderer_host/render_view_host.h" 11 #include "chrome/browser/tabs/tab_strip_model.h"
12 #include "chrome/browser/ui/browser.h" 12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/panels/native_panel.h" 13 #include "chrome/browser/ui/panels/native_panel.h"
14 #include "chrome/browser/ui/panels/panel_manager.h" 14 #include "chrome/browser/ui/panels/panel_manager.h"
15 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
15 #include "chrome/browser/ui/window_sizer.h" 16 #include "chrome/browser/ui/window_sizer.h"
16 #include "chrome/browser/web_applications/web_app.h" 17 #include "chrome/browser/web_applications/web_app.h"
17 #include "chrome/common/extensions/extension.h" 18 #include "chrome/common/extensions/extension.h"
19 #include "content/browser/renderer_host/render_view_host.h"
18 #include "content/browser/tab_contents/tab_contents.h" 20 #include "content/browser/tab_contents/tab_contents.h"
19 #include "content/common/view_messages.h" 21 #include "content/common/view_messages.h"
20 #include "content/public/browser/notification_service.h" 22 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/notification_source.h" 23 #include "content/public/browser/notification_source.h"
22 #include "content/public/browser/notification_types.h" 24 #include "content/public/browser/notification_types.h"
23 #include "ui/gfx/rect.h" 25 #include "ui/gfx/rect.h"
24 26
25 // static 27 // static
26 const Extension* Panel::GetExtensionFromBrowser(Browser* browser) { 28 const Extension* Panel::GetExtensionFromBrowser(Browser* browser) {
27 // Find the extension. When we create a panel from an extension, the extension 29 // Find the extension. When we create a panel from an extension, the extension
28 // ID is passed as the app name to the Browser. 30 // ID is passed as the app name to the Browser.
29 ExtensionService* extension_service = 31 ExtensionService* extension_service =
30 browser->GetProfile()->GetExtensionService(); 32 browser->GetProfile()->GetExtensionService();
31 return extension_service->GetExtensionById( 33 return extension_service->GetExtensionById(
32 web_app::GetExtensionIdFromApplicationName(browser->app_name()), false); 34 web_app::GetExtensionIdFromApplicationName(browser->app_name()), false);
33 } 35 }
34 36
35 Panel::Panel(Browser* browser, const gfx::Rect& bounds) 37 Panel::Panel(Browser* browser, const gfx::Rect& bounds)
36 : min_size_(bounds.size()), 38 : min_size_(bounds.size()),
37 max_size_(bounds.size()), 39 max_size_(bounds.size()),
38 native_panel_(NULL), 40 native_panel_(NULL),
39 expansion_state_(EXPANDED), 41 expansion_state_(EXPANDED),
40 restored_height_(bounds.height()) { 42 restored_height_(bounds.height()) {
41 native_panel_ = CreateNativePanel(browser, this, bounds); 43 native_panel_ = CreateNativePanel(browser, this, bounds);
42 44 browser->tabstrip_model()->AddObserver(this);
43 registrar_.Add(this,
44 content::NOTIFICATION_TAB_ADDED,
45 content::Source<TabContentsDelegate>(browser));
46 } 45 }
47 46
48 Panel::~Panel() { 47 Panel::~Panel() {
49 // Invoked by native panel so do not access native_panel_ here. 48 // Invoked by native panel destructor. Do not access native_panel_ here.
49 }
50
51 void Panel::OnNativePanelClosed() {
52 native_panel_->GetPanelBrowser()->tabstrip_model()->RemoveObserver(this);
53 manager()->Remove(this);
50 } 54 }
51 55
52 PanelManager* Panel::manager() const { 56 PanelManager* Panel::manager() const {
53 return PanelManager::GetInstance(); 57 return PanelManager::GetInstance();
54 } 58 }
55 59
56
57 const Extension* Panel::GetExtension() const { 60 const Extension* Panel::GetExtension() const {
58 return GetExtensionFromBrowser(browser()); 61 return GetExtensionFromBrowser(browser());
59 } 62 }
60 63
61 void Panel::SetPanelBounds(const gfx::Rect& bounds) { 64 void Panel::SetPanelBounds(const gfx::Rect& bounds) {
62 if (expansion_state_ == Panel::EXPANDED) 65 if (expansion_state_ == Panel::EXPANDED)
63 restored_height_ = bounds.height(); 66 restored_height_ = bounds.height();
64 67
65 native_panel_->SetPanelBounds(bounds); 68 native_panel_->SetPanelBounds(bounds);
66 69
67 content::NotificationService::current()->Notify( 70 content::NotificationService::current()->Notify(
68 chrome::NOTIFICATION_PANEL_CHANGED_BOUNDS, 71 chrome::NOTIFICATION_PANEL_CHANGED_BOUNDS,
69 content::Source<Panel>(this), 72 content::Source<Panel>(this),
70 content::NotificationService::NoDetails()); 73 content::NotificationService::NoDetails());
71 } 74 }
72 75
73 void Panel::SetMaxSize(const gfx::Size& max_size) { 76 void Panel::SetMaxSize(const gfx::Size& max_size) {
74 if (max_size_ == max_size) 77 if (max_size_ == max_size)
75 return; 78 return;
76 max_size_ = max_size; 79 max_size_ = max_size;
77 80
78 // Note: |render_view_host| might be NULL if the tab has not been created. 81 // Note: |render_view_host| might be NULL if the tab has not been created.
79 // If so, we will do it when NOTIFICATION_TAB_ADDED is received. 82 // If so, we will do it when TabInsertedAt() is invoked.
80 RenderViewHost* render_view_host = GetRenderViewHost(); 83 RenderViewHost* render_view_host = GetRenderViewHost();
81 if (render_view_host) 84 if (render_view_host)
82 RequestRenderViewHostToDisableScrollbars(render_view_host); 85 RequestRenderViewHostToDisableScrollbars(render_view_host);
83 } 86 }
84 87
85 void Panel::SetExpansionState(ExpansionState new_state) { 88 void Panel::SetExpansionState(ExpansionState new_state) {
86 if (expansion_state_ == new_state) 89 if (expansion_state_ == new_state)
87 return; 90 return;
88 91
89 ExpansionState old_state = expansion_state_; 92 ExpansionState old_state = expansion_state_;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 169
167 void Panel::SetBounds(const gfx::Rect& bounds) { 170 void Panel::SetBounds(const gfx::Rect& bounds) {
168 // Ignore any SetBounds requests since the bounds are completely controlled 171 // Ignore any SetBounds requests since the bounds are completely controlled
169 // by panel manager. 172 // by panel manager.
170 } 173 }
171 174
172 // Close() may be called multiple times if the browser window is not ready to 175 // Close() may be called multiple times if the browser window is not ready to
173 // close on the first attempt. 176 // close on the first attempt.
174 void Panel::Close() { 177 void Panel::Close() {
175 native_panel_->ClosePanel(); 178 native_panel_->ClosePanel();
176
177 // TODO(dimich): Only implemented fully async on Mac. Need to update other
178 // platforms. The panel should be removed from PanelManager when and if it
179 // was actually closed. The closing can be cancelled because of onbeforeunload
180 // handler on the web page. http://crbug.com/102720
181 #if !defined(OS_MACOSX)
182 manager()->Remove(this);
183 #endif
184 } 179 }
185 180
186 void Panel::Activate() { 181 void Panel::Activate() {
187 // Make sure the panel is expanded when activated programmatically, 182 // Make sure the panel is expanded when activated programmatically,
188 // so the user input does not go into collapsed window. 183 // so the user input does not go into collapsed window.
189 SetExpansionState(Panel::EXPANDED); 184 SetExpansionState(Panel::EXPANDED);
190 native_panel_->ActivatePanel(); 185 native_panel_->ActivatePanel();
191 } 186 }
192 187
193 void Panel::Deactivate() { 188 void Panel::Deactivate() {
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 void Panel::ShowAvatarBubble(TabContents* tab_contents, const gfx::Rect& rect) { 536 void Panel::ShowAvatarBubble(TabContents* tab_contents, const gfx::Rect& rect) {
542 // Panels will never show a new tab page so this should never be called. 537 // Panels will never show a new tab page so this should never be called.
543 NOTREACHED(); 538 NOTREACHED();
544 } 539 }
545 540
546 void Panel::ShowAvatarBubbleFromAvatarButton() { 541 void Panel::ShowAvatarBubbleFromAvatarButton() {
547 // Panels will never show an avatar button so this should never be called. 542 // Panels will never show an avatar button so this should never be called.
548 NOTREACHED(); 543 NOTREACHED();
549 } 544 }
550 545
546 void Panel::TabInsertedAt(TabContentsWrapper* contents,
547 int index,
548 bool foreground) {
549 DCHECK_EQ(0, index);
550 TabContents* tab_contents = contents->tab_contents();
551 EnableAutoResize(tab_contents->render_view_host());
552
553 // We also need to know when the render view host changes in order
554 // to turn on preferred size changed notifications in the new
555 // render view host.
556 registrar_.RemoveAll(); // Stop notifications for previous contents, if any.
557 registrar_.Add(
558 this,
559 content::NOTIFICATION_TAB_CONTENTS_SWAPPED,
560 content::Source<TabContents>(tab_contents));
561 }
562
551 void Panel::Observe(int type, 563 void Panel::Observe(int type,
552 const content::NotificationSource& source, 564 const content::NotificationSource& source,
553 const content::NotificationDetails& details) { 565 const content::NotificationDetails& details) {
554 switch (type) { 566 DCHECK_EQ(type, content::NOTIFICATION_TAB_CONTENTS_SWAPPED);
555 case content::NOTIFICATION_TAB_ADDED: 567 RenderViewHost* render_view_host =
556 // We also need to know when the render view host changes in order 568 content::Source<TabContents>(source).ptr()->render_view_host();
557 // to turn on preferred size changed notifications in the new 569 if (render_view_host)
558 // render view host. However, we cannot register for 570 EnableAutoResize(render_view_host);
559 // NOTIFICATION_TAB_CONTENTS_SWAPPED until we actually have a
560 // tab content so we register for it here.
561 registrar_.Add(
562 this,
563 content::NOTIFICATION_TAB_CONTENTS_SWAPPED,
564 content::Source<TabContents>(browser()->GetSelectedTabContents()));
565 // Fall-thru to update render view host.
566
567 case content::NOTIFICATION_TAB_CONTENTS_SWAPPED: {
568 RenderViewHost* render_view_host = GetRenderViewHost();
569 if (render_view_host) {
570 render_view_host->EnablePreferredSizeMode(
571 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow);
572 RequestRenderViewHostToDisableScrollbars(render_view_host);
573 }
574 break;
575 }
576 default:
577 NOTREACHED() << "Got a notification we didn't register for!";
578 break;
579 }
580 } 571 }
581 572
582 RenderViewHost* Panel::GetRenderViewHost() const { 573 RenderViewHost* Panel::GetRenderViewHost() const {
583 TabContents* tab_contents = browser()->GetSelectedTabContents(); 574 TabContents* tab_contents = browser()->GetSelectedTabContents();
584 if (!tab_contents) 575 if (!tab_contents)
585 return NULL; 576 return NULL;
586 return tab_contents->render_view_host(); 577 return tab_contents->render_view_host();
587 } 578 }
588 579
580 void Panel::EnableAutoResize(RenderViewHost* render_view_host) {
581 DCHECK(render_view_host);
582 render_view_host->EnablePreferredSizeMode(
583 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow);
584 RequestRenderViewHostToDisableScrollbars(render_view_host);
585 }
586
589 void Panel::RequestRenderViewHostToDisableScrollbars( 587 void Panel::RequestRenderViewHostToDisableScrollbars(
590 RenderViewHost* render_view_host) { 588 RenderViewHost* render_view_host) {
591 if (render_view_host) { 589 DCHECK(render_view_host);
592 render_view_host->DisableScrollbarsForThreshold( 590 render_view_host->DisableScrollbarsForThreshold(
593 native_panel_->ContentSizeFromWindowSize(max_size_)); 591 native_panel_->ContentSizeFromWindowSize(max_size_));
594 }
595 } 592 }
596 593
597 void Panel::OnWindowSizeAvailable() { 594 void Panel::OnWindowSizeAvailable() {
598 RequestRenderViewHostToDisableScrollbars(GetRenderViewHost()); 595 RenderViewHost* render_view_host = GetRenderViewHost();
596 if (render_view_host)
597 RequestRenderViewHostToDisableScrollbars(render_view_host);
599 } 598 }
600 599
601 Browser* Panel::browser() const { 600 Browser* Panel::browser() const {
602 return native_panel_->GetPanelBrowser(); 601 return native_panel_->GetPanelBrowser();
603 } 602 }
604 603
605 void Panel::DestroyBrowser() { 604 void Panel::DestroyBrowser() {
606 native_panel_->DestroyPanelBrowser(); 605 native_panel_->DestroyPanelBrowser();
607 } 606 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/panels/panel.h ('k') | chrome/browser/ui/panels/panel_browser_view.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698