Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / panels / panel.cc
blobc230beaaf59ccfb0645912026a15afd15d57178c
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_system.h"
18 #include "chrome/browser/extensions/extension_tab_util.h"
19 #include "chrome/browser/extensions/image_loader.h"
20 #include "chrome/browser/extensions/window_controller.h"
21 #include "chrome/browser/extensions/window_controller_list.h"
22 #include "chrome/browser/lifetime/application_lifetime.h"
23 #include "chrome/browser/profiles/profile.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 "chrome/common/extensions/manifest_handlers/icons_handler.h"
33 #include "content/public/browser/notification_service.h"
34 #include "content/public/browser/notification_source.h"
35 #include "content/public/browser/notification_types.h"
36 #include "content/public/browser/render_view_host.h"
37 #include "content/public/browser/user_metrics.h"
38 #include "content/public/browser/web_contents.h"
39 #include "extensions/common/extension.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 {
49 public:
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;
66 private:
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),
74 panel_(panel) {
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);
97 if (tab_value) {
98 base::ListValue* tab_list = new base::ListValue();
99 tab_list->Append(tab_value);
100 result->Set(extensions::tabs_constants::kTabsKey, tab_list);
102 return result;
105 base::DictionaryValue* PanelExtensionWindowController::CreateTabValue(
106 const extensions::Extension* extension, int tab_index) const {
107 if (tab_index > 0)
108 return NULL;
110 content::WebContents* web_contents = panel_->GetWebContents();
111 if (!web_contents)
112 return NULL;
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());
136 return tab_value;
139 bool PanelExtensionWindowController::CanClose(Reason* reason) const {
140 return true;
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
155 Panel::~Panel() {
156 DCHECK(!collection_);
157 // Invoked by native panel destructor. Do not access native_panel_ here.
158 chrome::EndKeepAlive(); // Remove shutdown prevention.
161 PanelManager* Panel::manager() const {
162 return PanelManager::GetInstance();
165 const std::string Panel::extension_id() const {
166 return web_app::GetExtensionIdFromApplicationName(app_name_);
169 CommandUpdater* Panel::command_updater() {
170 return &command_updater_;
173 Profile* Panel::profile() const {
174 return profile_;
177 const extensions::Extension* Panel::GetExtension() const {
178 ExtensionService* extension_service =
179 extensions::ExtensionSystem::Get(profile())->extension_service();
180 if (!extension_service || !extension_service->is_ready())
181 return NULL;
182 return extension_service->GetExtensionById(extension_id(), false);
185 content::WebContents* Panel::GetWebContents() const {
186 return panel_host_.get() ? panel_host_->web_contents() : NULL;
189 void Panel::SetExpansionState(ExpansionState new_state) {
190 if (expansion_state_ == new_state)
191 return;
192 native_panel_->PanelExpansionStateChanging(expansion_state_, new_state);
193 expansion_state_ = new_state;
195 manager()->OnPanelExpansionStateChanged(this);
197 DCHECK(initialized_ && collection_ != NULL);
198 native_panel_->PreventActivationByOS(collection_->IsPanelMinimized(this));
199 UpdateMinimizeRestoreButtonVisibility();
201 content::NotificationService::current()->Notify(
202 chrome::NOTIFICATION_PANEL_CHANGED_EXPANSION_STATE,
203 content::Source<Panel>(this),
204 content::NotificationService::NoDetails());
207 bool Panel::IsDrawingAttention() const {
208 return native_panel_->IsDrawingAttention();
211 void Panel::FullScreenModeChanged(bool is_full_screen) {
212 native_panel_->FullScreenModeChanged(is_full_screen);
215 int Panel::TitleOnlyHeight() const {
216 return native_panel_->TitleOnlyHeight();
219 bool Panel::CanShowMinimizeButton() const {
220 return collection_ && collection_->CanShowMinimizeButton(this);
223 bool Panel::CanShowRestoreButton() const {
224 return collection_ && collection_->CanShowRestoreButton(this);
227 bool Panel::IsActive() const {
228 return native_panel_->IsPanelActive();
231 bool Panel::IsMaximized() const {
232 // Size of panels is managed by PanelManager, they are never 'zoomed'.
233 return false;
236 bool Panel::IsMinimized() const {
237 return !collection_ || collection_->IsPanelMinimized(this);
240 bool Panel::IsFullscreen() const {
241 return false;
244 gfx::NativeWindow Panel::GetNativeWindow() {
245 return native_panel_->GetNativePanelWindow();
248 gfx::Rect Panel::GetRestoredBounds() const {
249 gfx::Rect bounds = native_panel_->GetPanelBounds();
250 bounds.set_y(bounds.bottom() - full_size_.height());
251 bounds.set_x(bounds.right() - full_size_.width());
252 bounds.set_size(full_size_);
253 return bounds;
256 ui::WindowShowState Panel::GetRestoredState() const {
257 return ui::SHOW_STATE_NORMAL;
260 gfx::Rect Panel::GetBounds() const {
261 return native_panel_->GetPanelBounds();
264 void Panel::Show() {
265 if (manager()->display_settings_provider()->is_full_screen() || !collection_)
266 return;
268 native_panel_->ShowPanel();
271 void Panel::Hide() {
272 // Not implemented.
275 void Panel::ShowInactive() {
276 if (manager()->display_settings_provider()->is_full_screen() || !collection_)
277 return;
279 native_panel_->ShowPanelInactive();
282 // Close() may be called multiple times if the panel window is not ready to
283 // close on the first attempt.
284 void Panel::Close() {
285 native_panel_->ClosePanel();
288 void Panel::Activate() {
289 if (!collection_)
290 return;
292 collection_->ActivatePanel(this);
293 native_panel_->ActivatePanel();
296 void Panel::Deactivate() {
297 native_panel_->DeactivatePanel();
300 void Panel::Maximize() {
301 Restore();
304 void Panel::Minimize() {
305 if (collection_)
306 collection_->MinimizePanel(this);
309 bool Panel::IsMinimizedBySystem() const {
310 return native_panel_->IsPanelMinimizedBySystem();
313 bool Panel::IsShownOnActiveDesktop() const {
314 return native_panel_->IsPanelShownOnActiveDesktop();
317 void Panel::ShowShadow(bool show) {
318 native_panel_->ShowShadow(show);
321 void Panel::Restore() {
322 if (collection_)
323 collection_->RestorePanel(this);
326 void Panel::SetBounds(const gfx::Rect& bounds) {
327 // Ignore bounds position as the panel manager controls all positioning.
328 if (!collection_)
329 return;
330 collection_->ResizePanelWindow(this, bounds.size());
331 SetAutoResizable(false);
334 void Panel::FlashFrame(bool draw_attention) {
335 if (IsDrawingAttention() == draw_attention || !collection_)
336 return;
338 // Don't draw attention for an active panel.
339 if (draw_attention && IsActive())
340 return;
342 // Invoking native panel to draw attention must be done before informing the
343 // panel collection because it needs to check internal state of the panel to
344 // determine if the panel has been drawing attention.
345 native_panel_->DrawAttention(draw_attention);
346 collection_->OnPanelAttentionStateChanged(this);
349 bool Panel::IsAlwaysOnTop() const {
350 return native_panel_->IsPanelAlwaysOnTop();
353 void Panel::SetAlwaysOnTop(bool on_top) {
354 native_panel_->SetPanelAlwaysOnTop(on_top);
357 void Panel::ExecuteCommandWithDisposition(int id,
358 WindowOpenDisposition disposition) {
359 DCHECK(command_updater_.IsCommandEnabled(id)) << "Invalid/disabled command "
360 << id;
362 if (!GetWebContents())
363 return;
365 switch (id) {
366 // Navigation
367 case IDC_RELOAD:
368 panel_host_->Reload();
369 break;
370 case IDC_RELOAD_IGNORING_CACHE:
371 panel_host_->ReloadIgnoringCache();
372 break;
373 case IDC_STOP:
374 panel_host_->StopLoading();
375 break;
377 // Window management
378 case IDC_CLOSE_WINDOW:
379 content::RecordAction(UserMetricsAction("CloseWindow"));
380 Close();
381 break;
382 case IDC_EXIT:
383 content::RecordAction(UserMetricsAction("Exit"));
384 chrome::AttemptUserExit();
385 break;
387 // Clipboard
388 case IDC_COPY:
389 content::RecordAction(UserMetricsAction("Copy"));
390 native_panel_->PanelCopy();
391 break;
392 case IDC_CUT:
393 content::RecordAction(UserMetricsAction("Cut"));
394 native_panel_->PanelCut();
395 break;
396 case IDC_PASTE:
397 content::RecordAction(UserMetricsAction("Paste"));
398 native_panel_->PanelPaste();
399 break;
401 // Zoom
402 case IDC_ZOOM_PLUS:
403 panel_host_->Zoom(content::PAGE_ZOOM_IN);
404 break;
405 case IDC_ZOOM_NORMAL:
406 panel_host_->Zoom(content::PAGE_ZOOM_RESET);
407 break;
408 case IDC_ZOOM_MINUS:
409 panel_host_->Zoom(content::PAGE_ZOOM_OUT);
410 break;
412 // DevTools
413 case IDC_DEV_TOOLS:
414 content::RecordAction(UserMetricsAction("DevTools_ToggleWindow"));
415 DevToolsWindow::OpenDevToolsWindow(
416 GetWebContents()->GetRenderViewHost(),
417 DevToolsToggleAction::Show());
418 break;
419 case IDC_DEV_TOOLS_CONSOLE:
420 content::RecordAction(UserMetricsAction("DevTools_ToggleConsole"));
421 DevToolsWindow::OpenDevToolsWindow(
422 GetWebContents()->GetRenderViewHost(),
423 DevToolsToggleAction::ShowConsole());
424 break;
426 default:
427 LOG(WARNING) << "Received unimplemented command: " << id;
428 break;
432 void Panel::Observe(int type,
433 const content::NotificationSource& source,
434 const content::NotificationDetails& details) {
435 switch (type) {
436 case content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED:
437 ConfigureAutoResize(content::Source<content::WebContents>(source).ptr());
438 break;
439 case chrome::NOTIFICATION_EXTENSION_UNLOADED:
440 if (content::Details<extensions::UnloadedExtensionInfo>(
441 details)->extension->id() == extension_id())
442 Close();
443 break;
444 case chrome::NOTIFICATION_APP_TERMINATING:
445 Close();
446 break;
447 default:
448 NOTREACHED() << "Received unexpected notification " << type;
452 void Panel::OnTitlebarClicked(panel::ClickModifier modifier) {
453 if (collection_)
454 collection_->OnPanelTitlebarClicked(this, modifier);
456 // Normally the system activates a window when the titlebar is clicked.
457 // However, we prevent system activation of minimized panels, thus the
458 // activation may not have occurred. Also, some OSes (Windows) will
459 // activate a minimized panel on mouse-down regardless of our attempts to
460 // prevent system activation. Attention state is not cleared in that case.
461 // See Panel::OnActiveStateChanged().
462 // Therefore, we ensure activation and clearing of attention state if the
463 // panel has been expanded. If the panel is in a stack, the titlebar click
464 // might minimize the panel and we do not want to activate it to make it
465 // expand again.
466 // These are no-ops if no changes are needed.
467 if (IsMinimized())
468 return;
469 Activate();
470 FlashFrame(false);
473 void Panel::OnMinimizeButtonClicked(panel::ClickModifier modifier) {
474 if (collection_)
475 collection_->OnMinimizeButtonClicked(this, modifier);
478 void Panel::OnRestoreButtonClicked(panel::ClickModifier modifier) {
479 // Clicking the restore button has the same behavior as clicking the titlebar.
480 OnTitlebarClicked(modifier);
483 void Panel::OnWindowSizeAvailable() {
484 ConfigureAutoResize(GetWebContents());
487 void Panel::OnNativePanelClosed() {
488 // Ensure previously enqueued OnImageLoaded callbacks are ignored.
489 image_loader_ptr_factory_.InvalidateWeakPtrs();
490 registrar_.RemoveAll();
491 manager()->OnPanelClosed(this);
492 DCHECK(!collection_);
495 StackedPanelCollection* Panel::stack() const {
496 return collection_ && collection_->type() == PanelCollection::STACKED ?
497 static_cast<StackedPanelCollection*>(collection_) : NULL;
500 panel::Resizability Panel::CanResizeByMouse() const {
501 if (!collection_)
502 return panel::NOT_RESIZABLE;
504 return collection_->GetPanelResizability(this);
507 void Panel::Initialize(const GURL& url,
508 const gfx::Rect& bounds,
509 bool always_on_top) {
510 DCHECK(!initialized_);
511 DCHECK(!collection_); // Cannot be added to a collection until fully created.
512 DCHECK_EQ(EXPANDED, expansion_state_);
513 DCHECK(!bounds.IsEmpty());
514 initialized_ = true;
515 full_size_ = bounds.size();
516 native_panel_ = CreateNativePanel(this, bounds, always_on_top);
518 extension_window_controller_.reset(
519 new panel_internal::PanelExtensionWindowController(this, profile_));
521 InitCommandState();
523 // Set up hosting for web contents.
524 panel_host_.reset(new PanelHost(this, profile_));
525 panel_host_->Init(url);
526 content::WebContents* web_contents = GetWebContents();
527 // The contents might be NULL for most of our tests.
528 if (web_contents)
529 native_panel_->AttachWebContents(web_contents);
531 // Close when the extension is unloaded or the browser is exiting.
532 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
533 content::Source<Profile>(profile_));
534 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
535 content::NotificationService::AllSources());
536 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
537 content::Source<ThemeService>(
538 ThemeServiceFactory::GetForProfile(profile_)));
540 // Prevent the browser process from shutting down while this window is open.
541 chrome::StartKeepAlive();
543 UpdateAppIcon();
546 void Panel::SetPanelBounds(const gfx::Rect& bounds) {
547 if (bounds != native_panel_->GetPanelBounds())
548 native_panel_->SetPanelBounds(bounds);
551 void Panel::SetPanelBoundsInstantly(const gfx::Rect& bounds) {
552 native_panel_->SetPanelBoundsInstantly(bounds);
555 void Panel::LimitSizeToWorkArea(const gfx::Rect& work_area) {
556 int max_width = manager()->GetMaxPanelWidth(work_area);
557 int max_height = manager()->GetMaxPanelHeight(work_area);
559 // If the custom max size is used, ensure that it does not exceed the display
560 // area.
561 if (max_size_policy_ == CUSTOM_MAX_SIZE) {
562 int current_max_width = max_size_.width();
563 if (current_max_width > max_width)
564 max_width = std::min(current_max_width, work_area.width());
565 int current_max_height = max_size_.height();
566 if (current_max_height > max_height)
567 max_height = std::min(current_max_height, work_area.height());
570 SetSizeRange(min_size_, gfx::Size(max_width, max_height));
572 // Ensure that full size does not exceed max size.
573 full_size_ = ClampSize(full_size_);
576 void Panel::SetAutoResizable(bool resizable) {
577 if (auto_resizable_ == resizable)
578 return;
580 auto_resizable_ = resizable;
581 content::WebContents* web_contents = GetWebContents();
582 if (auto_resizable_) {
583 if (web_contents)
584 EnableWebContentsAutoResize(web_contents);
585 } else {
586 if (web_contents) {
587 registrar_.Remove(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
588 content::Source<content::WebContents>(web_contents));
590 // NULL might be returned if the tab has not been added.
591 RenderViewHost* render_view_host = web_contents->GetRenderViewHost();
592 if (render_view_host)
593 render_view_host->DisableAutoResize(full_size_);
598 void Panel::EnableWebContentsAutoResize(content::WebContents* web_contents) {
599 DCHECK(web_contents);
600 ConfigureAutoResize(web_contents);
602 // We also need to know when the render view host changes in order
603 // to turn on auto-resize notifications in the new render view host.
604 if (!registrar_.IsRegistered(
605 this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
606 content::Source<content::WebContents>(web_contents))) {
607 registrar_.Add(
608 this,
609 content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
610 content::Source<content::WebContents>(web_contents));
614 void Panel::OnContentsAutoResized(const gfx::Size& new_content_size) {
615 DCHECK(auto_resizable_);
616 if (!collection_)
617 return;
619 gfx::Size new_window_size =
620 native_panel_->WindowSizeFromContentSize(new_content_size);
622 // Ignore content auto resizes until window frame size is known.
623 // This reduces extra resizes when panel is first shown.
624 // After window frame size is known, it will trigger another content
625 // auto resize.
626 if (new_content_size == new_window_size)
627 return;
629 collection_->ResizePanelWindow(this, new_window_size);
632 void Panel::OnWindowResizedByMouse(const gfx::Rect& new_bounds) {
633 if (collection_)
634 collection_->OnPanelResizedByMouse(this, new_bounds);
637 void Panel::SetSizeRange(const gfx::Size& min_size, const gfx::Size& max_size) {
638 if (min_size == min_size_ && max_size == max_size_)
639 return;
641 DCHECK(min_size.width() <= max_size.width());
642 DCHECK(min_size.height() <= max_size.height());
643 min_size_ = min_size;
644 max_size_ = max_size;
646 ConfigureAutoResize(GetWebContents());
649 void Panel::IncreaseMaxSize(const gfx::Size& desired_panel_size) {
650 gfx::Size new_max_size = max_size_;
651 if (new_max_size.width() < desired_panel_size.width())
652 new_max_size.set_width(desired_panel_size.width());
653 if (new_max_size.height() < desired_panel_size.height())
654 new_max_size.set_height(desired_panel_size.height());
656 SetSizeRange(min_size_, new_max_size);
659 void Panel::HandleKeyboardEvent(const content::NativeWebKeyboardEvent& event) {
660 native_panel_->HandlePanelKeyboardEvent(event);
663 void Panel::SetPreviewMode(bool in_preview) {
664 DCHECK_NE(in_preview_mode_, in_preview);
665 in_preview_mode_ = in_preview;
668 void Panel::UpdateMinimizeRestoreButtonVisibility() {
669 native_panel_->UpdatePanelMinimizeRestoreButtonVisibility();
672 gfx::Size Panel::ClampSize(const gfx::Size& size) const {
673 // The panel width:
674 // * cannot grow or shrink to go beyond [min_width, max_width]
675 int new_width = size.width();
676 if (new_width > max_size_.width())
677 new_width = max_size_.width();
678 if (new_width < min_size_.width())
679 new_width = min_size_.width();
681 // The panel height:
682 // * cannot grow or shrink to go beyond [min_height, max_height]
683 int new_height = size.height();
684 if (new_height > max_size_.height())
685 new_height = max_size_.height();
686 if (new_height < min_size_.height())
687 new_height = min_size_.height();
689 return gfx::Size(new_width, new_height);
692 void Panel::OnActiveStateChanged(bool active) {
693 // Clear attention state when an expanded panel becomes active.
694 // On some systems (e.g. Win), mouse-down activates a panel regardless of
695 // its expansion state. However, we don't want to clear draw attention if
696 // contents are not visible. In that scenario, if the mouse-down results
697 // in a mouse-click, draw attention will be cleared then.
698 // See Panel::OnTitlebarClicked().
699 if (active && IsDrawingAttention() && !IsMinimized())
700 FlashFrame(false);
702 if (collection_)
703 collection_->OnPanelActiveStateChanged(this);
705 // Send extension event about window changing active state.
706 extensions::TabsWindowsAPI* tabs_windows_api =
707 extensions::TabsWindowsAPI::Get(profile());
708 if (tabs_windows_api) {
709 tabs_windows_api->windows_event_router()->OnActiveWindowChanged(
710 active ? extension_window_controller_.get() : NULL);
713 content::NotificationService::current()->Notify(
714 chrome::NOTIFICATION_PANEL_CHANGED_ACTIVE_STATUS,
715 content::Source<Panel>(this),
716 content::NotificationService::NoDetails());
719 void Panel::OnPanelStartUserResizing() {
720 SetAutoResizable(false);
721 SetPreviewMode(true);
722 max_size_policy_ = CUSTOM_MAX_SIZE;
725 void Panel::OnPanelEndUserResizing() {
726 SetPreviewMode(false);
729 bool Panel::ShouldCloseWindow() {
730 return true;
733 void Panel::OnWindowClosing() {
734 if (GetWebContents()) {
735 native_panel_->DetachWebContents(GetWebContents());
736 panel_host_->DestroyWebContents();
740 bool Panel::ExecuteCommandIfEnabled(int id) {
741 if (command_updater()->SupportsCommand(id) &&
742 command_updater()->IsCommandEnabled(id)) {
743 ExecuteCommandWithDisposition(id, CURRENT_TAB);
744 return true;
746 return false;
749 base::string16 Panel::GetWindowTitle() const {
750 content::WebContents* contents = GetWebContents();
751 base::string16 title;
753 // |contents| can be NULL during the window's creation.
754 if (contents) {
755 title = contents->GetTitle();
756 FormatTitleForDisplay(&title);
759 if (title.empty())
760 title = base::UTF8ToUTF16(app_name());
762 return title;
765 gfx::Image Panel::GetCurrentPageIcon() const {
766 return panel_host_->GetPageIcon();
769 void Panel::UpdateTitleBar() {
770 native_panel_->UpdatePanelTitleBar();
773 void Panel::LoadingStateChanged(bool is_loading) {
774 command_updater_.UpdateCommandEnabled(IDC_STOP, is_loading);
775 native_panel_->UpdatePanelLoadingAnimations(is_loading);
776 UpdateTitleBar();
779 void Panel::WebContentsFocused(content::WebContents* contents) {
780 native_panel_->PanelWebContentsFocused(contents);
783 void Panel::MoveByInstantly(const gfx::Vector2d& delta_origin) {
784 gfx::Rect bounds = GetBounds();
785 bounds.Offset(delta_origin);
786 SetPanelBoundsInstantly(bounds);
789 void Panel::SetWindowCornerStyle(panel::CornerStyle corner_style) {
790 native_panel_->SetWindowCornerStyle(corner_style);
793 void Panel::MinimizeBySystem() {
794 native_panel_->MinimizePanelBySystem();
797 Panel::Panel(Profile* profile, const std::string& app_name,
798 const gfx::Size& min_size, const gfx::Size& max_size)
799 : app_name_(app_name),
800 profile_(profile),
801 collection_(NULL),
802 initialized_(false),
803 min_size_(min_size),
804 max_size_(max_size),
805 max_size_policy_(DEFAULT_MAX_SIZE),
806 auto_resizable_(false),
807 in_preview_mode_(false),
808 native_panel_(NULL),
809 attention_mode_(USE_PANEL_ATTENTION),
810 expansion_state_(EXPANDED),
811 command_updater_(this),
812 image_loader_ptr_factory_(this) {
815 void Panel::OnImageLoaded(const gfx::Image& image) {
816 if (!image.IsEmpty()) {
817 app_icon_ = image;
818 native_panel_->UpdatePanelTitleBar();
821 content::NotificationService::current()->Notify(
822 chrome::NOTIFICATION_PANEL_APP_ICON_LOADED,
823 content::Source<Panel>(this),
824 content::NotificationService::NoDetails());
827 void Panel::InitCommandState() {
828 // All supported commands whose state isn't set automagically some other way
829 // (like Stop during a page load) must have their state initialized here,
830 // otherwise they will be forever disabled.
832 // Navigation commands
833 command_updater_.UpdateCommandEnabled(IDC_RELOAD, true);
834 command_updater_.UpdateCommandEnabled(IDC_RELOAD_IGNORING_CACHE, true);
836 // Window management commands
837 command_updater_.UpdateCommandEnabled(IDC_CLOSE_WINDOW, true);
838 command_updater_.UpdateCommandEnabled(IDC_EXIT, true);
840 // Zoom
841 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MENU, true);
842 command_updater_.UpdateCommandEnabled(IDC_ZOOM_PLUS, true);
843 command_updater_.UpdateCommandEnabled(IDC_ZOOM_NORMAL, true);
844 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MINUS, true);
846 // Clipboard
847 command_updater_.UpdateCommandEnabled(IDC_COPY, true);
848 command_updater_.UpdateCommandEnabled(IDC_CUT, true);
849 command_updater_.UpdateCommandEnabled(IDC_PASTE, true);
851 // DevTools
852 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS, true);
853 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_CONSOLE, true);
856 void Panel::ConfigureAutoResize(content::WebContents* web_contents) {
857 if (!auto_resizable_ || !web_contents)
858 return;
860 // NULL might be returned if the tab has not been added.
861 RenderViewHost* render_view_host = web_contents->GetRenderViewHost();
862 if (!render_view_host)
863 return;
865 render_view_host->EnableAutoResize(
866 min_size_,
867 native_panel_->ContentSizeFromWindowSize(max_size_));
870 void Panel::UpdateAppIcon() {
871 const extensions::Extension* extension = GetExtension();
872 if (!extension)
873 return;
875 extensions::ImageLoader* loader = extensions::ImageLoader::Get(profile());
876 loader->LoadImageAsync(
877 extension,
878 extensions::IconsInfo::GetIconResource(
879 extension,
880 extension_misc::EXTENSION_ICON_SMALL,
881 ExtensionIconSet::MATCH_BIGGER),
882 gfx::Size(extension_misc::EXTENSION_ICON_SMALL,
883 extension_misc::EXTENSION_ICON_SMALL),
884 base::Bind(&Panel::OnImageLoaded,
885 image_loader_ptr_factory_.GetWeakPtr()));
888 // static
889 void Panel::FormatTitleForDisplay(base::string16* title) {
890 size_t current_index = 0;
891 size_t match_index;
892 while ((match_index = title->find(L'\n', current_index)) !=
893 base::string16::npos) {
894 title->replace(match_index, 1, base::string16());
895 current_index = match_index;