Don't create WebContents in NativeWindow
authorCheng Zhao <zcbenz@gmail.com>
Thu, 25 Jun 2015 01:47:57 +0000 (09:47 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 25 Jun 2015 01:47:57 +0000 (09:47 +0800)
atom/browser/api/atom_api_window.cc
atom/browser/api/atom_api_window.h
atom/browser/native_window.cc
atom/browser/native_window.h
atom/browser/native_window_mac.h
atom/browser/native_window_mac.mm
atom/browser/native_window_views.cc
atom/browser/native_window_views.h

index 86399ce..9869e36 100644 (file)
@@ -38,8 +38,16 @@ void OnCapturePageDone(
 }  // namespace
 
 
-Window::Window(const mate::Dictionary& options)
-    : window_(NativeWindow::Create(options)) {
+Window::Window(v8::Isolate* isolate, const mate::Dictionary& options) {
+  // Creates the WebContents used by BrowserWindow.
+  mate::Dictionary web_contents_options(isolate, v8::Object::New(isolate));
+  auto web_contents = WebContents::Create(isolate, web_contents_options);
+  web_contents_.Reset(isolate, web_contents.ToV8());
+
+  // Creates BrowserWindow.
+  window_.reset(NativeWindow::Create(web_contents->managed_web_contents(),
+                                     options));
+  web_contents->SetOwnerWindow(window_.get());
   window_->InitFromOptions(options);
   window_->AddObserver(this);
 }
@@ -49,13 +57,6 @@ Window::~Window() {
     Destroy();
 }
 
-void Window::AfterInit(v8::Isolate* isolate) {
-  mate::TrackableObject<Window>::AfterInit(isolate);
-  auto web_contents = window_->managed_web_contents();
-  auto handle = WebContents::CreateFrom(isolate, web_contents);
-  web_contents_.Reset(isolate, handle.ToV8());
-}
-
 void Window::OnPageTitleUpdated(bool* prevent_default,
                                 const std::string& title) {
   *prevent_default = Emit("page-title-updated", title);
@@ -173,7 +174,7 @@ mate::Wrappable* Window::New(v8::Isolate* isolate,
                      "Cannot create BrowserWindow before app is ready");
     return nullptr;
   }
-  return new Window(options);
+  return new Window(isolate, options);
 }
 
 void Window::Destroy() {
index a0403dd..f6c99eb 100644 (file)
@@ -45,12 +45,9 @@ class Window : public mate::TrackableObject<Window>,
   NativeWindow* window() const { return window_.get(); }
 
  protected:
-  explicit Window(const mate::Dictionary& options);
+  Window(v8::Isolate* isolate, const mate::Dictionary& options);
   virtual ~Window();
 
-  // mate::Wrappable:
-  void AfterInit(v8::Isolate* isolate) override;
-
   // NativeWindowObserver:
   void OnPageTitleUpdated(bool* prevent_default,
                           const std::string& title) override;
index 4d85f7c..fc32ee7 100644 (file)
@@ -83,9 +83,10 @@ std::string RemoveWhitespace(const std::string& str) {
 
 }  // namespace
 
-NativeWindow::NativeWindow(content::WebContents* web_contents,
-                           const mate::Dictionary& options)
-    : content::WebContentsObserver(web_contents),
+NativeWindow::NativeWindow(
+    brightray::InspectableWebContents* inspectable_web_contents,
+    const mate::Dictionary& options)
+    : content::WebContentsObserver(inspectable_web_contents->GetWebContents()),
       has_frame_(true),
       transparent_(false),
       enable_larger_than_screen_(false),
@@ -94,9 +95,6 @@ NativeWindow::NativeWindow(content::WebContents* web_contents,
       has_dialog_attached_(false),
       zoom_factor_(1.0),
       weak_factory_(this) {
-  InitWithWebContents(web_contents);
-  SetOwnerWindow(this);
-
   options.Get(switches::kFrame, &has_frame_);
   options.Get(switches::kTransparent, &transparent_);
   options.Get(switches::kEnableLargerThanScreen, &enable_larger_than_screen_);
@@ -137,7 +135,7 @@ NativeWindow::NativeWindow(content::WebContents* web_contents,
       RemoveWhitespace(browser->GetName()).c_str(),
       browser->GetVersion().c_str(),
       CHROME_VERSION_STRING);
-  web_contents->GetMutableRendererPrefs()->user_agent_override =
+  web_contents()->GetMutableRendererPrefs()->user_agent_override =
       content::BuildUserAgentFromProduct(product_name);
 }
 
@@ -148,13 +146,6 @@ NativeWindow::~NativeWindow() {
 }
 
 // static
-NativeWindow* NativeWindow::Create(const mate::Dictionary& options) {
-  auto browser_context = AtomBrowserMainParts::Get()->browser_context();
-  content::WebContents::CreateParams create_params(browser_context);
-  return Create(content::WebContents::Create(create_params), options);
-}
-
-// static
 NativeWindow* NativeWindow::FromWebContents(
     content::WebContents* web_contents) {
   WindowList& window_list = *WindowList::GetInstance();
index 8f3a04c..0f06145 100644 (file)
@@ -77,12 +77,9 @@ class NativeWindow : public CommonWebContentsDelegate,
 
   // Create window with existing WebContents, the caller is responsible for
   // managing the window's live.
-  static NativeWindow* Create(content::WebContents* web_contents,
-                              const mate::Dictionary& options);
-
-  // Create window with new WebContents, the caller is responsible for
-  // managing the window's live.
-  static NativeWindow* Create(const mate::Dictionary& options);
+  static NativeWindow* Create(
+      brightray::InspectableWebContents* inspectable_web_contents,
+      const mate::Dictionary& options);
 
   // Find a window from its WebContents
   static NativeWindow* FromWebContents(content::WebContents* web_contents);
@@ -200,7 +197,7 @@ class NativeWindow : public CommonWebContentsDelegate,
   }
 
   brightray::InspectableWebContents* inspectable_web_contents() const {
-    return managed_web_contents();
+    return inspectable_web_contents_;
   }
 
   bool has_frame() const { return has_frame_; }
@@ -210,8 +207,8 @@ class NativeWindow : public CommonWebContentsDelegate,
   }
 
  protected:
-  explicit NativeWindow(content::WebContents* web_contents,
-                        const mate::Dictionary& options);
+  NativeWindow(brightray::InspectableWebContents* inspectable_web_contents,
+               const mate::Dictionary& options);
 
   // Called when the window needs to update its draggable region.
   virtual void UpdateDraggableRegions(
@@ -300,6 +297,9 @@ class NativeWindow : public CommonWebContentsDelegate,
   // Page's default zoom factor.
   double zoom_factor_;
 
+  // The page this window is viewing.
+  brightray::InspectableWebContents* inspectable_web_contents_;
+
   base::WeakPtrFactory<NativeWindow> weak_factory_;
 
   DISALLOW_COPY_AND_ASSIGN(NativeWindow);
index 8a6d141..7f9272b 100644 (file)
@@ -23,9 +23,9 @@ namespace atom {
 
 class NativeWindowMac : public NativeWindow {
  public:
-  explicit NativeWindowMac(content::WebContents* web_contents,
-                           const mate::Dictionary& options);
-  virtual ~NativeWindowMac();
+  NativeWindowMac(brightray::InspectableWebContents* inspectable_web_contents,
+                  const mate::Dictionary& options);
+  ~NativeWindowMac() override;
 
   // NativeWindow implementation.
   void Close() override;
index 8130d2c..093b183 100644 (file)
@@ -104,7 +104,7 @@ static const CGFloat kAtomWindowCornerRadius = 4.0;
 
 - (void)windowDidMove:(NSNotification*)notification {
   // TODO(zcbenz): Remove the alias after figuring out a proper
-  // way to disptach move. 
+  // way to disptach move.
   shell_->NotifyWindowMove();
   shell_->NotifyWindowMoved();
 }
@@ -303,8 +303,9 @@ SkRegion* DraggableRegionsToSkRegion(
 
 }  // namespace
 
-NativeWindowMac::NativeWindowMac(content::WebContents* web_contents,
-                                 const mate::Dictionary& options)
+NativeWindowMac::NativeWindowMac(
+    brightray::InspectableWebContents* web_contents,
+    const mate::Dictionary& options)
     : NativeWindow(web_contents, options),
       is_kiosk_(false),
       attention_request_id_(0) {
@@ -834,9 +835,10 @@ void NativeWindowMac::InstallDraggableRegionView() {
 }
 
 // static
-NativeWindow* NativeWindow::Create(content::WebContents* web_contents,
-                                   const mate::Dictionary& options) {
-  return new NativeWindowMac(web_contents, options);
+NativeWindow* NativeWindow::Create(
+    brightray::InspectableWebContents* inspectable_web_contents,
+    const mate::Dictionary& options) {
+  return new NativeWindowMac(inspectable_web_contents, options);
 }
 
 }  // namespace atom
index 52309ba..4c7700d 100644 (file)
@@ -142,8 +142,9 @@ class NativeWindowClientView : public views::ClientView {
 
 }  // namespace
 
-NativeWindowViews::NativeWindowViews(content::WebContents* web_contents,
-                                     const mate::Dictionary& options)
+NativeWindowViews::NativeWindowViews(
+    brightray::InspectableWebContents* web_contents,
+    const mate::Dictionary& options)
     : NativeWindow(web_contents, options),
       window_(new views::Widget),
       web_view_(inspectable_web_contents()->GetView()->GetView()),
@@ -973,9 +974,10 @@ ui::WindowShowState NativeWindowViews::GetRestoredState() {
 }
 
 // static
-NativeWindow* NativeWindow::Create(content::WebContents* web_contents,
-                                   const mate::Dictionary& options) {
-  return new NativeWindowViews(web_contents, options);
+NativeWindow* NativeWindow::Create(
+    brightray::InspectableWebContents* inspectable_web_contents,
+    const mate::Dictionary& options) {
+  return new NativeWindowViews(inspectable_web_contents, options);
 }
 
 }  // namespace atom
index becd1b9..6211225 100644 (file)
@@ -28,9 +28,9 @@ class NativeWindowViews : public NativeWindow,
                           public views::WidgetDelegateView,
                           public views::WidgetObserver {
  public:
-  explicit NativeWindowViews(content::WebContents* web_contents,
-                            const mate::Dictionary& options);
-  virtual ~NativeWindowViews();
+  NativeWindowViews(brightray::InspectableWebContents* inspectable_web_contents,
+                    const mate::Dictionary& options);
+  ~NativeWindowViews() override;
 
   // NativeWindow:
   void Close() override;