From afa32ac2fb668941211e229f9ef422ecf28bf80a Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Sun, 21 Sep 2014 11:15:44 +0100 Subject: [PATCH] Implemented custom scheme handling. Closes #286. --- CMakeLists.txt | 1 + src/gui/app.cpp | 9 +++++++ src/gui/app.h | 1 + src/gui/scheme.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++ src/gui/scheme.h | 44 ++++++++++++++++++++++++++++++++++ 5 files changed, 114 insertions(+) create mode 100644 src/gui/scheme.cpp create mode 100644 src/gui/scheme.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a4475e2..e08b458c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,6 +59,7 @@ set (LOOT_GUI_SRC ${LOOT_SRC} "${CMAKE_SOURCE_DIR}/src/gui/main_win.cpp" "${CMAKE_SOURCE_DIR}/src/gui/handler.cpp" "${CMAKE_SOURCE_DIR}/src/gui/app.cpp" + "${CMAKE_SOURCE_DIR}/src/gui/scheme.cpp" "${CMAKE_SOURCE_DIR}/src/resource.rc") set (LOOT_API_SRC ${LOOT_SRC} diff --git a/src/gui/app.cpp b/src/gui/app.cpp index 4bd47757..b88a69c4 100644 --- a/src/gui/app.cpp +++ b/src/gui/app.cpp @@ -24,6 +24,7 @@ #include "app.h" #include "handler.h" +#include "scheme.h" #include "../backend/error.h" #include "../backend/globals.h" @@ -64,6 +65,11 @@ namespace loot { return this; } + void LootApp::OnRegisterCustomSchemes(CefRefPtr registrar) { + // Register "loot" as a standard scheme. + registrar->AddCustomScheme("loot", true, false, false); + } + void LootApp::OnContextInitialized() { //Make sure this is running in the UI thread. assert(CefCurrentlyOn(TID_UI)); @@ -79,6 +85,9 @@ namespace loot { // Set the handler for browser-level callbacks. CefRefPtr handler(new LootHandler()); + // Register the custom "loot" scheme handler. + CefRegisterSchemeHandlerFactory("loot", "l10n", new LootSchemeHandlerFactory()); + // Specify CEF browser settings here. CefBrowserSettings browser_settings; diff --git a/src/gui/app.h b/src/gui/app.h index ac51d977..66bb720f 100644 --- a/src/gui/app.h +++ b/src/gui/app.h @@ -43,6 +43,7 @@ namespace loot { // Override CefApp methods. virtual CefRefPtr GetBrowserProcessHandler() OVERRIDE; virtual CefRefPtr GetRenderProcessHandler() OVERRIDE; + virtual void OnRegisterCustomSchemes(CefRefPtr registrar) OVERRIDE; // Override CefBrowserProcessHandler methods. virtual void OnContextInitialized() OVERRIDE; diff --git a/src/gui/scheme.cpp b/src/gui/scheme.cpp new file mode 100644 index 00000000..b37215da --- /dev/null +++ b/src/gui/scheme.cpp @@ -0,0 +1,59 @@ +/* LOOT + +A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and +Fallout: New Vegas. + +Copyright (C) 2014 WrinklyNinja + +This file is part of LOOT. + +LOOT is free software: you can redistribute +it and/or modify it under the terms of the GNU General Public License +as published by the Free Software Foundation, either version 3 of +the License, or (at your option) any later version. + +LOOT is distributed in the hope that it will +be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with LOOT. If not, see +. +*/ + +#include "scheme.h" + +#include "../backend/globals.h" + +#include + +#include +#include + +using namespace std; + +namespace loot { + /////////////////////////////// + // LootSchemeHandlerFactory + /////////////////////////////// + + CefRefPtr LootSchemeHandlerFactory::Create(CefRefPtr browser, + CefRefPtr frame, + const CefString& scheme_name, + CefRefPtr request) { + BOOST_LOG_TRIVIAL(trace) << "Handling custom scheme: " << string(request->GetURL()); + + // Get the file from the custom URL. + string file = (g_path_l10n / string(request->GetURL()).substr(12)).string(); + + // Load the file into a CEF stream. + CefRefPtr stream = CefStreamReader::CreateForFile(file); + BOOST_LOG_TRIVIAL(trace) << "Loaded file: " << file; + + CefResponse::HeaderMap headers; + headers.emplace("Access-Control-Allow-Origin", "*"); + + return new CefStreamResourceHandler(200, "OK", "application/octet-stream", headers, stream); + } +} \ No newline at end of file diff --git a/src/gui/scheme.h b/src/gui/scheme.h new file mode 100644 index 00000000..d9634f4f --- /dev/null +++ b/src/gui/scheme.h @@ -0,0 +1,44 @@ +/* LOOT + +A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and +Fallout: New Vegas. + +Copyright (C) 2014 WrinklyNinja + +This file is part of LOOT. + +LOOT is free software: you can redistribute +it and/or modify it under the terms of the GNU General Public License +as published by the Free Software Foundation, either version 3 of +the License, or (at your option) any later version. + +LOOT is distributed in the hope that it will +be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with LOOT. If not, see +. +*/ + +#ifndef __LOOT_GUI_SCHEME_HANDLER__ +#define __LOOT_GUI_SCHEME_HANDLER__ + +#include +#include + +namespace loot { + class LootSchemeHandlerFactory : public CefSchemeHandlerFactory { + public: + virtual CefRefPtr Create(CefRefPtr browser, + CefRefPtr frame, + const CefString& scheme_name, + CefRefPtr request) + OVERRIDE; + + IMPLEMENT_REFCOUNTING(LootSchemeHandlerFactory); + }; +} + +#endif \ No newline at end of file