move pdfviewer ui data source to separate file
This commit is contained in:
parent
008497500b
commit
fd8c450ef3
5 changed files with 135 additions and 96 deletions
|
@ -4,107 +4,15 @@
|
||||||
|
|
||||||
#include "atom/browser/atom_web_ui_controller_factory.h"
|
#include "atom/browser/atom_web_ui_controller_factory.h"
|
||||||
|
|
||||||
#include <map>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "atom/browser/ui/webui/pdf_viewer_handler.h"
|
#include "atom/browser/ui/webui/pdf_viewer_ui.h"
|
||||||
#include "base/memory/ptr_util.h"
|
|
||||||
#include "base/strings/string_split.h"
|
#include "base/strings/string_split.h"
|
||||||
#include "base/strings/string_util.h"
|
#include "base/strings/string_util.h"
|
||||||
#include "content/public/browser/render_view_host.h"
|
|
||||||
#include "content/public/browser/url_data_source.h"
|
|
||||||
#include "content/public/browser/web_contents.h"
|
#include "content/public/browser/web_contents.h"
|
||||||
#include "content/public/browser/web_ui.h"
|
|
||||||
#include "content/public/browser/web_ui_controller.h"
|
|
||||||
#include "content/public/common/bindings_policy.h"
|
|
||||||
#include "grit/pdf_viewer_resources_map.h"
|
|
||||||
#include "net/base/mime_util.h"
|
|
||||||
#include "ui/base/resource/resource_bundle.h"
|
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
const char kChromeUIPdfViewerHost[] = "pdf-viewer";
|
|
||||||
|
|
||||||
std::string PathWithoutParams(const std::string& path) {
|
|
||||||
return GURL(std::string("chrome://pdf-viewer/") + path).path().substr(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
class BundledDataSource : public content::URLDataSource {
|
|
||||||
public:
|
|
||||||
BundledDataSource() {
|
|
||||||
for (size_t i = 0; i < kPdfViewerResourcesSize; ++i) {
|
|
||||||
base::FilePath resource_path =
|
|
||||||
base::FilePath().AppendASCII(kPdfViewerResources[i].name);
|
|
||||||
resource_path = resource_path.NormalizePathSeparators();
|
|
||||||
|
|
||||||
DCHECK(path_to_resource_id_.find(resource_path) ==
|
|
||||||
path_to_resource_id_.end());
|
|
||||||
path_to_resource_id_[resource_path] = kPdfViewerResources[i].value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// content::URLDataSource implementation.
|
|
||||||
std::string GetSource() const override { return kChromeUIPdfViewerHost; }
|
|
||||||
|
|
||||||
void StartDataRequest(const std::string& path,
|
|
||||||
int render_process_id,
|
|
||||||
int render_frame_id,
|
|
||||||
const GotDataCallback& callback) override {
|
|
||||||
std::string filename = PathWithoutParams(path);
|
|
||||||
std::map<base::FilePath, int>::const_iterator entry =
|
|
||||||
path_to_resource_id_.find(base::FilePath(filename));
|
|
||||||
if (entry != path_to_resource_id_.end()) {
|
|
||||||
int resource_id = entry->second;
|
|
||||||
const ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
|
|
||||||
callback.Run(rb.LoadDataResourceBytes(resource_id));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string GetMimeType(const std::string& path) const override {
|
|
||||||
std::string filename = PathWithoutParams(path);
|
|
||||||
std::string mime_type;
|
|
||||||
net::GetMimeTypeFromFile(base::FilePath(filename), &mime_type);
|
|
||||||
return mime_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ShouldAddContentSecurityPolicy() const override { return false; }
|
|
||||||
|
|
||||||
bool ShouldDenyXFrameOptions() const override { return false; }
|
|
||||||
|
|
||||||
bool ShouldServeMimeTypeAsContentTypeHeader() const override { return true; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
~BundledDataSource() override {}
|
|
||||||
|
|
||||||
// A map from a resource path to the resource ID.
|
|
||||||
std::map<base::FilePath, int> path_to_resource_id_;
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(BundledDataSource);
|
|
||||||
};
|
|
||||||
|
|
||||||
class PdfViewerUI : public content::WebUIController {
|
|
||||||
public:
|
|
||||||
PdfViewerUI(content::BrowserContext* browser_context,
|
|
||||||
content::WebUI* web_ui,
|
|
||||||
const std::string& view_id)
|
|
||||||
: content::WebUIController(web_ui) {
|
|
||||||
web_ui->AddMessageHandler(new PdfViewerHandler(view_id));
|
|
||||||
content::URLDataSource::Add(browser_context, new BundledDataSource);
|
|
||||||
}
|
|
||||||
|
|
||||||
// content::WebUIController implementation.
|
|
||||||
void RenderViewCreated(content::RenderViewHost* rvh) override {
|
|
||||||
rvh->AllowBindings(content::BINDINGS_POLICY_WEB_UI);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(PdfViewerUI);
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
// static
|
// static
|
||||||
AtomWebUIControllerFactory* AtomWebUIControllerFactory::GetInstance() {
|
AtomWebUIControllerFactory* AtomWebUIControllerFactory::GetInstance() {
|
||||||
return base::Singleton<AtomWebUIControllerFactory>::get();
|
return base::Singleton<AtomWebUIControllerFactory>::get();
|
||||||
|
@ -117,7 +25,7 @@ AtomWebUIControllerFactory::~AtomWebUIControllerFactory() {}
|
||||||
content::WebUI::TypeID AtomWebUIControllerFactory::GetWebUIType(
|
content::WebUI::TypeID AtomWebUIControllerFactory::GetWebUIType(
|
||||||
content::BrowserContext* browser_context,
|
content::BrowserContext* browser_context,
|
||||||
const GURL& url) const {
|
const GURL& url) const {
|
||||||
if (url.host() == kChromeUIPdfViewerHost) {
|
if (url.host() == PdfViewerUI::kHost) {
|
||||||
return const_cast<AtomWebUIControllerFactory*>(this);
|
return const_cast<AtomWebUIControllerFactory*>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,7 +47,7 @@ bool AtomWebUIControllerFactory::UseWebUIBindingsForURL(
|
||||||
content::WebUIController*
|
content::WebUIController*
|
||||||
AtomWebUIControllerFactory::CreateWebUIControllerForURL(content::WebUI* web_ui,
|
AtomWebUIControllerFactory::CreateWebUIControllerForURL(content::WebUI* web_ui,
|
||||||
const GURL& url) const {
|
const GURL& url) const {
|
||||||
if (url.host() == kChromeUIPdfViewerHost) {
|
if (url.host() == PdfViewerUI::kHost) {
|
||||||
base::StringPairs toplevel_params;
|
base::StringPairs toplevel_params;
|
||||||
base::SplitStringIntoKeyValuePairs(url.query(), '=', '&', &toplevel_params);
|
base::SplitStringIntoKeyValuePairs(url.query(), '=', '&', &toplevel_params);
|
||||||
std::string view_id;
|
std::string view_id;
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
|
|
||||||
#include "base/macros.h"
|
#include "base/macros.h"
|
||||||
#include "base/memory/singleton.h"
|
#include "base/memory/singleton.h"
|
||||||
#include "content/public/browser/web_ui.h"
|
|
||||||
#include "content/public/browser/web_ui_controller_factory.h"
|
#include "content/public/browser/web_ui_controller_factory.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
94
atom/browser/ui/webui/pdf_viewer_ui.cc
Normal file
94
atom/browser/ui/webui/pdf_viewer_ui.cc
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
// 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_ui.h"
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
#include "atom/browser/ui/webui/pdf_viewer_handler.h"
|
||||||
|
#include "content/public/browser/render_view_host.h"
|
||||||
|
#include "content/public/browser/url_data_source.h"
|
||||||
|
#include "content/public/common/bindings_policy.h"
|
||||||
|
#include "grit/pdf_viewer_resources_map.h"
|
||||||
|
#include "net/base/mime_util.h"
|
||||||
|
#include "ui/base/resource/resource_bundle.h"
|
||||||
|
|
||||||
|
namespace atom {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
std::string PathWithoutParams(const std::string& path) {
|
||||||
|
return GURL(std::string("chrome://pdf-viewer/") + path).path().substr(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
class BundledDataSource : public content::URLDataSource {
|
||||||
|
public:
|
||||||
|
BundledDataSource() {
|
||||||
|
for (size_t i = 0; i < kPdfViewerResourcesSize; ++i) {
|
||||||
|
base::FilePath resource_path =
|
||||||
|
base::FilePath().AppendASCII(kPdfViewerResources[i].name);
|
||||||
|
resource_path = resource_path.NormalizePathSeparators();
|
||||||
|
|
||||||
|
DCHECK(path_to_resource_id_.find(resource_path) ==
|
||||||
|
path_to_resource_id_.end());
|
||||||
|
path_to_resource_id_[resource_path] = kPdfViewerResources[i].value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// content::URLDataSource implementation.
|
||||||
|
std::string GetSource() const override { return PdfViewerUI::kHost; }
|
||||||
|
|
||||||
|
void StartDataRequest(const std::string& path,
|
||||||
|
int render_process_id,
|
||||||
|
int render_frame_id,
|
||||||
|
const GotDataCallback& callback) override {
|
||||||
|
std::string filename = PathWithoutParams(path);
|
||||||
|
std::map<base::FilePath, int>::const_iterator entry =
|
||||||
|
path_to_resource_id_.find(base::FilePath(filename));
|
||||||
|
if (entry != path_to_resource_id_.end()) {
|
||||||
|
int resource_id = entry->second;
|
||||||
|
const ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
|
||||||
|
callback.Run(rb.LoadDataResourceBytes(resource_id));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string GetMimeType(const std::string& path) const override {
|
||||||
|
std::string filename = PathWithoutParams(path);
|
||||||
|
std::string mime_type;
|
||||||
|
net::GetMimeTypeFromFile(base::FilePath(filename), &mime_type);
|
||||||
|
return mime_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ShouldAddContentSecurityPolicy() const override { return false; }
|
||||||
|
|
||||||
|
bool ShouldDenyXFrameOptions() const override { return false; }
|
||||||
|
|
||||||
|
bool ShouldServeMimeTypeAsContentTypeHeader() const override { return true; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
~BundledDataSource() override {}
|
||||||
|
|
||||||
|
// A map from a resource path to the resource ID.
|
||||||
|
std::map<base::FilePath, int> path_to_resource_id_;
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(BundledDataSource);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
const char PdfViewerUI::kHost[] = "pdf-viewer";
|
||||||
|
|
||||||
|
PdfViewerUI::PdfViewerUI(content::BrowserContext* browser_context,
|
||||||
|
content::WebUI* web_ui,
|
||||||
|
const std::string& view_id)
|
||||||
|
: content::WebUIController(web_ui) {
|
||||||
|
web_ui->AddMessageHandler(new PdfViewerHandler(view_id));
|
||||||
|
content::URLDataSource::Add(browser_context, new BundledDataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PdfViewerUI::RenderViewCreated(content::RenderViewHost* rvh) {
|
||||||
|
rvh->AllowBindings(content::BINDINGS_POLICY_WEB_UI);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace atom
|
36
atom/browser/ui/webui/pdf_viewer_ui.h
Normal file
36
atom/browser/ui/webui/pdf_viewer_ui.h
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
// 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_UI_H_
|
||||||
|
#define ATOM_BROWSER_UI_WEBUI_PDF_VIEWER_UI_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "base/macros.h"
|
||||||
|
#include "content/public/browser/web_ui_controller.h"
|
||||||
|
|
||||||
|
namespace content {
|
||||||
|
class BrowserContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace atom {
|
||||||
|
|
||||||
|
class PdfViewerUI : public content::WebUIController {
|
||||||
|
public:
|
||||||
|
static const char kHost[];
|
||||||
|
|
||||||
|
PdfViewerUI(content::BrowserContext* browser_context,
|
||||||
|
content::WebUI* web_ui,
|
||||||
|
const std::string& view_id);
|
||||||
|
|
||||||
|
// content::WebUIController implementation.
|
||||||
|
void RenderViewCreated(content::RenderViewHost* rvh) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(PdfViewerUI);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace atom
|
||||||
|
|
||||||
|
#endif // ATOM_BROWSER_UI_WEBUI_PDF_VIEWER_UI_H_
|
|
@ -330,6 +330,8 @@
|
||||||
'atom/browser/ui/views/win_frame_view.h',
|
'atom/browser/ui/views/win_frame_view.h',
|
||||||
'atom/browser/ui/webui/pdf_viewer_handler.cc',
|
'atom/browser/ui/webui/pdf_viewer_handler.cc',
|
||||||
'atom/browser/ui/webui/pdf_viewer_handler.h',
|
'atom/browser/ui/webui/pdf_viewer_handler.h',
|
||||||
|
'atom/browser/ui/webui/pdf_viewer_ui.cc',
|
||||||
|
'atom/browser/ui/webui/pdf_viewer_ui.h',
|
||||||
'atom/browser/ui/win/atom_desktop_native_widget_aura.cc',
|
'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_native_widget_aura.h',
|
||||||
'atom/browser/ui/win/atom_desktop_window_tree_host_win.cc',
|
'atom/browser/ui/win/atom_desktop_window_tree_host_win.cc',
|
||||||
|
|
Loading…
Reference in a new issue