diff --git a/atom/browser/atom_web_ui_controller_factory.cc b/atom/browser/atom_web_ui_controller_factory.cc index 20eeed07a51d..1c0c1be03e34 100644 --- a/atom/browser/atom_web_ui_controller_factory.cc +++ b/atom/browser/atom_web_ui_controller_factory.cc @@ -4,6 +4,8 @@ #include "atom/browser/atom_web_ui_controller_factory.h" +#include "atom/browser/ui/webui/pdf_viewer_handler.h" +#include "base/memory/ptr_util.h" #include "base/strings/string_util.h" #include "base/strings/string_split.h" #include "content/public/browser/render_view_host.h" @@ -107,29 +109,17 @@ class PdfViewerUI : public content::WebUIController { content::WebUI* web_ui, const std::string& stream_url, const std::string& original_url) - : content::WebUIController(web_ui), - stream_url_(stream_url), - original_url_(original_url) { - web_ui->RegisterMessageCallback( - "initialize", - base::Bind(&PdfViewerUI::OnInitialize, base::Unretained(this))); + : content::WebUIController(web_ui) { + web_ui->AddMessageHandler(new PdfViewerHandler(stream_url, original_url)); content::URLDataSource::Add(browser_context, new BundledDataSource); } + // content::WebUIController implementation. void RenderViewCreated(content::RenderViewHost* rvh) override { rvh->AllowBindings(content::BINDINGS_POLICY_WEB_UI); } - void OnInitialize(const base::ListValue* args) { - web_ui()->CallJavascriptFunctionUnsafe("main", - base::StringValue(original_url_), - base::StringValue(original_url_)); - } - private: - std::string stream_url_; - std::string original_url_; - DISALLOW_COPY_AND_ASSIGN(PdfViewerUI); }; } diff --git a/atom/browser/atom_web_ui_controller_factory.h b/atom/browser/atom_web_ui_controller_factory.h index d63aa0eceea6..d2713608e846 100644 --- a/atom/browser/atom_web_ui_controller_factory.h +++ b/atom/browser/atom_web_ui_controller_factory.h @@ -19,6 +19,7 @@ class AtomWebUIControllerFactory : public content::WebUIControllerFactory { AtomWebUIControllerFactory(); virtual ~AtomWebUIControllerFactory(); + // content::WebUIControllerFactory: content::WebUI::TypeID GetWebUIType(content::BrowserContext* browser_context, const GURL& url) const override; bool UseWebUIForURL(content::BrowserContext* browser_context, diff --git a/atom/browser/resources/pdf_viewer/ods-cpp.pdf b/atom/browser/resources/pdf_viewer/ods-cpp.pdf deleted file mode 100644 index 592eb38c2c3f..000000000000 Binary files a/atom/browser/resources/pdf_viewer/ods-cpp.pdf and /dev/null differ diff --git a/atom/browser/ui/webui/pdf_viewer_handler.cc b/atom/browser/ui/webui/pdf_viewer_handler.cc new file mode 100644 index 000000000000..611acd8eeb8d --- /dev/null +++ b/atom/browser/ui/webui/pdf_viewer_handler.cc @@ -0,0 +1,51 @@ +// Copyright (c) 2017 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "atom/browser/ui/webui/pdf_viewer_handler.h" + +#include "base/bind.h" +#include "base/bind_helpers.h" +#include "base/values.h" +#include "content/public/browser/web_contents.h" +#include "content/public/browser/web_ui.h" + +namespace atom { + +PdfViewerHandler::PdfViewerHandler(const std::string& stream_url, + const std::string& original_url) + : stream_url_(stream_url), original_url_(original_url) {} + +PdfViewerHandler::~PdfViewerHandler() {} + +void PdfViewerHandler::RegisterMessages() { + web_ui()->RegisterMessageCallback( + "initialize", + base::Bind(&PdfViewerHandler::Initialize, base::Unretained(this))); + web_ui()->RegisterMessageCallback( + "getTabId", + base::Bind(&PdfViewerHandler::GetTabId, base::Unretained(this))); +} + +void PdfViewerHandler::Initialize(const base::ListValue* args) { + AllowJavascript(); + + CHECK_EQ(1U, args->GetSize()); + const base::Value* callback_id; + CHECK(args->Get(0, &callback_id)); + std::unique_ptr stream_info(new base::DictionaryValue); + stream_info->SetString("streamURL", stream_url_); + stream_info->SetString("originalURL", original_url_); + ResolveJavascriptCallback(*callback_id, *stream_info); +} + +void PdfViewerHandler::GetTabId(const base::ListValue* args) { + if (!IsJavascriptAllowed()) + return; + CHECK_EQ(1U, args->GetSize()); + const base::Value* callback_id; + CHECK(args->Get(0, &callback_id)); + ResolveJavascriptCallback(*callback_id, base::FundamentalValue(-1)); +} + +} // namespace atom diff --git a/atom/browser/ui/webui/pdf_viewer_handler.h b/atom/browser/ui/webui/pdf_viewer_handler.h new file mode 100644 index 000000000000..c7661fdcda2c --- /dev/null +++ b/atom/browser/ui/webui/pdf_viewer_handler.h @@ -0,0 +1,41 @@ +// Copyright (c) 2017 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef ATOM_BROWSER_UI_WEBUI_PDF_VIEWER_HANDLER_H_ +#define ATOM_BROWSER_UI_WEBUI_PDF_VIEWER_HANDLER_H_ + +#include + +#include "base/compiler_specific.h" +#include "base/macros.h" +#include "content/public/browser/web_ui_message_handler.h" + +namespace base { +class ListValue; +} + +namespace atom { + +class PdfViewerHandler : public content::WebUIMessageHandler { + public: + PdfViewerHandler(const std::string& stream_url, + const std::string& original_url); + ~PdfViewerHandler() override; + + // WebUIMessageHandler implementation. + void RegisterMessages() override; + + private: + void Initialize(const base::ListValue* args); + void GetTabId(const base::ListValue* args); + + std::string stream_url_; + std::string original_url_; + + DISALLOW_COPY_AND_ASSIGN(PdfViewerHandler); +}; + +} // namespace atom + +#endif // ATOM_BROWSER_UI_WEBUI_PDF_VIEWER_HANDLER_H_ diff --git a/filenames.gypi b/filenames.gypi index 3c7e4d217293..60e4f41046cf 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -329,6 +329,8 @@ 'atom/browser/ui/views/submenu_button.h', 'atom/browser/ui/views/win_frame_view.cc', 'atom/browser/ui/views/win_frame_view.h', + 'atom/browser/ui/webui/pdf_viewer_handler.cc', + 'atom/browser/ui/webui/pdf_viewer_handler.h', 'atom/browser/ui/win/atom_desktop_native_widget_aura.cc', 'atom/browser/ui/win/atom_desktop_native_widget_aura.h', 'atom/browser/ui/win/atom_desktop_window_tree_host_win.cc', @@ -622,7 +624,6 @@ 'chromium_src/net/test/embedded_test_server/tcp_listen_socket.h', '<@(native_mate_files)', '<(SHARED_INTERMEDIATE_DIR)/atom_natives.h', - '<(SHARED_INTERMEDIATE_DIR)/grit/pdf_viewer_resources.h', '<(SHARED_INTERMEDIATE_DIR)/grit/pdf_viewer_resources_map.cc', '<(SHARED_INTERMEDIATE_DIR)/grit/pdf_viewer_resources_map.h', ], diff --git a/lib/renderer/init.js b/lib/renderer/init.js index 7bd707c102ec..24e19b9d2a60 100644 --- a/lib/renderer/init.js +++ b/lib/renderer/init.js @@ -76,7 +76,7 @@ if (window.location.protocol === 'chrome-devtools:') { // Override some inspector APIs. require('./inspector') nodeIntegration = 'true' -} else if (window.location.protocol === 'chrome-extension:' || window.location.protocol === 'chrome:') { +} else if (window.location.protocol === 'chrome-extension:') { // Add implementations of chrome API. require('./chrome-api').injectTo(window.location.hostname, isBackgroundPage, window) nodeIntegration = 'false'