1 // Copyright (c) 2012 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.
5 #include "chrome/browser/ui/panels/panel.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/app/chrome_command_ids.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/devtools/devtools_window.h"
13 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
14 #include "chrome/browser/extensions/api/tabs/tabs_windows_api.h"
15 #include "chrome/browser/extensions/api/tabs/windows_event_router.h"
16 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/extensions/extension_tab_util.h"
18 #include "chrome/browser/extensions/image_loader.h"
19 #include "chrome/browser/extensions/window_controller.h"
20 #include "chrome/browser/extensions/window_controller_list.h"
21 #include "chrome/browser/lifetime/application_lifetime.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/themes/theme_service.h"
24 #include "chrome/browser/themes/theme_service_factory.h"
25 #include "chrome/browser/ui/panels/native_panel.h"
26 #include "chrome/browser/ui/panels/panel_collection.h"
27 #include "chrome/browser/ui/panels/panel_host.h"
28 #include "chrome/browser/ui/panels/panel_manager.h"
29 #include "chrome/browser/ui/panels/stacked_panel_collection.h"
30 #include "chrome/browser/web_applications/web_app.h"
31 #include "content/public/browser/notification_service.h"
32 #include "content/public/browser/notification_source.h"
33 #include "content/public/browser/notification_types.h"
34 #include "content/public/browser/render_view_host.h"
35 #include "content/public/browser/user_metrics.h"
36 #include "content/public/browser/web_contents.h"
37 #include "extensions/browser/extension_system.h"
38 #include "extensions/common/extension.h"
39 #include "extensions/common/manifest_handlers/icons_handler.h"
40 #include "ui/gfx/image/image.h"
41 #include "ui/gfx/rect.h"
43 using base::UserMetricsAction;
44 using content::RenderViewHost;
46 namespace panel_internal {
48 class PanelExtensionWindowController : public extensions::WindowController {
50 PanelExtensionWindowController(Panel* panel, Profile* profile);
51 virtual ~PanelExtensionWindowController();
53 // Overridden from extensions::WindowController.
54 virtual int GetWindowId() const OVERRIDE;
55 virtual std::string GetWindowTypeText() const OVERRIDE;
56 virtual base::DictionaryValue* CreateWindowValueWithTabs(
57 const extensions::Extension* extension) const OVERRIDE;
58 virtual base::DictionaryValue* CreateTabValue(
59 const extensions::Extension* extension, int tab_index) const OVERRIDE;
60 virtual bool CanClose(Reason* reason) const OVERRIDE;
61 virtual void SetFullscreenMode(bool is_fullscreen,
62 const GURL& extension_url) const OVERRIDE;
63 virtual bool IsVisibleToExtension(
64 const extensions::Extension* extension) const OVERRIDE;
67 Panel* panel_; // Weak pointer. Owns us.
68 DISALLOW_COPY_AND_ASSIGN(PanelExtensionWindowController);
71 PanelExtensionWindowController::PanelExtensionWindowController(
72 Panel* panel, Profile* profile)
73 : extensions::WindowController(panel, profile),
75 extensions::WindowControllerList::GetInstance()->AddExtensionWindow(this);
78 PanelExtensionWindowController::~PanelExtensionWindowController() {
79 extensions::WindowControllerList::GetInstance()->RemoveExtensionWindow(this);
82 int PanelExtensionWindowController::GetWindowId() const {
83 return static_cast<int>(panel_->session_id().id());
86 std::string PanelExtensionWindowController::GetWindowTypeText() const {
87 return extensions::tabs_constants::kWindowTypeValuePanel;
90 base::DictionaryValue*
91 PanelExtensionWindowController::CreateWindowValueWithTabs(
92 const extensions::Extension* extension) const {
93 base::DictionaryValue* result = CreateWindowValue();
95 DCHECK(IsVisibleToExtension(extension));
96 base::DictionaryValue* tab_value = CreateTabValue(extension, 0);
98 base::ListValue* tab_list = new base::ListValue();
99 tab_list->Append(tab_value);
100 result->Set(extensions::tabs_constants::kTabsKey, tab_list);
105 base::DictionaryValue* PanelExtensionWindowController::CreateTabValue(
106 const extensions::Extension* extension, int tab_index) const {
110 content::WebContents* web_contents = panel_->GetWebContents();
114 DCHECK(IsVisibleToExtension(extension));
115 base::DictionaryValue* tab_value = new base::DictionaryValue();
116 tab_value->SetInteger(extensions::tabs_constants::kIdKey,
117 SessionID::IdForTab(web_contents));
118 tab_value->SetInteger(extensions::tabs_constants::kIndexKey, 0);
119 tab_value->SetInteger(extensions::tabs_constants::kWindowIdKey,
120 SessionID::IdForWindowContainingTab(web_contents));
121 tab_value->SetString(
122 extensions::tabs_constants::kUrlKey, web_contents->GetURL().spec());
123 tab_value->SetString(extensions::tabs_constants::kStatusKey,
124 extensions::ExtensionTabUtil::GetTabStatusText(
125 web_contents->IsLoading()));
126 tab_value->SetBoolean(
127 extensions::tabs_constants::kActiveKey, panel_->IsActive());
128 tab_value->SetBoolean(extensions::tabs_constants::kSelectedKey, true);
129 tab_value->SetBoolean(extensions::tabs_constants::kHighlightedKey, true);
130 tab_value->SetBoolean(extensions::tabs_constants::kPinnedKey, false);
131 tab_value->SetString(
132 extensions::tabs_constants::kTitleKey, web_contents->GetTitle());
133 tab_value->SetBoolean(
134 extensions::tabs_constants::kIncognitoKey,
135 web_contents->GetBrowserContext()->IsOffTheRecord());
139 bool PanelExtensionWindowController::CanClose(Reason* reason) const {
143 void PanelExtensionWindowController::SetFullscreenMode(
144 bool is_fullscreen, const GURL& extension_url) const {
145 // Do nothing. Panels cannot be fullscreen.
148 bool PanelExtensionWindowController::IsVisibleToExtension(
149 const extensions::Extension* extension) const {
150 return extension->id() == panel_->extension_id();
153 } // namespace panel_internal
156 DCHECK(!collection_);
157 #if !defined(USE_AURA)
158 // Invoked by native panel destructor. Do not access native_panel_ here.
159 chrome::DecrementKeepAliveCount(); // Remove shutdown prevention.
163 PanelManager* Panel::manager() const {
164 return PanelManager::GetInstance();
167 const std::string Panel::extension_id() const {
168 return web_app::GetExtensionIdFromApplicationName(app_name_);
171 CommandUpdater* Panel::command_updater() {
172 return &command_updater_;
175 Profile* Panel::profile() const {
179 const extensions::Extension* Panel::GetExtension() const {
180 ExtensionService* extension_service =
181 extensions::ExtensionSystem::Get(profile())->extension_service();
182 if (!extension_service || !extension_service->is_ready())
184 return extension_service->GetExtensionById(extension_id(), false);
187 content::WebContents* Panel::GetWebContents() const {
188 return panel_host_.get() ? panel_host_->web_contents() : NULL;
191 void Panel::SetExpansionState(ExpansionState new_state) {
192 if (expansion_state_ == new_state)
194 native_panel_->PanelExpansionStateChanging(expansion_state_, new_state);
195 expansion_state_ = new_state;
197 manager()->OnPanelExpansionStateChanged(this);
199 DCHECK(initialized_ && collection_ != NULL);
200 native_panel_->PreventActivationByOS(collection_->IsPanelMinimized(this));
201 UpdateMinimizeRestoreButtonVisibility();
203 content::NotificationService::current()->Notify(
204 chrome::NOTIFICATION_PANEL_CHANGED_EXPANSION_STATE,
205 content::Source<Panel>(this),
206 content::NotificationService::NoDetails());
209 bool Panel::IsDrawingAttention() const {
210 return native_panel_->IsDrawingAttention();
213 void Panel::FullScreenModeChanged(bool is_full_screen) {
214 native_panel_->FullScreenModeChanged(is_full_screen);
217 int Panel::TitleOnlyHeight() const {
218 return native_panel_->TitleOnlyHeight();
221 bool Panel::CanShowMinimizeButton() const {
222 return collection_ && collection_->CanShowMinimizeButton(this);
225 bool Panel::CanShowRestoreButton() const {
226 return collection_ && collection_->CanShowRestoreButton(this);
229 bool Panel::IsActive() const {
230 return native_panel_->IsPanelActive();
233 bool Panel::IsMaximized() const {
234 // Size of panels is managed by PanelManager, they are never 'zoomed'.
238 bool Panel::IsMinimized() const {
239 return !collection_ || collection_->IsPanelMinimized(this);
242 bool Panel::IsFullscreen() const {
246 gfx::NativeWindow Panel::GetNativeWindow() {
247 return native_panel_->GetNativePanelWindow();
250 gfx::Rect Panel::GetRestoredBounds() const {
251 gfx::Rect bounds = native_panel_->GetPanelBounds();
252 bounds.set_y(bounds.bottom() - full_size_.height());
253 bounds.set_x(bounds.right() - full_size_.width());
254 bounds.set_size(full_size_);
258 ui::WindowShowState Panel::GetRestoredState() const {
259 return ui::SHOW_STATE_NORMAL;
262 gfx::Rect Panel::GetBounds() const {
263 return native_panel_->GetPanelBounds();
267 if (manager()->display_settings_provider()->is_full_screen() || !collection_)
270 native_panel_->ShowPanel();
277 void Panel::ShowInactive() {
278 if (manager()->display_settings_provider()->is_full_screen() || !collection_)
281 native_panel_->ShowPanelInactive();
284 // Close() may be called multiple times if the panel window is not ready to
285 // close on the first attempt.
286 void Panel::Close() {
287 native_panel_->ClosePanel();
290 void Panel::Activate() {
294 collection_->ActivatePanel(this);
295 native_panel_->ActivatePanel();
298 void Panel::Deactivate() {
299 native_panel_->DeactivatePanel();
302 void Panel::Maximize() {
306 void Panel::Minimize() {
308 collection_->MinimizePanel(this);
311 bool Panel::IsMinimizedBySystem() const {
312 return native_panel_->IsPanelMinimizedBySystem();
315 bool Panel::IsShownOnActiveDesktop() const {
316 return native_panel_->IsPanelShownOnActiveDesktop();
319 void Panel::ShowShadow(bool show) {
320 native_panel_->ShowShadow(show);
323 void Panel::Restore() {
325 collection_->RestorePanel(this);
328 void Panel::SetBounds(const gfx::Rect& bounds) {
329 // Ignore bounds position as the panel manager controls all positioning.
332 collection_->ResizePanelWindow(this, bounds.size());
333 SetAutoResizable(false);
336 void Panel::FlashFrame(bool draw_attention) {
337 if (IsDrawingAttention() == draw_attention || !collection_)
340 // Don't draw attention for an active panel.
341 if (draw_attention && IsActive())
344 // Invoking native panel to draw attention must be done before informing the
345 // panel collection because it needs to check internal state of the panel to
346 // determine if the panel has been drawing attention.
347 native_panel_->DrawAttention(draw_attention);
348 collection_->OnPanelAttentionStateChanged(this);
351 bool Panel::IsAlwaysOnTop() const {
352 return native_panel_->IsPanelAlwaysOnTop();
355 void Panel::SetAlwaysOnTop(bool on_top) {
356 native_panel_->SetPanelAlwaysOnTop(on_top);
359 void Panel::ExecuteCommandWithDisposition(int id,
360 WindowOpenDisposition disposition) {
361 DCHECK(command_updater_.IsCommandEnabled(id)) << "Invalid/disabled command "
364 if (!GetWebContents())
370 panel_host_->Reload();
372 case IDC_RELOAD_IGNORING_CACHE:
373 panel_host_->ReloadIgnoringCache();
376 panel_host_->StopLoading();
380 case IDC_CLOSE_WINDOW:
381 content::RecordAction(UserMetricsAction("CloseWindow"));
385 content::RecordAction(UserMetricsAction("Exit"));
386 chrome::AttemptUserExit();
391 content::RecordAction(UserMetricsAction("Copy"));
392 native_panel_->PanelCopy();
395 content::RecordAction(UserMetricsAction("Cut"));
396 native_panel_->PanelCut();
399 content::RecordAction(UserMetricsAction("Paste"));
400 native_panel_->PanelPaste();
405 panel_host_->Zoom(content::PAGE_ZOOM_IN);
407 case IDC_ZOOM_NORMAL:
408 panel_host_->Zoom(content::PAGE_ZOOM_RESET);
411 panel_host_->Zoom(content::PAGE_ZOOM_OUT);
416 content::RecordAction(UserMetricsAction("DevTools_ToggleWindow"));
417 DevToolsWindow::OpenDevToolsWindow(
418 GetWebContents()->GetRenderViewHost(),
419 DevToolsToggleAction::Show());
421 case IDC_DEV_TOOLS_CONSOLE:
422 content::RecordAction(UserMetricsAction("DevTools_ToggleConsole"));
423 DevToolsWindow::OpenDevToolsWindow(
424 GetWebContents()->GetRenderViewHost(),
425 DevToolsToggleAction::ShowConsole());
429 LOG(WARNING) << "Received unimplemented command: " << id;
434 void Panel::Observe(int type,
435 const content::NotificationSource& source,
436 const content::NotificationDetails& details) {
438 case content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED:
439 ConfigureAutoResize(content::Source<content::WebContents>(source).ptr());
441 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED:
442 if (content::Details<extensions::UnloadedExtensionInfo>(
443 details)->extension->id() == extension_id())
446 case chrome::NOTIFICATION_APP_TERMINATING:
450 NOTREACHED() << "Received unexpected notification " << type;
454 void Panel::OnTitlebarClicked(panel::ClickModifier modifier) {
456 collection_->OnPanelTitlebarClicked(this, modifier);
458 // Normally the system activates a window when the titlebar is clicked.
459 // However, we prevent system activation of minimized panels, thus the
460 // activation may not have occurred. Also, some OSes (Windows) will
461 // activate a minimized panel on mouse-down regardless of our attempts to
462 // prevent system activation. Attention state is not cleared in that case.
463 // See Panel::OnActiveStateChanged().
464 // Therefore, we ensure activation and clearing of attention state if the
465 // panel has been expanded. If the panel is in a stack, the titlebar click
466 // might minimize the panel and we do not want to activate it to make it
468 // These are no-ops if no changes are needed.
475 void Panel::OnMinimizeButtonClicked(panel::ClickModifier modifier) {
477 collection_->OnMinimizeButtonClicked(this, modifier);
480 void Panel::OnRestoreButtonClicked(panel::ClickModifier modifier) {
481 // Clicking the restore button has the same behavior as clicking the titlebar.
482 OnTitlebarClicked(modifier);
485 void Panel::OnWindowSizeAvailable() {
486 ConfigureAutoResize(GetWebContents());
489 void Panel::OnNativePanelClosed() {
490 // Ensure previously enqueued OnImageLoaded callbacks are ignored.
491 image_loader_ptr_factory_.InvalidateWeakPtrs();
492 registrar_.RemoveAll();
493 manager()->OnPanelClosed(this);
494 DCHECK(!collection_);
497 StackedPanelCollection* Panel::stack() const {
498 return collection_ && collection_->type() == PanelCollection::STACKED ?
499 static_cast<StackedPanelCollection*>(collection_) : NULL;
502 panel::Resizability Panel::CanResizeByMouse() const {
504 return panel::NOT_RESIZABLE;
506 return collection_->GetPanelResizability(this);
509 void Panel::Initialize(const GURL& url,
510 const gfx::Rect& bounds,
511 bool always_on_top) {
512 DCHECK(!initialized_);
513 DCHECK(!collection_); // Cannot be added to a collection until fully created.
514 DCHECK_EQ(EXPANDED, expansion_state_);
515 DCHECK(!bounds.IsEmpty());
517 full_size_ = bounds.size();
518 native_panel_ = CreateNativePanel(this, bounds, always_on_top);
520 extension_window_controller_.reset(
521 new panel_internal::PanelExtensionWindowController(this, profile_));
525 // Set up hosting for web contents.
526 panel_host_.reset(new PanelHost(this, profile_));
527 panel_host_->Init(url);
528 content::WebContents* web_contents = GetWebContents();
529 // The contents might be NULL for most of our tests.
531 native_panel_->AttachWebContents(web_contents);
533 // Close when the extension is unloaded or the browser is exiting.
534 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
535 content::Source<Profile>(profile_));
536 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
537 content::NotificationService::AllSources());
538 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
539 content::Source<ThemeService>(
540 ThemeServiceFactory::GetForProfile(profile_)));
542 #if !defined(USE_AURA)
543 // Keep alive for AURA has been moved to panel_view.
544 // Prevent the browser process from shutting down while this window is open.
545 chrome::IncrementKeepAliveCount();
551 void Panel::SetPanelBounds(const gfx::Rect& bounds) {
552 if (bounds != native_panel_->GetPanelBounds())
553 native_panel_->SetPanelBounds(bounds);
556 void Panel::SetPanelBoundsInstantly(const gfx::Rect& bounds) {
557 native_panel_->SetPanelBoundsInstantly(bounds);
560 void Panel::LimitSizeToWorkArea(const gfx::Rect& work_area) {
561 int max_width = manager()->GetMaxPanelWidth(work_area);
562 int max_height = manager()->GetMaxPanelHeight(work_area);
564 // If the custom max size is used, ensure that it does not exceed the display
566 if (max_size_policy_ == CUSTOM_MAX_SIZE) {
567 int current_max_width = max_size_.width();
568 if (current_max_width > max_width)
569 max_width = std::min(current_max_width, work_area.width());
570 int current_max_height = max_size_.height();
571 if (current_max_height > max_height)
572 max_height = std::min(current_max_height, work_area.height());
575 SetSizeRange(min_size_, gfx::Size(max_width, max_height));
577 // Ensure that full size does not exceed max size.
578 full_size_ = ClampSize(full_size_);
581 void Panel::SetAutoResizable(bool resizable) {
582 if (auto_resizable_ == resizable)
585 auto_resizable_ = resizable;
586 content::WebContents* web_contents = GetWebContents();
587 if (auto_resizable_) {
589 EnableWebContentsAutoResize(web_contents);
592 registrar_.Remove(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
593 content::Source<content::WebContents>(web_contents));
595 // NULL might be returned if the tab has not been added.
596 RenderViewHost* render_view_host = web_contents->GetRenderViewHost();
597 if (render_view_host)
598 render_view_host->DisableAutoResize(full_size_);
603 void Panel::EnableWebContentsAutoResize(content::WebContents* web_contents) {
604 DCHECK(web_contents);
605 ConfigureAutoResize(web_contents);
607 // We also need to know when the render view host changes in order
608 // to turn on auto-resize notifications in the new render view host.
609 if (!registrar_.IsRegistered(
610 this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
611 content::Source<content::WebContents>(web_contents))) {
614 content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
615 content::Source<content::WebContents>(web_contents));
619 void Panel::OnContentsAutoResized(const gfx::Size& new_content_size) {
620 DCHECK(auto_resizable_);
624 gfx::Size new_window_size =
625 native_panel_->WindowSizeFromContentSize(new_content_size);
627 // Ignore content auto resizes until window frame size is known.
628 // This reduces extra resizes when panel is first shown.
629 // After window frame size is known, it will trigger another content
631 if (new_content_size == new_window_size)
634 collection_->ResizePanelWindow(this, new_window_size);
637 void Panel::OnWindowResizedByMouse(const gfx::Rect& new_bounds) {
639 collection_->OnPanelResizedByMouse(this, new_bounds);
642 void Panel::SetSizeRange(const gfx::Size& min_size, const gfx::Size& max_size) {
643 if (min_size == min_size_ && max_size == max_size_)
646 DCHECK(min_size.width() <= max_size.width());
647 DCHECK(min_size.height() <= max_size.height());
648 min_size_ = min_size;
649 max_size_ = max_size;
651 ConfigureAutoResize(GetWebContents());
654 void Panel::IncreaseMaxSize(const gfx::Size& desired_panel_size) {
655 gfx::Size new_max_size = max_size_;
656 if (new_max_size.width() < desired_panel_size.width())
657 new_max_size.set_width(desired_panel_size.width());
658 if (new_max_size.height() < desired_panel_size.height())
659 new_max_size.set_height(desired_panel_size.height());
661 SetSizeRange(min_size_, new_max_size);
664 void Panel::HandleKeyboardEvent(const content::NativeWebKeyboardEvent& event) {
665 native_panel_->HandlePanelKeyboardEvent(event);
668 void Panel::SetPreviewMode(bool in_preview) {
669 DCHECK_NE(in_preview_mode_, in_preview);
670 in_preview_mode_ = in_preview;
673 void Panel::UpdateMinimizeRestoreButtonVisibility() {
674 native_panel_->UpdatePanelMinimizeRestoreButtonVisibility();
677 gfx::Size Panel::ClampSize(const gfx::Size& size) const {
679 // * cannot grow or shrink to go beyond [min_width, max_width]
680 int new_width = size.width();
681 if (new_width > max_size_.width())
682 new_width = max_size_.width();
683 if (new_width < min_size_.width())
684 new_width = min_size_.width();
687 // * cannot grow or shrink to go beyond [min_height, max_height]
688 int new_height = size.height();
689 if (new_height > max_size_.height())
690 new_height = max_size_.height();
691 if (new_height < min_size_.height())
692 new_height = min_size_.height();
694 return gfx::Size(new_width, new_height);
697 void Panel::OnActiveStateChanged(bool active) {
698 // Clear attention state when an expanded panel becomes active.
699 // On some systems (e.g. Win), mouse-down activates a panel regardless of
700 // its expansion state. However, we don't want to clear draw attention if
701 // contents are not visible. In that scenario, if the mouse-down results
702 // in a mouse-click, draw attention will be cleared then.
703 // See Panel::OnTitlebarClicked().
704 if (active && IsDrawingAttention() && !IsMinimized())
708 collection_->OnPanelActiveStateChanged(this);
710 // Send extension event about window changing active state.
711 extensions::TabsWindowsAPI* tabs_windows_api =
712 extensions::TabsWindowsAPI::Get(profile());
713 if (tabs_windows_api) {
714 tabs_windows_api->windows_event_router()->OnActiveWindowChanged(
715 active ? extension_window_controller_.get() : NULL);
718 content::NotificationService::current()->Notify(
719 chrome::NOTIFICATION_PANEL_CHANGED_ACTIVE_STATUS,
720 content::Source<Panel>(this),
721 content::NotificationService::NoDetails());
724 void Panel::OnPanelStartUserResizing() {
725 SetAutoResizable(false);
726 SetPreviewMode(true);
727 max_size_policy_ = CUSTOM_MAX_SIZE;
730 void Panel::OnPanelEndUserResizing() {
731 SetPreviewMode(false);
734 bool Panel::ShouldCloseWindow() {
738 void Panel::OnWindowClosing() {
739 if (GetWebContents()) {
740 native_panel_->DetachWebContents(GetWebContents());
741 panel_host_->DestroyWebContents();
745 bool Panel::ExecuteCommandIfEnabled(int id) {
746 if (command_updater()->SupportsCommand(id) &&
747 command_updater()->IsCommandEnabled(id)) {
748 ExecuteCommandWithDisposition(id, CURRENT_TAB);
754 base::string16 Panel::GetWindowTitle() const {
755 content::WebContents* contents = GetWebContents();
756 base::string16 title;
758 // |contents| can be NULL during the window's creation.
760 title = contents->GetTitle();
761 FormatTitleForDisplay(&title);
765 title = base::UTF8ToUTF16(app_name());
770 gfx::Image Panel::GetCurrentPageIcon() const {
771 return panel_host_->GetPageIcon();
774 void Panel::UpdateTitleBar() {
775 native_panel_->UpdatePanelTitleBar();
778 void Panel::LoadingStateChanged(bool is_loading) {
779 command_updater_.UpdateCommandEnabled(IDC_STOP, is_loading);
780 native_panel_->UpdatePanelLoadingAnimations(is_loading);
784 void Panel::WebContentsFocused(content::WebContents* contents) {
785 native_panel_->PanelWebContentsFocused(contents);
788 void Panel::MoveByInstantly(const gfx::Vector2d& delta_origin) {
789 gfx::Rect bounds = GetBounds();
790 bounds.Offset(delta_origin);
791 SetPanelBoundsInstantly(bounds);
794 void Panel::SetWindowCornerStyle(panel::CornerStyle corner_style) {
795 native_panel_->SetWindowCornerStyle(corner_style);
798 void Panel::MinimizeBySystem() {
799 native_panel_->MinimizePanelBySystem();
802 Panel::Panel(Profile* profile, const std::string& app_name,
803 const gfx::Size& min_size, const gfx::Size& max_size)
804 : app_name_(app_name),
810 max_size_policy_(DEFAULT_MAX_SIZE),
811 auto_resizable_(false),
812 in_preview_mode_(false),
814 attention_mode_(USE_PANEL_ATTENTION),
815 expansion_state_(EXPANDED),
816 command_updater_(this),
817 image_loader_ptr_factory_(this) {
820 void Panel::OnImageLoaded(const gfx::Image& image) {
821 if (!image.IsEmpty()) {
823 native_panel_->UpdatePanelTitleBar();
826 content::NotificationService::current()->Notify(
827 chrome::NOTIFICATION_PANEL_APP_ICON_LOADED,
828 content::Source<Panel>(this),
829 content::NotificationService::NoDetails());
832 void Panel::InitCommandState() {
833 // All supported commands whose state isn't set automagically some other way
834 // (like Stop during a page load) must have their state initialized here,
835 // otherwise they will be forever disabled.
837 // Navigation commands
838 command_updater_.UpdateCommandEnabled(IDC_RELOAD, true);
839 command_updater_.UpdateCommandEnabled(IDC_RELOAD_IGNORING_CACHE, true);
841 // Window management commands
842 command_updater_.UpdateCommandEnabled(IDC_CLOSE_WINDOW, true);
843 command_updater_.UpdateCommandEnabled(IDC_EXIT, true);
846 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MENU, true);
847 command_updater_.UpdateCommandEnabled(IDC_ZOOM_PLUS, true);
848 command_updater_.UpdateCommandEnabled(IDC_ZOOM_NORMAL, true);
849 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MINUS, true);
852 command_updater_.UpdateCommandEnabled(IDC_COPY, true);
853 command_updater_.UpdateCommandEnabled(IDC_CUT, true);
854 command_updater_.UpdateCommandEnabled(IDC_PASTE, true);
857 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS, true);
858 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_CONSOLE, true);
861 void Panel::ConfigureAutoResize(content::WebContents* web_contents) {
862 if (!auto_resizable_ || !web_contents)
865 // NULL might be returned if the tab has not been added.
866 RenderViewHost* render_view_host = web_contents->GetRenderViewHost();
867 if (!render_view_host)
870 render_view_host->EnableAutoResize(
872 native_panel_->ContentSizeFromWindowSize(max_size_));
875 void Panel::UpdateAppIcon() {
876 const extensions::Extension* extension = GetExtension();
880 extensions::ImageLoader* loader = extensions::ImageLoader::Get(profile());
881 loader->LoadImageAsync(
883 extensions::IconsInfo::GetIconResource(
885 extension_misc::EXTENSION_ICON_SMALL,
886 ExtensionIconSet::MATCH_BIGGER),
887 gfx::Size(extension_misc::EXTENSION_ICON_SMALL,
888 extension_misc::EXTENSION_ICON_SMALL),
889 base::Bind(&Panel::OnImageLoaded,
890 image_loader_ptr_factory_.GetWeakPtr()));
894 void Panel::FormatTitleForDisplay(base::string16* title) {
895 size_t current_index = 0;
897 while ((match_index = title->find(L'\n', current_index)) !=
898 base::string16::npos) {
899 title->replace(match_index, 1, base::string16());
900 current_index = match_index;