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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
// Copyright (c) 2012 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/gtk/extensions/extension_popup_gtk.h"
#include <gtk/gtk.h>
#include <algorithm>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/i18n/rtl.h"
#include "base/message_loop.h"
#include "chrome/browser/debugger/devtools_window.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/gtk/gtk_theme_service.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "googleurl/src/gurl.h"
using content::RenderViewHost;
ExtensionPopupGtk* ExtensionPopupGtk::current_extension_popup_ = NULL;
// The minimum/maximum dimensions of the extension popup.
// The minimum is just a little larger than the size of a browser action button.
// The maximum is an arbitrary number that should be smaller than most screens.
const int ExtensionPopupGtk::kMinWidth = 25;
const int ExtensionPopupGtk::kMinHeight = 25;
const int ExtensionPopupGtk::kMaxWidth = 800;
const int ExtensionPopupGtk::kMaxHeight = 600;
ExtensionPopupGtk::ExtensionPopupGtk(Browser* browser,
ExtensionHost* host,
GtkWidget* anchor,
ShowAction show_action)
: browser_(browser),
bubble_(NULL),
host_(host),
anchor_(anchor),
weak_factory_(this) {
host_->view()->SetContainer(this);
being_inspected_ = show_action == SHOW_AND_INSPECT;
// If the host had somehow finished loading, then we'd miss the notification
// and not show. This seems to happen in single-process mode.
if (host->did_stop_loading()) {
ShowPopup();
} else {
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
content::Source<Profile>(host->profile()));
}
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE,
content::Source<Profile>(host->profile()));
registrar_.Add(this, content::NOTIFICATION_DEVTOOLS_WINDOW_CLOSING,
content::Source<Profile>(host->profile()));
if (!being_inspected_) {
registrar_.Add(this, content::NOTIFICATION_DEVTOOLS_WINDOW_OPENING,
content::Source<Profile>(host->profile()));
}
}
ExtensionPopupGtk::~ExtensionPopupGtk() {
}
// static
void ExtensionPopupGtk::Show(const GURL& url, Browser* browser,
GtkWidget* anchor, ShowAction show_action) {
ExtensionProcessManager* manager =
browser->profile()->GetExtensionProcessManager();
DCHECK(manager);
if (!manager)
return;
ExtensionHost* host = manager->CreatePopupHost(url, browser);
// This object will delete itself when the bubble is closed.
new ExtensionPopupGtk(browser, host, anchor, show_action);
}
void ExtensionPopupGtk::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING:
if (content::Details<ExtensionHost>(host_.get()) == details)
ShowPopup();
break;
case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE:
if (content::Details<ExtensionHost>(host_.get()) == details)
DestroyPopup();
break;
case content::NOTIFICATION_DEVTOOLS_WINDOW_OPENING:
// Make sure it's the devtools window that is inspecting our popup.
if (content::Details<RenderViewHost>(host_->render_view_host()) !=
details)
break;
// Make sure that the popup won't go away when the inspector is activated.
if (bubble_)
bubble_->StopGrabbingInput();
being_inspected_ = true;
break;
case content::NOTIFICATION_DEVTOOLS_WINDOW_CLOSING:
// Make sure it's the devtools window that is inspecting our popup.
if (content::Details<RenderViewHost>(host_->render_view_host()) !=
details)
break;
// If the devtools window is closing, we post a task to ourselves to
// close the popup. This gives the devtools window a chance to finish
// detaching from the inspected RenderViewHost.
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&ExtensionPopupGtk::DestroyPopupWithoutResult,
weak_factory_.GetWeakPtr()));
break;
default:
NOTREACHED() << "Received unexpected notification";
}
}
void ExtensionPopupGtk::BubbleClosing(BubbleGtk* bubble,
bool closed_by_escape) {
current_extension_popup_ = NULL;
delete this;
}
void ExtensionPopupGtk::OnExtensionSizeChanged(
ExtensionViewGtk* view,
const gfx::Size& new_size) {
int width = std::max(kMinWidth, std::min(kMaxWidth, new_size.width()));
int height = std::max(kMinHeight, std::min(kMaxHeight, new_size.height()));
view->render_view_host()->GetView()->SetSize(gfx::Size(width, height));
gtk_widget_set_size_request(view->native_view(), width, height);
}
bool ExtensionPopupGtk::DestroyPopup() {
if (!bubble_) {
NOTREACHED();
return false;
}
bubble_->Close();
return true;
}
void ExtensionPopupGtk::ShowPopup() {
if (bubble_) {
NOTREACHED();
return;
}
if (being_inspected_)
DevToolsWindow::OpenDevToolsWindow(host_->render_view_host());
// Only one instance should be showing at a time. Get rid of the old one, if
// any. Typically, |current_extension_popup_| will be NULL, but it can be
// non-NULL if a browser action button is clicked while another extension
// popup's extension host is still loading.
if (current_extension_popup_)
current_extension_popup_->DestroyPopup();
current_extension_popup_ = this;
// We'll be in the upper-right corner of the window for LTR languages, so we
// want to put the arrow at the upper-right corner of the bubble to match the
// page and app menus.
BubbleGtk::ArrowLocationGtk arrow_location =
!base::i18n::IsRTL() ?
BubbleGtk::ARROW_LOCATION_TOP_RIGHT :
BubbleGtk::ARROW_LOCATION_TOP_LEFT;
bubble_ = BubbleGtk::Show(anchor_,
NULL,
host_->view()->native_view(),
arrow_location,
being_inspected_ ? 0 :
BubbleGtk::POPUP_WINDOW | BubbleGtk::GRAB_INPUT,
GtkThemeService::GetFrom(browser_->profile()),
this);
}
void ExtensionPopupGtk::DestroyPopupWithoutResult() {
DestroyPopup();
}
gfx::Rect ExtensionPopupGtk::GetViewBounds() {
GtkAllocation allocation;
gtk_widget_get_allocation(host_->view()->native_view(), &allocation);
return gfx::Rect(allocation);
}
|