electron/shell/browser/ui/webui/pdf_viewer_ui.cc

259 lines
9.4 KiB
C++
Raw Normal View History

// 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 "shell/browser/ui/webui/pdf_viewer_ui.h"
#include <map>
#include <memory>
#include <utility>
2017-02-27 15:01:54 +00:00
#include "base/sequenced_task_runner_helpers.h"
#include "base/task/post_task.h"
#include "content/browser/loader/resource_dispatcher_host_impl.h"
#include "content/browser/loader/resource_request_info_impl.h"
#include "content/browser/loader/stream_resource_handler.h"
#include "content/browser/resource_context_impl.h"
#include "content/browser/streams/stream.h"
#include "content/browser/streams/stream_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/resource_context.h"
#include "content/public/browser/stream_handle.h"
#include "content/public/browser/stream_info.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_contents.h"
#include "grit/pdf_viewer_resources_map.h"
#include "net/base/load_flags.h"
2017-12-17 20:46:23 +00:00
#include "net/base/mime_util.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"
#include "services/network/public/cpp/resource_response.h"
#include "shell/browser/atom_browser_context.h"
#include "shell/browser/loader/layered_resource_handler.h"
#include "shell/browser/ui/webui/pdf_viewer_handler.h"
#include "shell/common/atom_constants.h"
#include "ui/base/resource/resource_bundle.h"
using content::BrowserThread;
namespace electron {
namespace {
// Extracts the path value from the URL without the leading '/',
// which follows the mapping of names in pdf_viewer_resources_map.
std::string PathWithoutParams(const std::string& path) {
2017-02-03 09:36:53 +00:00
return GURL(kPdfViewerUIOrigin + path).path().substr(1);
}
class BundledDataSource : public content::URLDataSource {
public:
BundledDataSource() {
for (size_t i = 0; i < kPdfViewerResourcesSize; ++i) {
std::string resource_path = kPdfViewerResources[i].name;
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.
2017-02-03 09:36:53 +00:00
std::string GetSource() const override { return kPdfViewerUIHost; }
2017-02-09 11:00:17 +00:00
void StartDataRequest(
const std::string& path,
const content::ResourceRequestInfo::WebContentsGetter& wc_getter,
const GotDataCallback& callback) override {
std::string filename = PathWithoutParams(path);
auto entry = path_to_resource_id_.find(filename);
2017-01-23 22:49:18 +00:00
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));
2017-02-14 10:49:40 +00:00
} else {
LOG(ERROR) << "Unable to find: " << path;
callback.Run(new base::RefCountedString());
}
}
std::string GetMimeType(const std::string& path) const override {
2017-12-22 02:29:09 +00:00
base::FilePath::StringType ext =
base::FilePath::FromUTF8Unsafe(PathWithoutParams(path)).Extension();
2017-12-17 20:46:23 +00:00
std::string mime_type;
if (!ext.empty() &&
net::GetWellKnownMimeTypeFromExtension(ext.substr(1), &mime_type))
return mime_type;
return "text/html";
}
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<std::string, int> path_to_resource_id_;
DISALLOW_COPY_AND_ASSIGN(BundledDataSource);
};
2017-02-27 15:01:54 +00:00
// Helper to convert from OnceCallback to Callback.
template <typename T>
void CallMigrationCallback(T callback,
std::unique_ptr<content::StreamInfo> stream_info) {
std::move(callback).Run(std::move(stream_info));
}
} // namespace
2017-02-27 15:01:54 +00:00
class PdfViewerUI::ResourceRequester
: public base::RefCountedThreadSafe<ResourceRequester,
BrowserThread::DeleteOnIOThread>,
public electron::LayeredResourceHandler::Delegate {
2017-02-27 15:01:54 +00:00
public:
2017-02-28 09:37:52 +00:00
explicit ResourceRequester(StreamResponseCallback cb)
2017-02-27 15:01:54 +00:00
: stream_response_cb_(std::move(cb)) {}
void StartRequest(const GURL& url,
const GURL& origin,
int render_process_id,
int render_view_id,
int render_frame_id,
content::ResourceContext* resource_context) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
const net::URLRequestContext* request_context =
resource_context->GetRequestContext();
std::unique_ptr<net::URLRequest> request(
request_context->CreateRequest(url, net::DEFAULT_PRIORITY, nullptr));
request->set_method("GET");
content::ResourceDispatcherHostImpl::Get()->InitializeURLRequest(
2017-06-16 20:42:33 +00:00
request.get(), content::Referrer(url, blink::kWebReferrerPolicyDefault),
2017-02-27 15:01:54 +00:00
false, // download.
2017-04-11 07:18:40 +00:00
render_process_id, render_view_id, render_frame_id,
content::PREVIEWS_OFF, resource_context);
2017-02-27 15:01:54 +00:00
content::ResourceRequestInfoImpl* info =
content::ResourceRequestInfoImpl::ForRequest(request.get());
content::StreamContext* stream_context =
content::GetStreamContextForResourceContext(resource_context);
2017-03-11 22:03:26 +00:00
std::unique_ptr<content::ResourceHandler> handler =
std::make_unique<content::StreamResourceHandler>(
request.get(), stream_context->registry(), origin, false);
2017-02-27 15:01:54 +00:00
info->set_is_stream(true);
stream_info_.reset(new content::StreamInfo);
stream_info_->handle =
static_cast<content::StreamResourceHandler*>(handler.get())
->stream()
->CreateHandle();
stream_info_->original_url = request->url();
// Helper to fill stream response details.
handler.reset(new electron::LayeredResourceHandler(
request.get(), std::move(handler), this));
2017-02-27 15:01:54 +00:00
content::ResourceDispatcherHostImpl::Get()->BeginURLRequest(
std::move(request), std::move(handler),
false, // download
false, // content_initiated (download specific)
false, // do_not_prompt_for_login (download specific)
resource_context);
}
protected:
// electron::LayeredResourceHandler::Delegate:
void OnResponseStarted(network::ResourceResponse* response) override {
2017-02-27 15:01:54 +00:00
DCHECK_CURRENTLY_ON(BrowserThread::IO);
auto resource_response_head = response->head;
auto headers = resource_response_head.headers;
auto mime_type = resource_response_head.mime_type;
if (headers.get())
stream_info_->response_headers =
new net::HttpResponseHeaders(headers->raw_headers());
stream_info_->mime_type = mime_type;
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(&CallMigrationCallback<StreamResponseCallback>,
base::Passed(&stream_response_cb_),
base::Passed(&stream_info_)));
2017-02-27 15:01:54 +00:00
}
private:
friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>;
friend class base::DeleteHelper<ResourceRequester>;
~ResourceRequester() override {}
StreamResponseCallback stream_response_cb_;
std::unique_ptr<content::StreamInfo> stream_info_;
DISALLOW_COPY_AND_ASSIGN(ResourceRequester);
};
PdfViewerUI::PdfViewerUI(content::BrowserContext* browser_context,
content::WebUI* web_ui,
2017-01-23 11:12:39 +00:00
const std::string& src)
2017-01-23 23:36:30 +00:00
: content::WebUIController(web_ui),
content::WebContentsObserver(web_ui->GetWebContents()),
src_(src) {
pdf_handler_ = new PdfViewerHandler(src);
2017-04-13 10:21:30 +00:00
web_ui->AddMessageHandler(
std::unique_ptr<content::WebUIMessageHandler>(pdf_handler_));
content::URLDataSource::Add(browser_context, new BundledDataSource);
}
PdfViewerUI::~PdfViewerUI() {}
bool PdfViewerUI::OnMessageReceived(
const IPC::Message& message,
content::RenderFrameHost* render_frame_host) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(PdfViewerUI, message)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void PdfViewerUI::OnPdfStreamCreated(
std::unique_ptr<content::StreamInfo> stream) {
stream_ = std::move(stream);
2017-03-01 17:11:51 +00:00
if (pdf_handler_)
pdf_handler_->SetPdfResourceStream(stream_.get());
2017-02-27 15:01:54 +00:00
resource_requester_ = nullptr;
}
void PdfViewerUI::RenderFrameCreated(content::RenderFrameHost* rfh) {
int render_process_id = rfh->GetProcess()->GetID();
int render_frame_id = rfh->GetRoutingID();
int render_view_id = rfh->GetRenderViewHost()->GetRoutingID();
auto resource_context =
web_contents()->GetBrowserContext()->GetResourceContext();
2017-02-27 15:01:54 +00:00
auto callback =
base::BindOnce(&PdfViewerUI::OnPdfStreamCreated, base::Unretained(this));
resource_requester_ = new ResourceRequester(std::move(callback));
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&ResourceRequester::StartRequest, resource_requester_,
GURL(src_), GURL(kPdfViewerUIOrigin), render_process_id,
render_view_id, render_frame_id, resource_context));
}
void PdfViewerUI::OnSaveURLAs(const GURL& url,
const content::Referrer& referrer) {
web_contents()->SaveFrame(url, referrer);
}
} // namespace electron