Don't create WebContents in NativeWindow

This commit is contained in:
Cheng Zhao 2015-06-25 09:47:57 +08:00
parent 081a4597e9
commit 5236b0c067
8 changed files with 47 additions and 54 deletions

View file

@ -38,8 +38,16 @@ void OnCapturePageDone(
} // namespace } // namespace
Window::Window(const mate::Dictionary& options) Window::Window(v8::Isolate* isolate, const mate::Dictionary& options) {
: window_(NativeWindow::Create(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_->InitFromOptions(options);
window_->AddObserver(this); window_->AddObserver(this);
} }
@ -49,13 +57,6 @@ Window::~Window() {
Destroy(); 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, void Window::OnPageTitleUpdated(bool* prevent_default,
const std::string& title) { const std::string& title) {
*prevent_default = Emit("page-title-updated", 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"); "Cannot create BrowserWindow before app is ready");
return nullptr; return nullptr;
} }
return new Window(options); return new Window(isolate, options);
} }
void Window::Destroy() { void Window::Destroy() {

View file

@ -45,12 +45,9 @@ class Window : public mate::TrackableObject<Window>,
NativeWindow* window() const { return window_.get(); } NativeWindow* window() const { return window_.get(); }
protected: protected:
explicit Window(const mate::Dictionary& options); Window(v8::Isolate* isolate, const mate::Dictionary& options);
virtual ~Window(); virtual ~Window();
// mate::Wrappable:
void AfterInit(v8::Isolate* isolate) override;
// NativeWindowObserver: // NativeWindowObserver:
void OnPageTitleUpdated(bool* prevent_default, void OnPageTitleUpdated(bool* prevent_default,
const std::string& title) override; const std::string& title) override;

View file

@ -83,9 +83,10 @@ std::string RemoveWhitespace(const std::string& str) {
} // namespace } // namespace
NativeWindow::NativeWindow(content::WebContents* web_contents, NativeWindow::NativeWindow(
brightray::InspectableWebContents* inspectable_web_contents,
const mate::Dictionary& options) const mate::Dictionary& options)
: content::WebContentsObserver(web_contents), : content::WebContentsObserver(inspectable_web_contents->GetWebContents()),
has_frame_(true), has_frame_(true),
transparent_(false), transparent_(false),
enable_larger_than_screen_(false), enable_larger_than_screen_(false),
@ -94,9 +95,6 @@ NativeWindow::NativeWindow(content::WebContents* web_contents,
has_dialog_attached_(false), has_dialog_attached_(false),
zoom_factor_(1.0), zoom_factor_(1.0),
weak_factory_(this) { weak_factory_(this) {
InitWithWebContents(web_contents);
SetOwnerWindow(this);
options.Get(switches::kFrame, &has_frame_); options.Get(switches::kFrame, &has_frame_);
options.Get(switches::kTransparent, &transparent_); options.Get(switches::kTransparent, &transparent_);
options.Get(switches::kEnableLargerThanScreen, &enable_larger_than_screen_); options.Get(switches::kEnableLargerThanScreen, &enable_larger_than_screen_);
@ -137,7 +135,7 @@ NativeWindow::NativeWindow(content::WebContents* web_contents,
RemoveWhitespace(browser->GetName()).c_str(), RemoveWhitespace(browser->GetName()).c_str(),
browser->GetVersion().c_str(), browser->GetVersion().c_str(),
CHROME_VERSION_STRING); CHROME_VERSION_STRING);
web_contents->GetMutableRendererPrefs()->user_agent_override = web_contents()->GetMutableRendererPrefs()->user_agent_override =
content::BuildUserAgentFromProduct(product_name); content::BuildUserAgentFromProduct(product_name);
} }
@ -147,13 +145,6 @@ NativeWindow::~NativeWindow() {
NotifyWindowClosed(); NotifyWindowClosed();
} }
// 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 // static
NativeWindow* NativeWindow::FromWebContents( NativeWindow* NativeWindow::FromWebContents(
content::WebContents* web_contents) { content::WebContents* web_contents) {

View file

@ -77,13 +77,10 @@ class NativeWindow : public CommonWebContentsDelegate,
// Create window with existing WebContents, the caller is responsible for // Create window with existing WebContents, the caller is responsible for
// managing the window's live. // managing the window's live.
static NativeWindow* Create(content::WebContents* web_contents, static NativeWindow* Create(
brightray::InspectableWebContents* inspectable_web_contents,
const mate::Dictionary& options); 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);
// Find a window from its WebContents // Find a window from its WebContents
static NativeWindow* FromWebContents(content::WebContents* web_contents); static NativeWindow* FromWebContents(content::WebContents* web_contents);
@ -200,7 +197,7 @@ class NativeWindow : public CommonWebContentsDelegate,
} }
brightray::InspectableWebContents* inspectable_web_contents() const { brightray::InspectableWebContents* inspectable_web_contents() const {
return managed_web_contents(); return inspectable_web_contents_;
} }
bool has_frame() const { return has_frame_; } bool has_frame() const { return has_frame_; }
@ -210,7 +207,7 @@ class NativeWindow : public CommonWebContentsDelegate,
} }
protected: protected:
explicit NativeWindow(content::WebContents* web_contents, NativeWindow(brightray::InspectableWebContents* inspectable_web_contents,
const mate::Dictionary& options); const mate::Dictionary& options);
// Called when the window needs to update its draggable region. // Called when the window needs to update its draggable region.
@ -300,6 +297,9 @@ class NativeWindow : public CommonWebContentsDelegate,
// Page's default zoom factor. // Page's default zoom factor.
double zoom_factor_; double zoom_factor_;
// The page this window is viewing.
brightray::InspectableWebContents* inspectable_web_contents_;
base::WeakPtrFactory<NativeWindow> weak_factory_; base::WeakPtrFactory<NativeWindow> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(NativeWindow); DISALLOW_COPY_AND_ASSIGN(NativeWindow);

View file

@ -23,9 +23,9 @@ namespace atom {
class NativeWindowMac : public NativeWindow { class NativeWindowMac : public NativeWindow {
public: public:
explicit NativeWindowMac(content::WebContents* web_contents, NativeWindowMac(brightray::InspectableWebContents* inspectable_web_contents,
const mate::Dictionary& options); const mate::Dictionary& options);
virtual ~NativeWindowMac(); ~NativeWindowMac() override;
// NativeWindow implementation. // NativeWindow implementation.
void Close() override; void Close() override;

View file

@ -303,7 +303,8 @@ SkRegion* DraggableRegionsToSkRegion(
} // namespace } // namespace
NativeWindowMac::NativeWindowMac(content::WebContents* web_contents, NativeWindowMac::NativeWindowMac(
brightray::InspectableWebContents* web_contents,
const mate::Dictionary& options) const mate::Dictionary& options)
: NativeWindow(web_contents, options), : NativeWindow(web_contents, options),
is_kiosk_(false), is_kiosk_(false),
@ -834,9 +835,10 @@ void NativeWindowMac::InstallDraggableRegionView() {
} }
// static // static
NativeWindow* NativeWindow::Create(content::WebContents* web_contents, NativeWindow* NativeWindow::Create(
brightray::InspectableWebContents* inspectable_web_contents,
const mate::Dictionary& options) { const mate::Dictionary& options) {
return new NativeWindowMac(web_contents, options); return new NativeWindowMac(inspectable_web_contents, options);
} }
} // namespace atom } // namespace atom

View file

@ -142,7 +142,8 @@ class NativeWindowClientView : public views::ClientView {
} // namespace } // namespace
NativeWindowViews::NativeWindowViews(content::WebContents* web_contents, NativeWindowViews::NativeWindowViews(
brightray::InspectableWebContents* web_contents,
const mate::Dictionary& options) const mate::Dictionary& options)
: NativeWindow(web_contents, options), : NativeWindow(web_contents, options),
window_(new views::Widget), window_(new views::Widget),
@ -973,9 +974,10 @@ ui::WindowShowState NativeWindowViews::GetRestoredState() {
} }
// static // static
NativeWindow* NativeWindow::Create(content::WebContents* web_contents, NativeWindow* NativeWindow::Create(
brightray::InspectableWebContents* inspectable_web_contents,
const mate::Dictionary& options) { const mate::Dictionary& options) {
return new NativeWindowViews(web_contents, options); return new NativeWindowViews(inspectable_web_contents, options);
} }
} // namespace atom } // namespace atom

View file

@ -28,9 +28,9 @@ class NativeWindowViews : public NativeWindow,
public views::WidgetDelegateView, public views::WidgetDelegateView,
public views::WidgetObserver { public views::WidgetObserver {
public: public:
explicit NativeWindowViews(content::WebContents* web_contents, NativeWindowViews(brightray::InspectableWebContents* inspectable_web_contents,
const mate::Dictionary& options); const mate::Dictionary& options);
virtual ~NativeWindowViews(); ~NativeWindowViews() override;
// NativeWindow: // NativeWindow:
void Close() override; void Close() override;