2017-01-21 14:57:29 +00:00
|
|
|
// Copyright (c) 2017 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/ui/webui/pdf_viewer_ui.h"
|
2017-01-21 14:57:29 +00:00
|
|
|
|
|
|
|
#include <map>
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
2017-01-21 14:57:29 +00:00
|
|
|
|
2017-02-27 15:01:54 +00:00
|
|
|
#include "base/sequenced_task_runner_helpers.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "base/task/post_task.h"
|
2017-02-27 06:10:49 +00:00
|
|
|
#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"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "content/public/browser/browser_task_traits.h"
|
2017-02-27 06:10:49 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "content/public/browser/render_frame_host.h"
|
|
|
|
#include "content/public/browser/render_process_host.h"
|
2017-01-21 14:57:29 +00:00
|
|
|
#include "content/public/browser/render_view_host.h"
|
2017-02-27 06:10:49 +00:00
|
|
|
#include "content/public/browser/resource_context.h"
|
|
|
|
#include "content/public/browser/stream_handle.h"
|
2017-02-27 07:14:43 +00:00
|
|
|
#include "content/public/browser/stream_info.h"
|
2017-01-21 14:57:29 +00:00
|
|
|
#include "content/public/browser/url_data_source.h"
|
2017-01-21 19:52:23 +00:00
|
|
|
#include "content/public/browser/web_contents.h"
|
2017-01-21 14:57:29 +00:00
|
|
|
#include "grit/pdf_viewer_resources_map.h"
|
2017-02-27 06:10:49 +00:00
|
|
|
#include "net/base/load_flags.h"
|
2017-12-17 20:46:23 +00:00
|
|
|
#include "net/base/mime_util.h"
|
2017-02-27 06:10:49 +00:00
|
|
|
#include "net/url_request/url_request.h"
|
|
|
|
#include "net/url_request/url_request_context.h"
|
2018-04-11 10:45:42 +00:00
|
|
|
#include "services/network/public/cpp/resource_response.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#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"
|
2017-01-21 14:57:29 +00:00
|
|
|
#include "ui/base/resource/resource_bundle.h"
|
|
|
|
|
2017-02-27 06:10:49 +00:00
|
|
|
using content::BrowserThread;
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2017-01-21 14:57:29 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-02-03 10:16:23 +00:00
|
|
|
// Extracts the path value from the URL without the leading '/',
|
|
|
|
// which follows the mapping of names in pdf_viewer_resources_map.
|
2017-01-21 14:57:29 +00:00
|
|
|
std::string PathWithoutParams(const std::string& path) {
|
2017-02-03 09:36:53 +00:00
|
|
|
return GURL(kPdfViewerUIOrigin + path).path().substr(1);
|
2017-01-21 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class BundledDataSource : public content::URLDataSource {
|
|
|
|
public:
|
|
|
|
BundledDataSource() {
|
|
|
|
for (size_t i = 0; i < kPdfViewerResourcesSize; ++i) {
|
2017-02-14 10:55:05 +00:00
|
|
|
std::string resource_path = kPdfViewerResources[i].name;
|
2017-01-21 14:57:29 +00:00
|
|
|
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-01-21 14:57:29 +00:00
|
|
|
|
2017-02-09 11:00:17 +00:00
|
|
|
void StartDataRequest(
|
|
|
|
const std::string& path,
|
|
|
|
const content::ResourceRequestInfo::WebContentsGetter& wc_getter,
|
|
|
|
const GotDataCallback& callback) override {
|
2017-01-21 14:57:29 +00:00
|
|
|
std::string filename = PathWithoutParams(path);
|
2017-02-14 10:55:05 +00:00
|
|
|
auto entry = path_to_resource_id_.find(filename);
|
2017-01-23 22:49:18 +00:00
|
|
|
|
2017-01-21 14:57:29 +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());
|
2017-01-21 14:57:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2017-12-16 09:28:30 +00:00
|
|
|
return "text/html";
|
2017-01-21 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2017-02-14 10:55:05 +00:00
|
|
|
std::map<std::string, int> path_to_resource_id_;
|
2017-01-21 14:57:29 +00:00
|
|
|
|
|
|
|
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));
|
2017-02-27 06:10:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 14:57:29 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-02-27 15:01:54 +00:00
|
|
|
class PdfViewerUI::ResourceRequester
|
|
|
|
: public base::RefCountedThreadSafe<ResourceRequester,
|
|
|
|
BrowserThread::DeleteOnIOThread>,
|
2019-06-19 21:23:04 +00:00
|
|
|
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 =
|
2018-04-12 12:48:32 +00:00
|
|
|
std::make_unique<content::StreamResourceHandler>(
|
2017-08-08 14:04:16 +00:00
|
|
|
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.
|
2019-06-19 21:23:04 +00:00
|
|
|
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:
|
2019-06-19 21:23:04 +00:00
|
|
|
// electron::LayeredResourceHandler::Delegate:
|
2018-04-11 10:45:42 +00:00
|
|
|
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;
|
|
|
|
|
2019-01-12 01:00:43 +00:00
|
|
|
base::PostTaskWithTraits(
|
|
|
|
FROM_HERE, {BrowserThread::UI},
|
2019-05-02 15:32:33 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2017-01-21 14:57:29 +00:00
|
|
|
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) {
|
2017-02-27 06:10:49 +00:00
|
|
|
pdf_handler_ = new PdfViewerHandler(src);
|
2017-04-13 10:21:30 +00:00
|
|
|
web_ui->AddMessageHandler(
|
|
|
|
std::unique_ptr<content::WebUIMessageHandler>(pdf_handler_));
|
2017-01-21 14:57:29 +00:00
|
|
|
content::URLDataSource::Add(browser_context, new BundledDataSource);
|
|
|
|
}
|
|
|
|
|
2017-01-21 19:52:23 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-27 06:10:49 +00:00
|
|
|
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;
|
2017-02-27 06:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2019-01-12 01:00:43 +00:00
|
|
|
base::PostTaskWithTraits(
|
|
|
|
FROM_HERE, {BrowserThread::IO},
|
2019-05-02 15:32:33 +00:00
|
|
|
base::BindOnce(&ResourceRequester::StartRequest, resource_requester_,
|
|
|
|
GURL(src_), GURL(kPdfViewerUIOrigin), render_process_id,
|
|
|
|
render_view_id, render_frame_id, resource_context));
|
2017-02-27 06:10:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 19:52:23 +00:00
|
|
|
void PdfViewerUI::OnSaveURLAs(const GURL& url,
|
|
|
|
const content::Referrer& referrer) {
|
|
|
|
web_contents()->SaveFrame(url, referrer);
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|