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/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" |
(...skipping 15 matching lines...) Expand all Loading... |
26 // static | 26 // static |
27 const Extension* Panel::GetExtensionFromBrowser(Browser* browser) { | 27 const Extension* Panel::GetExtensionFromBrowser(Browser* browser) { |
28 // Find the extension. When we create a panel from an extension, the extension | 28 // Find the extension. When we create a panel from an extension, the extension |
29 // ID is passed as the app name to the Browser. | 29 // ID is passed as the app name to the Browser. |
30 ExtensionService* extension_service = | 30 ExtensionService* extension_service = |
31 browser->GetProfile()->GetExtensionService(); | 31 browser->GetProfile()->GetExtensionService(); |
32 return extension_service->GetExtensionById( | 32 return extension_service->GetExtensionById( |
33 web_app::GetExtensionIdFromApplicationName(browser->app_name()), false); | 33 web_app::GetExtensionIdFromApplicationName(browser->app_name()), false); |
34 } | 34 } |
35 | 35 |
36 Panel::Panel(Browser* browser, const gfx::Rect& bounds) | 36 Panel::Panel(Browser* browser, |
37 : min_size_(bounds.size()), | 37 const gfx::Rect& bounds, |
38 max_size_(bounds.size()), | 38 const gfx::Size& min_size, |
39 native_panel_(NULL), | 39 const gfx::Size& max_size, |
| 40 bool auto_resizable) |
| 41 : min_size_(min_size), |
| 42 max_size_(max_size), |
| 43 auto_resizable_(false), |
40 expansion_state_(EXPANDED), | 44 expansion_state_(EXPANDED), |
41 restored_height_(bounds.height()) { | 45 restored_height_(bounds.height()) { |
42 native_panel_ = CreateNativePanel(browser, this, bounds); | 46 native_panel_ = CreateNativePanel(browser, this, bounds); |
43 browser->tabstrip_model()->AddObserver(this); | 47 SetAutoResizable(auto_resizable); |
44 } | 48 } |
45 | 49 |
46 Panel::~Panel() { | 50 Panel::~Panel() { |
47 // Invoked by native panel destructor. Do not access native_panel_ here. | 51 // Invoked by native panel destructor. Do not access native_panel_ here. |
48 } | 52 } |
49 | 53 |
50 void Panel::OnNativePanelClosed() { | 54 void Panel::OnNativePanelClosed() { |
51 native_panel_->GetPanelBrowser()->tabstrip_model()->RemoveObserver(this); | 55 if (auto_resizable_) |
| 56 native_panel_->GetPanelBrowser()->tabstrip_model()->RemoveObserver(this); |
52 manager()->Remove(this); | 57 manager()->Remove(this); |
53 } | 58 } |
54 | 59 |
55 PanelManager* Panel::manager() const { | 60 PanelManager* Panel::manager() const { |
56 return PanelManager::GetInstance(); | 61 return PanelManager::GetInstance(); |
57 } | 62 } |
58 | 63 |
59 const Extension* Panel::GetExtension() const { | 64 const Extension* Panel::GetExtension() const { |
60 return GetExtensionFromBrowser(browser()); | 65 return GetExtensionFromBrowser(browser()); |
61 } | 66 } |
62 | 67 |
63 void Panel::SetPanelBounds(const gfx::Rect& bounds) { | 68 void Panel::SetPanelBounds(const gfx::Rect& bounds) { |
64 if (expansion_state_ == Panel::EXPANDED) | 69 if (expansion_state_ == Panel::EXPANDED) |
65 restored_height_ = bounds.height(); | 70 restored_height_ = bounds.height(); |
66 | 71 |
67 native_panel_->SetPanelBounds(bounds); | 72 native_panel_->SetPanelBounds(bounds); |
68 | 73 |
69 content::NotificationService::current()->Notify( | 74 content::NotificationService::current()->Notify( |
70 chrome::NOTIFICATION_PANEL_CHANGED_BOUNDS, | 75 chrome::NOTIFICATION_PANEL_CHANGED_BOUNDS, |
71 content::Source<Panel>(this), | 76 content::Source<Panel>(this), |
72 content::NotificationService::NoDetails()); | 77 content::NotificationService::NoDetails()); |
73 } | 78 } |
74 | 79 |
75 void Panel::SetMaxSize(const gfx::Size& max_size) { | 80 void Panel::SetAutoResizable(bool resizable) { |
76 if (max_size_ == max_size) | 81 if (auto_resizable_ == resizable) |
77 return; | 82 return; |
78 max_size_ = max_size; | |
79 | 83 |
80 // Note: |render_view_host| might be NULL if the tab has not been created. | 84 auto_resizable_ = resizable; |
81 // If so, we will do it when TabInsertedAt() is invoked. | 85 if (auto_resizable_) { |
82 RenderViewHost* render_view_host = GetRenderViewHost(); | 86 browser()->tabstrip_model()->AddObserver(this); |
83 if (render_view_host) | 87 TabContents* tab_contents = browser()->GetSelectedTabContents(); |
84 RequestRenderViewHostToDisableScrollbars(render_view_host); | 88 if (tab_contents) |
| 89 EnableTabContentsAutoResize(tab_contents); |
| 90 } else { |
| 91 browser()->tabstrip_model()->RemoveObserver(this); |
| 92 registrar_.RemoveAll(); |
| 93 } |
85 } | 94 } |
86 | 95 |
87 void Panel::SetExpansionState(ExpansionState new_state) { | 96 void Panel::SetExpansionState(ExpansionState new_state) { |
88 if (expansion_state_ == new_state) | 97 if (expansion_state_ == new_state) |
89 return; | 98 return; |
90 | 99 |
91 ExpansionState old_state = expansion_state_; | 100 ExpansionState old_state = expansion_state_; |
92 expansion_state_ = new_state; | 101 expansion_state_ = new_state; |
93 | 102 |
94 int height; | 103 int height; |
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 NOTIMPLEMENTED(); | 529 NOTIMPLEMENTED(); |
521 } | 530 } |
522 | 531 |
523 void Panel::ShowKeyboardOverlay(gfx::NativeWindow owning_window) { | 532 void Panel::ShowKeyboardOverlay(gfx::NativeWindow owning_window) { |
524 NOTIMPLEMENTED(); | 533 NOTIMPLEMENTED(); |
525 } | 534 } |
526 #endif | 535 #endif |
527 | 536 |
528 void Panel::UpdatePreferredSize(TabContents* tab_contents, | 537 void Panel::UpdatePreferredSize(TabContents* tab_contents, |
529 const gfx::Size& pref_size) { | 538 const gfx::Size& pref_size) { |
530 return manager()->OnPreferredWindowSizeChanged(this, | 539 if (auto_resizable_) { |
531 native_panel_->WindowSizeFromContentSize(pref_size)); | 540 return manager()->OnPreferredWindowSizeChanged(this, |
| 541 native_panel_->WindowSizeFromContentSize(pref_size)); |
| 542 } |
532 } | 543 } |
533 | 544 |
534 void Panel::ShowAvatarBubble(TabContents* tab_contents, const gfx::Rect& rect) { | 545 void Panel::ShowAvatarBubble(TabContents* tab_contents, const gfx::Rect& rect) { |
535 // Panels will never show a new tab page so this should never be called. | 546 // Panels will never show a new tab page so this should never be called. |
536 NOTREACHED(); | 547 NOTREACHED(); |
537 } | 548 } |
538 | 549 |
539 void Panel::ShowAvatarBubbleFromAvatarButton() { | 550 void Panel::ShowAvatarBubbleFromAvatarButton() { |
540 // Panels will never show an avatar button so this should never be called. | 551 // Panels will never show an avatar button so this should never be called. |
541 NOTREACHED(); | 552 NOTREACHED(); |
542 } | 553 } |
543 | 554 |
544 void Panel::TabInsertedAt(TabContentsWrapper* contents, | 555 void Panel::TabInsertedAt(TabContentsWrapper* contents, |
545 int index, | 556 int index, |
546 bool foreground) { | 557 bool foreground) { |
547 DCHECK_EQ(0, index); | 558 if (auto_resizable_) { |
548 TabContents* tab_contents = contents->tab_contents(); | 559 DCHECK_EQ(0, index); |
549 EnableAutoResize(tab_contents->render_view_host()); | 560 EnableTabContentsAutoResize(contents->tab_contents()); |
| 561 } |
| 562 } |
| 563 |
| 564 void Panel::EnableTabContentsAutoResize(TabContents* tab_contents) { |
| 565 DCHECK(tab_contents); |
| 566 RenderViewHost* render_view_host = tab_contents->render_view_host(); |
| 567 if (render_view_host) |
| 568 EnableRendererAutoResize(render_view_host); |
550 | 569 |
551 // We also need to know when the render view host changes in order | 570 // We also need to know when the render view host changes in order |
552 // to turn on preferred size changed notifications in the new | 571 // to turn on preferred size changed notifications in the new |
553 // render view host. | 572 // render view host. |
554 registrar_.RemoveAll(); // Stop notifications for previous contents, if any. | 573 registrar_.RemoveAll(); // Stop notifications for previous contents, if any. |
555 registrar_.Add( | 574 registrar_.Add( |
556 this, | 575 this, |
557 content::NOTIFICATION_TAB_CONTENTS_SWAPPED, | 576 content::NOTIFICATION_TAB_CONTENTS_SWAPPED, |
558 content::Source<TabContents>(tab_contents)); | 577 content::Source<TabContents>(tab_contents)); |
559 } | 578 } |
560 | 579 |
561 void Panel::Observe(int type, | 580 void Panel::Observe(int type, |
562 const content::NotificationSource& source, | 581 const content::NotificationSource& source, |
563 const content::NotificationDetails& details) { | 582 const content::NotificationDetails& details) { |
564 DCHECK_EQ(type, content::NOTIFICATION_TAB_CONTENTS_SWAPPED); | 583 if (auto_resizable_) { |
565 RenderViewHost* render_view_host = | 584 DCHECK_EQ(type, content::NOTIFICATION_TAB_CONTENTS_SWAPPED); |
566 content::Source<TabContents>(source).ptr()->render_view_host(); | 585 RenderViewHost* render_view_host = |
567 if (render_view_host) | 586 content::Source<TabContents>(source).ptr()->render_view_host(); |
568 EnableAutoResize(render_view_host); | 587 if (render_view_host) |
| 588 EnableRendererAutoResize(render_view_host); |
| 589 } |
569 } | 590 } |
570 | 591 |
571 RenderViewHost* Panel::GetRenderViewHost() const { | 592 RenderViewHost* Panel::GetRenderViewHost() const { |
572 TabContents* tab_contents = browser()->GetSelectedTabContents(); | 593 TabContents* tab_contents = browser()->GetSelectedTabContents(); |
573 if (!tab_contents) | 594 if (!tab_contents) |
574 return NULL; | 595 return NULL; |
575 return tab_contents->render_view_host(); | 596 return tab_contents->render_view_host(); |
576 } | 597 } |
577 | 598 |
578 void Panel::EnableAutoResize(RenderViewHost* render_view_host) { | 599 void Panel::EnableRendererAutoResize(RenderViewHost* render_view_host) { |
| 600 DCHECK(auto_resizable_); |
579 DCHECK(render_view_host); | 601 DCHECK(render_view_host); |
580 render_view_host->EnablePreferredSizeMode(); | 602 render_view_host->EnablePreferredSizeMode(); |
581 RequestRenderViewHostToDisableScrollbars(render_view_host); | 603 RequestRenderViewHostToDisableScrollbars(render_view_host); |
582 } | 604 } |
583 | 605 |
584 void Panel::RequestRenderViewHostToDisableScrollbars( | 606 void Panel::RequestRenderViewHostToDisableScrollbars( |
585 RenderViewHost* render_view_host) { | 607 RenderViewHost* render_view_host) { |
| 608 DCHECK(auto_resizable_); |
586 DCHECK(render_view_host); | 609 DCHECK(render_view_host); |
587 render_view_host->DisableScrollbarsForThreshold( | 610 render_view_host->DisableScrollbarsForThreshold( |
588 native_panel_->ContentSizeFromWindowSize(max_size_)); | 611 native_panel_->ContentSizeFromWindowSize(max_size_)); |
589 } | 612 } |
590 | 613 |
591 void Panel::OnWindowSizeAvailable() { | 614 void Panel::OnWindowSizeAvailable() { |
592 RenderViewHost* render_view_host = GetRenderViewHost(); | 615 if (auto_resizable_) { |
593 if (render_view_host) | 616 RenderViewHost* render_view_host = GetRenderViewHost(); |
594 RequestRenderViewHostToDisableScrollbars(render_view_host); | 617 if (render_view_host) |
| 618 RequestRenderViewHostToDisableScrollbars(render_view_host); |
| 619 } |
595 } | 620 } |
596 | 621 |
597 Browser* Panel::browser() const { | 622 Browser* Panel::browser() const { |
598 return native_panel_->GetPanelBrowser(); | 623 return native_panel_->GetPanelBrowser(); |
599 } | 624 } |
600 | 625 |
601 void Panel::DestroyBrowser() { | 626 void Panel::DestroyBrowser() { |
602 native_panel_->DestroyPanelBrowser(); | 627 native_panel_->DestroyPanelBrowser(); |
603 } | 628 } |
OLD | NEW |