diff options
author | Jocelyn Turcotte <jocelyn.turcotte@digia.com> | 2013-06-11 15:40:30 +0200 |
---|---|---|
committer | Jocelyn Turcotte <jocelyn.turcotte@digia.com> | 2013-06-11 15:51:23 +0200 |
commit | e67c21a033121618bfe4cf62686d009d0bacd8c3 (patch) | |
tree | 74675f8f098fdcb2352770b15bb8c24194f4acd4 /lib | |
parent | e80719ed2714779cd4afac099916be069742fe77 (diff) |
Get rid of BlinqApplication.
Rename the class to WebEngineContext and keep it as a ref-counted member
of pages instead. Also:
- Change the user-agent product to QtWebEngine
- Don't pass actual command line arguments to Chromium anymore
- Allow attaching to the event loop through Before/AfterRun instead of blocking
Diffstat (limited to 'lib')
-rw-r--r-- | lib/lib.pro | 10 | ||||
-rw-r--r-- | lib/qquickwebcontentsview.cpp | 10 | ||||
-rw-r--r-- | lib/qquickwebcontentsview.h | 2 | ||||
-rw-r--r-- | lib/qwebcontentsview.cpp | 4 | ||||
-rw-r--r-- | lib/web_engine_context.cpp (renamed from lib/blinqapplication.cpp) | 83 | ||||
-rw-r--r-- | lib/web_engine_context.h (renamed from lib/blinqapplication.h) | 23 |
6 files changed, 85 insertions, 47 deletions
diff --git a/lib/lib.pro b/lib/lib.pro index d8c47ef88..4db81a6f8 100644 --- a/lib/lib.pro +++ b/lib/lib.pro @@ -20,23 +20,21 @@ CONFIG(release, debug|release): DEFINES += NDEBUG QT += widgets quick SOURCES = \ - blinqapplication.cpp \ content_browser_client_qt.cpp \ qquickwebcontentsview.cpp \ qwebcontentsview.cpp \ resource_context_qt.cpp \ url_request_context_getter_qt.cpp \ - web_contents_delegate_qt.cpp - web_event_factory.cpp + web_contents_delegate_qt.cpp \ + web_engine_context.cpp HEADERS = \ - blinqapplication.h \ browser_context_qt.h \ content_browser_client_qt.h \ qquickwebcontentsview.h \ qwebcontentsview.h \ resource_context_qt.h \ url_request_context_getter_qt.h \ - web_contents_delegate_qt.h - web_event_factory.h + web_contents_delegate_qt.h \ + web_engine_context.h diff --git a/lib/qquickwebcontentsview.cpp b/lib/qquickwebcontentsview.cpp index 469b030fd..06ba95b79 100644 --- a/lib/qquickwebcontentsview.cpp +++ b/lib/qquickwebcontentsview.cpp @@ -51,19 +51,29 @@ #include "browser_context_qt.h" #include "content_browser_client_qt.h" #include "web_contents_delegate_qt.h" +#include "web_engine_context.h" #include <QWidget> #include <QUrl> +void QQuickWebContentsView::registerType() +{ + // FIXME: Do a proper plugin. + qmlRegisterType<QQuickWebContentsView>("QtWebEngine", 1, 0, "WebContentsView"); +} + class QQuickWebContentsViewPrivate { public: + scoped_refptr<WebEngineContext> context; scoped_ptr<WebContentsDelegateQt> webContentsDelegate; }; QQuickWebContentsView::QQuickWebContentsView() { d.reset(new QQuickWebContentsViewPrivate); + // This has to be the first thing we do. + 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)); diff --git a/lib/qquickwebcontentsview.h b/lib/qquickwebcontentsview.h index 809bbf25e..9e9979b0a 100644 --- a/lib/qquickwebcontentsview.h +++ b/lib/qquickwebcontentsview.h @@ -51,6 +51,8 @@ class Q_DECL_EXPORT QQuickWebContentsView : public QQuickItem { Q_OBJECT Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged) public: + static void registerType(); + QQuickWebContentsView(); ~QQuickWebContentsView(); diff --git a/lib/qwebcontentsview.cpp b/lib/qwebcontentsview.cpp index 64bdb9c36..1f23fb0d1 100644 --- a/lib/qwebcontentsview.cpp +++ b/lib/qwebcontentsview.cpp @@ -51,6 +51,7 @@ #include "browser_context_qt.h" #include "content_browser_client_qt.h" #include "web_contents_delegate_qt.h" +#include "web_engine_context.h" #include <QWidget> #include <QUrl> @@ -58,12 +59,15 @@ class QWebContentsViewPrivate { public: + scoped_refptr<WebEngineContext> context; scoped_ptr<WebContentsDelegateQt> webContentsDelegate; }; QWebContentsView::QWebContentsView() { d.reset(new QWebContentsViewPrivate); + // This has to be the first thing we do. + 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)); diff --git a/lib/blinqapplication.cpp b/lib/web_engine_context.cpp index 732b7d1c7..5b9855a27 100644 --- a/lib/blinqapplication.cpp +++ b/lib/web_engine_context.cpp @@ -39,7 +39,7 @@ ** ****************************************************************************/ -#include "blinqapplication.h" +#include "web_engine_context.h" #include <math.h> @@ -57,10 +57,13 @@ #include "webkit/common/user_agent/user_agent_util.h" #include "content_browser_client_qt.h" -#include "qquickwebcontentsview.h" +#include <QCoreApplication> +#include <QStringList> namespace { +static WebEngineContext* sContext = 0; + static inline base::FilePath::StringType qStringToStringType(const QString &str) { #if defined(OS_POSIX) @@ -70,7 +73,7 @@ static inline base::FilePath::StringType qStringToStringType(const QString &str) #endif } -static QByteArray blinqProcessPath() { +static QByteArray subProcessPath() { static bool initialized = false; #ifdef BLINQ_PROCESS_PATH static QByteArray processPath(BLINQ_PROCESS_PATH); @@ -95,7 +98,7 @@ static void initializeBlinkPaths() if (initialized) return; - PathService::Override(content::CHILD_PROCESS_EXE, base::FilePath(qStringToStringType(QString(blinqProcessPath())))); + PathService::Override(content::CHILD_PROCESS_EXE, base::FilePath(qStringToStringType(QString(subProcessPath())))); } // Return a timeout suitable for the glib loop, -1 to block forever, @@ -114,29 +117,27 @@ int GetTimeIntervalMilliseconds(const base::TimeTicks& from) { return delay < 0 ? 0 : delay; } -class MessagePump : public QObject, +class MessagePumpForUIQt : public QObject, public base::MessagePump { public: - MessagePump() - : m_delegate(0) + MessagePumpForUIQt() + // Usually this gets passed through Run, but since we have + // our own event loop, attach it explicitely ourselves. + : m_delegate(base::MessageLoopForUI::current()) { } virtual void Run(Delegate *delegate) { - // It would be possible to do like the Android message loop and use - // Start(Delegate*) instead of Run to avoid blocking, but we still - // need to grab the command line arguments, so keep it simple for now - // by forcing the use of BlinqApplication. - m_delegate = delegate; - QApplication::exec(); - m_delegate = 0; + // FIXME: This could be needed if we want to run Chromium tests. + // We could run a QEventLoop here. + Q_ASSERT(false); } virtual void Quit() { - QCoreApplication::instance()->quit(); + Q_ASSERT(false); } virtual void ScheduleWork() @@ -189,7 +190,7 @@ private: base::MessagePump* messagePumpFactory() { - return new MessagePump; + return new MessagePumpForUIQt; } class ContentMainDelegateQt : public content::ContentMainDelegate @@ -207,31 +208,31 @@ private: } -BlinqApplication::BlinqApplication(int &argc, char **argv) - : QApplication(argc, argv) +WebEngineContext::WebEngineContext() { - { - int myArgc = argc + 3; - const char **myArgv = new const char *[myArgc]; + Q_ASSERT(!sContext); + sContext = this; - for (int i = 0; i < argc; ++i) - myArgv[i] = argv[i]; + { QByteArray subProcessPathOption("--browser-subprocess-path="); - subProcessPathOption.append(blinqProcessPath()); - myArgv[argc] = subProcessPathOption.constData(); - myArgv[argc + 1] = "--no-sandbox"; + subProcessPathOption.append(subProcessPath()); - std::string ua = webkit_glue::BuildUserAgentFromProduct("Qrome/0.1"); + std::string ua = webkit_glue::BuildUserAgentFromProduct("QtWebEngine/0.1"); QByteArray userAgentParameter("--user-agent="); userAgentParameter.append(QString::fromStdString(ua).toUtf8()); - myArgv[argc + 2] = userAgentParameter.constData(); - CommandLine::Init(myArgc, myArgv); + const int argc = 4; + const char* argv[4]; + argv[0] = QCoreApplication::arguments()[0].toLatin1().constData(); + argv[1] = subProcessPathOption.constData(); + argv[2] = "--no-sandbox"; + argv[3] = userAgentParameter.constData(); - delete [] myArgv; + CommandLine::Init(argc, argv); } + // This needs to be set before the MessageLoop is created by BrowserMainRunner. base::MessageLoop::InitMessagePumpForUIFactory(::messagePumpFactory); static content::ContentMainRunner *runner = 0; @@ -253,12 +254,24 @@ BlinqApplication::BlinqApplication(int &argc, char **argv) base::ThreadRestrictions::SetIOAllowed(true); - // FIXME: Do a proper plugin. - qmlRegisterType<QQuickWebContentsView>("QtWebEngine", 1, 0, "WebContentsView"); + // Once the MessageLoop has been created, attach a top-level RunLoop. + m_runLoop.reset(new base::RunLoop); + m_runLoop->BeforeRun(); +} + +WebEngineContext::~WebEngineContext() +{ + m_runLoop->AfterRun(); + + Q_ASSERT(sContext == this); + sContext = 0; } -int BlinqApplication::exec() +scoped_refptr<WebEngineContext> WebEngineContext::current() { - base::RunLoop runLoop; - runLoop.Run(); + scoped_refptr<WebEngineContext> current = sContext; + if (!current) + current = new WebEngineContext; + Q_ASSERT(sContext == current); + return current; } diff --git a/lib/blinqapplication.h b/lib/web_engine_context.h index 68034e613..2bf20ac4b 100644 --- a/lib/blinqapplication.h +++ b/lib/web_engine_context.h @@ -39,15 +39,26 @@ ** ****************************************************************************/ -#ifndef BLINQAPPLICATION_H -#define BLINQAPPLICATION_H +#ifndef WEB_ENGINE_CONTEXT_H +#define WEB_ENGINE_CONTEXT_H -#include <QApplication> +#include "base/memory/ref_counted.h" +#include "base/memory/scoped_ptr.h" -class Q_DECL_EXPORT BlinqApplication : public QApplication { +namespace base { +class RunLoop; +} + +class WebEngineContext : public base::RefCounted<WebEngineContext> { public: - BlinqApplication(int &argc, char **argv); - static int exec(); + static scoped_refptr<WebEngineContext> current(); + +private: + friend class base::RefCounted<WebEngineContext>; + WebEngineContext(); + ~WebEngineContext(); + + scoped_ptr<base::RunLoop> m_runLoop; }; #endif |