From 43ccfd1f398c06556340dbbbcbaaf68ee36ca1b4 Mon Sep 17 00:00:00 2001
From: Pierre Rossi <pierre.rossi@digia.com>
Date: Fri, 31 May 2013 16:01:09 +0200
Subject: [PATCH] Stop relying on ShellBrowserClient

We now have our own minimalistic subclass, which still makes use of
some shell parts.
---
 lib/blinqapplication.cpp          | 15 +-----
 lib/blinqpage.cpp                 | 81 ++-----------------------------
 lib/browser_context_qt.h          | 73 ++++++++++++++++++++++++++++
 lib/content_browser_client_qt.cpp | 41 ++++++++++++++++
 lib/content_browser_client_qt.h   | 38 +++++++++++++++
 lib/lib.pro                       |  9 +++-
 lib/resource_context_qt.cpp       | 25 ++++++++++
 lib/resource_context_qt.h         | 33 +++++++++++++
 8 files changed, 223 insertions(+), 92 deletions(-)
 create mode 100644 lib/browser_context_qt.h
 create mode 100644 lib/content_browser_client_qt.cpp
 create mode 100644 lib/content_browser_client_qt.h
 create mode 100644 lib/resource_context_qt.cpp
 create mode 100644 lib/resource_context_qt.h

diff --git a/lib/blinqapplication.cpp b/lib/blinqapplication.cpp
index 5eeddf026..49e6c8c4e 100644
--- a/lib/blinqapplication.cpp
+++ b/lib/blinqapplication.cpp
@@ -54,10 +54,9 @@
 #include "content/public/browser/browser_main_runner.h"
 #include "content/public/common/content_paths.h"
 #include "content/public/common/main_function_params.h"
-#include "content/shell/shell_browser_context.h"
-#include "content/shell/shell_content_browser_client.h"
 #include "webkit/common/user_agent/user_agent_util.h"
 
+#include "content_browser_client_qt.h"
 #include "web_contents_view_qt.h"
 
 namespace {
@@ -193,18 +192,6 @@ base::MessagePump* messagePumpFactory()
     return new MessagePump;
 }
 
-class ContentBrowserClientQt : public content::ShellContentBrowserClient
-{
-public:
-    virtual content::WebContentsViewPort* OverrideCreateWebContentsView(content::WebContents* web_contents, content::RenderViewHostDelegateView** render_view_host_delegate_view)
-    {
-      fprintf(stderr, "OverrideCreateWebContentsView\n");
-        WebContentsViewQt* rv = new WebContentsViewQt(web_contents);
-        *render_view_host_delegate_view = rv;
-        return rv;
-    }
-};
-
 class ContentMainDelegateQt : public content::ContentMainDelegate
 {
 public:
diff --git a/lib/blinqpage.cpp b/lib/blinqpage.cpp
index 5d01503a9..84223fc2b 100644
--- a/lib/blinqpage.cpp
+++ b/lib/blinqpage.cpp
@@ -45,13 +45,9 @@
 #define CONTENT_IMPLEMENTATION
 
 #include "content/public/browser/web_contents.h"
-#include "content/public/browser/browser_context.h"
-#include "content/public/browser/resource_context.h"
-#include "content/public/browser/storage_partition.h"
 #include "content/public/browser/notification_service.h"
 #include "content/public/browser/web_contents_view.h"
 #include "net/url_request/url_request_context_getter.h"
-#include "base/files/scoped_temp_dir.h"
 #include "base/event_types.h"
 #include "ui/gfx/insets.h"
 #include "ui/gfx/screen.h"
@@ -63,12 +59,14 @@
 #include "content/browser/renderer_host/render_view_host_factory.h"
 #include "content/browser/renderer_host/render_view_host_impl.h"
 #include "content/browser/renderer_host/backing_store.h"
-#include "content/shell/shell_browser_context.h"
-#include "content/shell/shell_content_browser_client.h"
 #include "skia/ext/platform_canvas.h"
 
 #include "backing_store_qt.h"
+#include "browser_context_qt.h"
+#include "content_browser_client_qt.h"
+#include "raster_window.h"
 #include "render_widget_host_view_qt.h"
+#include "resource_context_qt.h"
 
 #include <QByteArray>
 #include <QWindow>
@@ -81,75 +79,6 @@
 
 namespace {
 
-class Context;
-
-class ResourceContext : public content::ResourceContext
-{
-public:
-    ResourceContext(Context *ctx)
-        : context(ctx)
-    {}
-
-    virtual net::HostResolver* GetHostResolver() { return 0; }
-
-    inline virtual net::URLRequestContext* GetRequestContext();
-
-private:
-    Context *context;
-};
-
-class Context : public content::BrowserContext
-{
-public:
-   Context()
-   {
-       tempBasePath.CreateUniqueTempDir();
-       resourceContext.reset(new ResourceContext(this));
-   }
-   virtual ~Context() {}
-
-   virtual base::FilePath GetPath()
-   {
-       return tempBasePath.path();
-   }
-
-   virtual bool IsOffTheRecord() const
-   {
-       return false;
-   }
-
-   virtual net::URLRequestContextGetter* GetRequestContext()
-   {
-       return GetDefaultStoragePartition(this)->GetURLRequestContext();
-   }
-   virtual net::URLRequestContextGetter* GetRequestContextForRenderProcess(int) { return GetRequestContext(); }
-   virtual net::URLRequestContextGetter* GetMediaRequestContext() { return GetRequestContext(); }
-   virtual net::URLRequestContextGetter* GetMediaRequestContextForRenderProcess(int) { return GetRequestContext(); }
-   virtual net::URLRequestContextGetter* GetMediaRequestContextForStoragePartition(const base::FilePath&, bool) { return GetRequestContext(); }
-
-   virtual content::ResourceContext* GetResourceContext()
-   {
-       return resourceContext.get();
-   }
-
-   virtual content::DownloadManagerDelegate* GetDownloadManagerDelegate() { return 0; }
-   virtual content::GeolocationPermissionContext* GetGeolocationPermissionContext() { return 0; }
-   virtual content::SpeechRecognitionPreferences* GetSpeechRecognitionPreferences() { return 0; }
-   virtual quota::SpecialStoragePolicy* GetSpecialStoragePolicy() { return 0; }
-
-private:
-    scoped_ptr<content::ResourceContext> resourceContext;
-    base::ScopedTempDir tempBasePath; // ### Should become permanent location.
-
-    DISALLOW_COPY_AND_ASSIGN(Context);
-};
-
-inline net::URLRequestContext* ResourceContext::GetRequestContext()
-{
-    return context->GetRequestContext()->GetURLRequestContext();
-}
-
-
 class RenderViewHost : public content::RenderViewHostImpl
 {
 public:
@@ -179,7 +108,7 @@ BlinqPage::BlinqPage()
 {
     d.reset(new BlinqPagePrivate);
 
-    d->context.reset(static_cast<content::ShellContentBrowserClient*>(content::GetContentClient()->browser())->browser_context());
+    d->context.reset(static_cast<ContentBrowserClientQt*>(content::GetContentClient()->browser())->browser_context());
     content::WebContents::CreateParams p(d->context.get());
 //    d->contents.reset(content::WebContents::Create(p));
 
diff --git a/lib/browser_context_qt.h b/lib/browser_context_qt.h
new file mode 100644
index 000000000..79eb13f1d
--- /dev/null
+++ b/lib/browser_context_qt.h
@@ -0,0 +1,73 @@
+#include "content/public/browser/browser_context.h"
+
+#include "base/files/scoped_temp_dir.h"
+#include "base/time.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/content_browser_client.h"
+#include "content/public/browser/resource_context.h"
+#include "content/public/browser/storage_partition.h"
+#include "net/url_request/url_request_context_getter.h"
+
+#include "content/shell/shell_url_request_context_getter.h"
+
+#include "resource_context_qt.h"
+
+#ifndef BROWSER_CONTEXT_QT
+#define BROWSER_CONTEXT_QT
+
+class BrowserContextQt : public content::BrowserContext
+{
+public:
+   explicit BrowserContextQt()
+   {
+       tempBasePath.CreateUniqueTempDir();
+       resourceContext.reset(new ResourceContextQt(this));
+   }
+   virtual ~BrowserContextQt() {}
+
+   virtual base::FilePath GetPath()
+   {
+       return tempBasePath.path();
+   }
+
+   virtual bool IsOffTheRecord() const
+   {
+       return false;
+   }
+
+   virtual net::URLRequestContextGetter* GetRequestContext()
+   {
+       return GetDefaultStoragePartition(this)->GetURLRequestContext();
+   }
+   virtual net::URLRequestContextGetter* GetRequestContextForRenderProcess(int) { return GetRequestContext(); }
+   virtual net::URLRequestContextGetter* GetMediaRequestContext() { return GetRequestContext(); }
+   virtual net::URLRequestContextGetter* GetMediaRequestContextForRenderProcess(int) { return GetRequestContext(); }
+   virtual net::URLRequestContextGetter* GetMediaRequestContextForStoragePartition(const base::FilePath&, bool) { return GetRequestContext(); }
+
+   virtual content::ResourceContext* GetResourceContext()
+   {
+       return resourceContext.get();
+   }
+
+   virtual content::DownloadManagerDelegate* GetDownloadManagerDelegate() { return 0; }
+   virtual content::GeolocationPermissionContext* GetGeolocationPermissionContext() { return 0; }
+   virtual content::SpeechRecognitionPreferences* GetSpeechRecognitionPreferences() { return 0; }
+   virtual quota::SpecialStoragePolicy* GetSpecialStoragePolicy() { return 0; }
+
+   net::URLRequestContextGetter *CreateRequestContext(content::ProtocolHandlerMap* protocol_handlers)
+   {
+       url_request_getter_ = new content::ShellURLRequestContextGetter(/*ignore_certificate_errors = */ false, GetPath(), content::BrowserThread::UnsafeGetMessageLoopForThread(content::BrowserThread::IO)
+                                                                       , content::BrowserThread::UnsafeGetMessageLoopForThread(content::BrowserThread::FILE), protocol_handlers);
+       static_cast<ResourceContextQt*>(resourceContext.get())->set_url_request_context_getter(url_request_getter_.get());
+       return url_request_getter_.get();
+   }
+
+private:
+   scoped_ptr<content::ResourceContext> resourceContext;
+   base::ScopedTempDir tempBasePath; // ### Should become permanent location.
+   scoped_refptr<content::ShellURLRequestContextGetter> url_request_getter_;
+
+   DISALLOW_COPY_AND_ASSIGN(BrowserContextQt);
+};
+
+#endif //BROWSER_CONTEXT_QT
diff --git a/lib/content_browser_client_qt.cpp b/lib/content_browser_client_qt.cpp
new file mode 100644
index 000000000..4251a9fe1
--- /dev/null
+++ b/lib/content_browser_client_qt.cpp
@@ -0,0 +1,41 @@
+#include "content_browser_client_qt.h"
+
+#include "content/public/browser/notification_source.h"
+#include "content/public/browser/notification_types.h"
+#include "content/public/common/main_function_params.h"
+#include "content/public/browser/render_process_host.h"
+
+// To be cleaned up
+#include "content/shell/shell_browser_main_parts.h"
+
+#include "browser_context_qt.h"
+#include "web_contents_view_qt.h"
+
+
+content::WebContentsViewPort* ContentBrowserClientQt::OverrideCreateWebContentsView(content::WebContents* web_contents, content::RenderViewHostDelegateView** render_view_host_delegate_view)
+{
+    fprintf(stderr, "OverrideCreateWebContentsView\n");
+    WebContentsViewQt* rv = new WebContentsViewQt(web_contents);
+    *render_view_host_delegate_view = rv;
+    return rv;
+}
+
+content::BrowserMainParts *ContentBrowserClientQt::CreateBrowserMainParts(const content::MainFunctionParams &parameters)
+{
+    m_browserMainParts = new content::ShellBrowserMainParts(parameters);
+    m_browser_context = new BrowserContextQt();
+    return m_browserMainParts;
+}
+
+
+BrowserContextQt* ContentBrowserClientQt::browser_context() {
+
+    return m_browser_context;
+}
+
+net::URLRequestContextGetter* ContentBrowserClientQt::CreateRequestContext(content::BrowserContext* content_browser_context, content::ProtocolHandlerMap* protocol_handlers)
+{
+    if (content_browser_context != browser_context())
+        fprintf(stderr, "Warning: off the record browser context not implemented !\n");
+    return browser_context()->CreateRequestContext(protocol_handlers);
+}
diff --git a/lib/content_browser_client_qt.h b/lib/content_browser_client_qt.h
new file mode 100644
index 000000000..c3520fc0b
--- /dev/null
+++ b/lib/content_browser_client_qt.h
@@ -0,0 +1,38 @@
+#ifndef CONTENT_BROWSER_CLIENT_QT
+#define CONTENT_BROWSER_CLIENT_QT
+
+#include "content/public/browser/content_browser_client.h"
+
+namespace net {
+class URLRequestContextGetter;
+}
+
+namespace content {
+class BrowserMainParts;
+class RenderProcessHost;
+class RenderViewHostDelegateView;
+class ShellBrowserMainParts;
+class WebContentsViewPort;
+class WebContents;
+struct MainFunctionParams;
+}
+
+class BrowserContextQt;
+
+class ContentBrowserClientQt : public content::ContentBrowserClient {
+
+public:
+    virtual content::WebContentsViewPort* OverrideCreateWebContentsView(content::WebContents* , content::RenderViewHostDelegateView**) /*Q_DECL_OVERRIDE*/;
+    virtual content::BrowserMainParts* CreateBrowserMainParts(const content::MainFunctionParams& parameters) /*Q_DECL_OVERRIDE*/;
+
+
+    BrowserContextQt *browser_context();
+
+    net::URLRequestContextGetter *CreateRequestContext(content::BrowserContext *content_browser_context, content::ProtocolHandlerMap *protocol_handlers);
+private:
+    BrowserContextQt* m_browser_context;
+    content::ShellBrowserMainParts* m_browserMainParts;
+
+};
+
+#endif // CONTENT_BROWSER_CLIENT_QT
diff --git a/lib/lib.pro b/lib/lib.pro
index cb766577e..67dd25ead 100644
--- a/lib/lib.pro
+++ b/lib/lib.pro
@@ -10,7 +10,7 @@ TARGET = blinq
 # Defining keywords such as 'signal' clashes with the chromium code base.
 DEFINES += QT_NO_KEYWORDS
 
-# We need a way to tap into gyp's Debug vs. Release configuration
+# We need a way to tap into gyp´s Debug vs. Release configuration
 PER_CONFIG_DEFINES = BLINQ_PROCESS_PATH=\\\"$$getOutDir()/%config/$$BLINQ_PROCESS_NAME\\\"
 
 # Keep Skia happy
@@ -22,18 +22,23 @@ SOURCES = \
         backing_store_qt.cpp \
         blinqapplication.cpp \
         blinqpage.cpp \
+        content_browser_client_qt.cpp \
         raster_window.cpp \
         render_widget_host_view_qt.cpp \
+        resource_context_qt.cpp \
         shell_qt.cpp \
         signal_connector.cpp \
-        web_event_factory.cpp \
+        web_event_factory.cpp
 
 HEADERS = \
         backing_store_qt.h \
         blinqapplication.h \
         blinqpage.h \
+        browser_context_qt.h \
+        content_browser_client_qt.h \
         raster_window.h \
         render_widget_host_view_qt.h \
+        resource_context_qt.h \
         web_event_factory.h \
         signal_connector.h
 
diff --git a/lib/resource_context_qt.cpp b/lib/resource_context_qt.cpp
new file mode 100644
index 000000000..ca680e6c1
--- /dev/null
+++ b/lib/resource_context_qt.cpp
@@ -0,0 +1,25 @@
+#include "resource_context_qt.h"
+
+#include "net/url_request/url_request_context_getter.h"
+
+#include "content/shell/shell_url_request_context_getter.h"
+
+#include "browser_context_qt.h"
+
+net::HostResolver *ResourceContextQt::GetHostResolver()
+{
+    CHECK(getter_);
+    return getter_->host_resolver();
+}
+
+net::URLRequestContext* ResourceContextQt::GetRequestContext()
+{
+    if (getter_)
+        return getter_->GetURLRequestContext();
+    return context->GetRequestContext()->GetURLRequestContext();
+}
+
+void ResourceContextQt::set_url_request_context_getter(content::ShellURLRequestContextGetter *getter)
+{
+    getter_ = getter;
+}
diff --git a/lib/resource_context_qt.h b/lib/resource_context_qt.h
new file mode 100644
index 000000000..c4c3faca4
--- /dev/null
+++ b/lib/resource_context_qt.h
@@ -0,0 +1,33 @@
+#include "content/public/browser/resource_context.h"
+
+#ifndef RESOURCE_CONTEXT_QT
+#define RESOURCE_CONTEXT_QT
+
+namespace content {
+class ShellURLRequestContextGetter;
+}
+
+class BrowserContextQt;
+
+class ResourceContextQt : public content::ResourceContext
+{
+public:
+    ResourceContextQt(BrowserContextQt *ctx)
+        : context(ctx)
+        , getter_(0)
+    {}
+
+    virtual net::HostResolver* GetHostResolver();
+
+    virtual net::URLRequestContext* GetRequestContext();
+
+    void set_url_request_context_getter(content::ShellURLRequestContextGetter* getter);
+
+private:
+    BrowserContextQt *context;
+    content::ShellURLRequestContextGetter* getter_;
+
+    DISALLOW_COPY_AND_ASSIGN(ResourceContextQt);
+};
+
+#endif //RESOURCE_CONTEXT_QT
-- 
GitLab