diff --git a/lib/lib.pro b/lib/lib.pro index f86bf8ac65198ccb6c05d230a9757d199b9f7adb..92b5e61b6c329a5b9c7dc8811652af82d2580af9 100644 --- a/lib/lib.pro +++ b/lib/lib.pro @@ -36,5 +36,6 @@ HEADERS = \ resource_context_qt.h \ url_request_context_getter_qt.h \ web_contents_delegate_qt.h \ + web_contents_view_qt.h \ web_engine_context.h diff --git a/lib/qquickwebcontentsview.cpp b/lib/qquickwebcontentsview.cpp index 06ba95b793d9e05c9beec5626dde6e1baca6e1b2..154f4ea5e4ffaace1bc064e1be1ccef10a5d2e18 100644 --- a/lib/qquickwebcontentsview.cpp +++ b/lib/qquickwebcontentsview.cpp @@ -51,9 +51,9 @@ #include "browser_context_qt.h" #include "content_browser_client_qt.h" #include "web_contents_delegate_qt.h" +#include "web_contents_view_qt.h" #include "web_engine_context.h" -#include <QWidget> #include <QUrl> void QQuickWebContentsView::registerType() @@ -76,7 +76,10 @@ QQuickWebContentsView::QQuickWebContentsView() d->context = WebEngineContext::current(); content::BrowserContext* browser_context = static_cast<ContentBrowserClientQt*>(content::GetContentClient()->browser())->browser_context(); - d->webContentsDelegate.reset(WebContentsDelegateQt::CreateNewWindow(browser_context, GURL(std::string("http://qt-project.org/")), NULL, MSG_ROUTING_NONE, gfx::Size(), this)); + d->webContentsDelegate.reset(WebContentsDelegateQt::CreateNewWindow(browser_context, GURL(std::string("http://qt-project.org/")), NULL, MSG_ROUTING_NONE, gfx::Size())); + WebContentsViewQt* content_view = static_cast<WebContentsViewQt*>(d->webContentsDelegate->web_contents()->GetView()); + QQuickItem* windowContainer = content_view->windowContainer()->qQuickItem(); + windowContainer->setParentItem(this); } QQuickWebContentsView::~QQuickWebContentsView() diff --git a/lib/qwebcontentsview.cpp b/lib/qwebcontentsview.cpp index 1f23fb0d184859528c5bd8cb1a59f10f2780e388..35eed18237b0aa35b7f643404e4d2df61351d5b2 100644 --- a/lib/qwebcontentsview.cpp +++ b/lib/qwebcontentsview.cpp @@ -51,8 +51,10 @@ #include "browser_context_qt.h" #include "content_browser_client_qt.h" #include "web_contents_delegate_qt.h" +#include "web_contents_view_qt.h" #include "web_engine_context.h" +#include <QVBoxLayout> #include <QWidget> #include <QUrl> @@ -70,7 +72,12 @@ QWebContentsView::QWebContentsView() d->context = WebEngineContext::current(); content::BrowserContext* browser_context = static_cast<ContentBrowserClientQt*>(content::GetContentClient()->browser())->browser_context(); - d->webContentsDelegate.reset(WebContentsDelegateQt::CreateNewWindow(browser_context, GURL(std::string("http://qt-project.org/")), NULL, MSG_ROUTING_NONE, gfx::Size(), this)); + d->webContentsDelegate.reset(WebContentsDelegateQt::CreateNewWindow(browser_context, GURL(std::string("http://qt-project.org/")), NULL, MSG_ROUTING_NONE, gfx::Size())); + QVBoxLayout *layout = new QVBoxLayout; + setLayout(layout); + + WebContentsViewQt* content_view = static_cast<WebContentsViewQt*>(d->webContentsDelegate->web_contents()->GetView()); + layout->addLayout(content_view->windowContainer()->widget()); } QWebContentsView::~QWebContentsView() diff --git a/lib/web_contents_delegate_qt.cpp b/lib/web_contents_delegate_qt.cpp index 646728fa514d1cedad1be208c8c5c8338d599c9c..db470c7e4664f19c34034eae5ce5f6e442b75cfa 100644 --- a/lib/web_contents_delegate_qt.cpp +++ b/lib/web_contents_delegate_qt.cpp @@ -61,10 +61,10 @@ static const int kTestWindowHeight = 600; std::vector<WebContentsDelegateQt*> WebContentsDelegateQt::m_windows; -WebContentsDelegateQt::WebContentsDelegateQt(content::WebContents* web_contents, QWebContentsView* contentsView) - : m_contentsView(contentsView) - , m_quickContentsView(NULL) +WebContentsDelegateQt::WebContentsDelegateQt(content::WebContents* web_contents) + : m_webContents(web_contents) { + m_webContents->SetDelegate(this); // const CommandLine& command_line = *CommandLine::ForCurrentProcess(); // registrar_.Add(this, NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED, // Source<WebContents>(web_contents)); @@ -76,74 +76,28 @@ WebContentsDelegateQt::WebContentsDelegateQt(content::WebContents* web_contents, // } } -WebContentsDelegateQt::WebContentsDelegateQt(content::WebContents* web_contents, QQuickWebContentsView* contentsView) - : m_contentsView(NULL) - , m_quickContentsView(contentsView) -{ - // const CommandLine& command_line = *CommandLine::ForCurrentProcess(); - // registrar_.Add(this, NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED, - // Source<WebContents>(web_contents)); - m_windows.push_back(this); - - // if (!shell_created_callback_.is_null()) { - // shell_created_callback_.Run(this); - // shell_created_callback_.Reset(); - // } -} - -WebContentsDelegateQt* WebContentsDelegateQt::commonCreate(content::WebContents* web_contents, const gfx::Size& initial_size, WebContentsDelegateQt* delegate) -{ - delegate->PlatformCreateWindow(initial_size.width(), initial_size.height()); - - delegate->m_webContents.reset(web_contents); - web_contents->SetDelegate(delegate); - - delegate->PlatformSetContents(); - delegate->PlatformResizeSubViews(); - - return delegate; -} - -content::WebContents* commonCreateWebContents(content::WebContents::CreateParams& create_params, int routing_id, const gfx::Size& initial_size) +WebContentsDelegateQt* WebContentsDelegateQt::CreateNewWindow(content::BrowserContext* browser_context, const GURL& url, content::SiteInstance* site_instance, int routing_id, const gfx::Size& initial_size) { + content::WebContents::CreateParams create_params(browser_context, site_instance); create_params.routing_id = routing_id; if (!initial_size.IsEmpty()) create_params.initial_size = initial_size; else create_params.initial_size = gfx::Size(kTestWindowWidth, kTestWindowHeight); - return content::WebContents::Create(create_params); -} - -WebContentsDelegateQt* WebContentsDelegateQt::Create(content::WebContents* web_contents, const gfx::Size& initial_size, QWebContentsView* contentsView) -{ - WebContentsDelegateQt* delegate = new WebContentsDelegateQt(web_contents, contentsView); - return commonCreate(web_contents, initial_size, delegate); -} - -WebContentsDelegateQt* WebContentsDelegateQt::Create(content::WebContents* web_contents, const gfx::Size& initial_size, QQuickWebContentsView* contentsView) -{ - WebContentsDelegateQt* delegate = new WebContentsDelegateQt(web_contents, contentsView); - return commonCreate(web_contents, initial_size, delegate); -} + content::WebContents::Create(create_params); + content::WebContents* web_contents = content::WebContents::Create(create_params); + WebContentsDelegateQt* delegate = new WebContentsDelegateQt(web_contents); -WebContentsDelegateQt* WebContentsDelegateQt::CreateNewWindow(content::BrowserContext* browser_context, const GURL& url, content::SiteInstance* site_instance, int routing_id, const gfx::Size& initial_size, QWebContentsView* contentsView) -{ - content::WebContents::CreateParams create_params(browser_context, site_instance); - content::WebContents* web_contents = commonCreateWebContents(create_params, routing_id, initial_size); - WebContentsDelegateQt* contentsDelegate = Create(web_contents, create_params.initial_size, contentsView); - if (!url.is_empty()) - contentsDelegate->LoadURL(url); - return contentsDelegate; -} + content::RendererPreferences* rendererPrefs = delegate->m_webContents->GetMutableRendererPrefs(); + rendererPrefs->use_custom_colors = true; + // Qt returns a flash time (the whole cycle) in ms, chromium expects just the interval in seconds + const int qtCursorFlashTime = QGuiApplication::styleHints()->cursorFlashTime(); + rendererPrefs->caret_blink_interval = 0.5 * static_cast<double>(qtCursorFlashTime) / 1000; + delegate->m_webContents->GetRenderViewHost()->SyncRendererPrefs(); -WebContentsDelegateQt* WebContentsDelegateQt::CreateNewWindow(content::BrowserContext* browser_context, const GURL& url, content::SiteInstance* site_instance, int routing_id, const gfx::Size& initial_size, QQuickWebContentsView* contentsView) -{ - content::WebContents::CreateParams create_params(browser_context, site_instance); - content::WebContents* web_contents = commonCreateWebContents(create_params, routing_id, initial_size); - WebContentsDelegateQt* contentsDelegate = Create(web_contents, create_params.initial_size, contentsView); if (!url.is_empty()) - contentsDelegate->LoadURL(url); - return contentsDelegate; + delegate->LoadURL(url); + return delegate; } content::WebContents* WebContentsDelegateQt::web_contents() @@ -151,40 +105,6 @@ content::WebContents* WebContentsDelegateQt::web_contents() return m_webContents.get(); } -void WebContentsDelegateQt::PlatformCreateWindow(int width, int height) -{ - if (m_contentsView) { - // The layout is used in PlatformSetContents. - QVBoxLayout* layout = new QVBoxLayout; - m_contentsView->setLayout(layout); - } -} - -void WebContentsDelegateQt::PlatformSetContents() -{ - content::RendererPreferences* rendererPrefs = m_webContents->GetMutableRendererPrefs(); - rendererPrefs->use_custom_colors = true; - // Qt returns a flash time (the whole cycle) in ms, chromium expects just the interval in seconds - const int qtCursorFlashTime = QGuiApplication::styleHints()->cursorFlashTime(); - rendererPrefs->caret_blink_interval = 0.5 * static_cast<double>(qtCursorFlashTime) / 1000; - m_webContents->GetRenderViewHost()->SyncRendererPrefs(); - - if (m_contentsView) { - WebContentsViewQt* content_view = static_cast<WebContentsViewQt*>(m_webContents->GetView()); - QVBoxLayout* layout = qobject_cast<QVBoxLayout*>(m_contentsView->layout()); - if (layout) - layout->addLayout(content_view->windowContainer()->widget()); - } else if (m_quickContentsView) { - WebContentsViewQt* content_view = static_cast<WebContentsViewQt*>(m_webContents->GetView()); - QQuickItem* windowContainer = content_view->windowContainer()->qQuickItem(); - windowContainer->setParentItem(m_quickContentsView); - } -} - -void WebContentsDelegateQt::PlatformResizeSubViews() -{ - -} void WebContentsDelegateQt::GoBackOrForward(int offset) { m_webContents->GetController().GoToOffset(offset); diff --git a/lib/web_contents_delegate_qt.h b/lib/web_contents_delegate_qt.h index 9042bd47086671db8cef0fc7b0dd1b530fbbbf4a..4f289269ebcdec34ee01454153324c23eef96166 100644 --- a/lib/web_contents_delegate_qt.h +++ b/lib/web_contents_delegate_qt.h @@ -45,6 +45,7 @@ #include "content/public/browser/notification_observer.h" #include "content/public/browser/web_contents_delegate.h" + namespace content { class BrowserContext; class SiteInstance; @@ -57,10 +58,7 @@ class WebContentsDelegateQt : public content::WebContentsDelegate , public content::NotificationObserver { public: - static WebContentsDelegateQt* Create(content::WebContents*, const gfx::Size& initial_size, QWebContentsView*); - static WebContentsDelegateQt* Create(content::WebContents*, const gfx::Size& initial_size, QQuickWebContentsView*); - static WebContentsDelegateQt* CreateNewWindow(content::BrowserContext*, const GURL&, content::SiteInstance*, int routing_id, const gfx::Size& initial_size, QWebContentsView*); - static WebContentsDelegateQt* CreateNewWindow(content::BrowserContext*, const GURL&, content::SiteInstance*, int routing_id, const gfx::Size& initial_size, QQuickWebContentsView*); + static WebContentsDelegateQt* CreateNewWindow(content::BrowserContext*, const GURL&, content::SiteInstance*, int routing_id, const gfx::Size& initial_size); content::WebContents* web_contents(); void LoadURL(const GURL&); void LoadURLForFrame(const GURL&, const std::string& frame_name); @@ -71,15 +69,8 @@ public: virtual void Observe(int, const content::NotificationSource&, const content::NotificationDetails&); private: - static WebContentsDelegateQt* commonCreate(content::WebContents* web_contents, const gfx::Size& initial_size, WebContentsDelegateQt* delegate); - WebContentsDelegateQt(content::WebContents*, QWebContentsView*); - WebContentsDelegateQt(content::WebContents*, QQuickWebContentsView*); - void PlatformCreateWindow(int width, int height); - void PlatformSetContents(); - void PlatformResizeSubViews(); + WebContentsDelegateQt(content::WebContents*); - QWebContentsView* m_contentsView; - QQuickWebContentsView* m_quickContentsView; scoped_ptr<content::WebContents> m_webContents; static std::vector<WebContentsDelegateQt*> m_windows; diff --git a/shared/native_view_container_qt.h b/shared/native_view_container_qt.h index 6937d146d95a6ce759c207a89a9d307c61dbadc9..fbb29ee85922f3540e9e63d8e50168f7b0248dd2 100644 --- a/shared/native_view_container_qt.h +++ b/shared/native_view_container_qt.h @@ -15,7 +15,7 @@ public: : m_embeddable(0) , m_currentQQuickNativeView(0) , m_currentQWidgetNativeView(0) - , m_isQQuick(false) + , m_isQQuick(!qgetenv("QQUICKWEBENGINE").isNull()) { }