summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/extensions/extension_dialog.cc
blob: 50ac515246dabb2fc9e5cd7586f314f9cfb275f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/extensions/extension_dialog.h"

#include "chrome/browser/extensions/extension_host.h"
#include "chrome/browser/extensions/extension_process_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/views/bubble/bubble_border.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/common/extensions/extension.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/renderer_host/render_widget_host_view.h"
#include "content/common/notification_details.h"
#include "content/common/notification_source.h"
#include "content/common/notification_type.h"
#include "googleurl/src/gurl.h"
#include "views/widget/root_view.h"
#include "views/window/window.h"

#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/wm_ipc.h"
#include "third_party/cros/chromeos_wm_ipc_enums.h"
#endif

ExtensionDialog::ExtensionDialog(ExtensionHost* host, views::Widget* frame,
                                 const gfx::Rect& relative_to, int width,
                                 int height, Observer* observer)
    : BrowserBubble(host->view(),
                    frame,
                    relative_to,
                    BubbleBorder::FLOAT),  // Centered over rectangle.
      extension_host_(host),
      width_(width),
      height_(height),
      closing_(false),
      observer_(observer) {
  AddRef();  // Balanced in Close();
  set_delegate(this);
  host->view()->SetContainer(this);

  // Listen for the containing view calling window.close();
  registrar_.Add(this, NotificationType::EXTENSION_HOST_VIEW_SHOULD_CLOSE,
                 Source<Profile>(host->profile()));

  // Use a fixed-size view.
  host->view()->SetBounds(host->view()->x(), host->view()->y(),
                          width, height);
  ResizeToView();
}

ExtensionDialog::~ExtensionDialog() {
}

// static
ExtensionDialog* ExtensionDialog::Show(
    const GURL& url,
    Browser* browser,
    int width,
    int height,
    Observer* observer) {
  CHECK(browser);
  ExtensionProcessManager* manager =
      browser->profile()->GetExtensionProcessManager();
  DCHECK(manager);
  if (!manager)
    return NULL;
  ExtensionHost* host = manager->CreateDialogHost(url, browser);
  DCHECK(host);
  views::Widget* frame = BrowserView::GetBrowserViewForNativeWindow(
      browser->window()->GetNativeHandle())->GetWidget();
  gfx::Rect relative_to = browser->window()->GetBounds();
  ExtensionDialog* popup = new ExtensionDialog(host, frame, relative_to, width,
                                               height, observer);
  popup->Show(true);

  return popup;
}

void ExtensionDialog::ObserverDestroyed() {
  observer_ = NULL;
}

void ExtensionDialog::Close() {
  if (closing_)
    return;
  closing_ = true;
  DetachFromBrowser();

  if (observer_)
    observer_->ExtensionDialogIsClosing(this);

  Release();  // Balanced in ctor.
}

void ExtensionDialog::Show(bool activate) {
  if (popup_->IsVisible())
    return;

#if defined(OS_WIN)
  frame_->GetContainingWindow()->DisableInactiveRendering();
#endif

  BrowserBubble::Show(activate);
}

/////////////////////////////////////////////////////////////////////////////
// BrowserBubble::Delegate methods.

void ExtensionDialog::BubbleBrowserWindowMoved(BrowserBubble* bubble) {
}

void ExtensionDialog::BubbleBrowserWindowClosing(BrowserBubble* bubble) {
  if (!closing_)
    Close();
}

void ExtensionDialog::BubbleGotFocus(BrowserBubble* bubble) {
  // Forward the focus to the renderer.
  host()->render_view_host()->view()->Focus();
}

void ExtensionDialog::BubbleLostFocus(BrowserBubble* bubble,
    bool lost_focus_to_child) {
}

/////////////////////////////////////////////////////////////////////////////
// ExtensionView::Container overrides.

void ExtensionDialog::OnExtensionMouseMove(ExtensionView* view) {
}

void ExtensionDialog::OnExtensionMouseLeave(ExtensionView* view) {
}

void ExtensionDialog::OnExtensionPreferredSizeChanged(ExtensionView* view) {
}

/////////////////////////////////////////////////////////////////////////////
// NotificationObserver overrides.

void ExtensionDialog::Observe(NotificationType type,
                             const NotificationSource& source,
                             const NotificationDetails& details) {
  switch (type.value) {
    case NotificationType::EXTENSION_HOST_VIEW_SHOULD_CLOSE:
      // If we aren't the host of the popup, then disregard the notification.
      if (Details<ExtensionHost>(host()) != details)
        return;
      Close();
      break;
    default:
      NOTREACHED() << L"Received unexpected notification";
      break;
  }
}