2017-01-18 13:58:20 +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_handler.h"
|
2017-01-18 13:58:20 +00:00
|
|
|
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
2017-01-23 11:12:39 +00:00
|
|
|
#include "base/bind.h"
|
2017-01-18 13:58:20 +00:00
|
|
|
#include "base/values.h"
|
2017-12-16 09:21:29 +00:00
|
|
|
#include "chrome/browser/browser_process.h"
|
2017-01-20 19:16:38 +00:00
|
|
|
#include "content/public/browser/stream_handle.h"
|
|
|
|
#include "content/public/browser/stream_info.h"
|
2017-01-18 13:58:20 +00:00
|
|
|
#include "content/public/browser/web_contents.h"
|
|
|
|
#include "content/public/browser/web_ui.h"
|
2017-01-18 16:25:36 +00:00
|
|
|
#include "content/public/common/page_zoom.h"
|
2017-02-03 09:36:53 +00:00
|
|
|
#include "content/public/common/url_constants.h"
|
2017-01-20 19:16:38 +00:00
|
|
|
#include "net/http/http_response_headers.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/atom_constants.h"
|
2017-01-22 14:50:38 +00:00
|
|
|
#include "ui/base/l10n/l10n_util.h"
|
|
|
|
#include "ui/base/webui/web_ui_util.h"
|
2017-01-18 13:58:20 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2017-01-18 13:58:20 +00:00
|
|
|
|
2017-01-20 19:16:38 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void CreateResponseHeadersDictionary(const net::HttpResponseHeaders* headers,
|
|
|
|
base::DictionaryValue* result) {
|
|
|
|
if (!headers)
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t iter = 0;
|
|
|
|
std::string header_name;
|
|
|
|
std::string header_value;
|
|
|
|
while (headers->EnumerateHeaderLines(&iter, &header_name, &header_value)) {
|
|
|
|
base::Value* existing_value = nullptr;
|
|
|
|
if (result->Get(header_name, &existing_value)) {
|
2017-04-05 09:02:06 +00:00
|
|
|
std::string src = existing_value->GetString();
|
|
|
|
result->SetString(header_name, src + ", " + header_value);
|
2017-01-20 19:16:38 +00:00
|
|
|
} else {
|
|
|
|
result->SetString(header_name, header_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-27 06:10:49 +00:00
|
|
|
void PopulateStreamInfo(base::DictionaryValue* stream_info,
|
|
|
|
content::StreamInfo* stream,
|
|
|
|
const std::string& original_url) {
|
2018-04-12 12:48:32 +00:00
|
|
|
auto headers_dict = std::make_unique<base::DictionaryValue>();
|
2017-02-27 06:10:49 +00:00
|
|
|
auto stream_url = stream->handle->GetURL().spec();
|
|
|
|
CreateResponseHeadersDictionary(stream->response_headers.get(),
|
|
|
|
headers_dict.get());
|
|
|
|
stream_info->SetString("streamURL", stream_url);
|
|
|
|
stream_info->SetString("originalURL", original_url);
|
|
|
|
stream_info->Set("responseHeaders", std::move(headers_dict));
|
|
|
|
}
|
|
|
|
|
2017-01-20 19:16:38 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-02-27 06:10:49 +00:00
|
|
|
PdfViewerHandler::PdfViewerHandler(const std::string& src)
|
2018-05-21 22:18:38 +00:00
|
|
|
: original_url_(src) {}
|
2017-01-18 13:58:20 +00:00
|
|
|
|
2017-10-19 14:27:08 +00:00
|
|
|
PdfViewerHandler::~PdfViewerHandler() {
|
|
|
|
RemoveObserver();
|
|
|
|
}
|
2017-01-18 13:58:20 +00:00
|
|
|
|
2017-02-27 06:10:49 +00:00
|
|
|
void PdfViewerHandler::SetPdfResourceStream(content::StreamInfo* stream) {
|
|
|
|
stream_ = stream;
|
2017-03-11 22:03:26 +00:00
|
|
|
if (!!initialize_callback_id_.get()) {
|
2018-04-12 12:48:32 +00:00
|
|
|
auto list = std::make_unique<base::ListValue>();
|
2017-02-27 06:10:49 +00:00
|
|
|
list->Set(0, std::move(initialize_callback_id_));
|
|
|
|
Initialize(list.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 13:58:20 +00:00
|
|
|
void PdfViewerHandler::RegisterMessages() {
|
|
|
|
web_ui()->RegisterMessageCallback(
|
2019-05-02 15:32:33 +00:00
|
|
|
"initialize", base::BindRepeating(&PdfViewerHandler::Initialize,
|
|
|
|
base::Unretained(this)));
|
2017-01-18 13:58:20 +00:00
|
|
|
web_ui()->RegisterMessageCallback(
|
2019-05-02 15:32:33 +00:00
|
|
|
"getDefaultZoom", base::BindRepeating(&PdfViewerHandler::GetInitialZoom,
|
|
|
|
base::Unretained(this)));
|
2017-01-18 16:25:36 +00:00
|
|
|
web_ui()->RegisterMessageCallback(
|
2019-05-02 15:32:33 +00:00
|
|
|
"getInitialZoom", base::BindRepeating(&PdfViewerHandler::GetInitialZoom,
|
|
|
|
base::Unretained(this)));
|
2017-03-01 17:11:51 +00:00
|
|
|
web_ui()->RegisterMessageCallback(
|
|
|
|
"setZoom",
|
2019-05-02 15:32:33 +00:00
|
|
|
base::BindRepeating(&PdfViewerHandler::SetZoom, base::Unretained(this)));
|
2017-01-22 14:50:38 +00:00
|
|
|
web_ui()->RegisterMessageCallback(
|
2019-05-02 15:32:33 +00:00
|
|
|
"getStrings", base::BindRepeating(&PdfViewerHandler::GetStrings,
|
|
|
|
base::Unretained(this)));
|
2017-02-14 01:25:52 +00:00
|
|
|
web_ui()->RegisterMessageCallback(
|
2019-05-02 15:32:33 +00:00
|
|
|
"reload",
|
|
|
|
base::BindRepeating(&PdfViewerHandler::Reload, base::Unretained(this)));
|
2017-01-18 16:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PdfViewerHandler::OnJavascriptAllowed() {
|
2017-10-19 14:27:08 +00:00
|
|
|
AddObserver();
|
2017-01-18 16:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PdfViewerHandler::OnJavascriptDisallowed() {
|
2017-10-19 14:27:08 +00:00
|
|
|
RemoveObserver();
|
2017-01-18 13:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PdfViewerHandler::Initialize(const base::ListValue* args) {
|
|
|
|
CHECK_EQ(1U, args->GetSize());
|
|
|
|
const base::Value* callback_id;
|
|
|
|
CHECK(args->Get(0, &callback_id));
|
2017-02-27 06:10:49 +00:00
|
|
|
|
2017-01-23 11:12:39 +00:00
|
|
|
if (stream_) {
|
2017-03-11 22:03:26 +00:00
|
|
|
CHECK(!initialize_callback_id_.get());
|
2017-02-27 06:10:49 +00:00
|
|
|
AllowJavascript();
|
|
|
|
|
2018-04-12 12:48:32 +00:00
|
|
|
auto stream_info = std::make_unique<base::DictionaryValue>();
|
2017-02-27 06:10:49 +00:00
|
|
|
PopulateStreamInfo(stream_info.get(), stream_, original_url_);
|
|
|
|
ResolveJavascriptCallback(*callback_id, *stream_info);
|
|
|
|
} else {
|
2018-08-03 21:23:07 +00:00
|
|
|
initialize_callback_id_ =
|
|
|
|
base::Value::ToUniquePtrValue(callback_id.Clone());
|
2017-01-23 11:12:39 +00:00
|
|
|
}
|
2017-06-08 07:57:24 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
auto zoom_controller =
|
|
|
|
WebContentsZoomController::FromWebContents(web_ui()->GetWebContents());
|
2019-05-03 18:11:41 +00:00
|
|
|
zoom_controller->SetZoomMode(WebContentsZoomController::ZoomMode::MANUAL);
|
2017-06-08 00:13:49 +00:00
|
|
|
zoom_controller->SetZoomLevel(0);
|
2017-01-18 13:58:20 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 16:25:36 +00:00
|
|
|
void PdfViewerHandler::GetDefaultZoom(const base::ListValue* args) {
|
|
|
|
if (!IsJavascriptAllowed())
|
|
|
|
return;
|
|
|
|
CHECK_EQ(1U, args->GetSize());
|
|
|
|
const base::Value* callback_id;
|
|
|
|
CHECK(args->Get(0, &callback_id));
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
auto zoom_controller =
|
|
|
|
WebContentsZoomController::FromWebContents(web_ui()->GetWebContents());
|
2017-06-08 00:13:49 +00:00
|
|
|
double zoom_level = zoom_controller->GetDefaultZoomLevel();
|
2017-01-18 16:25:36 +00:00
|
|
|
ResolveJavascriptCallback(
|
2018-04-18 01:55:30 +00:00
|
|
|
*callback_id, base::Value(content::ZoomLevelToZoomFactor(zoom_level)));
|
2017-01-18 16:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PdfViewerHandler::GetInitialZoom(const base::ListValue* args) {
|
2017-01-18 13:58:20 +00:00
|
|
|
if (!IsJavascriptAllowed())
|
|
|
|
return;
|
|
|
|
CHECK_EQ(1U, args->GetSize());
|
|
|
|
const base::Value* callback_id;
|
|
|
|
CHECK(args->Get(0, &callback_id));
|
2017-01-18 16:25:36 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
auto zoom_controller =
|
|
|
|
WebContentsZoomController::FromWebContents(web_ui()->GetWebContents());
|
2017-06-08 00:13:49 +00:00
|
|
|
double zoom_level = zoom_controller->GetZoomLevel();
|
2017-01-18 16:25:36 +00:00
|
|
|
ResolveJavascriptCallback(
|
2018-04-18 01:55:30 +00:00
|
|
|
*callback_id, base::Value(content::ZoomLevelToZoomFactor(zoom_level)));
|
2017-01-18 16:25:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 17:11:51 +00:00
|
|
|
void PdfViewerHandler::SetZoom(const base::ListValue* args) {
|
|
|
|
if (!IsJavascriptAllowed())
|
|
|
|
return;
|
|
|
|
CHECK_EQ(2U, args->GetSize());
|
|
|
|
const base::Value* callback_id;
|
|
|
|
CHECK(args->Get(0, &callback_id));
|
|
|
|
double zoom_level = 0.0;
|
|
|
|
CHECK(args->GetDouble(1, &zoom_level));
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
auto zoom_controller =
|
|
|
|
WebContentsZoomController::FromWebContents(web_ui()->GetWebContents());
|
2017-06-08 00:13:49 +00:00
|
|
|
zoom_controller->SetZoomLevel(zoom_level);
|
2017-04-05 08:34:53 +00:00
|
|
|
ResolveJavascriptCallback(*callback_id, base::Value(zoom_level));
|
2017-03-01 17:11:51 +00:00
|
|
|
}
|
|
|
|
|
2017-01-22 14:50:38 +00:00
|
|
|
void PdfViewerHandler::GetStrings(const base::ListValue* args) {
|
|
|
|
if (!IsJavascriptAllowed())
|
|
|
|
return;
|
|
|
|
CHECK_EQ(1U, args->GetSize());
|
|
|
|
const base::Value* callback_id;
|
|
|
|
CHECK(args->Get(0, &callback_id));
|
|
|
|
|
2018-04-12 12:48:32 +00:00
|
|
|
auto result = std::make_unique<base::DictionaryValue>();
|
2017-02-01 15:42:17 +00:00
|
|
|
// TODO(deepak1556): Generate strings from components/pdf_strings.grdp.
|
2017-01-22 14:50:38 +00:00
|
|
|
#define SET_STRING(id, resource) result->SetString(id, resource)
|
|
|
|
SET_STRING("passwordPrompt",
|
|
|
|
"This document is password protected. Please enter a password.");
|
|
|
|
SET_STRING("passwordSubmit", "Submit");
|
|
|
|
SET_STRING("passwordInvalid", "Incorrect password");
|
|
|
|
SET_STRING("pageLoading", "Loading...");
|
|
|
|
SET_STRING("pageLoadFailed", "Failed to load PDF document");
|
|
|
|
SET_STRING("pageReload", "Reload");
|
|
|
|
SET_STRING("bookmarks", "Bookmarks");
|
|
|
|
SET_STRING("labelPageNumber", "Page number");
|
|
|
|
SET_STRING("tooltipRotateCW", "Rotate clockwise");
|
|
|
|
SET_STRING("tooltipDownload", "Download");
|
|
|
|
SET_STRING("tooltipFitToPage", "Fit to page");
|
|
|
|
SET_STRING("tooltipFitToWidth", "Fit to width");
|
|
|
|
SET_STRING("tooltipZoomIn", "Zoom in");
|
|
|
|
SET_STRING("tooltipZoomOut", "Zoom out");
|
|
|
|
#undef SET_STRING
|
|
|
|
|
2017-12-16 09:21:29 +00:00
|
|
|
webui::SetLoadTimeDataDefaults(g_browser_process->GetApplicationLocale(),
|
2017-01-22 14:50:38 +00:00
|
|
|
result.get());
|
|
|
|
ResolveJavascriptCallback(*callback_id, *result);
|
|
|
|
}
|
|
|
|
|
2017-02-14 01:25:52 +00:00
|
|
|
void PdfViewerHandler::Reload(const base::ListValue* args) {
|
|
|
|
CHECK_EQ(0U, args->GetSize());
|
|
|
|
web_ui()->GetWebContents()->ReloadFocusedFrame(false);
|
|
|
|
}
|
|
|
|
|
2017-06-08 00:13:49 +00:00
|
|
|
void PdfViewerHandler::OnZoomLevelChanged(content::WebContents* web_contents,
|
2018-04-18 01:55:30 +00:00
|
|
|
double level,
|
|
|
|
bool is_temporary) {
|
2017-06-08 00:13:49 +00:00
|
|
|
if (web_ui()->GetWebContents() == web_contents) {
|
2017-06-08 07:57:24 +00:00
|
|
|
CallJavascriptFunction("cr.webUIListenerCallback",
|
2018-04-18 01:55:30 +00:00
|
|
|
base::Value("onZoomLevelChanged"),
|
|
|
|
base::Value(content::ZoomLevelToZoomFactor(level)));
|
2017-01-18 16:25:36 +00:00
|
|
|
}
|
2017-01-18 13:58:20 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 14:27:08 +00:00
|
|
|
void PdfViewerHandler::AddObserver() {
|
|
|
|
auto zoom_controller =
|
|
|
|
WebContentsZoomController::FromWebContents(web_ui()->GetWebContents());
|
|
|
|
zoom_controller->AddObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PdfViewerHandler::RemoveObserver() {
|
|
|
|
auto zoom_controller =
|
|
|
|
WebContentsZoomController::FromWebContents(web_ui()->GetWebContents());
|
|
|
|
zoom_controller->RemoveObserver(this);
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|