Browse Source

Refactor NativeWindow (Part 3): Remove is_offscreen_dummy from NativeWindow (#12503)

* Don't use is_offscreen_dummy in MessageBox

* Don't use is_offscreen_dummy in DownloadManagerDelegate

* Don't use is_offscreen_dummy in CommonWebContentsDelegate

* Remove is_offscreen_dummy from NativeWindow
Cheng Zhao 7 years ago
parent
commit
8fc5c6c862

+ 0 - 1
atom/browser/api/atom_api_browser_window.cc

@@ -154,7 +154,6 @@ void BrowserWindow::Init(v8::Isolate* isolate,
       options,
       parent.IsEmpty() ? nullptr : parent->window_.get()));
   web_contents->SetOwnerWindow(window_.get());
-  window_->set_is_offscreen_dummy(api_web_contents_->IsOffScreen());
 
   // Tell the content module to initialize renderer widget with transparent
   // mode.

+ 5 - 1
atom/browser/atom_download_manager_delegate.cc

@@ -10,6 +10,7 @@
 #include "atom/browser/atom_browser_context.h"
 #include "atom/browser/native_window.h"
 #include "atom/browser/ui/file_dialog.h"
+#include "atom/browser/web_contents_preferences.h"
 #include "base/bind.h"
 #include "base/files/file_util.h"
 #include "chrome/common/pref_names.h"
@@ -89,12 +90,15 @@ void AtomDownloadManagerDelegate::OnDownloadPathGenerated(
   if (relay)
     window = relay->window.get();
 
+  auto* web_preferences = WebContentsPreferences::From(web_contents);
+  bool offscreen = !web_preferences || web_preferences->IsEnabled("offscreen");
+
   base::FilePath path;
   GetItemSavePath(item, &path);
   // Show save dialog if save path was not set already on item
   file_dialog::DialogSettings settings;
   settings.parent_window = window;
-  settings.force_detached = window->is_offscreen_dummy();
+  settings.force_detached = offscreen;
   settings.title = item->GetURL().spec();
   settings.default_path = default_path;
   if (path.empty() && file_dialog::ShowSaveDialog(settings, &path)) {

+ 15 - 9
atom/browser/atom_javascript_dialog_manager.cc

@@ -55,19 +55,25 @@ void AtomJavaScriptDialogManager::RunJavaScriptDialog(
 
   origin_counts_[origin]++;
 
+  auto* web_preferences = WebContentsPreferences::From(web_contents);
   std::string checkbox;
-  if (origin_counts_[origin] > 1) {
-    auto* web_preferences = WebContentsPreferences::From(web_contents);
-    if (web_preferences &&
-        web_preferences->IsEnabled("safeDialogs") &&
-        !web_preferences->dict()->GetString("safeDialogsMessage", &checkbox)) {
-      checkbox = "Prevent this app from creating additional dialogs";
-    }
+  if (origin_counts_[origin] > 1 &&
+      web_preferences &&
+      web_preferences->IsEnabled("safeDialogs") &&
+      !web_preferences->dict()->GetString("safeDialogsMessage", &checkbox)) {
+    checkbox = "Prevent this app from creating additional dialogs";
+  }
+
+  // Don't set parent for offscreen window.
+  NativeWindow* window = nullptr;
+  if (web_preferences && !web_preferences->IsEnabled("offscreen")) {
+    auto* relay = NativeWindowRelay::FromWebContents(web_contents);
+    if (relay)
+      window = relay->window.get();
   }
 
-  auto* relay = NativeWindowRelay::FromWebContents(web_contents);
   atom::ShowMessageBox(
-      relay ? relay->window.get() : nullptr,
+      window,
       atom::MessageBoxType::MESSAGE_BOX_TYPE_NONE, buttons, -1, 0,
       atom::MessageBoxOptions::MESSAGE_BOX_NONE, "",
       base::UTF16ToUTF8(message_text), "", checkbox,

+ 11 - 7
atom/browser/common_web_contents_delegate.cc

@@ -11,6 +11,7 @@
 #include "atom/browser/atom_browser_context.h"
 #include "atom/browser/native_window.h"
 #include "atom/browser/ui/file_dialog.h"
+#include "atom/browser/web_contents_preferences.h"
 #include "atom/browser/web_dialog_helper.h"
 #include "atom/common/atom_constants.h"
 #include "base/files/file_util.h"
@@ -150,7 +151,8 @@ bool IsDevToolsFileSystemAdded(
 }  // namespace
 
 CommonWebContentsDelegate::CommonWebContentsDelegate()
-    : ignore_menu_shortcuts_(false),
+    : offscreen_(false),
+      ignore_menu_shortcuts_(false),
       html_fullscreen_(false),
       native_fullscreen_(false),
       devtools_file_system_indexer_(new DevToolsFileSystemIndexer) {
@@ -168,6 +170,10 @@ void CommonWebContentsDelegate::InitWithWebContents(
   printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
   printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents);
 
+  // Determien whether the WebContents is offscreen.
+  auto* web_preferences = WebContentsPreferences::From(web_contents);
+  offscreen_ = !web_preferences || web_preferences->IsEnabled("offscreen");
+
   // Create InspectableWebContents.
   web_contents_.reset(brightray::InspectableWebContents::Create(web_contents));
   web_contents_->SetDelegate(this);
@@ -243,8 +249,7 @@ void CommonWebContentsDelegate::RunFileChooser(
     content::RenderFrameHost* render_frame_host,
     const content::FileChooserParams& params) {
   if (!web_dialog_helper_)
-    web_dialog_helper_.reset(new WebDialogHelper(
-        owner_window(), owner_window()->is_offscreen_dummy()));
+    web_dialog_helper_.reset(new WebDialogHelper(owner_window(), offscreen_));
   web_dialog_helper_->RunFileChooser(render_frame_host, params);
 }
 
@@ -252,8 +257,7 @@ void CommonWebContentsDelegate::EnumerateDirectory(content::WebContents* guest,
                                                    int request_id,
                                                    const base::FilePath& path) {
   if (!web_dialog_helper_)
-    web_dialog_helper_.reset(new WebDialogHelper(
-        owner_window(), owner_window()->is_offscreen_dummy()));
+    web_dialog_helper_.reset(new WebDialogHelper(owner_window(), offscreen_));
   web_dialog_helper_->EnumerateDirectory(guest, request_id, path);
 }
 
@@ -301,7 +305,7 @@ void CommonWebContentsDelegate::DevToolsSaveToFile(
   } else {
     file_dialog::DialogSettings settings;
     settings.parent_window = owner_window();
-    settings.force_detached = owner_window()->is_offscreen_dummy();
+    settings.force_detached = offscreen_;
     settings.title = url;
     settings.default_path = base::FilePath::FromUTF8Unsafe(url);
     if (!file_dialog::ShowSaveDialog(settings, &path)) {
@@ -368,7 +372,7 @@ void CommonWebContentsDelegate::DevToolsAddFileSystem(
     std::vector<base::FilePath> paths;
     file_dialog::DialogSettings settings;
     settings.parent_window = owner_window();
-    settings.force_detached = owner_window()->is_offscreen_dummy();
+    settings.force_detached = offscreen_;
     settings.properties = file_dialog::FILE_DIALOG_OPEN_DIRECTORY;
     if (!file_dialog::ShowOpenDialog(settings, &paths))
       return;

+ 1 - 0
atom/browser/common_web_contents_delegate.h

@@ -141,6 +141,7 @@ class CommonWebContentsDelegate
   // The window that this WebContents belongs to.
   base::WeakPtr<NativeWindow> owner_window_;
 
+  bool offscreen_;
   bool ignore_menu_shortcuts_;
 
   // Whether window is fullscreened by HTML5 api.

+ 0 - 1
atom/browser/native_window.cc

@@ -32,7 +32,6 @@ NativeWindow::NativeWindow(
       aspect_ratio_(0.0),
       parent_(parent),
       is_modal_(false),
-      is_osr_dummy_(false),
       browser_view_(nullptr),
       weak_factory_(this) {
   options.Get(options::kFrame, &has_frame_);

+ 0 - 6
atom/browser/native_window.h

@@ -271,9 +271,6 @@ class NativeWindow : public base::SupportsUserData {
   bool transparent() const { return transparent_; }
   bool enable_larger_than_screen() const { return enable_larger_than_screen_; }
 
-  void set_is_offscreen_dummy(bool is_dummy) { is_osr_dummy_ = is_dummy; }
-  bool is_offscreen_dummy() const { return is_osr_dummy_; }
-
   NativeBrowserView* browser_view() const { return browser_view_; }
   NativeWindow* parent() const { return parent_; }
   bool is_modal() const { return is_modal_; }
@@ -319,9 +316,6 @@ class NativeWindow : public base::SupportsUserData {
   // Is this a modal window.
   bool is_modal_;
 
-  // Is this a dummy window for an offscreen WebContents.
-  bool is_osr_dummy_;
-
   // The browser view layer.
   NativeBrowserView* browser_view_;
 

+ 2 - 4
atom/browser/ui/message_box_mac.mm

@@ -146,8 +146,7 @@ int ShowMessageBox(NativeWindow* parent_window,
 
   // Use runModal for synchronous alert without parent, since we don't have a
   // window to wait for.
-  if (!parent_window || !parent_window->GetNativeWindow() ||
-      parent_window->is_offscreen_dummy())
+  if (!parent_window)
     return [[alert autorelease] runModal];
 
   int ret_code = -1;
@@ -185,8 +184,7 @@ void ShowMessageBox(NativeWindow* parent_window,
 
   // Use runModal for synchronous alert without parent, since we don't have a
   // window to wait for.
-  if (!parent_window || !parent_window->GetNativeWindow() ||
-      parent_window->is_offscreen_dummy()) {
+  if (!parent_window) {
     int ret = [[alert autorelease] runModal];
     callback.Run(ret, alert.suppressionButton.state == NSOnState);
   } else {