2013-04-12 07:04:46 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "browser/native_window.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2013-07-23 07:29:56 +00:00
|
|
|
#include "base/message_loop.h"
|
2013-04-18 12:50:58 +00:00
|
|
|
#include "base/utf_string_conversions.h"
|
2013-04-12 07:04:46 +00:00
|
|
|
#include "base/values.h"
|
|
|
|
#include "brightray/browser/inspectable_web_contents.h"
|
|
|
|
#include "brightray/browser/inspectable_web_contents_view.h"
|
2013-04-22 13:32:48 +00:00
|
|
|
#include "browser/api/atom_browser_bindings.h"
|
2013-04-20 05:42:39 +00:00
|
|
|
#include "browser/atom_browser_context.h"
|
2013-04-22 13:32:48 +00:00
|
|
|
#include "browser/atom_browser_main_parts.h"
|
2013-04-30 15:56:50 +00:00
|
|
|
#include "browser/atom_javascript_dialog_manager.h"
|
2013-05-02 14:54:09 +00:00
|
|
|
#include "browser/window_list.h"
|
2013-07-18 11:21:25 +00:00
|
|
|
#include "content/public/browser/devtools_agent_host.h"
|
2013-06-10 12:42:10 +00:00
|
|
|
#include "content/public/browser/invalidate_type.h"
|
2013-04-18 12:50:58 +00:00
|
|
|
#include "content/public/browser/navigation_entry.h"
|
|
|
|
#include "content/public/browser/notification_details.h"
|
|
|
|
#include "content/public/browser/notification_source.h"
|
|
|
|
#include "content/public/browser/notification_types.h"
|
2013-04-23 04:18:07 +00:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
2013-05-01 07:42:30 +00:00
|
|
|
#include "content/public/browser/render_view_host.h"
|
2013-08-16 04:56:25 +00:00
|
|
|
#include "content/public/browser/render_widget_host_view.h"
|
2013-04-22 13:32:48 +00:00
|
|
|
#include "common/api/api_messages.h"
|
2013-04-12 07:04:46 +00:00
|
|
|
#include "common/options_switches.h"
|
2013-04-22 13:32:48 +00:00
|
|
|
#include "ipc/ipc_message_macros.h"
|
2013-04-12 07:04:46 +00:00
|
|
|
#include "ui/gfx/point.h"
|
|
|
|
#include "ui/gfx/rect.h"
|
|
|
|
#include "ui/gfx/size.h"
|
|
|
|
|
2013-04-18 12:50:58 +00:00
|
|
|
using content::NavigationEntry;
|
|
|
|
|
2013-04-12 07:04:46 +00:00
|
|
|
namespace atom {
|
|
|
|
|
2013-04-20 05:42:39 +00:00
|
|
|
NativeWindow::NativeWindow(content::WebContents* web_contents,
|
2013-04-12 07:04:46 +00:00
|
|
|
base::DictionaryValue* options)
|
2013-04-22 13:32:48 +00:00
|
|
|
: content::WebContentsObserver(web_contents),
|
2013-09-05 13:43:47 +00:00
|
|
|
has_frame_(true),
|
2013-05-02 12:08:23 +00:00
|
|
|
is_closed_(false),
|
2013-07-23 07:29:56 +00:00
|
|
|
not_responding_(false),
|
2013-04-22 13:32:48 +00:00
|
|
|
inspectable_web_contents_(
|
2013-05-01 08:12:00 +00:00
|
|
|
brightray::InspectableWebContents::Create(web_contents)) {
|
2013-09-05 13:43:47 +00:00
|
|
|
options->GetBoolean(switches::kFrame, &has_frame_);
|
|
|
|
|
2013-04-18 12:50:58 +00:00
|
|
|
web_contents->SetDelegate(this);
|
|
|
|
|
2013-05-02 14:54:09 +00:00
|
|
|
WindowList::AddWindow(this);
|
2013-04-30 12:32:23 +00:00
|
|
|
|
2013-04-21 03:01:04 +00:00
|
|
|
// Get notified of title updated message.
|
2013-04-18 12:50:58 +00:00
|
|
|
registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED,
|
|
|
|
content::Source<content::WebContents>(web_contents));
|
2013-04-12 07:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NativeWindow::~NativeWindow() {
|
2013-05-02 12:08:23 +00:00
|
|
|
// It's possible that the windows gets destroyed before it's closed, in that
|
|
|
|
// case we need to ensure the OnWindowClosed message is still notified.
|
|
|
|
NotifyWindowClosed();
|
2013-04-12 07:04:46 +00:00
|
|
|
}
|
|
|
|
|
2013-04-20 05:42:39 +00:00
|
|
|
// static
|
|
|
|
NativeWindow* NativeWindow::Create(base::DictionaryValue* options) {
|
|
|
|
content::WebContents::CreateParams create_params(AtomBrowserContext::Get());
|
|
|
|
return Create(content::WebContents::Create(create_params), options);
|
|
|
|
}
|
|
|
|
|
2013-04-30 12:32:23 +00:00
|
|
|
// static
|
2013-05-03 11:31:24 +00:00
|
|
|
NativeWindow* NativeWindow::FromRenderView(int process_id, int routing_id) {
|
2013-04-30 12:32:23 +00:00
|
|
|
// Stupid iterating.
|
2013-05-02 14:54:09 +00:00
|
|
|
WindowList& window_list = *WindowList::GetInstance();
|
2013-07-01 14:01:17 +00:00
|
|
|
for (auto w = window_list.begin(); w != window_list.end(); ++w) {
|
|
|
|
auto& window = *w;
|
2013-04-30 12:32:23 +00:00
|
|
|
content::WebContents* web_contents = window->GetWebContents();
|
|
|
|
int window_process_id = web_contents->GetRenderProcessHost()->GetID();
|
|
|
|
int window_routing_id = web_contents->GetRoutingID();
|
|
|
|
if (window_routing_id == routing_id && window_process_id == process_id)
|
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-04-12 07:04:46 +00:00
|
|
|
void NativeWindow::InitFromOptions(base::DictionaryValue* options) {
|
|
|
|
// Setup window from options.
|
2013-07-01 14:01:17 +00:00
|
|
|
int x = -1, y = -1;
|
2013-05-10 12:34:05 +00:00
|
|
|
bool center;
|
2013-04-12 07:04:46 +00:00
|
|
|
if (options->GetInteger(switches::kX, &x) &&
|
|
|
|
options->GetInteger(switches::kY, &y)) {
|
2013-07-01 14:01:17 +00:00
|
|
|
int width = -1, height = -1;
|
2013-04-12 07:04:46 +00:00
|
|
|
options->GetInteger(switches::kWidth, &width);
|
|
|
|
options->GetInteger(switches::kHeight, &height);
|
|
|
|
Move(gfx::Rect(x, y, width, height));
|
2013-05-10 12:34:05 +00:00
|
|
|
} else if (options->GetBoolean(switches::kCenter, ¢er) && center) {
|
|
|
|
Center();
|
2013-04-12 07:04:46 +00:00
|
|
|
}
|
2013-07-01 14:01:17 +00:00
|
|
|
int min_height = -1, min_width = -1;
|
2013-04-12 07:04:46 +00:00
|
|
|
if (options->GetInteger(switches::kMinHeight, &min_height) &&
|
|
|
|
options->GetInteger(switches::kMinWidth, &min_width)) {
|
2013-04-17 14:49:49 +00:00
|
|
|
SetMinimumSize(gfx::Size(min_width, min_height));
|
2013-04-12 07:04:46 +00:00
|
|
|
}
|
2013-07-01 14:01:17 +00:00
|
|
|
int max_height = -1, max_width = -1;
|
2013-04-12 07:04:46 +00:00
|
|
|
if (options->GetInteger(switches::kMaxHeight, &max_height) &&
|
|
|
|
options->GetInteger(switches::kMaxWidth, &max_width)) {
|
2013-04-17 14:49:49 +00:00
|
|
|
SetMaximumSize(gfx::Size(max_width, max_height));
|
2013-04-12 07:04:46 +00:00
|
|
|
}
|
|
|
|
bool resizable;
|
|
|
|
if (options->GetBoolean(switches::kResizable, &resizable)) {
|
|
|
|
SetResizable(resizable);
|
|
|
|
}
|
|
|
|
bool top;
|
|
|
|
if (options->GetBoolean(switches::kAlwaysOnTop, &top) && top) {
|
|
|
|
SetAlwaysOnTop(true);
|
|
|
|
}
|
|
|
|
bool fullscreen;
|
|
|
|
if (options->GetBoolean(switches::kFullscreen, &fullscreen) && fullscreen) {
|
|
|
|
SetFullscreen(true);
|
|
|
|
}
|
|
|
|
bool kiosk;
|
|
|
|
if (options->GetBoolean(switches::kKiosk, &kiosk) && kiosk) {
|
|
|
|
SetKiosk(kiosk);
|
|
|
|
}
|
|
|
|
std::string title("Atom Shell");
|
|
|
|
options->GetString(switches::kTitle, &title);
|
|
|
|
SetTitle(title);
|
|
|
|
|
|
|
|
// Then show it.
|
|
|
|
bool show = true;
|
|
|
|
options->GetBoolean(switches::kShow, &show);
|
|
|
|
if (show)
|
|
|
|
Show();
|
|
|
|
}
|
|
|
|
|
2013-05-10 12:34:05 +00:00
|
|
|
void NativeWindow::OpenDevTools() {
|
2013-04-12 07:53:29 +00:00
|
|
|
inspectable_web_contents()->ShowDevTools();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::CloseDevTools() {
|
2013-04-12 08:41:15 +00:00
|
|
|
inspectable_web_contents()->GetView()->CloseDevTools();
|
2013-04-12 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2013-07-18 11:21:25 +00:00
|
|
|
void NativeWindow::InspectElement(int x, int y) {
|
|
|
|
OpenDevTools();
|
|
|
|
content::RenderViewHost* rvh = GetWebContents()->GetRenderViewHost();
|
|
|
|
scoped_refptr<content::DevToolsAgentHost> agent(
|
|
|
|
content::DevToolsAgentHost::GetOrCreateFor(rvh));
|
|
|
|
agent->InspectElement(x, y);
|
|
|
|
}
|
|
|
|
|
2013-05-24 09:51:15 +00:00
|
|
|
void NativeWindow::FocusOnWebView() {
|
|
|
|
GetWebContents()->GetRenderViewHost()->Focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::BlurWebView() {
|
|
|
|
GetWebContents()->GetRenderViewHost()->Blur();
|
|
|
|
}
|
|
|
|
|
2013-08-16 04:56:25 +00:00
|
|
|
bool NativeWindow::IsWebViewFocused() {
|
|
|
|
return GetWebContents()->GetRenderViewHost()->GetView()->HasFocus();
|
|
|
|
}
|
|
|
|
|
2013-06-07 10:42:57 +00:00
|
|
|
void NativeWindow::RestartHangMonitorTimeout() {
|
|
|
|
GetWebContents()->GetRenderViewHost()->RestartHangMonitorTimeout();
|
|
|
|
}
|
|
|
|
|
2013-05-01 08:12:00 +00:00
|
|
|
void NativeWindow::CloseWebContents() {
|
2013-05-01 08:33:19 +00:00
|
|
|
bool prevent_default = false;
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver,
|
|
|
|
observers_,
|
|
|
|
WillCloseWindow(&prevent_default));
|
2013-06-26 07:35:10 +00:00
|
|
|
if (prevent_default) {
|
|
|
|
WindowList::WindowCloseCancelled(this);
|
2013-05-01 08:33:19 +00:00
|
|
|
return;
|
2013-06-26 07:35:10 +00:00
|
|
|
}
|
2013-05-01 08:33:19 +00:00
|
|
|
|
2013-05-01 07:42:30 +00:00
|
|
|
content::WebContents* web_contents(GetWebContents());
|
|
|
|
|
|
|
|
if (web_contents->NeedToFireBeforeUnload())
|
|
|
|
web_contents->GetRenderViewHost()->FirePageBeforeUnload(false);
|
|
|
|
else
|
|
|
|
web_contents->Close();
|
|
|
|
}
|
|
|
|
|
2013-04-12 07:04:46 +00:00
|
|
|
content::WebContents* NativeWindow::GetWebContents() const {
|
|
|
|
return inspectable_web_contents_->GetWebContents();
|
|
|
|
}
|
|
|
|
|
2013-05-02 12:08:23 +00:00
|
|
|
void NativeWindow::NotifyWindowClosed() {
|
|
|
|
if (is_closed_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
is_closed_ = true;
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowClosed());
|
2013-05-07 08:20:52 +00:00
|
|
|
|
|
|
|
WindowList::RemoveWindow(this);
|
2013-05-02 12:08:23 +00:00
|
|
|
}
|
|
|
|
|
2013-05-24 09:51:15 +00:00
|
|
|
void NativeWindow::NotifyWindowBlur() {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowBlur());
|
|
|
|
}
|
|
|
|
|
2013-04-20 05:56:01 +00:00
|
|
|
// Window opened by window.open.
|
|
|
|
void NativeWindow::WebContentsCreated(
|
|
|
|
content::WebContents* source_contents,
|
|
|
|
int64 source_frame_id,
|
|
|
|
const string16& frame_name,
|
|
|
|
const GURL& target_url,
|
|
|
|
content::WebContents* new_contents) {
|
|
|
|
LOG(WARNING) << "Please use node-style Window API to create window, "
|
|
|
|
"using window.open has very strict constrains.";
|
|
|
|
|
|
|
|
scoped_ptr<base::DictionaryValue> options(new base::DictionaryValue);
|
|
|
|
options->SetInteger(switches::kWidth, 800);
|
|
|
|
options->SetInteger(switches::kHeight, 600);
|
|
|
|
|
|
|
|
NativeWindow* window = Create(new_contents, options.get());
|
|
|
|
window->InitFromOptions(options.get());
|
|
|
|
}
|
|
|
|
|
2013-04-30 15:56:50 +00:00
|
|
|
content::JavaScriptDialogManager* NativeWindow::GetJavaScriptDialogManager() {
|
|
|
|
if (!dialog_manager_)
|
|
|
|
dialog_manager_.reset(new AtomJavaScriptDialogManager);
|
|
|
|
|
|
|
|
return dialog_manager_.get();
|
|
|
|
}
|
|
|
|
|
2013-05-02 15:43:23 +00:00
|
|
|
void NativeWindow::BeforeUnloadFired(content::WebContents* tab,
|
|
|
|
bool proceed,
|
|
|
|
bool* proceed_to_fire_unload) {
|
|
|
|
*proceed_to_fire_unload = proceed;
|
|
|
|
|
|
|
|
if (!proceed)
|
|
|
|
WindowList::WindowCloseCancelled(this);
|
|
|
|
}
|
|
|
|
|
2013-05-15 11:11:30 +00:00
|
|
|
void NativeWindow::RequestToLockMouse(content::WebContents* web_contents,
|
|
|
|
bool user_gesture,
|
|
|
|
bool last_unlocked_by_target) {
|
|
|
|
GetWebContents()->GotResponseToLockMouseRequest(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NativeWindow::CanOverscrollContent() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::ActivateContents(content::WebContents* contents) {
|
2013-05-24 09:51:15 +00:00
|
|
|
FocusOnWebView();
|
2013-05-15 11:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::DeactivateContents(content::WebContents* contents) {
|
2013-05-24 09:51:15 +00:00
|
|
|
BlurWebView();
|
2013-05-15 11:11:30 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 03:17:15 +00:00
|
|
|
void NativeWindow::LoadingStateChanged(content::WebContents* source) {
|
|
|
|
bool is_loading = source->IsLoading();
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver,
|
|
|
|
observers_,
|
|
|
|
OnLoadingStateChanged(is_loading));
|
|
|
|
}
|
|
|
|
|
2013-05-15 05:56:25 +00:00
|
|
|
void NativeWindow::MoveContents(content::WebContents* source,
|
|
|
|
const gfx::Rect& pos) {
|
|
|
|
SetPosition(pos.origin());
|
|
|
|
SetSize(pos.size());
|
|
|
|
}
|
|
|
|
|
2013-05-01 07:42:30 +00:00
|
|
|
void NativeWindow::CloseContents(content::WebContents* source) {
|
|
|
|
// When the web contents is gone, close the window immediately, but the
|
2013-05-01 08:12:00 +00:00
|
|
|
// memory will not be freed until you call delete.
|
2013-05-01 08:33:19 +00:00
|
|
|
// In this way, it would be safe to manage windows via smart pointers. If you
|
|
|
|
// want to free memory when the window is closed, you can do deleting by
|
2013-05-15 05:47:15 +00:00
|
|
|
// overriding the OnWindowClosed method in the observer.
|
2013-05-01 08:12:00 +00:00
|
|
|
CloseImmediately();
|
2013-05-01 08:33:19 +00:00
|
|
|
|
2013-05-02 12:08:23 +00:00
|
|
|
NotifyWindowClosed();
|
2013-05-01 07:42:30 +00:00
|
|
|
}
|
|
|
|
|
2013-05-15 05:56:25 +00:00
|
|
|
bool NativeWindow::IsPopupOrPanel(const content::WebContents* source) const {
|
|
|
|
// Only popup window can use things like window.moveTo.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-06 11:45:48 +00:00
|
|
|
void NativeWindow::RendererUnresponsive(content::WebContents* source) {
|
2013-07-23 07:29:56 +00:00
|
|
|
not_responding_ = true;
|
|
|
|
base::MessageLoop::current()->PostDelayedTask(
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&NativeWindow::RendererUnresponsiveDelayed,
|
|
|
|
base::Unretained(this)),
|
|
|
|
base::TimeDelta::FromSeconds(1));
|
2013-06-06 11:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::RendererResponsive(content::WebContents* source) {
|
2013-07-23 07:29:56 +00:00
|
|
|
not_responding_ = false;
|
2013-06-06 11:45:48 +00:00
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnRendererResponsive());
|
|
|
|
}
|
|
|
|
|
2013-04-21 03:01:04 +00:00
|
|
|
bool NativeWindow::OnMessageReceived(const IPC::Message& message) {
|
2013-04-22 13:32:48 +00:00
|
|
|
bool handled = true;
|
|
|
|
IPC_BEGIN_MESSAGE_MAP(NativeWindow, message)
|
|
|
|
IPC_MESSAGE_HANDLER(AtomViewHostMsg_Message, OnRendererMessage)
|
2013-09-20 14:32:59 +00:00
|
|
|
IPC_MESSAGE_HANDLER_DELAY_REPLY(AtomViewHostMsg_Message_Sync,
|
|
|
|
OnRendererMessageSync)
|
2013-09-05 12:06:54 +00:00
|
|
|
IPC_MESSAGE_HANDLER(AtomViewHostMsg_UpdateDraggableRegions,
|
|
|
|
UpdateDraggableRegions)
|
2013-04-22 13:32:48 +00:00
|
|
|
IPC_MESSAGE_UNHANDLED(handled = false)
|
|
|
|
IPC_END_MESSAGE_MAP()
|
|
|
|
|
|
|
|
return handled;
|
2013-04-21 03:01:04 +00:00
|
|
|
}
|
|
|
|
|
2013-07-18 10:37:40 +00:00
|
|
|
void NativeWindow::RenderViewGone(base::TerminationStatus status) {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnRendererCrashed());
|
|
|
|
}
|
|
|
|
|
2013-07-23 07:29:56 +00:00
|
|
|
void NativeWindow::RendererUnresponsiveDelayed() {
|
|
|
|
if (not_responding_)
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver,
|
|
|
|
observers_,
|
|
|
|
OnRendererUnresponsive());
|
|
|
|
}
|
|
|
|
|
2013-04-18 12:50:58 +00:00
|
|
|
void NativeWindow::Observe(int type,
|
|
|
|
const content::NotificationSource& source,
|
|
|
|
const content::NotificationDetails& details) {
|
|
|
|
if (type == content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED) {
|
|
|
|
std::pair<NavigationEntry*, bool>* title =
|
|
|
|
content::Details<std::pair<NavigationEntry*, bool>>(details).ptr();
|
|
|
|
|
|
|
|
if (title->first) {
|
|
|
|
bool prevent_default = false;
|
|
|
|
std::string text = UTF16ToUTF8(title->first->GetTitle());
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver,
|
|
|
|
observers_,
|
|
|
|
OnPageTitleUpdated(&prevent_default, text));
|
|
|
|
|
|
|
|
if (!prevent_default)
|
|
|
|
SetTitle(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-23 04:18:07 +00:00
|
|
|
void NativeWindow::OnRendererMessage(const std::string& channel,
|
|
|
|
const base::ListValue& args) {
|
2013-04-22 13:32:48 +00:00
|
|
|
AtomBrowserMainParts::Get()->atom_bindings()->OnRendererMessage(
|
2013-04-23 04:18:07 +00:00
|
|
|
GetWebContents()->GetRenderProcessHost()->GetID(),
|
|
|
|
GetWebContents()->GetRoutingID(),
|
|
|
|
channel,
|
|
|
|
args);
|
2013-04-22 13:32:48 +00:00
|
|
|
}
|
|
|
|
|
2013-04-23 13:52:19 +00:00
|
|
|
void NativeWindow::OnRendererMessageSync(const std::string& channel,
|
|
|
|
const base::ListValue& args,
|
2013-09-20 14:32:59 +00:00
|
|
|
IPC::Message* reply_msg) {
|
|
|
|
std::string json;
|
2013-04-23 13:52:19 +00:00
|
|
|
AtomBrowserMainParts::Get()->atom_bindings()->OnRendererMessageSync(
|
|
|
|
GetWebContents()->GetRenderProcessHost()->GetID(),
|
|
|
|
GetWebContents()->GetRoutingID(),
|
|
|
|
channel,
|
|
|
|
args,
|
2013-09-20 14:32:59 +00:00
|
|
|
&json);
|
|
|
|
|
|
|
|
AtomViewHostMsg_Message_Sync::WriteReplyParams(reply_msg, json);
|
|
|
|
Send(reply_msg);
|
2013-04-23 13:52:19 +00:00
|
|
|
}
|
|
|
|
|
2013-04-12 07:04:46 +00:00
|
|
|
} // namespace atom
|