native_window.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/native_window.h"
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. #include "atom/browser/atom_browser_context.h"
  9. #include "atom/browser/atom_browser_main_parts.h"
  10. #include "atom/browser/browser.h"
  11. #include "atom/browser/unresponsive_suppressor.h"
  12. #include "atom/browser/window_list.h"
  13. #include "atom/common/api/api_messages.h"
  14. #include "atom/common/native_mate_converters/file_path_converter.h"
  15. #include "atom/common/options_switches.h"
  16. #include "base/files/file_util.h"
  17. #include "base/json/json_writer.h"
  18. #include "base/message_loop/message_loop.h"
  19. #include "base/strings/utf_string_conversions.h"
  20. #include "brightray/browser/inspectable_web_contents.h"
  21. #include "brightray/browser/inspectable_web_contents_view.h"
  22. #include "components/prefs/pref_service.h"
  23. #include "content/browser/renderer_host/render_widget_host_impl.h"
  24. #include "content/public/browser/navigation_entry.h"
  25. #include "content/public/browser/plugin_service.h"
  26. #include "content/public/browser/render_process_host.h"
  27. #include "content/public/browser/render_view_host.h"
  28. #include "content/public/browser/render_widget_host.h"
  29. #include "content/public/browser/render_widget_host_view.h"
  30. #include "content/public/common/content_switches.h"
  31. #include "ipc/ipc_message_macros.h"
  32. #include "native_mate/dictionary.h"
  33. #include "third_party/skia/include/core/SkRegion.h"
  34. #include "ui/gfx/codec/png_codec.h"
  35. #include "ui/gfx/geometry/point.h"
  36. #include "ui/gfx/geometry/rect.h"
  37. #include "ui/gfx/geometry/size.h"
  38. #include "ui/gfx/geometry/size_conversions.h"
  39. #include "ui/gl/gpu_switching_manager.h"
  40. #if defined(OS_LINUX) || defined(OS_WIN)
  41. #include "content/public/common/renderer_preferences.h"
  42. #include "ui/gfx/font_render_params.h"
  43. #endif
  44. DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::NativeWindowRelay);
  45. namespace atom {
  46. NativeWindow::NativeWindow(
  47. brightray::InspectableWebContents* inspectable_web_contents,
  48. const mate::Dictionary& options,
  49. NativeWindow* parent)
  50. : content::WebContentsObserver(inspectable_web_contents->GetWebContents()),
  51. has_frame_(true),
  52. transparent_(false),
  53. enable_larger_than_screen_(false),
  54. is_closed_(false),
  55. sheet_offset_x_(0.0),
  56. sheet_offset_y_(0.0),
  57. aspect_ratio_(0.0),
  58. parent_(parent),
  59. is_modal_(false),
  60. inspectable_web_contents_(inspectable_web_contents),
  61. weak_factory_(this) {
  62. options.Get(options::kFrame, &has_frame_);
  63. options.Get(options::kTransparent, &transparent_);
  64. options.Get(options::kEnableLargerThanScreen, &enable_larger_than_screen_);
  65. if (parent)
  66. options.Get("modal", &is_modal_);
  67. #if defined(OS_LINUX) || defined(OS_WIN)
  68. auto* prefs = web_contents()->GetMutableRendererPrefs();
  69. // Update font settings.
  70. CR_DEFINE_STATIC_LOCAL(const gfx::FontRenderParams, params,
  71. (gfx::GetFontRenderParams(gfx::FontRenderParamsQuery(), nullptr)));
  72. prefs->should_antialias_text = params.antialiasing;
  73. prefs->use_subpixel_positioning = params.subpixel_positioning;
  74. prefs->hinting = params.hinting;
  75. prefs->use_autohinter = params.autohinter;
  76. prefs->use_bitmaps = params.use_bitmaps;
  77. prefs->subpixel_rendering = params.subpixel_rendering;
  78. #endif
  79. // Tell the content module to initialize renderer widget with transparent
  80. // mode.
  81. ui::GpuSwitchingManager::SetTransparent(transparent_);
  82. WindowList::AddWindow(this);
  83. }
  84. NativeWindow::~NativeWindow() {
  85. // It's possible that the windows gets destroyed before it's closed, in that
  86. // case we need to ensure the OnWindowClosed message is still notified.
  87. NotifyWindowClosed();
  88. }
  89. // static
  90. NativeWindow* NativeWindow::FromWebContents(
  91. content::WebContents* web_contents) {
  92. WindowList& window_list = *WindowList::GetInstance();
  93. for (NativeWindow* window : window_list) {
  94. if (window->web_contents() == web_contents)
  95. return window;
  96. }
  97. return nullptr;
  98. }
  99. void NativeWindow::InitFromOptions(const mate::Dictionary& options) {
  100. // Setup window from options.
  101. int x = -1, y = -1;
  102. bool center;
  103. if (options.Get(options::kX, &x) && options.Get(options::kY, &y)) {
  104. SetPosition(gfx::Point(x, y));
  105. } else if (options.Get(options::kCenter, &center) && center) {
  106. Center();
  107. }
  108. // On Linux and Window we may already have maximum size defined.
  109. extensions::SizeConstraints size_constraints(GetContentSizeConstraints());
  110. int min_height = 0, min_width = 0;
  111. if (options.Get(options::kMinHeight, &min_height) |
  112. options.Get(options::kMinWidth, &min_width)) {
  113. size_constraints.set_minimum_size(gfx::Size(min_width, min_height));
  114. }
  115. int max_height = INT_MAX, max_width = INT_MAX;
  116. if (options.Get(options::kMaxHeight, &max_height) |
  117. options.Get(options::kMaxWidth, &max_width)) {
  118. size_constraints.set_maximum_size(gfx::Size(max_width, max_height));
  119. }
  120. bool use_content_size = false;
  121. options.Get(options::kUseContentSize, &use_content_size);
  122. if (use_content_size) {
  123. SetContentSizeConstraints(size_constraints);
  124. } else {
  125. SetSizeConstraints(size_constraints);
  126. }
  127. #if defined(USE_X11)
  128. bool resizable;
  129. if (options.Get(options::kResizable, &resizable)) {
  130. SetResizable(resizable);
  131. }
  132. #endif
  133. #if defined(OS_WIN) || defined(USE_X11)
  134. bool closable;
  135. if (options.Get(options::kClosable, &closable)) {
  136. SetClosable(closable);
  137. }
  138. #endif
  139. bool movable;
  140. if (options.Get(options::kMovable, &movable)) {
  141. SetMovable(movable);
  142. }
  143. bool has_shadow;
  144. if (options.Get(options::kHasShadow, &has_shadow)) {
  145. SetHasShadow(has_shadow);
  146. }
  147. bool top;
  148. if (options.Get(options::kAlwaysOnTop, &top) && top) {
  149. SetAlwaysOnTop(true);
  150. }
  151. bool fullscreenable = true;
  152. bool fullscreen = false;
  153. if (options.Get(options::kFullscreen, &fullscreen) && !fullscreen) {
  154. // Disable fullscreen button if 'fullscreen' is specified to false.
  155. #if defined(OS_MACOSX)
  156. fullscreenable = false;
  157. #endif
  158. }
  159. // Overriden by 'fullscreenable'.
  160. options.Get(options::kFullScreenable, &fullscreenable);
  161. SetFullScreenable(fullscreenable);
  162. if (fullscreen) {
  163. SetFullScreen(true);
  164. }
  165. bool skip;
  166. if (options.Get(options::kSkipTaskbar, &skip)) {
  167. SetSkipTaskbar(skip);
  168. }
  169. bool kiosk;
  170. if (options.Get(options::kKiosk, &kiosk) && kiosk) {
  171. SetKiosk(kiosk);
  172. }
  173. std::string color;
  174. if (options.Get(options::kBackgroundColor, &color)) {
  175. SetBackgroundColor(color);
  176. } else if (!transparent()) {
  177. // For normal window, use white as default background.
  178. SetBackgroundColor("#FFFF");
  179. }
  180. std::string title(Browser::Get()->GetName());
  181. options.Get(options::kTitle, &title);
  182. SetTitle(title);
  183. // Then show it.
  184. bool show = true;
  185. options.Get(options::kShow, &show);
  186. if (show)
  187. Show();
  188. }
  189. void NativeWindow::SetSize(const gfx::Size& size, bool animate) {
  190. SetBounds(gfx::Rect(GetPosition(), size), animate);
  191. }
  192. gfx::Size NativeWindow::GetSize() {
  193. return GetBounds().size();
  194. }
  195. void NativeWindow::SetPosition(const gfx::Point& position, bool animate) {
  196. SetBounds(gfx::Rect(position, GetSize()), animate);
  197. }
  198. gfx::Point NativeWindow::GetPosition() {
  199. return GetBounds().origin();
  200. }
  201. void NativeWindow::SetContentSize(const gfx::Size& size, bool animate) {
  202. SetSize(ContentBoundsToWindowBounds(gfx::Rect(size)).size(), animate);
  203. }
  204. gfx::Size NativeWindow::GetContentSize() {
  205. return GetContentBounds().size();
  206. }
  207. void NativeWindow::SetContentBounds(const gfx::Rect& bounds, bool animate) {
  208. SetBounds(ContentBoundsToWindowBounds(bounds), animate);
  209. }
  210. gfx::Rect NativeWindow::GetContentBounds() {
  211. return WindowBoundsToContentBounds(GetBounds());
  212. }
  213. void NativeWindow::SetSizeConstraints(
  214. const extensions::SizeConstraints& window_constraints) {
  215. extensions::SizeConstraints content_constraints(GetContentSizeConstraints());
  216. if (window_constraints.HasMaximumSize()) {
  217. gfx::Rect max_bounds = WindowBoundsToContentBounds(
  218. gfx::Rect(window_constraints.GetMaximumSize()));
  219. content_constraints.set_maximum_size(max_bounds.size());
  220. }
  221. if (window_constraints.HasMinimumSize()) {
  222. gfx::Rect min_bounds = WindowBoundsToContentBounds(
  223. gfx::Rect(window_constraints.GetMinimumSize()));
  224. content_constraints.set_minimum_size(min_bounds.size());
  225. }
  226. SetContentSizeConstraints(content_constraints);
  227. }
  228. extensions::SizeConstraints NativeWindow::GetSizeConstraints() {
  229. extensions::SizeConstraints content_constraints = GetContentSizeConstraints();
  230. extensions::SizeConstraints window_constraints;
  231. if (content_constraints.HasMaximumSize()) {
  232. gfx::Rect max_bounds = ContentBoundsToWindowBounds(
  233. gfx::Rect(content_constraints.GetMaximumSize()));
  234. window_constraints.set_maximum_size(max_bounds.size());
  235. }
  236. if (content_constraints.HasMinimumSize()) {
  237. gfx::Rect min_bounds = ContentBoundsToWindowBounds(
  238. gfx::Rect(content_constraints.GetMinimumSize()));
  239. window_constraints.set_minimum_size(min_bounds.size());
  240. }
  241. return window_constraints;
  242. }
  243. void NativeWindow::SetContentSizeConstraints(
  244. const extensions::SizeConstraints& size_constraints) {
  245. size_constraints_ = size_constraints;
  246. }
  247. extensions::SizeConstraints NativeWindow::GetContentSizeConstraints() {
  248. return size_constraints_;
  249. }
  250. void NativeWindow::SetMinimumSize(const gfx::Size& size) {
  251. extensions::SizeConstraints size_constraints;
  252. size_constraints.set_minimum_size(size);
  253. SetSizeConstraints(size_constraints);
  254. }
  255. gfx::Size NativeWindow::GetMinimumSize() {
  256. return GetSizeConstraints().GetMinimumSize();
  257. }
  258. void NativeWindow::SetMaximumSize(const gfx::Size& size) {
  259. extensions::SizeConstraints size_constraints;
  260. size_constraints.set_maximum_size(size);
  261. SetSizeConstraints(size_constraints);
  262. }
  263. gfx::Size NativeWindow::GetMaximumSize() {
  264. return GetSizeConstraints().GetMaximumSize();
  265. }
  266. void NativeWindow::SetSheetOffset(const double offsetX, const double offsetY) {
  267. sheet_offset_x_ = offsetX;
  268. sheet_offset_y_ = offsetY;
  269. }
  270. double NativeWindow::GetSheetOffsetX() {
  271. return sheet_offset_x_;
  272. }
  273. double NativeWindow::GetSheetOffsetY() {
  274. return sheet_offset_y_;
  275. }
  276. void NativeWindow::SetRepresentedFilename(const std::string& filename) {
  277. }
  278. std::string NativeWindow::GetRepresentedFilename() {
  279. return "";
  280. }
  281. void NativeWindow::SetDocumentEdited(bool edited) {
  282. }
  283. bool NativeWindow::IsDocumentEdited() {
  284. return false;
  285. }
  286. void NativeWindow::SetFocusable(bool focusable) {
  287. }
  288. void NativeWindow::SetMenu(AtomMenuModel* menu) {
  289. }
  290. void NativeWindow::SetParentWindow(NativeWindow* parent) {
  291. parent_ = parent;
  292. }
  293. void NativeWindow::SetAutoHideCursor(bool auto_hide) {
  294. }
  295. void NativeWindow::SetVibrancy(const std::string& filename) {
  296. }
  297. void NativeWindow::FocusOnWebView() {
  298. web_contents()->GetRenderViewHost()->GetWidget()->Focus();
  299. }
  300. void NativeWindow::BlurWebView() {
  301. web_contents()->GetRenderViewHost()->GetWidget()->Blur();
  302. }
  303. bool NativeWindow::IsWebViewFocused() {
  304. auto host_view = web_contents()->GetRenderViewHost()->GetWidget()->GetView();
  305. return host_view && host_view->HasFocus();
  306. }
  307. void NativeWindow::SetAutoHideMenuBar(bool auto_hide) {
  308. }
  309. bool NativeWindow::IsMenuBarAutoHide() {
  310. return false;
  311. }
  312. void NativeWindow::SetMenuBarVisibility(bool visible) {
  313. }
  314. bool NativeWindow::IsMenuBarVisible() {
  315. return true;
  316. }
  317. double NativeWindow::GetAspectRatio() {
  318. return aspect_ratio_;
  319. }
  320. gfx::Size NativeWindow::GetAspectRatioExtraSize() {
  321. return aspect_ratio_extraSize_;
  322. }
  323. void NativeWindow::SetAspectRatio(double aspect_ratio,
  324. const gfx::Size& extra_size) {
  325. aspect_ratio_ = aspect_ratio;
  326. aspect_ratio_extraSize_ = extra_size;
  327. }
  328. void NativeWindow::PreviewFile(const std::string& path,
  329. const std::string& display_name) {
  330. }
  331. void NativeWindow::CloseFilePreview() {
  332. }
  333. void NativeWindow::RequestToClosePage() {
  334. bool prevent_default = false;
  335. FOR_EACH_OBSERVER(NativeWindowObserver,
  336. observers_,
  337. WillCloseWindow(&prevent_default));
  338. if (prevent_default) {
  339. WindowList::WindowCloseCancelled(this);
  340. return;
  341. }
  342. // Assume the window is not responding if it doesn't cancel the close and is
  343. // not closed in 5s, in this way we can quickly show the unresponsive
  344. // dialog when the window is busy executing some script withouth waiting for
  345. // the unresponsive timeout.
  346. if (window_unresposive_closure_.IsCancelled())
  347. ScheduleUnresponsiveEvent(5000);
  348. if (!web_contents())
  349. // Already closed by renderer
  350. return;
  351. if (web_contents()->NeedToFireBeforeUnload())
  352. web_contents()->DispatchBeforeUnload();
  353. else
  354. web_contents()->Close();
  355. }
  356. void NativeWindow::CloseContents(content::WebContents* source) {
  357. if (!inspectable_web_contents_)
  358. return;
  359. inspectable_web_contents_->GetView()->SetDelegate(nullptr);
  360. inspectable_web_contents_ = nullptr;
  361. Observe(nullptr);
  362. FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
  363. WillDestroyNativeObject());
  364. // When the web contents is gone, close the window immediately, but the
  365. // memory will not be freed until you call delete.
  366. // In this way, it would be safe to manage windows via smart pointers. If you
  367. // want to free memory when the window is closed, you can do deleting by
  368. // overriding the OnWindowClosed method in the observer.
  369. CloseImmediately();
  370. // Do not sent "unresponsive" event after window is closed.
  371. window_unresposive_closure_.Cancel();
  372. }
  373. void NativeWindow::RendererUnresponsive(content::WebContents* source) {
  374. // Schedule the unresponsive shortly later, since we may receive the
  375. // responsive event soon. This could happen after the whole application had
  376. // blocked for a while.
  377. // Also notice that when closing this event would be ignored because we have
  378. // explicitly started a close timeout counter. This is on purpose because we
  379. // don't want the unresponsive event to be sent too early when user is closing
  380. // the window.
  381. ScheduleUnresponsiveEvent(50);
  382. }
  383. void NativeWindow::RendererResponsive(content::WebContents* source) {
  384. window_unresposive_closure_.Cancel();
  385. FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnRendererResponsive());
  386. }
  387. void NativeWindow::NotifyWindowClosed() {
  388. if (is_closed_)
  389. return;
  390. WindowList::RemoveWindow(this);
  391. is_closed_ = true;
  392. FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowClosed());
  393. }
  394. void NativeWindow::NotifyWindowBlur() {
  395. FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowBlur());
  396. }
  397. void NativeWindow::NotifyWindowFocus() {
  398. FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowFocus());
  399. }
  400. void NativeWindow::NotifyWindowShow() {
  401. FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowShow());
  402. }
  403. void NativeWindow::NotifyWindowHide() {
  404. FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowHide());
  405. }
  406. void NativeWindow::NotifyWindowMaximize() {
  407. FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowMaximize());
  408. }
  409. void NativeWindow::NotifyWindowUnmaximize() {
  410. FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowUnmaximize());
  411. }
  412. void NativeWindow::NotifyWindowMinimize() {
  413. FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowMinimize());
  414. }
  415. void NativeWindow::NotifyWindowRestore() {
  416. FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowRestore());
  417. }
  418. void NativeWindow::NotifyWindowResize() {
  419. FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowResize());
  420. }
  421. void NativeWindow::NotifyWindowMove() {
  422. FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowMove());
  423. }
  424. void NativeWindow::NotifyWindowMoved() {
  425. FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowMoved());
  426. }
  427. void NativeWindow::NotifyWindowEnterFullScreen() {
  428. FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
  429. OnWindowEnterFullScreen());
  430. }
  431. void NativeWindow::NotifyWindowScrollTouchBegin() {
  432. FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
  433. OnWindowScrollTouchBegin());
  434. }
  435. void NativeWindow::NotifyWindowScrollTouchEnd() {
  436. FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
  437. OnWindowScrollTouchEnd());
  438. }
  439. void NativeWindow::NotifyWindowScrollTouchEdge() {
  440. FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
  441. OnWindowScrollTouchEdge());
  442. }
  443. void NativeWindow::NotifyWindowSwipe(const std::string& direction) {
  444. FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
  445. OnWindowSwipe(direction));
  446. }
  447. void NativeWindow::NotifyWindowLeaveFullScreen() {
  448. FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
  449. OnWindowLeaveFullScreen());
  450. }
  451. void NativeWindow::NotifyWindowEnterHtmlFullScreen() {
  452. FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
  453. OnWindowEnterHtmlFullScreen());
  454. }
  455. void NativeWindow::NotifyWindowLeaveHtmlFullScreen() {
  456. FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
  457. OnWindowLeaveHtmlFullScreen());
  458. }
  459. void NativeWindow::NotifyWindowExecuteWindowsCommand(
  460. const std::string& command) {
  461. FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
  462. OnExecuteWindowsCommand(command));
  463. }
  464. #if defined(OS_WIN)
  465. void NativeWindow::NotifyWindowMessage(
  466. UINT message, WPARAM w_param, LPARAM l_param) {
  467. FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
  468. OnWindowMessage(message, w_param, l_param));
  469. }
  470. #endif
  471. std::unique_ptr<SkRegion> NativeWindow::DraggableRegionsToSkRegion(
  472. const std::vector<DraggableRegion>& regions) {
  473. std::unique_ptr<SkRegion> sk_region(new SkRegion);
  474. for (const DraggableRegion& region : regions) {
  475. sk_region->op(
  476. region.bounds.x(),
  477. region.bounds.y(),
  478. region.bounds.right(),
  479. region.bounds.bottom(),
  480. region.draggable ? SkRegion::kUnion_Op : SkRegion::kDifference_Op);
  481. }
  482. return sk_region;
  483. }
  484. void NativeWindow::RenderViewCreated(
  485. content::RenderViewHost* render_view_host) {
  486. if (!transparent_)
  487. return;
  488. content::RenderWidgetHostImpl* impl = content::RenderWidgetHostImpl::FromID(
  489. render_view_host->GetProcess()->GetID(),
  490. render_view_host->GetRoutingID());
  491. if (impl)
  492. impl->SetBackgroundOpaque(false);
  493. }
  494. void NativeWindow::BeforeUnloadDialogCancelled() {
  495. WindowList::WindowCloseCancelled(this);
  496. // Cancel unresponsive event when window close is cancelled.
  497. window_unresposive_closure_.Cancel();
  498. }
  499. void NativeWindow::DidFirstVisuallyNonEmptyPaint() {
  500. if (IsVisible())
  501. return;
  502. // When there is a non-empty first paint, resize the RenderWidget to force
  503. // Chromium to draw.
  504. const auto view = web_contents()->GetRenderWidgetHostView();
  505. view->Show();
  506. view->SetSize(GetContentSize());
  507. // Emit the ReadyToShow event in next tick in case of pending drawing work.
  508. base::MessageLoop::current()->PostTask(
  509. FROM_HERE,
  510. base::Bind(&NativeWindow::NotifyReadyToShow, GetWeakPtr()));
  511. }
  512. bool NativeWindow::OnMessageReceived(const IPC::Message& message) {
  513. bool handled = true;
  514. IPC_BEGIN_MESSAGE_MAP(NativeWindow, message)
  515. IPC_MESSAGE_HANDLER(AtomViewHostMsg_UpdateDraggableRegions,
  516. UpdateDraggableRegions)
  517. IPC_MESSAGE_UNHANDLED(handled = false)
  518. IPC_END_MESSAGE_MAP()
  519. return handled;
  520. }
  521. void NativeWindow::UpdateDraggableRegions(
  522. const std::vector<DraggableRegion>& regions) {
  523. // Draggable region is not supported for non-frameless window.
  524. if (has_frame_)
  525. return;
  526. draggable_region_ = DraggableRegionsToSkRegion(regions);
  527. }
  528. void NativeWindow::ScheduleUnresponsiveEvent(int ms) {
  529. if (!window_unresposive_closure_.IsCancelled())
  530. return;
  531. window_unresposive_closure_.Reset(
  532. base::Bind(&NativeWindow::NotifyWindowUnresponsive,
  533. weak_factory_.GetWeakPtr()));
  534. base::MessageLoop::current()->PostDelayedTask(
  535. FROM_HERE,
  536. window_unresposive_closure_.callback(),
  537. base::TimeDelta::FromMilliseconds(ms));
  538. }
  539. void NativeWindow::NotifyWindowUnresponsive() {
  540. window_unresposive_closure_.Cancel();
  541. if (!is_closed_ && !IsUnresponsiveEventSuppressed() && IsEnabled())
  542. FOR_EACH_OBSERVER(NativeWindowObserver,
  543. observers_,
  544. OnRendererUnresponsive());
  545. }
  546. void NativeWindow::NotifyReadyToShow() {
  547. FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnReadyToShow());
  548. }
  549. } // namespace atom