2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-12 07:04:46 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/native_window.h"
|
2013-04-12 07:04:46 +00:00
|
|
|
|
|
|
|
#include <string>
|
2014-03-16 01:13:06 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2013-04-12 07:04:46 +00:00
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
2015-06-18 09:01:23 +00:00
|
|
|
#include "atom/browser/atom_browser_main_parts.h"
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/window_list.h"
|
2014-04-04 14:04:42 +00:00
|
|
|
#include "atom/common/api/api_messages.h"
|
2014-06-23 14:26:01 +00:00
|
|
|
#include "atom/common/native_mate_converters/image_converter.h"
|
2014-06-23 14:08:40 +00:00
|
|
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
2014-04-04 14:04:42 +00:00
|
|
|
#include "atom/common/options_switches.h"
|
2015-01-10 01:45:50 +00:00
|
|
|
#include "base/files/file_util.h"
|
2014-04-04 16:10:57 +00:00
|
|
|
#include "base/json/json_writer.h"
|
2014-04-04 14:04:42 +00:00
|
|
|
#include "base/prefs/pref_service.h"
|
|
|
|
#include "base/message_loop/message_loop.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2014-08-11 02:01:05 +00:00
|
|
|
#include "brightray/browser/inspectable_web_contents.h"
|
|
|
|
#include "brightray/browser/inspectable_web_contents_view.h"
|
2014-12-23 19:17:32 +00:00
|
|
|
#include "content/browser/renderer_host/render_widget_host_impl.h"
|
2013-04-18 12:50:58 +00:00
|
|
|
#include "content/public/browser/navigation_entry.h"
|
2014-05-22 15:48:00 +00:00
|
|
|
#include "content/public/browser/plugin_service.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"
|
2014-09-18 13:49:04 +00:00
|
|
|
#include "content/public/common/content_switches.h"
|
2013-04-22 13:32:48 +00:00
|
|
|
#include "ipc/ipc_message_macros.h"
|
2014-06-23 13:51:42 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2013-11-21 13:50:06 +00:00
|
|
|
#include "ui/gfx/codec/png_codec.h"
|
2015-03-10 22:35:53 +00:00
|
|
|
#include "ui/gfx/geometry/point.h"
|
|
|
|
#include "ui/gfx/geometry/rect.h"
|
|
|
|
#include "ui/gfx/geometry/size.h"
|
2016-01-04 12:06:36 +00:00
|
|
|
#include "ui/gfx/geometry/size_conversions.h"
|
2015-04-02 15:02:22 +00:00
|
|
|
#include "ui/gfx/screen.h"
|
|
|
|
#include "ui/gl/gpu_switching_manager.h"
|
2013-04-12 07:04:46 +00:00
|
|
|
|
2015-06-24 14:14:46 +00:00
|
|
|
DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::NativeWindowRelay);
|
|
|
|
|
2013-04-12 07:04:46 +00:00
|
|
|
namespace atom {
|
|
|
|
|
2015-06-25 01:47:57 +00:00
|
|
|
NativeWindow::NativeWindow(
|
|
|
|
brightray::InspectableWebContents* inspectable_web_contents,
|
|
|
|
const mate::Dictionary& options)
|
|
|
|
: content::WebContentsObserver(inspectable_web_contents->GetWebContents()),
|
2013-09-05 13:43:47 +00:00
|
|
|
has_frame_(true),
|
2015-09-25 08:38:36 +00:00
|
|
|
force_using_draggable_region_(false),
|
2014-12-23 19:17:32 +00:00
|
|
|
transparent_(false),
|
2014-08-17 04:23:00 +00:00
|
|
|
enable_larger_than_screen_(false),
|
2013-05-02 12:08:23 +00:00
|
|
|
is_closed_(false),
|
2014-02-10 12:07:38 +00:00
|
|
|
has_dialog_attached_(false),
|
2015-07-23 02:07:58 +00:00
|
|
|
aspect_ratio_(0.0),
|
2015-06-25 03:07:23 +00:00
|
|
|
inspectable_web_contents_(inspectable_web_contents),
|
2015-06-05 06:55:07 +00:00
|
|
|
weak_factory_(this) {
|
2015-11-13 05:58:31 +00:00
|
|
|
options.Get(options::kFrame, &has_frame_);
|
|
|
|
options.Get(options::kTransparent, &transparent_);
|
|
|
|
options.Get(options::kEnableLargerThanScreen, &enable_larger_than_screen_);
|
2013-09-05 13:43:47 +00:00
|
|
|
|
2014-12-23 19:17:32 +00:00
|
|
|
// Tell the content module to initialize renderer widget with transparent
|
|
|
|
// mode.
|
2015-04-02 15:02:22 +00:00
|
|
|
ui::GpuSwitchingManager::SetTransparent(transparent_);
|
2014-12-23 19:17:32 +00:00
|
|
|
|
2014-01-30 13:57:01 +00:00
|
|
|
// Read icon before window is created.
|
2015-11-13 05:58:31 +00:00
|
|
|
options.Get(options::kIcon, &icon_);
|
2014-01-30 13:57:01 +00:00
|
|
|
|
2013-05-02 14:54:09 +00:00
|
|
|
WindowList::AddWindow(this);
|
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-30 12:32:23 +00:00
|
|
|
// static
|
2015-05-20 02:27:16 +00:00
|
|
|
NativeWindow* NativeWindow::FromWebContents(
|
|
|
|
content::WebContents* web_contents) {
|
2013-05-02 14:54:09 +00:00
|
|
|
WindowList& window_list = *WindowList::GetInstance();
|
2015-05-20 02:27:16 +00:00
|
|
|
for (NativeWindow* window : window_list) {
|
2015-09-18 06:12:48 +00:00
|
|
|
if (window->web_contents() == web_contents)
|
2013-04-30 12:32:23 +00:00
|
|
|
return window;
|
|
|
|
}
|
2014-12-17 22:40:19 +00:00
|
|
|
return nullptr;
|
2013-04-30 12:32:23 +00:00
|
|
|
}
|
|
|
|
|
2014-06-23 13:51:42 +00:00
|
|
|
void NativeWindow::InitFromOptions(const mate::Dictionary& options) {
|
2013-04-12 07:04:46 +00:00
|
|
|
// 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;
|
2015-11-13 05:58:31 +00:00
|
|
|
if (options.Get(options::kX, &x) && options.Get(options::kY, &y)) {
|
2016-01-15 16:31:31 +00:00
|
|
|
SetPosition(gfx::Point(x, y));
|
2015-11-13 05:58:31 +00:00
|
|
|
} else if (options.Get(options::kCenter, ¢er) && center) {
|
2013-05-10 12:34:05 +00:00
|
|
|
Center();
|
2013-04-12 07:04:46 +00:00
|
|
|
}
|
2015-10-15 08:35:50 +00:00
|
|
|
// On Linux and Window we may already have maximum size defined.
|
|
|
|
extensions::SizeConstraints size_constraints(GetContentSizeConstraints());
|
2014-08-19 13:43:18 +00:00
|
|
|
int min_height = 0, min_width = 0;
|
2015-11-13 05:58:31 +00:00
|
|
|
if (options.Get(options::kMinHeight, &min_height) |
|
|
|
|
options.Get(options::kMinWidth, &min_width)) {
|
2015-10-05 12:36:28 +00:00
|
|
|
size_constraints.set_minimum_size(gfx::Size(min_width, min_height));
|
2013-04-12 07:04:46 +00:00
|
|
|
}
|
2015-03-26 04:23:38 +00:00
|
|
|
int max_height = INT_MAX, max_width = INT_MAX;
|
2015-11-13 05:58:31 +00:00
|
|
|
if (options.Get(options::kMaxHeight, &max_height) |
|
|
|
|
options.Get(options::kMaxWidth, &max_width)) {
|
2015-10-05 12:36:28 +00:00
|
|
|
size_constraints.set_maximum_size(gfx::Size(max_width, max_height));
|
|
|
|
}
|
|
|
|
bool use_content_size = false;
|
2015-11-13 05:58:31 +00:00
|
|
|
options.Get(options::kUseContentSize, &use_content_size);
|
2015-10-05 12:36:28 +00:00
|
|
|
if (use_content_size) {
|
|
|
|
SetContentSizeConstraints(size_constraints);
|
|
|
|
} else {
|
|
|
|
SetSizeConstraints(size_constraints);
|
2013-04-12 07:04:46 +00:00
|
|
|
}
|
2015-10-06 07:23:23 +00:00
|
|
|
#if defined(OS_WIN) || defined(USE_X11)
|
2013-04-12 07:04:46 +00:00
|
|
|
bool resizable;
|
2015-11-13 05:58:31 +00:00
|
|
|
if (options.Get(options::kResizable, &resizable)) {
|
2013-04-12 07:04:46 +00:00
|
|
|
SetResizable(resizable);
|
|
|
|
}
|
2015-10-06 07:23:23 +00:00
|
|
|
#endif
|
2013-04-12 07:04:46 +00:00
|
|
|
bool top;
|
2015-11-13 05:58:31 +00:00
|
|
|
if (options.Get(options::kAlwaysOnTop, &top) && top) {
|
2013-04-12 07:04:46 +00:00
|
|
|
SetAlwaysOnTop(true);
|
|
|
|
}
|
2015-06-29 13:53:33 +00:00
|
|
|
#if defined(OS_MACOSX) || defined(OS_WIN)
|
2013-04-12 07:04:46 +00:00
|
|
|
bool fullscreen;
|
2015-11-13 05:58:31 +00:00
|
|
|
if (options.Get(options::kFullscreen, &fullscreen) && fullscreen) {
|
2014-11-25 06:34:14 +00:00
|
|
|
SetFullScreen(true);
|
2013-04-12 07:04:46 +00:00
|
|
|
}
|
2015-06-29 13:53:33 +00:00
|
|
|
#endif
|
2014-06-16 02:29:51 +00:00
|
|
|
bool skip;
|
2015-11-13 05:58:31 +00:00
|
|
|
if (options.Get(options::kSkipTaskbar, &skip) && skip) {
|
2014-06-16 02:29:51 +00:00
|
|
|
SetSkipTaskbar(skip);
|
|
|
|
}
|
2013-04-12 07:04:46 +00:00
|
|
|
bool kiosk;
|
2015-11-13 05:58:31 +00:00
|
|
|
if (options.Get(options::kKiosk, &kiosk) && kiosk) {
|
2013-04-12 07:04:46 +00:00
|
|
|
SetKiosk(kiosk);
|
|
|
|
}
|
2015-10-23 03:35:33 +00:00
|
|
|
std::string color;
|
2015-11-13 05:58:31 +00:00
|
|
|
if (options.Get(options::kBackgroundColor, &color)) {
|
2015-10-23 03:35:33 +00:00
|
|
|
SetBackgroundColor(color);
|
|
|
|
}
|
2015-04-08 17:16:10 +00:00
|
|
|
std::string title("Electron");
|
2015-11-13 05:58:31 +00:00
|
|
|
options.Get(options::kTitle, &title);
|
2013-04-12 07:04:46 +00:00
|
|
|
SetTitle(title);
|
|
|
|
|
|
|
|
// Then show it.
|
|
|
|
bool show = true;
|
2015-11-13 05:58:31 +00:00
|
|
|
options.Get(options::kShow, &show);
|
2015-10-01 09:46:11 +00:00
|
|
|
if (show)
|
2013-04-12 07:04:46 +00:00
|
|
|
Show();
|
|
|
|
}
|
|
|
|
|
2016-01-15 04:54:12 +00:00
|
|
|
void NativeWindow::SetSize(const gfx::Size& size, bool animate) {
|
|
|
|
SetBounds(gfx::Rect(GetPosition(), size), animate);
|
2015-05-04 04:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Size NativeWindow::GetSize() {
|
|
|
|
return GetBounds().size();
|
|
|
|
}
|
|
|
|
|
2016-01-15 04:54:12 +00:00
|
|
|
void NativeWindow::SetPosition(const gfx::Point& position, bool animate) {
|
|
|
|
SetBounds(gfx::Rect(position, GetSize()), animate);
|
2015-05-04 04:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Point NativeWindow::GetPosition() {
|
|
|
|
return GetBounds().origin();
|
|
|
|
}
|
|
|
|
|
2016-01-15 04:54:12 +00:00
|
|
|
void NativeWindow::SetContentSize(const gfx::Size& size, bool animate) {
|
|
|
|
SetSize(ContentSizeToWindowSize(size), animate);
|
2015-10-05 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Size NativeWindow::GetContentSize() {
|
|
|
|
return WindowSizeToContentSize(GetSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::SetSizeConstraints(
|
|
|
|
const extensions::SizeConstraints& window_constraints) {
|
|
|
|
extensions::SizeConstraints content_constraints;
|
|
|
|
if (window_constraints.HasMaximumSize())
|
|
|
|
content_constraints.set_maximum_size(
|
|
|
|
WindowSizeToContentSize(window_constraints.GetMaximumSize()));
|
|
|
|
if (window_constraints.HasMinimumSize())
|
|
|
|
content_constraints.set_minimum_size(
|
|
|
|
WindowSizeToContentSize(window_constraints.GetMinimumSize()));
|
|
|
|
SetContentSizeConstraints(content_constraints);
|
|
|
|
}
|
|
|
|
|
|
|
|
extensions::SizeConstraints NativeWindow::GetSizeConstraints() {
|
|
|
|
extensions::SizeConstraints content_constraints = GetContentSizeConstraints();
|
|
|
|
extensions::SizeConstraints window_constraints;
|
|
|
|
if (content_constraints.HasMaximumSize())
|
|
|
|
window_constraints.set_maximum_size(
|
|
|
|
ContentSizeToWindowSize(content_constraints.GetMaximumSize()));
|
|
|
|
if (content_constraints.HasMinimumSize())
|
|
|
|
window_constraints.set_minimum_size(
|
|
|
|
ContentSizeToWindowSize(content_constraints.GetMinimumSize()));
|
|
|
|
return window_constraints;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::SetContentSizeConstraints(
|
|
|
|
const extensions::SizeConstraints& size_constraints) {
|
|
|
|
size_constraints_ = size_constraints;
|
|
|
|
}
|
|
|
|
|
|
|
|
extensions::SizeConstraints NativeWindow::GetContentSizeConstraints() {
|
|
|
|
return size_constraints_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::SetMinimumSize(const gfx::Size& size) {
|
|
|
|
extensions::SizeConstraints size_constraints;
|
|
|
|
size_constraints.set_minimum_size(size);
|
|
|
|
SetSizeConstraints(size_constraints);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Size NativeWindow::GetMinimumSize() {
|
|
|
|
return GetSizeConstraints().GetMinimumSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::SetMaximumSize(const gfx::Size& size) {
|
|
|
|
extensions::SizeConstraints size_constraints;
|
|
|
|
size_constraints.set_maximum_size(size);
|
|
|
|
SetSizeConstraints(size_constraints);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Size NativeWindow::GetMaximumSize() {
|
|
|
|
return GetSizeConstraints().GetMaximumSize();
|
|
|
|
}
|
|
|
|
|
2014-05-27 06:15:34 +00:00
|
|
|
void NativeWindow::SetRepresentedFilename(const std::string& filename) {
|
|
|
|
}
|
|
|
|
|
2014-07-18 13:42:26 +00:00
|
|
|
std::string NativeWindow::GetRepresentedFilename() {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-05-27 06:15:34 +00:00
|
|
|
void NativeWindow::SetDocumentEdited(bool edited) {
|
|
|
|
}
|
|
|
|
|
2014-08-21 13:00:49 +00:00
|
|
|
bool NativeWindow::IsDocumentEdited() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-06 02:14:54 +00:00
|
|
|
void NativeWindow::SetIgnoreMouseEvents(bool ignore) {
|
|
|
|
}
|
|
|
|
|
2014-07-04 08:54:10 +00:00
|
|
|
void NativeWindow::SetMenu(ui::MenuModel* menu) {
|
|
|
|
}
|
|
|
|
|
2014-02-10 12:07:38 +00:00
|
|
|
bool NativeWindow::HasModalDialog() {
|
|
|
|
return has_dialog_attached_;
|
|
|
|
}
|
|
|
|
|
2013-05-24 09:51:15 +00:00
|
|
|
void NativeWindow::FocusOnWebView() {
|
2015-06-25 06:54:00 +00:00
|
|
|
web_contents()->GetRenderViewHost()->Focus();
|
2013-05-24 09:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::BlurWebView() {
|
2015-06-25 06:54:00 +00:00
|
|
|
web_contents()->GetRenderViewHost()->Blur();
|
2013-05-24 09:51:15 +00:00
|
|
|
}
|
|
|
|
|
2013-08-16 04:56:25 +00:00
|
|
|
bool NativeWindow::IsWebViewFocused() {
|
2015-06-25 06:54:00 +00:00
|
|
|
auto host_view = web_contents()->GetRenderViewHost()->GetView();
|
2014-03-10 01:24:51 +00:00
|
|
|
return host_view && host_view->HasFocus();
|
2013-08-16 04:56:25 +00:00
|
|
|
}
|
|
|
|
|
2013-11-21 13:50:06 +00:00
|
|
|
void NativeWindow::CapturePage(const gfx::Rect& rect,
|
|
|
|
const CapturePageCallback& callback) {
|
2015-06-25 06:54:00 +00:00
|
|
|
const auto view = web_contents()->GetRenderWidgetHostView();
|
|
|
|
const auto host = view ? view->GetRenderWidgetHost() : nullptr;
|
2014-11-24 05:44:45 +00:00
|
|
|
if (!view || !host) {
|
2015-03-20 02:26:22 +00:00
|
|
|
callback.Run(SkBitmap());
|
2014-04-07 07:43:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-24 05:44:45 +00:00
|
|
|
// Capture full page if user doesn't specify a |rect|.
|
2016-01-04 11:37:25 +00:00
|
|
|
const gfx::Size view_size = rect.IsEmpty() ? view->GetViewBounds().size() :
|
|
|
|
rect.size();
|
2014-11-24 05:44:45 +00:00
|
|
|
|
|
|
|
// By default, the requested bitmap size is the view size in screen
|
|
|
|
// coordinates. However, if there's more pixel detail available on the
|
|
|
|
// current system, increase the requested bitmap size to capture it all.
|
2016-01-04 11:37:25 +00:00
|
|
|
gfx::Size bitmap_size = view_size;
|
2014-11-24 05:44:45 +00:00
|
|
|
const gfx::NativeView native_view = view->GetNativeView();
|
|
|
|
gfx::Screen* const screen = gfx::Screen::GetScreenFor(native_view);
|
|
|
|
const float scale =
|
|
|
|
screen->GetDisplayNearestWindow(native_view).device_scale_factor();
|
|
|
|
if (scale > 1.0f)
|
2016-01-04 11:37:25 +00:00
|
|
|
bitmap_size = gfx::ScaleToCeiledSize(view_size, scale);
|
2014-11-24 05:44:45 +00:00
|
|
|
|
|
|
|
host->CopyFromBackingStore(
|
2016-01-04 12:06:36 +00:00
|
|
|
gfx::Rect(rect.origin(), view_size),
|
2014-11-24 05:44:45 +00:00
|
|
|
bitmap_size,
|
2013-11-21 13:50:06 +00:00
|
|
|
base::Bind(&NativeWindow::OnCapturePageDone,
|
2013-11-29 06:52:12 +00:00
|
|
|
weak_factory_.GetWeakPtr(),
|
2014-06-28 14:33:00 +00:00
|
|
|
callback),
|
2014-11-24 05:44:45 +00:00
|
|
|
kBGRA_8888_SkColorType);
|
2013-11-21 13:50:06 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 05:49:33 +00:00
|
|
|
void NativeWindow::ShowDefinitionForSelection() {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::SetAutoHideMenuBar(bool auto_hide) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NativeWindow::IsMenuBarAutoHide() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::SetMenuBarVisibility(bool visible) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NativeWindow::IsMenuBarVisible() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
double NativeWindow::GetAspectRatio() {
|
|
|
|
return aspect_ratio_;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Size NativeWindow::GetAspectRatioExtraSize() {
|
|
|
|
return aspect_ratio_extraSize_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::SetAspectRatio(double aspect_ratio,
|
|
|
|
const gfx::Size& extra_size) {
|
|
|
|
aspect_ratio_ = aspect_ratio;
|
|
|
|
aspect_ratio_extraSize_ = extra_size;
|
|
|
|
}
|
|
|
|
|
2015-06-25 05:27:51 +00:00
|
|
|
void NativeWindow::RequestToClosePage() {
|
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-11-29 06:52:12 +00:00
|
|
|
// Assume the window is not responding if it doesn't cancel the close and is
|
2014-05-09 04:10:37 +00:00
|
|
|
// not closed in 5s, in this way we can quickly show the unresponsive
|
2013-11-29 06:52:12 +00:00
|
|
|
// dialog when the window is busy executing some script withouth waiting for
|
|
|
|
// the unresponsive timeout.
|
2014-04-08 06:22:22 +00:00
|
|
|
if (window_unresposive_closure_.IsCancelled())
|
2014-05-09 04:10:37 +00:00
|
|
|
ScheduleUnresponsiveEvent(5000);
|
2013-11-29 06:52:12 +00:00
|
|
|
|
2015-06-25 06:54:00 +00:00
|
|
|
if (web_contents()->NeedToFireBeforeUnload())
|
|
|
|
web_contents()->DispatchBeforeUnload(false);
|
2013-05-01 07:42:30 +00:00
|
|
|
else
|
2015-06-25 06:54:00 +00:00
|
|
|
web_contents()->Close();
|
2013-05-01 07:42:30 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 05:29:30 +00:00
|
|
|
void NativeWindow::CloseContents(content::WebContents* source) {
|
|
|
|
if (!inspectable_web_contents_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
inspectable_web_contents_->GetView()->SetDelegate(nullptr);
|
|
|
|
inspectable_web_contents_ = nullptr;
|
2015-06-25 06:54:00 +00:00
|
|
|
Observe(nullptr);
|
2015-06-25 05:29:30 +00:00
|
|
|
|
|
|
|
// When the web contents is gone, close the window immediately, but the
|
|
|
|
// memory will not be freed until you call delete.
|
|
|
|
// 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
|
|
|
|
// overriding the OnWindowClosed method in the observer.
|
|
|
|
CloseImmediately();
|
|
|
|
|
|
|
|
// Do not sent "unresponsive" event after window is closed.
|
|
|
|
window_unresposive_closure_.Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::RendererUnresponsive(content::WebContents* source) {
|
|
|
|
// Schedule the unresponsive shortly later, since we may receive the
|
|
|
|
// responsive event soon. This could happen after the whole application had
|
|
|
|
// blocked for a while.
|
|
|
|
// Also notice that when closing this event would be ignored because we have
|
|
|
|
// explicity started a close timeout counter. This is on purpose because we
|
|
|
|
// don't want the unresponsive event to be sent too early when user is closing
|
|
|
|
// the window.
|
|
|
|
ScheduleUnresponsiveEvent(50);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::RendererResponsive(content::WebContents* source) {
|
|
|
|
window_unresposive_closure_.Cancel();
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnRendererResponsive());
|
|
|
|
}
|
|
|
|
|
2013-05-02 12:08:23 +00:00
|
|
|
void NativeWindow::NotifyWindowClosed() {
|
|
|
|
if (is_closed_)
|
|
|
|
return;
|
|
|
|
|
2015-06-27 09:01:20 +00:00
|
|
|
WindowList::RemoveWindow(this);
|
|
|
|
|
2013-05-02 12:08:23 +00:00
|
|
|
is_closed_ = true;
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowClosed());
|
|
|
|
}
|
|
|
|
|
2013-05-24 09:51:15 +00:00
|
|
|
void NativeWindow::NotifyWindowBlur() {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowBlur());
|
|
|
|
}
|
|
|
|
|
2014-05-21 17:46:13 +00:00
|
|
|
void NativeWindow::NotifyWindowFocus() {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowFocus());
|
|
|
|
}
|
|
|
|
|
2014-11-25 04:43:25 +00:00
|
|
|
void NativeWindow::NotifyWindowMaximize() {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowMaximize());
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::NotifyWindowUnmaximize() {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowUnmaximize());
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::NotifyWindowMinimize() {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowMinimize());
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::NotifyWindowRestore() {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowRestore());
|
|
|
|
}
|
|
|
|
|
2015-05-09 15:55:10 +00:00
|
|
|
void NativeWindow::NotifyWindowResize() {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowResize());
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::NotifyWindowMove() {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowMove());
|
|
|
|
}
|
|
|
|
|
2015-05-20 08:37:13 +00:00
|
|
|
void NativeWindow::NotifyWindowMoved() {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowMoved());
|
|
|
|
}
|
|
|
|
|
2014-11-25 04:43:25 +00:00
|
|
|
void NativeWindow::NotifyWindowEnterFullScreen() {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
|
|
|
|
OnWindowEnterFullScreen());
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::NotifyWindowLeaveFullScreen() {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
|
|
|
|
OnWindowLeaveFullScreen());
|
|
|
|
}
|
|
|
|
|
2015-05-21 05:09:31 +00:00
|
|
|
void NativeWindow::NotifyWindowEnterHtmlFullScreen() {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
|
|
|
|
OnWindowEnterHtmlFullScreen());
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::NotifyWindowLeaveHtmlFullScreen() {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
|
|
|
|
OnWindowLeaveHtmlFullScreen());
|
|
|
|
}
|
|
|
|
|
2015-08-05 04:46:32 +00:00
|
|
|
void NativeWindow::NotifyWindowExecuteWindowsCommand(
|
|
|
|
const std::string& command) {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
|
|
|
|
OnExecuteWindowsCommand(command));
|
|
|
|
}
|
|
|
|
|
2015-10-27 01:12:01 +00:00
|
|
|
#if defined(OS_WIN)
|
2015-10-29 02:53:48 +00:00
|
|
|
void NativeWindow::NotifyWindowMessage(
|
|
|
|
UINT message, WPARAM w_param, LPARAM l_param) {
|
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
|
|
|
|
OnWindowMessage(message, w_param, l_param));
|
2015-10-27 01:12:01 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-20 23:33:43 +00:00
|
|
|
scoped_ptr<SkRegion> NativeWindow::DraggableRegionsToSkRegion(
|
|
|
|
const std::vector<DraggableRegion>& regions) {
|
|
|
|
scoped_ptr<SkRegion> sk_region(new SkRegion);
|
|
|
|
for (const DraggableRegion& region : regions) {
|
|
|
|
sk_region->op(
|
|
|
|
region.bounds.x(),
|
|
|
|
region.bounds.y(),
|
|
|
|
region.bounds.right(),
|
|
|
|
region.bounds.bottom(),
|
|
|
|
region.draggable ? SkRegion::kUnion_Op : SkRegion::kDifference_Op);
|
|
|
|
}
|
|
|
|
return sk_region.Pass();
|
|
|
|
}
|
|
|
|
|
2015-06-25 05:29:30 +00:00
|
|
|
void NativeWindow::RenderViewCreated(
|
|
|
|
content::RenderViewHost* render_view_host) {
|
|
|
|
if (!transparent_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
content::RenderWidgetHostImpl* impl = content::RenderWidgetHostImpl::FromID(
|
|
|
|
render_view_host->GetProcess()->GetID(),
|
|
|
|
render_view_host->GetRoutingID());
|
|
|
|
if (impl)
|
|
|
|
impl->SetBackgroundOpaque(false);
|
|
|
|
}
|
|
|
|
|
2015-06-24 13:28:36 +00:00
|
|
|
void NativeWindow::BeforeUnloadDialogCancelled() {
|
|
|
|
WindowList::WindowCloseCancelled(this);
|
|
|
|
|
|
|
|
// Cancel unresponsive event when window close is cancelled.
|
|
|
|
window_unresposive_closure_.Cancel();
|
|
|
|
}
|
|
|
|
|
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)
|
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
|
|
|
}
|
|
|
|
|
2015-08-05 04:32:22 +00:00
|
|
|
void NativeWindow::UpdateDraggableRegions(
|
|
|
|
const std::vector<DraggableRegion>& regions) {
|
|
|
|
// Draggable region is not supported for non-frameless window.
|
2015-09-25 08:38:36 +00:00
|
|
|
if (has_frame_ && !force_using_draggable_region_)
|
2015-08-05 04:32:22 +00:00
|
|
|
return;
|
2015-08-05 04:34:45 +00:00
|
|
|
draggable_region_ = DraggableRegionsToSkRegion(regions);
|
2015-08-05 04:32:22 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 10:10:51 +00:00
|
|
|
void NativeWindow::ScheduleUnresponsiveEvent(int ms) {
|
2014-05-09 03:26:13 +00:00
|
|
|
if (!window_unresposive_closure_.IsCancelled())
|
|
|
|
return;
|
|
|
|
|
2014-03-25 10:10:51 +00:00
|
|
|
window_unresposive_closure_.Reset(
|
|
|
|
base::Bind(&NativeWindow::NotifyWindowUnresponsive,
|
|
|
|
weak_factory_.GetWeakPtr()));
|
|
|
|
base::MessageLoop::current()->PostDelayedTask(
|
|
|
|
FROM_HERE,
|
|
|
|
window_unresposive_closure_.callback(),
|
|
|
|
base::TimeDelta::FromMilliseconds(ms));
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindow::NotifyWindowUnresponsive() {
|
|
|
|
window_unresposive_closure_.Cancel();
|
|
|
|
|
2014-05-09 04:08:15 +00:00
|
|
|
if (!is_closed_ && !HasModalDialog())
|
2014-03-25 10:10:51 +00:00
|
|
|
FOR_EACH_OBSERVER(NativeWindowObserver,
|
|
|
|
observers_,
|
|
|
|
OnRendererUnresponsive());
|
|
|
|
}
|
|
|
|
|
2013-11-21 14:03:29 +00:00
|
|
|
void NativeWindow::OnCapturePageDone(const CapturePageCallback& callback,
|
2015-03-10 23:39:20 +00:00
|
|
|
const SkBitmap& bitmap,
|
|
|
|
content::ReadbackResponse response) {
|
2015-03-20 02:26:22 +00:00
|
|
|
callback.Run(bitmap);
|
2013-11-21 13:50:06 +00:00
|
|
|
}
|
|
|
|
|
2016-01-19 23:24:16 +00:00
|
|
|
SkColor NativeWindow::ParseHexColor(const std::string& name) {
|
|
|
|
SkColor result = 0xFF000000;
|
|
|
|
unsigned value = 0;
|
|
|
|
auto color = name.substr(1);
|
|
|
|
unsigned length = color.size();
|
|
|
|
if (length != 3 && length != 6)
|
|
|
|
return result;
|
|
|
|
for (unsigned i = 0; i < length; ++i) {
|
|
|
|
if (!base::IsHexDigit(color[i]))
|
|
|
|
return result;
|
|
|
|
value <<= 4;
|
|
|
|
value |= (color[i] < 'A' ? color[i] - '0' : (color[i] - 'A' + 10) & 0xF);
|
|
|
|
}
|
|
|
|
if (length == 6) {
|
|
|
|
result |= value;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
result |= (value & 0xF00) << 12 | (value & 0xF00) << 8
|
|
|
|
| (value & 0xF0) << 8 | (value & 0xF0) << 4
|
|
|
|
| (value & 0xF) << 4 | (value & 0xF);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-04-12 07:04:46 +00:00
|
|
|
} // namespace atom
|