diff options
author | Zeno Albisser <zeno.albisser@digia.com> | 2013-06-06 17:29:45 +0200 |
---|---|---|
committer | Zeno Albisser <zeno.albisser@digia.com> | 2013-06-10 15:01:02 +0200 |
commit | 83a2bc6d446387d25fd10164e87627c9d49878eb (patch) | |
tree | 164f0c7cc5b4d8cb195afbf388f628e62eaa1b91 /lib | |
parent | 54ed434fe3ab34ca007bc58111d19196c686ecc0 (diff) |
Replace Shell with WebContentsDelegateQt.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/content_browser_client_qt.cpp | 16 | ||||
-rw-r--r-- | lib/lib.pro | 7 | ||||
-rw-r--r-- | lib/qquickwebcontentsview.cpp | 27 | ||||
-rw-r--r-- | lib/qwebcontentsview.cpp | 26 | ||||
-rw-r--r-- | lib/shell_qt.cpp | 170 | ||||
-rw-r--r-- | lib/web_contents_delegate_qt.cpp | 176 | ||||
-rw-r--r-- | lib/web_contents_delegate_qt.h | 47 |
7 files changed, 266 insertions, 203 deletions
diff --git a/lib/content_browser_client_qt.cpp b/lib/content_browser_client_qt.cpp index 65ef0f75c..8acad3b03 100644 --- a/lib/content_browser_client_qt.cpp +++ b/lib/content_browser_client_qt.cpp @@ -14,6 +14,22 @@ #include "net/base/net_util.h" #include "browser_context_qt.h" #include "web_contents_view_qt.h" +#include "web_contents_delegate_qt.h" + +static GURL GetStartupURL() { + CommandLine* command_line = CommandLine::ForCurrentProcess(); + const CommandLine::StringVector& args = command_line->GetArgs(); + + if (args.empty()) + return GURL("http://www.google.com/"); + + GURL url(args[0]); + if (url.is_valid() && url.has_scheme()) + return url; + + return net::FilePathToFileURL(base::FilePath(args[0])); +} + class BrowserMainPartsQt : public content::BrowserMainParts { diff --git a/lib/lib.pro b/lib/lib.pro index f47717e73..457e4ffdb 100644 --- a/lib/lib.pro +++ b/lib/lib.pro @@ -26,9 +26,9 @@ SOURCES = \ qwebcontentsview.cpp \ render_widget_host_view_qt.cpp \ resource_context_qt.cpp \ - shell_qt.cpp \ web_event_factory.cpp \ - native_view_qt.cpp + native_view_qt.cpp \ + web_contents_delegate_qt.cpp HEADERS = \ backing_store_qt.h \ @@ -41,5 +41,6 @@ HEADERS = \ qwebcontentsview.h \ render_widget_host_view_qt.h \ resource_context_qt.h \ - web_event_factory.h + web_event_factory.h \ + web_contents_delegate_qt.h diff --git a/lib/qquickwebcontentsview.cpp b/lib/qquickwebcontentsview.cpp index db9053b27..54ae779c6 100644 --- a/lib/qquickwebcontentsview.cpp +++ b/lib/qquickwebcontentsview.cpp @@ -50,6 +50,7 @@ #include "content/shell/shell_browser_context.h" #include "content_browser_client_qt.h" +#include "web_contents_delegate_qt.h" #include <QWidget> #include <QUrl> @@ -61,7 +62,7 @@ QQuickWebContentsView* gQuickView = 0; class QQuickWebContentsViewPrivate { public: - scoped_ptr<content::Shell> shell; + scoped_ptr<WebContentsDelegateQt> webContentsDelegate; }; QQuickWebContentsView::QQuickWebContentsView() @@ -73,11 +74,7 @@ QQuickWebContentsView::QQuickWebContentsView() content::gQuickView = this; content::BrowserContext* browser_context = static_cast<ContentBrowserClientQt*>(content::GetContentClient()->browser())->browser_context(); - d->shell.reset(content::Shell::CreateNewWindow(browser_context, - GURL(std::string("http://qt-project.org/")), - NULL, - MSG_ROUTING_NONE, - gfx::Size())); + d->webContentsDelegate.reset(WebContentsDelegateQt::CreateNewWindow(browser_context, GURL(std::string("http://qt-project.org/")), NULL, MSG_ROUTING_NONE, gfx::Size(), this)); } QQuickWebContentsView::~QQuickWebContentsView() @@ -86,7 +83,7 @@ QQuickWebContentsView::~QQuickWebContentsView() QUrl QQuickWebContentsView::url() const { - GURL gurl = d->shell->web_contents()->GetActiveURL(); + GURL gurl = d->webContentsDelegate->web_contents()->GetActiveURL(); return QUrl(QString::fromStdString(gurl.spec())); } @@ -99,24 +96,24 @@ void QQuickWebContentsView::setUrl(const QUrl& url) content::NavigationController::LoadURLParams params(gurl); params.transition_type = content::PageTransitionFromInt(content::PAGE_TRANSITION_TYPED | content::PAGE_TRANSITION_FROM_ADDRESS_BAR); - d->shell->web_contents()->GetController().LoadURLWithParams(params); - d->shell->web_contents()->GetView()->Focus(); + d->webContentsDelegate->web_contents()->GetController().LoadURLWithParams(params); + d->webContentsDelegate->web_contents()->GetView()->Focus(); } void QQuickWebContentsView::goBack() { - d->shell->web_contents()->GetController().GoToOffset(-1); - d->shell->web_contents()->GetView()->Focus(); + d->webContentsDelegate->web_contents()->GetController().GoToOffset(-1); + d->webContentsDelegate->web_contents()->GetView()->Focus(); } void QQuickWebContentsView::goForward() { - d->shell->web_contents()->GetController().GoToOffset(1); - d->shell->web_contents()->GetView()->Focus(); + d->webContentsDelegate->web_contents()->GetController().GoToOffset(1); + d->webContentsDelegate->web_contents()->GetView()->Focus(); } void QQuickWebContentsView::reload() { - d->shell->web_contents()->GetController().Reload(false); - d->shell->web_contents()->GetView()->Focus(); + d->webContentsDelegate->web_contents()->GetController().Reload(false); + d->webContentsDelegate->web_contents()->GetView()->Focus(); } diff --git a/lib/qwebcontentsview.cpp b/lib/qwebcontentsview.cpp index 0d538af82..c30973a48 100644 --- a/lib/qwebcontentsview.cpp +++ b/lib/qwebcontentsview.cpp @@ -50,6 +50,7 @@ #include "content/shell/shell_browser_context.h" #include "content_browser_client_qt.h" +#include "web_contents_delegate_qt.h" #include <QWidget> #include <QUrl> @@ -61,7 +62,7 @@ QWebContentsView* gWidgetView = 0; class QWebContentsViewPrivate { public: - scoped_ptr<content::Shell> shell; + scoped_ptr<WebContentsDelegateQt> webContentsDelegate; }; QWebContentsView::QWebContentsView() @@ -70,14 +71,9 @@ QWebContentsView::QWebContentsView() // Cheap hack to allow getting signals from shell_qt.cpp. Q_ASSERT(!content::gWidgetView); - content::gWidgetView = this; content::BrowserContext* browser_context = static_cast<ContentBrowserClientQt*>(content::GetContentClient()->browser())->browser_context(); - d->shell.reset(content::Shell::CreateNewWindow(browser_context, - GURL(std::string("http://qt-project.org/")), - NULL, - MSG_ROUTING_NONE, - gfx::Size())); + d->webContentsDelegate.reset(WebContentsDelegateQt::CreateNewWindow(browser_context, GURL(std::string("http://qt-project.org/")), NULL, MSG_ROUTING_NONE, gfx::Size(), this)); } QWebContentsView::~QWebContentsView() @@ -93,24 +89,24 @@ void QWebContentsView::load(const QUrl& url) content::NavigationController::LoadURLParams params(gurl); params.transition_type = content::PageTransitionFromInt(content::PAGE_TRANSITION_TYPED | content::PAGE_TRANSITION_FROM_ADDRESS_BAR); - d->shell->web_contents()->GetController().LoadURLWithParams(params); - d->shell->web_contents()->GetView()->Focus(); + d->webContentsDelegate->web_contents()->GetController().LoadURLWithParams(params); + d->webContentsDelegate->web_contents()->GetView()->Focus(); } void QWebContentsView::back() { - d->shell->web_contents()->GetController().GoToOffset(-1); - d->shell->web_contents()->GetView()->Focus(); + d->webContentsDelegate->web_contents()->GetController().GoToOffset(-1); + d->webContentsDelegate->web_contents()->GetView()->Focus(); } void QWebContentsView::forward() { - d->shell->web_contents()->GetController().GoToOffset(1); - d->shell->web_contents()->GetView()->Focus(); + d->webContentsDelegate->web_contents()->GetController().GoToOffset(1); + d->webContentsDelegate->web_contents()->GetView()->Focus(); } void QWebContentsView::reload() { - d->shell->web_contents()->GetController().Reload(false); - d->shell->web_contents()->GetView()->Focus(); + d->webContentsDelegate->web_contents()->GetController().Reload(false); + d->webContentsDelegate->web_contents()->GetView()->Focus(); } diff --git a/lib/shell_qt.cpp b/lib/shell_qt.cpp deleted file mode 100644 index d1da44938..000000000 --- a/lib/shell_qt.cpp +++ /dev/null @@ -1,170 +0,0 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "content/shell/shell.h" - -#include <gdk/gdkkeysyms.h> - -#include "base/logging.h" -#include "base/strings/string_piece.h" -#include "base/utf_string_conversions.h" -#include "content/public/browser/browser_context.h" -#include "content/public/browser/native_web_keyboard_event.h" -#include "content/public/browser/render_view_host.h" -#include "content/public/browser/web_contents.h" -#include "content/public/common/renderer_preferences.h" -#include "content/shell/shell_browser_context.h" -#include "content/shell/shell_content_browser_client.h" - -#include "qquickwebcontentsview.h" -#include "qwebcontentsview.h" -#include "web_contents_view_qt.h" - -#include <QApplication> -#include <QVBoxLayout> -#include <QUrl> - -namespace content { - -extern QWebContentsView* gWidgetView; -extern QQuickWebContentsView* gQuickView; - -void Shell::PlatformInitialize(const gfx::Size& default_window_size) -{ -} - -void Shell::PlatformCleanUp() -{ -} - -void Shell::PlatformEnableUIControl(UIControl control, bool is_enabled) -{ -} - -void Shell::PlatformSetAddressBarURL(const GURL& url) -{ - if (headless_) - return; - - fprintf(stderr, "Set Address to: %s\n", url.spec().c_str()); - if (gWidgetView) - gWidgetView->urlChanged(QUrl(QString::fromStdString(url.spec()))); - else if (gQuickView) - gQuickView->urlChanged(); -} - - -void Shell::PlatformSetIsLoading(bool loading) -{ - // FIXME: we might want to emit some loadStarted signal here or something... -} - -void Shell::PlatformCreateWindow(int width, int height) { - SizeTo(width, height); - - if (headless_) - return; - - if (gWidgetView) { - // The layout is used in PlatformSetContents. - QVBoxLayout* layout = new QVBoxLayout; - gWidgetView->setLayout(layout); - window_ = reinterpret_cast<gfx::NativeWindow>(gWidgetView); - } else if (gQuickView) { - window_ = reinterpret_cast<gfx::NativeWindow>(gQuickView); - } -} - -void Shell::PlatformSetContents() -{ - if (headless_) - return; - - content::RendererPreferences* rendererPrefs = web_contents_->GetMutableRendererPrefs(); - rendererPrefs->use_custom_colors = true; - // Qt returns a flash time (the whole cycle) in ms, chromium expects just the interval in seconds - rendererPrefs->caret_blink_interval = static_cast<double>(qApp->cursorFlashTime())/2000; - web_contents_->GetRenderViewHost()->SyncRendererPrefs(); - - if (gWidgetView) { - WebContentsViewQt* content_view = static_cast<WebContentsViewQt*>(web_contents_->GetView()); - QVBoxLayout* layout = qobject_cast<QVBoxLayout*>(gWidgetView->layout()); - if (layout) - layout->addLayout(content_view->windowContainer()->widget()); - } else if (gQuickView) { - WebContentsViewQt* content_view = static_cast<WebContentsViewQt*>(web_contents_->GetView()); - QQuickItem* windowContainer = content_view->windowContainer()->qQuickItem(); - windowContainer->setParentItem(gQuickView); - } -} - -void Shell::SizeTo(int width, int height) -{ - QT_NOT_YET_IMPLEMENTED -} - -void Shell::PlatformResizeSubViews() -{ - SizeTo(content_width_, content_height_); -} - -void Shell::Close() -{ - if (headless_) { - delete this; - return; - } -} - -void Shell::OnBackButtonClicked(GtkWidget* widget) { } - -void Shell::OnForwardButtonClicked(GtkWidget* widget) { } - -void Shell::OnReloadButtonClicked(GtkWidget* widget) { } - -void Shell::OnStopButtonClicked(GtkWidget* widget) -{ - Stop(); -} - -void Shell::OnURLEntryActivate(GtkWidget* entry) { } - -// Callback for when the main window is destroyed. -gboolean Shell::OnWindowDestroyed(GtkWidget* window) -{ - delete this; - return FALSE; // Don't stop this message. -} - -gboolean Shell::OnCloseWindowKeyPressed(GtkAccelGroup* accel_group, GObject* acceleratable, guint keyval, GdkModifierType modifier) -{ - QT_NOT_YET_IMPLEMENTED - return TRUE; -} - -gboolean Shell::OnNewWindowKeyPressed(GtkAccelGroup* accel_group, GObject* acceleratable, guint keyval, GdkModifierType modifier) -{ - ShellBrowserContext* browser_context = ShellContentBrowserClient::Get()->browser_context(); - Shell::CreateNewWindow(browser_context, GURL(), NULL, MSG_ROUTING_NONE, gfx::Size()); - return TRUE; -} - -gboolean Shell::OnHighlightURLView(GtkAccelGroup* accel_group, GObject* acceleratable, guint keyval, GdkModifierType modifier) -{ - return TRUE; -} - -void Shell::PlatformSetTitle(const string16& title) -{ - if (headless_) - return; - - if (gWidgetView) { - std::string title_utf8 = UTF16ToUTF8(title); - gWidgetView->titleChanged(QString::fromStdString(title_utf8)); - } else if (gQuickView) - gQuickView->titleChanged(); -} - -} // namespace content diff --git a/lib/web_contents_delegate_qt.cpp b/lib/web_contents_delegate_qt.cpp new file mode 100644 index 000000000..2757f59cd --- /dev/null +++ b/lib/web_contents_delegate_qt.cpp @@ -0,0 +1,176 @@ +#include "web_contents_delegate_qt.h" + +#include "content/public/browser/web_contents.h" +#include "content/public/browser/navigation_controller.h" +#include "web_contents_view_qt.h" +#include "qwebcontentsview.h" +#include "qquickwebcontentsview.h" + +#include <QWidget> +#include <QVBoxLayout> +#include <QToolButton> +#include <QLineEdit> +#include <QQuickView> +#include <QApplication> + +static const int kTestWindowWidth = 800; +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) +{ + // 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(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) +{ + 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); +} + +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; +} + +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; +} + +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 + rendererPrefs->caret_blink_interval = static_cast<double>(qApp->cursorFlashTime())/2000; + 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); + m_webContents->GetView()->Focus(); +} + +void WebContentsDelegateQt::Reload() { + m_webContents->GetController().Reload(false); + m_webContents->GetView()->Focus(); +} + +void WebContentsDelegateQt::Stop() { + m_webContents->Stop(); + m_webContents->GetView()->Focus(); +} + +void WebContentsDelegateQt::LoadURL(const GURL& url) +{ + LoadURLForFrame(url, std::string()); +} + +void WebContentsDelegateQt::LoadURLForFrame(const GURL& url, const std::string& frame_name) { + content::NavigationController::LoadURLParams params(url); + params.transition_type = content::PageTransitionFromInt(content::PAGE_TRANSITION_TYPED | content::PAGE_TRANSITION_FROM_ADDRESS_BAR); + params.frame_name = frame_name; + m_webContents->GetController().LoadURLWithParams(params); + m_webContents->GetView()->Focus(); +} + +void WebContentsDelegateQt::Observe(int, const content::NotificationSource&, const content::NotificationDetails&) +{ + // IMPLEMENT THIS!!!! +} diff --git a/lib/web_contents_delegate_qt.h b/lib/web_contents_delegate_qt.h new file mode 100644 index 000000000..92a85feaf --- /dev/null +++ b/lib/web_contents_delegate_qt.h @@ -0,0 +1,47 @@ +#ifndef WEB_CONTENTS_DELEGATE_QT +#define WEB_CONTENTS_DELEGATE_QT + +#include "content/public/browser/notification_observer.h" +#include "content/public/browser/web_contents_delegate.h" + +namespace content { + class BrowserContext; + class SiteInstance; +} + +class QWebContentsView; +class QQuickWebContentsView; + +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*); + content::WebContents* web_contents(); + void LoadURL(const GURL&); + void LoadURLForFrame(const GURL&, const std::string& frame_name); + void GoBackOrForward(int offset); + void Reload(); + void Stop(); + + 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(); + + QWebContentsView* m_contentsView; + QQuickWebContentsView* m_quickContentsView; + scoped_ptr<content::WebContents> m_webContents; + + static std::vector<WebContentsDelegateQt*> m_windows; +}; + +#endif |