native_window.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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 "base/threading/thread_task_runner_handle.h"
  21. #include "brightray/browser/inspectable_web_contents.h"
  22. #include "brightray/browser/inspectable_web_contents_view.h"
  23. #include "components/prefs/pref_service.h"
  24. #include "content/browser/renderer_host/render_widget_host_impl.h"
  25. #include "content/public/browser/navigation_entry.h"
  26. #include "content/public/browser/plugin_service.h"
  27. #include "content/public/browser/render_process_host.h"
  28. #include "content/public/browser/render_view_host.h"
  29. #include "content/public/browser/render_widget_host.h"
  30. #include "content/public/browser/render_widget_host_view.h"
  31. #include "content/public/common/content_switches.h"
  32. #include "ipc/ipc_message_macros.h"
  33. #include "native_mate/dictionary.h"
  34. #include "third_party/skia/include/core/SkRegion.h"
  35. #include "ui/gfx/codec/png_codec.h"
  36. #include "ui/gfx/geometry/point.h"
  37. #include "ui/gfx/geometry/rect.h"
  38. #include "ui/gfx/geometry/size.h"
  39. #include "ui/gfx/geometry/size_conversions.h"
  40. #include "ui/gl/gpu_switching_manager.h"
  41. #if defined(OS_LINUX) || defined(OS_WIN)
  42. #include "content/public/common/renderer_preferences.h"
  43. #include "ui/gfx/font_render_params.h"
  44. #endif
  45. DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::NativeWindowRelay);
  46. namespace atom {
  47. NativeWindow::NativeWindow(
  48. brightray::InspectableWebContents* inspectable_web_contents,
  49. const mate::Dictionary& options,
  50. NativeWindow* parent)
  51. : content::WebContentsObserver(inspectable_web_contents->GetWebContents()),
  52. has_frame_(true),
  53. transparent_(false),
  54. enable_larger_than_screen_(false),
  55. is_closed_(false),
  56. sheet_offset_x_(0.0),
  57. sheet_offset_y_(0.0),
  58. aspect_ratio_(0.0),
  59. parent_(parent),
  60. is_modal_(false),
  61. inspectable_web_contents_(inspectable_web_contents),
  62. weak_factory_(this) {
  63. options.Get(options::kFrame, &has_frame_);
  64. options.Get(options::kTransparent, &transparent_);
  65. options.Get(options::kEnableLargerThanScreen, &enable_larger_than_screen_);
  66. if (parent)
  67. options.Get("modal", &is_modal_);
  68. #if defined(OS_LINUX) || defined(OS_WIN)
  69. auto* prefs = web_contents()->GetMutableRendererPrefs();
  70. // Update font settings.
  71. CR_DEFINE_STATIC_LOCAL(const gfx::FontRenderParams, params,
  72. (gfx::GetFontRenderParams(gfx::FontRenderParamsQuery(), nullptr)));
  73. prefs->should_antialias_text = params.antialiasing;
  74. prefs->use_subpixel_positioning = params.subpixel_positioning;
  75. prefs->hinting = params.hinting;
  76. prefs->use_autohinter = params.autohinter;
  77. prefs->use_bitmaps = params.use_bitmaps;
  78. prefs->subpixel_rendering = params.subpixel_rendering;
  79. #endif
  80. // Tell the content module to initialize renderer widget with transparent
  81. // mode.
  82. ui::GpuSwitchingManager::SetTransparent(transparent_);
  83. WindowList::AddWindow(this);
  84. }
  85. NativeWindow::~NativeWindow() {
  86. // It's possible that the windows gets destroyed before it's closed, in that
  87. // case we need to ensure the OnWindowClosed message is still notified.
  88. NotifyWindowClosed();
  89. }
  90. // static
  91. NativeWindow* NativeWindow::FromWebContents(
  92. content::WebContents* web_contents) {
  93. for (const auto& window : WindowList::GetWindows()) {
  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::SetTouchBar(
  298. const std::vector<mate::PersistentDictionary>& items) {
  299. }
  300. void NativeWindow::RefreshTouchBarItem(const std::string& item_id) {
  301. }
  302. void NativeWindow::SetEscapeTouchBarItem(
  303. const mate::PersistentDictionary& item) {
  304. }
  305. void NativeWindow::FocusOnWebView() {
  306. web_contents()->GetRenderViewHost()->GetWidget()->Focus();
  307. }
  308. void NativeWindow::BlurWebView() {
  309. web_contents()->GetRenderViewHost()->GetWidget()->Blur();
  310. }
  311. bool NativeWindow::IsWebViewFocused() {
  312. auto host_view = web_contents()->GetRenderViewHost()->GetWidget()->GetView();
  313. return host_view && host_view->HasFocus();
  314. }
  315. void NativeWindow::SetAutoHideMenuBar(bool auto_hide) {
  316. }
  317. bool NativeWindow::IsMenuBarAutoHide() {
  318. return false;
  319. }
  320. void NativeWindow::SetMenuBarVisibility(bool visible) {
  321. }
  322. bool NativeWindow::IsMenuBarVisible() {
  323. return true;
  324. }
  325. double NativeWindow::GetAspectRatio() {
  326. return aspect_ratio_;
  327. }
  328. gfx::Size NativeWindow::GetAspectRatioExtraSize() {
  329. return aspect_ratio_extraSize_;
  330. }
  331. void NativeWindow::SetAspectRatio(double aspect_ratio,
  332. const gfx::Size& extra_size) {
  333. aspect_ratio_ = aspect_ratio;
  334. aspect_ratio_extraSize_ = extra_size;
  335. }
  336. void NativeWindow::PreviewFile(const std::string& path,
  337. const std::string& display_name) {
  338. }
  339. void NativeWindow::CloseFilePreview() {
  340. }
  341. void NativeWindow::RequestToClosePage() {
  342. bool prevent_default = false;
  343. for (NativeWindowObserver& observer : observers_)
  344. observer.WillCloseWindow(&prevent_default);
  345. if (prevent_default) {
  346. WindowList::WindowCloseCancelled(this);
  347. return;
  348. }
  349. // Assume the window is not responding if it doesn't cancel the close and is
  350. // not closed in 5s, in this way we can quickly show the unresponsive
  351. // dialog when the window is busy executing some script withouth waiting for
  352. // the unresponsive timeout.
  353. if (window_unresposive_closure_.IsCancelled())
  354. ScheduleUnresponsiveEvent(5000);
  355. if (!web_contents())
  356. // Already closed by renderer
  357. return;
  358. if (web_contents()->NeedToFireBeforeUnload())
  359. web_contents()->DispatchBeforeUnload();
  360. else
  361. web_contents()->Close();
  362. }
  363. void NativeWindow::CloseContents(content::WebContents* source) {
  364. if (!inspectable_web_contents_)
  365. return;
  366. inspectable_web_contents_->GetView()->SetDelegate(nullptr);
  367. inspectable_web_contents_ = nullptr;
  368. Observe(nullptr);
  369. for (NativeWindowObserver& observer : observers_)
  370. observer.WillDestroyNativeObject();
  371. // When the web contents is gone, close the window immediately, but the
  372. // memory will not be freed until you call delete.
  373. // In this way, it would be safe to manage windows via smart pointers. If you
  374. // want to free memory when the window is closed, you can do deleting by
  375. // overriding the OnWindowClosed method in the observer.
  376. CloseImmediately();
  377. // Do not sent "unresponsive" event after window is closed.
  378. window_unresposive_closure_.Cancel();
  379. }
  380. void NativeWindow::RendererUnresponsive(content::WebContents* source) {
  381. // Schedule the unresponsive shortly later, since we may receive the
  382. // responsive event soon. This could happen after the whole application had
  383. // blocked for a while.
  384. // Also notice that when closing this event would be ignored because we have
  385. // explicitly started a close timeout counter. This is on purpose because we
  386. // don't want the unresponsive event to be sent too early when user is closing
  387. // the window.
  388. ScheduleUnresponsiveEvent(50);
  389. }
  390. void NativeWindow::RendererResponsive(content::WebContents* source) {
  391. window_unresposive_closure_.Cancel();
  392. for (NativeWindowObserver& observer : observers_)
  393. observer.OnRendererResponsive();
  394. }
  395. void NativeWindow::NotifyWindowClosed() {
  396. if (is_closed_)
  397. return;
  398. WindowList::RemoveWindow(this);
  399. is_closed_ = true;
  400. for (NativeWindowObserver& observer : observers_)
  401. observer.OnWindowClosed();
  402. }
  403. void NativeWindow::NotifyWindowEndSession() {
  404. for (NativeWindowObserver& observer : observers_)
  405. observer.OnWindowEndSession();
  406. }
  407. void NativeWindow::NotifyWindowBlur() {
  408. for (NativeWindowObserver& observer : observers_)
  409. observer.OnWindowBlur();
  410. }
  411. void NativeWindow::NotifyWindowFocus() {
  412. for (NativeWindowObserver& observer : observers_)
  413. observer.OnWindowFocus();
  414. }
  415. void NativeWindow::NotifyWindowShow() {
  416. for (NativeWindowObserver& observer : observers_)
  417. observer.OnWindowShow();
  418. }
  419. void NativeWindow::NotifyWindowHide() {
  420. for (NativeWindowObserver& observer : observers_)
  421. observer.OnWindowHide();
  422. }
  423. void NativeWindow::NotifyWindowMaximize() {
  424. for (NativeWindowObserver& observer : observers_)
  425. observer.OnWindowMaximize();
  426. }
  427. void NativeWindow::NotifyWindowUnmaximize() {
  428. for (NativeWindowObserver& observer : observers_)
  429. observer.OnWindowUnmaximize();
  430. }
  431. void NativeWindow::NotifyWindowMinimize() {
  432. for (NativeWindowObserver& observer : observers_)
  433. observer.OnWindowMinimize();
  434. }
  435. void NativeWindow::NotifyWindowRestore() {
  436. for (NativeWindowObserver& observer : observers_)
  437. observer.OnWindowRestore();
  438. }
  439. void NativeWindow::NotifyWindowResize() {
  440. for (NativeWindowObserver& observer : observers_)
  441. observer.OnWindowResize();
  442. }
  443. void NativeWindow::NotifyWindowMove() {
  444. for (NativeWindowObserver& observer : observers_)
  445. observer.OnWindowMove();
  446. }
  447. void NativeWindow::NotifyWindowMoved() {
  448. for (NativeWindowObserver& observer : observers_)
  449. observer.OnWindowMoved();
  450. }
  451. void NativeWindow::NotifyWindowEnterFullScreen() {
  452. for (NativeWindowObserver& observer : observers_)
  453. observer.OnWindowEnterFullScreen();
  454. }
  455. void NativeWindow::NotifyWindowScrollTouchBegin() {
  456. for (NativeWindowObserver& observer : observers_)
  457. observer.OnWindowScrollTouchBegin();
  458. }
  459. void NativeWindow::NotifyWindowScrollTouchEnd() {
  460. for (NativeWindowObserver& observer : observers_)
  461. observer.OnWindowScrollTouchEnd();
  462. }
  463. void NativeWindow::NotifyWindowScrollTouchEdge() {
  464. for (NativeWindowObserver& observer : observers_)
  465. observer.OnWindowScrollTouchEdge();
  466. }
  467. void NativeWindow::NotifyWindowSwipe(const std::string& direction) {
  468. for (NativeWindowObserver& observer : observers_)
  469. observer.OnWindowSwipe(direction);
  470. }
  471. void NativeWindow::NotifyWindowSheetBegin() {
  472. for (NativeWindowObserver& observer : observers_)
  473. observer.OnWindowSheetBegin();
  474. }
  475. void NativeWindow::NotifyWindowSheetEnd() {
  476. for (NativeWindowObserver& observer : observers_)
  477. observer.OnWindowSheetEnd();
  478. }
  479. void NativeWindow::NotifyWindowLeaveFullScreen() {
  480. for (NativeWindowObserver& observer : observers_)
  481. observer.OnWindowLeaveFullScreen();
  482. }
  483. void NativeWindow::NotifyWindowEnterHtmlFullScreen() {
  484. for (NativeWindowObserver& observer : observers_)
  485. observer.OnWindowEnterHtmlFullScreen();
  486. }
  487. void NativeWindow::NotifyWindowLeaveHtmlFullScreen() {
  488. for (NativeWindowObserver& observer : observers_)
  489. observer.OnWindowLeaveHtmlFullScreen();
  490. }
  491. void NativeWindow::NotifyWindowExecuteWindowsCommand(
  492. const std::string& command) {
  493. for (NativeWindowObserver& observer : observers_)
  494. observer.OnExecuteWindowsCommand(command);
  495. }
  496. void NativeWindow::NotifyTouchBarItemInteraction(
  497. const std::string& item_id,
  498. const base::DictionaryValue& details) {
  499. for (NativeWindowObserver& observer : observers_)
  500. observer.OnTouchBarItemResult(item_id, details);
  501. }
  502. #if defined(OS_WIN)
  503. void NativeWindow::NotifyWindowMessage(
  504. UINT message, WPARAM w_param, LPARAM l_param) {
  505. for (NativeWindowObserver& observer : observers_)
  506. observer.OnWindowMessage(message, w_param, l_param);
  507. }
  508. #endif
  509. std::unique_ptr<SkRegion> NativeWindow::DraggableRegionsToSkRegion(
  510. const std::vector<DraggableRegion>& regions) {
  511. std::unique_ptr<SkRegion> sk_region(new SkRegion);
  512. for (const DraggableRegion& region : regions) {
  513. sk_region->op(
  514. region.bounds.x(),
  515. region.bounds.y(),
  516. region.bounds.right(),
  517. region.bounds.bottom(),
  518. region.draggable ? SkRegion::kUnion_Op : SkRegion::kDifference_Op);
  519. }
  520. return sk_region;
  521. }
  522. void NativeWindow::RenderViewCreated(
  523. content::RenderViewHost* render_view_host) {
  524. if (!transparent_)
  525. return;
  526. content::RenderWidgetHostImpl* impl = content::RenderWidgetHostImpl::FromID(
  527. render_view_host->GetProcess()->GetID(),
  528. render_view_host->GetRoutingID());
  529. if (impl)
  530. impl->SetBackgroundOpaque(false);
  531. }
  532. void NativeWindow::BeforeUnloadDialogCancelled() {
  533. WindowList::WindowCloseCancelled(this);
  534. // Cancel unresponsive event when window close is cancelled.
  535. window_unresposive_closure_.Cancel();
  536. }
  537. void NativeWindow::DidFirstVisuallyNonEmptyPaint() {
  538. if (IsVisible())
  539. return;
  540. // When there is a non-empty first paint, resize the RenderWidget to force
  541. // Chromium to draw.
  542. const auto view = web_contents()->GetRenderWidgetHostView();
  543. view->Show();
  544. view->SetSize(GetContentSize());
  545. // Emit the ReadyToShow event in next tick in case of pending drawing work.
  546. base::ThreadTaskRunnerHandle::Get()->PostTask(
  547. FROM_HERE,
  548. base::Bind(&NativeWindow::NotifyReadyToShow, GetWeakPtr()));
  549. }
  550. bool NativeWindow::OnMessageReceived(const IPC::Message& message) {
  551. bool handled = true;
  552. IPC_BEGIN_MESSAGE_MAP(NativeWindow, message)
  553. IPC_MESSAGE_HANDLER(AtomViewHostMsg_UpdateDraggableRegions,
  554. UpdateDraggableRegions)
  555. IPC_MESSAGE_UNHANDLED(handled = false)
  556. IPC_END_MESSAGE_MAP()
  557. return handled;
  558. }
  559. void NativeWindow::UpdateDraggableRegions(
  560. const std::vector<DraggableRegion>& regions) {
  561. // Draggable region is not supported for non-frameless window.
  562. if (has_frame_)
  563. return;
  564. draggable_region_ = DraggableRegionsToSkRegion(regions);
  565. }
  566. void NativeWindow::ScheduleUnresponsiveEvent(int ms) {
  567. if (!window_unresposive_closure_.IsCancelled())
  568. return;
  569. window_unresposive_closure_.Reset(
  570. base::Bind(&NativeWindow::NotifyWindowUnresponsive,
  571. weak_factory_.GetWeakPtr()));
  572. base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
  573. FROM_HERE,
  574. window_unresposive_closure_.callback(),
  575. base::TimeDelta::FromMilliseconds(ms));
  576. }
  577. void NativeWindow::NotifyWindowUnresponsive() {
  578. window_unresposive_closure_.Cancel();
  579. if (!is_closed_ && !IsUnresponsiveEventSuppressed() && IsEnabled()) {
  580. for (NativeWindowObserver& observer : observers_)
  581. observer.OnRendererUnresponsive();
  582. }
  583. }
  584. void NativeWindow::NotifyReadyToShow() {
  585. for (NativeWindowObserver& observer : observers_)
  586. observer.OnReadyToShow();
  587. }
  588. } // namespace atom