cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / chrome / browser / ui / panels / panel.cc
blob56618ba689a3b5aaf4687bd3d216f4bfd6cc4881
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/window_controller.h"
19 #include "chrome/browser/extensions/window_controller_list.h"
20 #include "chrome/browser/lifetime/application_lifetime.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/sessions/session_tab_helper.h"
23 #include "chrome/browser/task_management/web_contents_tags.h"
24 #include "chrome/browser/themes/theme_service.h"
25 #include "chrome/browser/themes/theme_service_factory.h"
26 #include "chrome/browser/ui/panels/native_panel.h"
27 #include "chrome/browser/ui/panels/panel_collection.h"
28 #include "chrome/browser/ui/panels/panel_host.h"
29 #include "chrome/browser/ui/panels/panel_manager.h"
30 #include "chrome/browser/ui/panels/stacked_panel_collection.h"
31 #include "chrome/browser/web_applications/web_app.h"
32 #include "content/public/browser/notification_service.h"
33 #include "content/public/browser/notification_source.h"
34 #include "content/public/browser/notification_types.h"
35 #include "content/public/browser/render_view_host.h"
36 #include "content/public/browser/user_metrics.h"
37 #include "content/public/browser/web_contents.h"
38 #include "extensions/browser/extension_registry.h"
39 #include "extensions/browser/extension_system.h"
40 #include "extensions/browser/image_loader.h"
41 #include "extensions/common/constants.h"
42 #include "extensions/common/extension.h"
43 #include "extensions/common/manifest_handlers/icons_handler.h"
44 #include "ui/gfx/geometry/rect.h"
45 #include "ui/gfx/image/image.h"
47 using base::UserMetricsAction;
48 using content::RenderViewHost;
50 namespace panel_internal {
52 class PanelExtensionWindowController : public extensions::WindowController {
53 public:
54 PanelExtensionWindowController(Panel* panel, Profile* profile);
55 ~PanelExtensionWindowController() override;
57 // Overridden from extensions::WindowController.
58 int GetWindowId() const override;
59 std::string GetWindowTypeText() const override;
60 base::DictionaryValue* CreateWindowValueWithTabs(
61 const extensions::Extension* extension) const override;
62 base::DictionaryValue* CreateTabValue(const extensions::Extension* extension,
63 int tab_index) const override;
64 bool CanClose(Reason* reason) const override;
65 void SetFullscreenMode(bool is_fullscreen,
66 const GURL& extension_url) const override;
67 bool IsVisibleToExtension(
68 const extensions::Extension* extension) const override;
70 private:
71 Panel* panel_; // Weak pointer. Owns us.
72 DISALLOW_COPY_AND_ASSIGN(PanelExtensionWindowController);
75 PanelExtensionWindowController::PanelExtensionWindowController(
76 Panel* panel, Profile* profile)
77 : extensions::WindowController(panel, profile),
78 panel_(panel) {
79 extensions::WindowControllerList::GetInstance()->AddExtensionWindow(this);
82 PanelExtensionWindowController::~PanelExtensionWindowController() {
83 extensions::WindowControllerList::GetInstance()->RemoveExtensionWindow(this);
86 int PanelExtensionWindowController::GetWindowId() const {
87 return static_cast<int>(panel_->session_id().id());
90 std::string PanelExtensionWindowController::GetWindowTypeText() const {
91 return extensions::tabs_constants::kWindowTypeValuePanel;
94 base::DictionaryValue*
95 PanelExtensionWindowController::CreateWindowValueWithTabs(
96 const extensions::Extension* extension) const {
97 base::DictionaryValue* result = CreateWindowValue();
99 base::DictionaryValue* tab_value = CreateTabValue(extension, 0);
100 if (tab_value) {
101 base::ListValue* tab_list = new base::ListValue();
102 tab_list->Append(tab_value);
103 result->Set(extensions::tabs_constants::kTabsKey, tab_list);
105 return result;
108 base::DictionaryValue* PanelExtensionWindowController::CreateTabValue(
109 const extensions::Extension* extension, int tab_index) const {
110 if (tab_index > 0)
111 return NULL;
113 content::WebContents* web_contents = panel_->GetWebContents();
114 if (!web_contents)
115 return NULL;
117 base::DictionaryValue* tab_value = new base::DictionaryValue();
118 tab_value->SetInteger(extensions::tabs_constants::kIdKey,
119 SessionTabHelper::IdForTab(web_contents));
120 tab_value->SetInteger(extensions::tabs_constants::kIndexKey, 0);
121 tab_value->SetInteger(
122 extensions::tabs_constants::kWindowIdKey,
123 SessionTabHelper::IdForWindowContainingTab(web_contents));
124 tab_value->SetString(
125 extensions::tabs_constants::kUrlKey, web_contents->GetURL().spec());
126 tab_value->SetString(extensions::tabs_constants::kStatusKey,
127 extensions::ExtensionTabUtil::GetTabStatusText(
128 web_contents->IsLoading()));
129 tab_value->SetBoolean(
130 extensions::tabs_constants::kActiveKey, panel_->IsActive());
131 tab_value->SetBoolean(extensions::tabs_constants::kSelectedKey, true);
132 tab_value->SetBoolean(extensions::tabs_constants::kHighlightedKey, true);
133 tab_value->SetBoolean(extensions::tabs_constants::kPinnedKey, false);
134 tab_value->SetString(
135 extensions::tabs_constants::kTitleKey, web_contents->GetTitle());
136 tab_value->SetBoolean(
137 extensions::tabs_constants::kIncognitoKey,
138 web_contents->GetBrowserContext()->IsOffTheRecord());
139 return tab_value;
142 bool PanelExtensionWindowController::CanClose(Reason* reason) const {
143 return true;
146 void PanelExtensionWindowController::SetFullscreenMode(
147 bool is_fullscreen, const GURL& extension_url) const {
148 // Do nothing. Panels cannot be fullscreen.
151 bool PanelExtensionWindowController::IsVisibleToExtension(
152 const extensions::Extension* extension) const {
153 DCHECK(extension);
154 return extension->id() == panel_->extension_id();
157 } // namespace panel_internal
159 Panel::~Panel() {
160 DCHECK(!collection_);
161 #if !defined(USE_AURA)
162 // Invoked by native panel destructor. Do not access native_panel_ here.
163 chrome::DecrementKeepAliveCount(); // Remove shutdown prevention.
164 #endif
167 PanelManager* Panel::manager() const {
168 return PanelManager::GetInstance();
171 const std::string Panel::extension_id() const {
172 return web_app::GetExtensionIdFromApplicationName(app_name_);
175 CommandUpdater* Panel::command_updater() {
176 return &command_updater_;
179 Profile* Panel::profile() const {
180 return profile_;
183 const extensions::Extension* Panel::GetExtension() const {
184 ExtensionService* extension_service =
185 extensions::ExtensionSystem::Get(profile())->extension_service();
186 if (!extension_service || !extension_service->is_ready())
187 return NULL;
188 return extension_service->GetExtensionById(extension_id(), false);
191 content::WebContents* Panel::GetWebContents() const {
192 return panel_host_.get() ? panel_host_->web_contents() : NULL;
195 void Panel::SetExpansionState(ExpansionState new_state) {
196 if (expansion_state_ == new_state)
197 return;
198 native_panel_->PanelExpansionStateChanging(expansion_state_, new_state);
199 expansion_state_ = new_state;
201 manager()->OnPanelExpansionStateChanged(this);
203 DCHECK(initialized_ && collection_ != NULL);
204 native_panel_->PreventActivationByOS(collection_->IsPanelMinimized(this));
205 UpdateMinimizeRestoreButtonVisibility();
207 content::NotificationService::current()->Notify(
208 chrome::NOTIFICATION_PANEL_CHANGED_EXPANSION_STATE,
209 content::Source<Panel>(this),
210 content::NotificationService::NoDetails());
213 bool Panel::IsDrawingAttention() const {
214 return native_panel_->IsDrawingAttention();
217 void Panel::FullScreenModeChanged(bool is_full_screen) {
218 native_panel_->FullScreenModeChanged(is_full_screen);
221 int Panel::TitleOnlyHeight() const {
222 return native_panel_->TitleOnlyHeight();
225 bool Panel::CanShowMinimizeButton() const {
226 return collection_ && collection_->CanShowMinimizeButton(this);
229 bool Panel::CanShowRestoreButton() const {
230 return collection_ && collection_->CanShowRestoreButton(this);
233 bool Panel::IsActive() const {
234 return native_panel_->IsPanelActive();
237 bool Panel::IsMaximized() const {
238 // Size of panels is managed by PanelManager, they are never 'zoomed'.
239 return false;
242 bool Panel::IsMinimized() const {
243 return !collection_ || collection_->IsPanelMinimized(this);
246 bool Panel::IsFullscreen() const {
247 return false;
250 gfx::NativeWindow Panel::GetNativeWindow() const {
251 return native_panel_->GetNativePanelWindow();
254 gfx::Rect Panel::GetRestoredBounds() const {
255 gfx::Rect bounds = native_panel_->GetPanelBounds();
256 bounds.set_y(bounds.bottom() - full_size_.height());
257 bounds.set_x(bounds.right() - full_size_.width());
258 bounds.set_size(full_size_);
259 return bounds;
262 ui::WindowShowState Panel::GetRestoredState() const {
263 return ui::SHOW_STATE_NORMAL;
266 gfx::Rect Panel::GetBounds() const {
267 return native_panel_->GetPanelBounds();
270 void Panel::Show() {
271 if (manager()->display_settings_provider()->is_full_screen() || !collection_)
272 return;
274 native_panel_->ShowPanel();
277 void Panel::Hide() {
278 // Not implemented.
281 void Panel::ShowInactive() {
282 if (manager()->display_settings_provider()->is_full_screen() || !collection_)
283 return;
285 native_panel_->ShowPanelInactive();
288 // Close() may be called multiple times if the panel window is not ready to
289 // close on the first attempt.
290 void Panel::Close() {
291 native_panel_->ClosePanel();
294 void Panel::Activate() {
295 if (!collection_)
296 return;
298 collection_->ActivatePanel(this);
299 native_panel_->ActivatePanel();
302 void Panel::Deactivate() {
303 native_panel_->DeactivatePanel();
306 void Panel::Maximize() {
307 Restore();
310 void Panel::Minimize() {
311 if (collection_)
312 collection_->MinimizePanel(this);
315 bool Panel::IsMinimizedBySystem() const {
316 return native_panel_->IsPanelMinimizedBySystem();
319 bool Panel::IsShownOnActiveDesktop() const {
320 return native_panel_->IsPanelShownOnActiveDesktop();
323 void Panel::ShowShadow(bool show) {
324 native_panel_->ShowShadow(show);
327 void Panel::Restore() {
328 if (collection_)
329 collection_->RestorePanel(this);
332 void Panel::SetBounds(const gfx::Rect& bounds) {
333 // Ignore bounds position as the panel manager controls all positioning.
334 if (!collection_)
335 return;
336 collection_->ResizePanelWindow(this, bounds.size());
337 SetAutoResizable(false);
340 void Panel::FlashFrame(bool draw_attention) {
341 if (IsDrawingAttention() == draw_attention || !collection_)
342 return;
344 // Don't draw attention for an active panel.
345 if (draw_attention && IsActive())
346 return;
348 // Invoking native panel to draw attention must be done before informing the
349 // panel collection because it needs to check internal state of the panel to
350 // determine if the panel has been drawing attention.
351 native_panel_->DrawAttention(draw_attention);
352 collection_->OnPanelAttentionStateChanged(this);
355 bool Panel::IsAlwaysOnTop() const {
356 return native_panel_->IsPanelAlwaysOnTop();
359 void Panel::SetAlwaysOnTop(bool on_top) {
360 native_panel_->SetPanelAlwaysOnTop(on_top);
363 void Panel::ExecuteCommandWithDisposition(int id,
364 WindowOpenDisposition disposition) {
365 DCHECK(command_updater_.IsCommandEnabled(id)) << "Invalid/disabled command "
366 << id;
368 if (!GetWebContents())
369 return;
371 switch (id) {
372 // Navigation
373 case IDC_RELOAD:
374 panel_host_->Reload();
375 break;
376 case IDC_RELOAD_IGNORING_CACHE:
377 panel_host_->ReloadIgnoringCache();
378 break;
379 case IDC_STOP:
380 panel_host_->StopLoading();
381 break;
383 // Window management
384 case IDC_CLOSE_WINDOW:
385 content::RecordAction(UserMetricsAction("CloseWindow"));
386 Close();
387 break;
388 case IDC_EXIT:
389 content::RecordAction(UserMetricsAction("Exit"));
390 chrome::AttemptUserExit();
391 break;
393 // Clipboard
394 case IDC_COPY:
395 content::RecordAction(UserMetricsAction("Copy"));
396 native_panel_->PanelCopy();
397 break;
398 case IDC_CUT:
399 content::RecordAction(UserMetricsAction("Cut"));
400 native_panel_->PanelCut();
401 break;
402 case IDC_PASTE:
403 content::RecordAction(UserMetricsAction("Paste"));
404 native_panel_->PanelPaste();
405 break;
407 // Zoom
408 case IDC_ZOOM_PLUS:
409 panel_host_->Zoom(content::PAGE_ZOOM_IN);
410 break;
411 case IDC_ZOOM_NORMAL:
412 panel_host_->Zoom(content::PAGE_ZOOM_RESET);
413 break;
414 case IDC_ZOOM_MINUS:
415 panel_host_->Zoom(content::PAGE_ZOOM_OUT);
416 break;
418 // DevTools
419 case IDC_DEV_TOOLS:
420 content::RecordAction(UserMetricsAction("DevTools_ToggleWindow"));
421 DevToolsWindow::OpenDevToolsWindow(GetWebContents(),
422 DevToolsToggleAction::Show());
423 break;
424 case IDC_DEV_TOOLS_CONSOLE:
425 content::RecordAction(UserMetricsAction("DevTools_ToggleConsole"));
426 DevToolsWindow::OpenDevToolsWindow(GetWebContents(),
427 DevToolsToggleAction::ShowConsole());
428 break;
430 default:
431 LOG(WARNING) << "Received unimplemented command: " << id;
432 break;
436 void Panel::Observe(int type,
437 const content::NotificationSource& source,
438 const content::NotificationDetails& details) {
439 switch (type) {
440 case chrome::NOTIFICATION_APP_TERMINATING:
441 Close();
442 break;
443 default:
444 NOTREACHED() << "Received unexpected notification " << type;
448 void Panel::RenderViewHostChanged(content::RenderViewHost* old_host,
449 content::RenderViewHost* new_host) {
450 ConfigureAutoResize(web_contents());
453 void Panel::OnExtensionUnloaded(
454 content::BrowserContext* browser_context,
455 const extensions::Extension* extension,
456 extensions::UnloadedExtensionInfo::Reason reason) {
457 if (extension->id() == extension_id())
458 Close();
460 void Panel::OnTitlebarClicked(panel::ClickModifier modifier) {
461 if (collection_)
462 collection_->OnPanelTitlebarClicked(this, modifier);
464 // Normally the system activates a window when the titlebar is clicked.
465 // However, we prevent system activation of minimized panels, thus the
466 // activation may not have occurred. Also, some OSes (Windows) will
467 // activate a minimized panel on mouse-down regardless of our attempts to
468 // prevent system activation. Attention state is not cleared in that case.
469 // See Panel::OnActiveStateChanged().
470 // Therefore, we ensure activation and clearing of attention state if the
471 // panel has been expanded. If the panel is in a stack, the titlebar click
472 // might minimize the panel and we do not want to activate it to make it
473 // expand again.
474 // These are no-ops if no changes are needed.
475 if (IsMinimized())
476 return;
477 Activate();
478 FlashFrame(false);
481 void Panel::OnMinimizeButtonClicked(panel::ClickModifier modifier) {
482 if (collection_)
483 collection_->OnMinimizeButtonClicked(this, modifier);
486 void Panel::OnRestoreButtonClicked(panel::ClickModifier modifier) {
487 // Clicking the restore button has the same behavior as clicking the titlebar.
488 OnTitlebarClicked(modifier);
491 void Panel::OnWindowSizeAvailable() {
492 ConfigureAutoResize(GetWebContents());
495 void Panel::OnNativePanelClosed() {
496 // Ensure previously enqueued OnImageLoaded callbacks are ignored.
497 image_loader_ptr_factory_.InvalidateWeakPtrs();
498 registrar_.RemoveAll();
499 extension_registry_->RemoveObserver(this);
500 manager()->OnPanelClosed(this);
501 DCHECK(!collection_);
504 StackedPanelCollection* Panel::stack() const {
505 return collection_ && collection_->type() == PanelCollection::STACKED ?
506 static_cast<StackedPanelCollection*>(collection_) : NULL;
509 panel::Resizability Panel::CanResizeByMouse() const {
510 if (!collection_)
511 return panel::NOT_RESIZABLE;
513 return collection_->GetPanelResizability(this);
516 void Panel::Initialize(const GURL& url,
517 const gfx::Rect& bounds,
518 bool always_on_top) {
519 DCHECK(!initialized_);
520 DCHECK(!collection_); // Cannot be added to a collection until fully created.
521 DCHECK_EQ(EXPANDED, expansion_state_);
522 DCHECK(!bounds.IsEmpty());
523 initialized_ = true;
524 full_size_ = bounds.size();
525 native_panel_ = CreateNativePanel(this, bounds, always_on_top);
527 extension_window_controller_.reset(
528 new panel_internal::PanelExtensionWindowController(this, profile_));
530 InitCommandState();
532 // Set up hosting for web contents.
533 panel_host_.reset(new PanelHost(this, profile_));
534 panel_host_->Init(url);
535 content::WebContents* web_contents = GetWebContents();
536 // The contents might be NULL for most of our tests.
537 if (web_contents) {
538 native_panel_->AttachWebContents(web_contents);
540 // Make the panel show up in the task manager.
541 task_management::WebContentsTags::CreateForPanel(web_contents, this);
544 // Close when the extension is unloaded or the browser is exiting.
545 extension_registry_->AddObserver(this);
546 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
547 content::NotificationService::AllSources());
548 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
549 content::Source<ThemeService>(
550 ThemeServiceFactory::GetForProfile(profile_)));
552 #if !defined(USE_AURA)
553 // Keep alive for AURA has been moved to panel_view.
554 // Prevent the browser process from shutting down while this window is open.
555 chrome::IncrementKeepAliveCount();
556 #endif
558 UpdateAppIcon();
561 void Panel::SetPanelBounds(const gfx::Rect& bounds) {
562 if (bounds != native_panel_->GetPanelBounds())
563 native_panel_->SetPanelBounds(bounds);
566 void Panel::SetPanelBoundsInstantly(const gfx::Rect& bounds) {
567 native_panel_->SetPanelBoundsInstantly(bounds);
570 void Panel::LimitSizeToWorkArea(const gfx::Rect& work_area) {
571 int max_width = manager()->GetMaxPanelWidth(work_area);
572 int max_height = manager()->GetMaxPanelHeight(work_area);
574 // If the custom max size is used, ensure that it does not exceed the display
575 // area.
576 if (max_size_policy_ == CUSTOM_MAX_SIZE) {
577 int current_max_width = max_size_.width();
578 if (current_max_width > max_width)
579 max_width = std::min(current_max_width, work_area.width());
580 int current_max_height = max_size_.height();
581 if (current_max_height > max_height)
582 max_height = std::min(current_max_height, work_area.height());
585 SetSizeRange(min_size_, gfx::Size(max_width, max_height));
587 // Ensure that full size does not exceed max size.
588 full_size_ = ClampSize(full_size_);
591 void Panel::SetAutoResizable(bool resizable) {
592 if (auto_resizable_ == resizable)
593 return;
595 auto_resizable_ = resizable;
596 content::WebContents* web_contents = GetWebContents();
597 if (auto_resizable_) {
598 if (web_contents)
599 EnableWebContentsAutoResize(web_contents);
600 } else {
601 if (web_contents) {
602 content::WebContentsObserver::Observe(nullptr);
604 // NULL might be returned if the tab has not been added.
605 RenderViewHost* render_view_host = web_contents->GetRenderViewHost();
606 if (render_view_host)
607 render_view_host->DisableAutoResize(full_size_);
612 void Panel::EnableWebContentsAutoResize(content::WebContents* web_contents) {
613 DCHECK(web_contents);
614 ConfigureAutoResize(web_contents);
616 // We also need to know when the render view host changes in order
617 // to turn on auto-resize notifications in the new render view host.
618 content::WebContentsObserver::Observe(web_contents);
621 void Panel::OnContentsAutoResized(const gfx::Size& new_content_size) {
622 DCHECK(auto_resizable_);
623 if (!collection_)
624 return;
626 gfx::Size new_window_size =
627 native_panel_->WindowSizeFromContentSize(new_content_size);
629 // Ignore content auto resizes until window frame size is known.
630 // This reduces extra resizes when panel is first shown.
631 // After window frame size is known, it will trigger another content
632 // auto resize.
633 if (new_content_size == new_window_size)
634 return;
636 collection_->ResizePanelWindow(this, new_window_size);
639 void Panel::OnWindowResizedByMouse(const gfx::Rect& new_bounds) {
640 if (collection_)
641 collection_->OnPanelResizedByMouse(this, new_bounds);
644 void Panel::SetSizeRange(const gfx::Size& min_size, const gfx::Size& max_size) {
645 if (min_size == min_size_ && max_size == max_size_)
646 return;
648 DCHECK(min_size.width() <= max_size.width());
649 DCHECK(min_size.height() <= max_size.height());
650 min_size_ = min_size;
651 max_size_ = max_size;
653 ConfigureAutoResize(GetWebContents());
656 void Panel::IncreaseMaxSize(const gfx::Size& desired_panel_size) {
657 gfx::Size new_max_size = max_size_;
658 if (new_max_size.width() < desired_panel_size.width())
659 new_max_size.set_width(desired_panel_size.width());
660 if (new_max_size.height() < desired_panel_size.height())
661 new_max_size.set_height(desired_panel_size.height());
663 SetSizeRange(min_size_, new_max_size);
666 void Panel::HandleKeyboardEvent(const content::NativeWebKeyboardEvent& event) {
667 native_panel_->HandlePanelKeyboardEvent(event);
670 void Panel::SetPreviewMode(bool in_preview) {
671 DCHECK_NE(in_preview_mode_, in_preview);
672 in_preview_mode_ = in_preview;
675 void Panel::UpdateMinimizeRestoreButtonVisibility() {
676 native_panel_->UpdatePanelMinimizeRestoreButtonVisibility();
679 gfx::Size Panel::ClampSize(const gfx::Size& size) const {
680 // The panel width:
681 // * cannot grow or shrink to go beyond [min_width, max_width]
682 int new_width = size.width();
683 if (new_width > max_size_.width())
684 new_width = max_size_.width();
685 if (new_width < min_size_.width())
686 new_width = min_size_.width();
688 // The panel height:
689 // * cannot grow or shrink to go beyond [min_height, max_height]
690 int new_height = size.height();
691 if (new_height > max_size_.height())
692 new_height = max_size_.height();
693 if (new_height < min_size_.height())
694 new_height = min_size_.height();
696 return gfx::Size(new_width, new_height);
699 void Panel::OnActiveStateChanged(bool active) {
700 // Clear attention state when an expanded panel becomes active.
701 // On some systems (e.g. Win), mouse-down activates a panel regardless of
702 // its expansion state. However, we don't want to clear draw attention if
703 // contents are not visible. In that scenario, if the mouse-down results
704 // in a mouse-click, draw attention will be cleared then.
705 // See Panel::OnTitlebarClicked().
706 if (active && IsDrawingAttention() && !IsMinimized())
707 FlashFrame(false);
709 if (collection_)
710 collection_->OnPanelActiveStateChanged(this);
712 // Send extension event about window changing active state.
713 extensions::TabsWindowsAPI* tabs_windows_api =
714 extensions::TabsWindowsAPI::Get(profile());
715 if (tabs_windows_api) {
716 tabs_windows_api->windows_event_router()->OnActiveWindowChanged(
717 active ? extension_window_controller_.get() : NULL);
720 content::NotificationService::current()->Notify(
721 chrome::NOTIFICATION_PANEL_CHANGED_ACTIVE_STATUS,
722 content::Source<Panel>(this),
723 content::NotificationService::NoDetails());
726 void Panel::OnPanelStartUserResizing() {
727 SetAutoResizable(false);
728 SetPreviewMode(true);
729 max_size_policy_ = CUSTOM_MAX_SIZE;
732 void Panel::OnPanelEndUserResizing() {
733 SetPreviewMode(false);
736 bool Panel::ShouldCloseWindow() {
737 return true;
740 void Panel::OnWindowClosing() {
741 if (GetWebContents()) {
742 native_panel_->DetachWebContents(GetWebContents());
743 panel_host_->DestroyWebContents();
747 bool Panel::ExecuteCommandIfEnabled(int id) {
748 if (command_updater()->SupportsCommand(id) &&
749 command_updater()->IsCommandEnabled(id)) {
750 ExecuteCommandWithDisposition(id, CURRENT_TAB);
751 return true;
753 return false;
756 base::string16 Panel::GetWindowTitle() const {
757 content::WebContents* contents = GetWebContents();
758 base::string16 title;
760 // |contents| can be NULL during the window's creation.
761 if (contents) {
762 title = contents->GetTitle();
763 FormatTitleForDisplay(&title);
766 if (title.empty())
767 title = base::UTF8ToUTF16(app_name());
769 return title;
772 gfx::Image Panel::GetCurrentPageIcon() const {
773 return panel_host_.get() ? panel_host_->GetPageIcon() : gfx::Image();
776 void Panel::UpdateTitleBar() {
777 native_panel_->UpdatePanelTitleBar();
780 void Panel::LoadingStateChanged(bool is_loading) {
781 command_updater_.UpdateCommandEnabled(IDC_STOP, is_loading);
782 native_panel_->UpdatePanelLoadingAnimations(is_loading);
783 UpdateTitleBar();
786 void Panel::MoveByInstantly(const gfx::Vector2d& delta_origin) {
787 gfx::Rect bounds = GetBounds();
788 bounds.Offset(delta_origin);
789 SetPanelBoundsInstantly(bounds);
792 void Panel::SetWindowCornerStyle(panel::CornerStyle corner_style) {
793 native_panel_->SetWindowCornerStyle(corner_style);
796 void Panel::MinimizeBySystem() {
797 native_panel_->MinimizePanelBySystem();
800 Panel::Panel(Profile* profile,
801 const std::string& app_name,
802 const gfx::Size& min_size,
803 const gfx::Size& max_size)
804 : app_name_(app_name),
805 profile_(profile),
806 collection_(NULL),
807 initialized_(false),
808 min_size_(min_size),
809 max_size_(max_size),
810 max_size_policy_(DEFAULT_MAX_SIZE),
811 auto_resizable_(false),
812 in_preview_mode_(false),
813 native_panel_(NULL),
814 attention_mode_(USE_PANEL_ATTENTION),
815 expansion_state_(EXPANDED),
816 command_updater_(this),
817 extension_registry_(extensions::ExtensionRegistry::Get(profile_)),
818 image_loader_ptr_factory_(this) {
821 void Panel::OnImageLoaded(const gfx::Image& image) {
822 if (!image.IsEmpty()) {
823 app_icon_ = image;
824 native_panel_->UpdatePanelTitleBar();
827 content::NotificationService::current()->Notify(
828 chrome::NOTIFICATION_PANEL_APP_ICON_LOADED,
829 content::Source<Panel>(this),
830 content::NotificationService::NoDetails());
833 void Panel::InitCommandState() {
834 // All supported commands whose state isn't set automagically some other way
835 // (like Stop during a page load) must have their state initialized here,
836 // otherwise they will be forever disabled.
838 // Navigation commands
839 command_updater_.UpdateCommandEnabled(IDC_RELOAD, true);
840 command_updater_.UpdateCommandEnabled(IDC_RELOAD_IGNORING_CACHE, true);
842 // Window management commands
843 command_updater_.UpdateCommandEnabled(IDC_CLOSE_WINDOW, true);
844 command_updater_.UpdateCommandEnabled(IDC_EXIT, true);
846 // Zoom
847 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MENU, true);
848 command_updater_.UpdateCommandEnabled(IDC_ZOOM_PLUS, true);
849 command_updater_.UpdateCommandEnabled(IDC_ZOOM_NORMAL, true);
850 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MINUS, true);
852 // Clipboard
853 command_updater_.UpdateCommandEnabled(IDC_COPY, true);
854 command_updater_.UpdateCommandEnabled(IDC_CUT, true);
855 command_updater_.UpdateCommandEnabled(IDC_PASTE, true);
857 // DevTools
858 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS, true);
859 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_CONSOLE, true);
862 void Panel::ConfigureAutoResize(content::WebContents* web_contents) {
863 if (!auto_resizable_ || !web_contents)
864 return;
866 // NULL might be returned if the tab has not been added.
867 RenderViewHost* render_view_host = web_contents->GetRenderViewHost();
868 if (!render_view_host)
869 return;
871 render_view_host->EnableAutoResize(
872 min_size_,
873 native_panel_->ContentSizeFromWindowSize(max_size_));
876 void Panel::UpdateAppIcon() {
877 const extensions::Extension* extension = GetExtension();
878 if (!extension)
879 return;
881 extensions::ImageLoader* loader = extensions::ImageLoader::Get(profile());
882 loader->LoadImageAsync(
883 extension,
884 extensions::IconsInfo::GetIconResource(
885 extension,
886 extension_misc::EXTENSION_ICON_SMALL,
887 ExtensionIconSet::MATCH_BIGGER),
888 gfx::Size(extension_misc::EXTENSION_ICON_SMALL,
889 extension_misc::EXTENSION_ICON_SMALL),
890 base::Bind(&Panel::OnImageLoaded,
891 image_loader_ptr_factory_.GetWeakPtr()));
894 // static
895 void Panel::FormatTitleForDisplay(base::string16* title) {
896 size_t current_index = 0;
897 size_t match_index;
898 while ((match_index = title->find(L'\n', current_index)) !=
899 base::string16::npos) {
900 title->replace(match_index, 1, base::string16());
901 current_index = match_index;