This source file includes following definitions.
- weak_factory_
- Show
- Observe
- OnDevToolsStateChanged
- BubbleClosing
- OnExtensionSizeChanged
- DestroyPopup
- ShowPopup
- DestroyPopupWithoutResult
- GetViewBounds
#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/message_loop.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/devtools/devtools_window.h"
#include "chrome/browser/extensions/extension_view_host.h"
#include "chrome/browser/extensions/extension_view_host_factory.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 "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/devtools_manager.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 "url/gurl.h"
using content::BrowserContext;
using content::RenderViewHost;
ExtensionPopupGtk* ExtensionPopupGtk::current_extension_popup_ = NULL;
const int ExtensionPopupGtk::kMinWidth = 25;
const int ExtensionPopupGtk::kMinHeight = 25;
const int ExtensionPopupGtk::kMaxWidth = 800;
const int ExtensionPopupGtk::kMaxHeight = 600;
ExtensionPopupGtk::ExtensionPopupGtk(Browser* browser,
extensions::ExtensionViewHost* host,
GtkWidget* anchor,
ShowAction show_action)
: browser_(browser),
bubble_(NULL),
host_(host),
anchor_(anchor),
devtools_callback_(base::Bind(
&ExtensionPopupGtk::OnDevToolsStateChanged, base::Unretained(this))),
weak_factory_(this) {
host_->view()->SetContainer(this);
being_inspected_ = show_action == SHOW_AND_INSPECT;
if (host->did_stop_loading()) {
ShowPopup();
} else {
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
content::Source<BrowserContext>(host->browser_context()));
}
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE,
content::Source<BrowserContext>(host->browser_context()));
content::DevToolsManager::GetInstance()->AddAgentStateCallback(
devtools_callback_);
}
ExtensionPopupGtk::~ExtensionPopupGtk() {
content::DevToolsManager::GetInstance()->RemoveAgentStateCallback(
devtools_callback_);
}
void ExtensionPopupGtk::Show(const GURL& url, Browser* browser,
GtkWidget* anchor, ShowAction show_action) {
extensions::ExtensionViewHost* host =
extensions::ExtensionViewHostFactory::CreatePopupHost(url, browser);
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<extensions::ExtensionHost>(host_.get()) == details)
ShowPopup();
break;
case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE:
if (content::Details<extensions::ExtensionHost>(host_.get()) == details)
DestroyPopup();
break;
default:
NOTREACHED() << "Received unexpected notification";
}
}
void ExtensionPopupGtk::OnDevToolsStateChanged(
content::DevToolsAgentHost* agent_host, bool attached) {
if (host_->render_view_host() != agent_host->GetRenderViewHost())
return;
if (attached) {
if (bubble_)
bubble_->StopGrabbingInput();
being_inspected_ = true;
} else {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&ExtensionPopupGtk::DestroyPopupWithoutResult,
weak_factory_.GetWeakPtr()));
}
}
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());
if (current_extension_popup_)
current_extension_popup_->DestroyPopup();
current_extension_popup_ = this;
GtkWidget* border_box = gtk_alignment_new(0, 0, 1.0, 1.0);
gtk_container_set_border_width(GTK_CONTAINER(border_box), 2);
gtk_container_add(GTK_CONTAINER(border_box), host_->view()->native_view());
bubble_ = BubbleGtk::Show(anchor_,
NULL,
border_box,
BubbleGtk::ANCHOR_TOP_RIGHT,
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);
}