Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / panels / panel.cc
blob7c98b7d6853a6be89288048db11cf6578a27e107
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/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_registry.h"
38 #include "extensions/browser/extension_system.h"
39 #include "extensions/browser/image_loader.h"
40 #include "extensions/common/constants.h"
41 #include "extensions/common/extension.h"
42 #include "extensions/common/manifest_handlers/icons_handler.h"
43 #include "ui/gfx/geometry/rect.h"
44 #include "ui/gfx/image/image.h"
46 using base::UserMetricsAction;
47 using content::RenderViewHost;
49 namespace panel_internal {
51 class PanelExtensionWindowController : public extensions::WindowController {
52 public:
53 PanelExtensionWindowController(Panel* panel, Profile* profile);
54 ~PanelExtensionWindowController() override;
56 // Overridden from extensions::WindowController.
57 int GetWindowId() const override;
58 std::string GetWindowTypeText() const override;
59 base::DictionaryValue* CreateWindowValueWithTabs(
60 const extensions::Extension* extension) const override;
61 base::DictionaryValue* CreateTabValue(const extensions::Extension* extension,
62 int tab_index) const override;
63 bool CanClose(Reason* reason) const override;
64 void SetFullscreenMode(bool is_fullscreen,
65 const GURL& extension_url) const override;
66 bool IsVisibleToExtension(
67 const extensions::Extension* extension) const override;
69 private:
70 Panel* panel_; // Weak pointer. Owns us.
71 DISALLOW_COPY_AND_ASSIGN(PanelExtensionWindowController);
74 PanelExtensionWindowController::PanelExtensionWindowController(
75 Panel* panel, Profile* profile)
76 : extensions::WindowController(panel, profile),
77 panel_(panel) {
78 extensions::WindowControllerList::GetInstance()->AddExtensionWindow(this);
81 PanelExtensionWindowController::~PanelExtensionWindowController() {
82 extensions::WindowControllerList::GetInstance()->RemoveExtensionWindow(this);
85 int PanelExtensionWindowController::GetWindowId() const {
86 return static_cast<int>(panel_->session_id().id());
89 std::string PanelExtensionWindowController::GetWindowTypeText() const {
90 return extensions::tabs_constants::kWindowTypeValuePanel;
93 base::DictionaryValue*
94 PanelExtensionWindowController::CreateWindowValueWithTabs(
95 const extensions::Extension* extension) const {
96 base::DictionaryValue* result = CreateWindowValue();
98 DCHECK(IsVisibleToExtension(extension));
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 DCHECK(IsVisibleToExtension(extension));
118 base::DictionaryValue* tab_value = new base::DictionaryValue();
119 tab_value->SetInteger(extensions::tabs_constants::kIdKey,
120 SessionTabHelper::IdForTab(web_contents));
121 tab_value->SetInteger(extensions::tabs_constants::kIndexKey, 0);
122 tab_value->SetInteger(
123 extensions::tabs_constants::kWindowIdKey,
124 SessionTabHelper::IdForWindowContainingTab(web_contents));
125 tab_value->SetString(
126 extensions::tabs_constants::kUrlKey, web_contents->GetURL().spec());
127 tab_value->SetString(extensions::tabs_constants::kStatusKey,
128 extensions::ExtensionTabUtil::GetTabStatusText(
129 web_contents->IsLoading()));
130 tab_value->SetBoolean(
131 extensions::tabs_constants::kActiveKey, panel_->IsActive());
132 tab_value->SetBoolean(extensions::tabs_constants::kSelectedKey, true);
133 tab_value->SetBoolean(extensions::tabs_constants::kHighlightedKey, true);
134 tab_value->SetBoolean(extensions::tabs_constants::kPinnedKey, false);
135 tab_value->SetString(
136 extensions::tabs_constants::kTitleKey, web_contents->GetTitle());
137 tab_value->SetBoolean(
138 extensions::tabs_constants::kIncognitoKey,
139 web_contents->GetBrowserContext()->IsOffTheRecord());
140 return tab_value;
143 bool PanelExtensionWindowController::CanClose(Reason* reason) const {
144 return true;
147 void PanelExtensionWindowController::SetFullscreenMode(
148 bool is_fullscreen, const GURL& extension_url) const {
149 // Do nothing. Panels cannot be fullscreen.
152 bool PanelExtensionWindowController::IsVisibleToExtension(
153 const extensions::Extension* extension) const {
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 // Close when the extension is unloaded or the browser is exiting.
541 extension_registry_->AddObserver(this);
542 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
543 content::NotificationService::AllSources());
544 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
545 content::Source<ThemeService>(
546 ThemeServiceFactory::GetForProfile(profile_)));
548 #if !defined(USE_AURA)
549 // Keep alive for AURA has been moved to panel_view.
550 // Prevent the browser process from shutting down while this window is open.
551 chrome::IncrementKeepAliveCount();
552 #endif
554 UpdateAppIcon();
557 void Panel::SetPanelBounds(const gfx::Rect& bounds) {
558 if (bounds != native_panel_->GetPanelBounds())
559 native_panel_->SetPanelBounds(bounds);
562 void Panel::SetPanelBoundsInstantly(const gfx::Rect& bounds) {
563 native_panel_->SetPanelBoundsInstantly(bounds);
566 void Panel::LimitSizeToWorkArea(const gfx::Rect& work_area) {
567 int max_width = manager()->GetMaxPanelWidth(work_area);
568 int max_height = manager()->GetMaxPanelHeight(work_area);
570 // If the custom max size is used, ensure that it does not exceed the display
571 // area.
572 if (max_size_policy_ == CUSTOM_MAX_SIZE) {
573 int current_max_width = max_size_.width();
574 if (current_max_width > max_width)
575 max_width = std::min(current_max_width, work_area.width());
576 int current_max_height = max_size_.height();
577 if (current_max_height > max_height)
578 max_height = std::min(current_max_height, work_area.height());
581 SetSizeRange(min_size_, gfx::Size(max_width, max_height));
583 // Ensure that full size does not exceed max size.
584 full_size_ = ClampSize(full_size_);
587 void Panel::SetAutoResizable(bool resizable) {
588 if (auto_resizable_ == resizable)
589 return;
591 auto_resizable_ = resizable;
592 content::WebContents* web_contents = GetWebContents();
593 if (auto_resizable_) {
594 if (web_contents)
595 EnableWebContentsAutoResize(web_contents);
596 } else {
597 if (web_contents) {
598 content::WebContentsObserver::Observe(nullptr);
600 // NULL might be returned if the tab has not been added.
601 RenderViewHost* render_view_host = web_contents->GetRenderViewHost();
602 if (render_view_host)
603 render_view_host->DisableAutoResize(full_size_);
608 void Panel::EnableWebContentsAutoResize(content::WebContents* web_contents) {
609 DCHECK(web_contents);
610 ConfigureAutoResize(web_contents);
612 // We also need to know when the render view host changes in order
613 // to turn on auto-resize notifications in the new render view host.
614 content::WebContentsObserver::Observe(web_contents);
617 void Panel::OnContentsAutoResized(const gfx::Size& new_content_size) {
618 DCHECK(auto_resizable_);
619 if (!collection_)
620 return;
622 gfx::Size new_window_size =
623 native_panel_->WindowSizeFromContentSize(new_content_size);
625 // Ignore content auto resizes until window frame size is known.
626 // This reduces extra resizes when panel is first shown.
627 // After window frame size is known, it will trigger another content
628 // auto resize.
629 if (new_content_size == new_window_size)
630 return;
632 collection_->ResizePanelWindow(this, new_window_size);
635 void Panel::OnWindowResizedByMouse(const gfx::Rect& new_bounds) {
636 if (collection_)
637 collection_->OnPanelResizedByMouse(this, new_bounds);
640 void Panel::SetSizeRange(const gfx::Size& min_size, const gfx::Size& max_size) {
641 if (min_size == min_size_ && max_size == max_size_)
642 return;
644 DCHECK(min_size.width() <= max_size.width());
645 DCHECK(min_size.height() <= max_size.height());
646 min_size_ = min_size;
647 max_size_ = max_size;
649 ConfigureAutoResize(GetWebContents());
652 void Panel::IncreaseMaxSize(const gfx::Size& desired_panel_size) {
653 gfx::Size new_max_size = max_size_;
654 if (new_max_size.width() < desired_panel_size.width())
655 new_max_size.set_width(desired_panel_size.width());
656 if (new_max_size.height() < desired_panel_size.height())
657 new_max_size.set_height(desired_panel_size.height());
659 SetSizeRange(min_size_, new_max_size);
662 void Panel::HandleKeyboardEvent(const content::NativeWebKeyboardEvent& event) {
663 native_panel_->HandlePanelKeyboardEvent(event);
666 void Panel::SetPreviewMode(bool in_preview) {
667 DCHECK_NE(in_preview_mode_, in_preview);
668 in_preview_mode_ = in_preview;
671 void Panel::UpdateMinimizeRestoreButtonVisibility() {
672 native_panel_->UpdatePanelMinimizeRestoreButtonVisibility();
675 gfx::Size Panel::ClampSize(const gfx::Size& size) const {
676 // The panel width:
677 // * cannot grow or shrink to go beyond [min_width, max_width]
678 int new_width = size.width();
679 if (new_width > max_size_.width())
680 new_width = max_size_.width();
681 if (new_width < min_size_.width())
682 new_width = min_size_.width();
684 // The panel height:
685 // * cannot grow or shrink to go beyond [min_height, max_height]
686 int new_height = size.height();
687 if (new_height > max_size_.height())
688 new_height = max_size_.height();
689 if (new_height < min_size_.height())
690 new_height = min_size_.height();
692 return gfx::Size(new_width, new_height);
695 void Panel::OnActiveStateChanged(bool active) {
696 // Clear attention state when an expanded panel becomes active.
697 // On some systems (e.g. Win), mouse-down activates a panel regardless of
698 // its expansion state. However, we don't want to clear draw attention if
699 // contents are not visible. In that scenario, if the mouse-down results
700 // in a mouse-click, draw attention will be cleared then.
701 // See Panel::OnTitlebarClicked().
702 if (active && IsDrawingAttention() && !IsMinimized())
703 FlashFrame(false);
705 if (collection_)
706 collection_->OnPanelActiveStateChanged(this);
708 // Send extension event about window changing active state.
709 extensions::TabsWindowsAPI* tabs_windows_api =
710 extensions::TabsWindowsAPI::Get(profile());
711 if (tabs_windows_api) {
712 tabs_windows_api->windows_event_router()->OnActiveWindowChanged(
713 active ? extension_window_controller_.get() : NULL);
716 content::NotificationService::current()->Notify(
717 chrome::NOTIFICATION_PANEL_CHANGED_ACTIVE_STATUS,
718 content::Source<Panel>(this),
719 content::NotificationService::NoDetails());
722 void Panel::OnPanelStartUserResizing() {
723 SetAutoResizable(false);
724 SetPreviewMode(true);
725 max_size_policy_ = CUSTOM_MAX_SIZE;
728 void Panel::OnPanelEndUserResizing() {
729 SetPreviewMode(false);
732 bool Panel::ShouldCloseWindow() {
733 return true;
736 void Panel::OnWindowClosing() {
737 if (GetWebContents()) {
738 native_panel_->DetachWebContents(GetWebContents());
739 panel_host_->DestroyWebContents();
743 bool Panel::ExecuteCommandIfEnabled(int id) {
744 if (command_updater()->SupportsCommand(id) &&
745 command_updater()->IsCommandEnabled(id)) {
746 ExecuteCommandWithDisposition(id, CURRENT_TAB);
747 return true;
749 return false;
752 base::string16 Panel::GetWindowTitle() const {
753 content::WebContents* contents = GetWebContents();
754 base::string16 title;
756 // |contents| can be NULL during the window's creation.
757 if (contents) {
758 title = contents->GetTitle();
759 FormatTitleForDisplay(&title);
762 if (title.empty())
763 title = base::UTF8ToUTF16(app_name());
765 return title;
768 gfx::Image Panel::GetCurrentPageIcon() const {
769 return panel_host_.get() ? panel_host_->GetPageIcon() : gfx::Image();
772 void Panel::UpdateTitleBar() {
773 native_panel_->UpdatePanelTitleBar();
776 void Panel::LoadingStateChanged(bool is_loading) {
777 command_updater_.UpdateCommandEnabled(IDC_STOP, is_loading);
778 native_panel_->UpdatePanelLoadingAnimations(is_loading);
779 UpdateTitleBar();
782 void Panel::MoveByInstantly(const gfx::Vector2d& delta_origin) {
783 gfx::Rect bounds = GetBounds();
784 bounds.Offset(delta_origin);
785 SetPanelBoundsInstantly(bounds);
788 void Panel::SetWindowCornerStyle(panel::CornerStyle corner_style) {
789 native_panel_->SetWindowCornerStyle(corner_style);
792 void Panel::MinimizeBySystem() {
793 native_panel_->MinimizePanelBySystem();
796 Panel::Panel(Profile* profile,
797 const std::string& app_name,
798 const gfx::Size& min_size,
799 const gfx::Size& max_size)
800 : app_name_(app_name),
801 profile_(profile),
802 collection_(NULL),
803 initialized_(false),
804 min_size_(min_size),
805 max_size_(max_size),
806 max_size_policy_(DEFAULT_MAX_SIZE),
807 auto_resizable_(false),
808 in_preview_mode_(false),
809 native_panel_(NULL),
810 attention_mode_(USE_PANEL_ATTENTION),
811 expansion_state_(EXPANDED),
812 command_updater_(this),
813 extension_registry_(extensions::ExtensionRegistry::Get(profile_)),
814 image_loader_ptr_factory_(this) {
817 void Panel::OnImageLoaded(const gfx::Image& image) {
818 if (!image.IsEmpty()) {
819 app_icon_ = image;
820 native_panel_->UpdatePanelTitleBar();
823 content::NotificationService::current()->Notify(
824 chrome::NOTIFICATION_PANEL_APP_ICON_LOADED,
825 content::Source<Panel>(this),
826 content::NotificationService::NoDetails());
829 void Panel::InitCommandState() {
830 // All supported commands whose state isn't set automagically some other way
831 // (like Stop during a page load) must have their state initialized here,
832 // otherwise they will be forever disabled.
834 // Navigation commands
835 command_updater_.UpdateCommandEnabled(IDC_RELOAD, true);
836 command_updater_.UpdateCommandEnabled(IDC_RELOAD_IGNORING_CACHE, true);
838 // Window management commands
839 command_updater_.UpdateCommandEnabled(IDC_CLOSE_WINDOW, true);
840 command_updater_.UpdateCommandEnabled(IDC_EXIT, true);
842 // Zoom
843 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MENU, true);
844 command_updater_.UpdateCommandEnabled(IDC_ZOOM_PLUS, true);
845 command_updater_.UpdateCommandEnabled(IDC_ZOOM_NORMAL, true);
846 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MINUS, true);
848 // Clipboard
849 command_updater_.UpdateCommandEnabled(IDC_COPY, true);
850 command_updater_.UpdateCommandEnabled(IDC_CUT, true);
851 command_updater_.UpdateCommandEnabled(IDC_PASTE, true);
853 // DevTools
854 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS, true);
855 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_CONSOLE, true);
858 void Panel::ConfigureAutoResize(content::WebContents* web_contents) {
859 if (!auto_resizable_ || !web_contents)
860 return;
862 // NULL might be returned if the tab has not been added.
863 RenderViewHost* render_view_host = web_contents->GetRenderViewHost();
864 if (!render_view_host)
865 return;
867 render_view_host->EnableAutoResize(
868 min_size_,
869 native_panel_->ContentSizeFromWindowSize(max_size_));
872 void Panel::UpdateAppIcon() {
873 const extensions::Extension* extension = GetExtension();
874 if (!extension)
875 return;
877 extensions::ImageLoader* loader = extensions::ImageLoader::Get(profile());
878 loader->LoadImageAsync(
879 extension,
880 extensions::IconsInfo::GetIconResource(
881 extension,
882 extension_misc::EXTENSION_ICON_SMALL,
883 ExtensionIconSet::MATCH_BIGGER),
884 gfx::Size(extension_misc::EXTENSION_ICON_SMALL,
885 extension_misc::EXTENSION_ICON_SMALL),
886 base::Bind(&Panel::OnImageLoaded,
887 image_loader_ptr_factory_.GetWeakPtr()));
890 // static
891 void Panel::FormatTitleForDisplay(base::string16* title) {
892 size_t current_index = 0;
893 size_t match_index;
894 while ((match_index = title->find(L'\n', current_index)) !=
895 base::string16::npos) {
896 title->replace(match_index, 1, base::string16());
897 current_index = match_index;