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

232 lines
8 KiB
C++
Raw Normal View History

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.
#include "atom/browser/ui/webui/pdf_viewer_handler.h"
#include <memory>
#include <utility>
2017-02-03 09:36:53 +00:00
#include "atom/common/atom_constants.h"
2017-01-23 11:12:39 +00:00
#include "base/bind.h"
2017-01-18 13:58:20 +00:00
#include "base/values.h"
#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"
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
namespace atom {
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)) {
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);
}
}
}
void PopulateStreamInfo(base::DictionaryValue* stream_info,
content::StreamInfo* stream,
const std::string& original_url) {
auto headers_dict = std::make_unique<base::DictionaryValue>();
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
PdfViewerHandler::PdfViewerHandler(const std::string& src)
: original_url_(src) {}
2017-01-18 13:58:20 +00:00
PdfViewerHandler::~PdfViewerHandler() {
RemoveObserver();
}
2017-01-18 13:58:20 +00:00
void PdfViewerHandler::SetPdfResourceStream(content::StreamInfo* stream) {
stream_ = stream;
2017-03-11 22:03:26 +00:00
if (!!initialize_callback_id_.get()) {
auto list = std::make_unique<base::ListValue>();
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(
"initialize", base::BindRepeating(&PdfViewerHandler::Initialize,
base::Unretained(this)));
2017-01-18 13:58:20 +00:00
web_ui()->RegisterMessageCallback(
"getDefaultZoom", base::BindRepeating(&PdfViewerHandler::GetInitialZoom,
base::Unretained(this)));
2017-01-18 16:25:36 +00:00
web_ui()->RegisterMessageCallback(
"getInitialZoom", base::BindRepeating(&PdfViewerHandler::GetInitialZoom,
base::Unretained(this)));
2017-03-01 17:11:51 +00:00
web_ui()->RegisterMessageCallback(
"setZoom",
base::BindRepeating(&PdfViewerHandler::SetZoom, base::Unretained(this)));
2017-01-22 14:50:38 +00:00
web_ui()->RegisterMessageCallback(
"getStrings", base::BindRepeating(&PdfViewerHandler::GetStrings,
base::Unretained(this)));
2017-02-14 01:25:52 +00:00
web_ui()->RegisterMessageCallback(
"reload",
base::BindRepeating(&PdfViewerHandler::Reload, base::Unretained(this)));
2017-01-18 16:25:36 +00:00
}
void PdfViewerHandler::OnJavascriptAllowed() {
AddObserver();
2017-01-18 16:25:36 +00:00
}
void PdfViewerHandler::OnJavascriptDisallowed() {
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-01-23 11:12:39 +00:00
if (stream_) {
2017-03-11 22:03:26 +00:00
CHECK(!initialize_callback_id_.get());
AllowJavascript();
auto stream_info = std::make_unique<base::DictionaryValue>();
PopulateStreamInfo(stream_info.get(), stream_, original_url_);
ResolveJavascriptCallback(*callback_id, *stream_info);
} else {
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());
zoom_controller->SetZoomMode(WebContentsZoomController::ZoomMode::MANUAL);
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());
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());
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());
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));
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
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);
}
void PdfViewerHandler::OnZoomLevelChanged(content::WebContents* web_contents,
2018-04-18 01:55:30 +00:00
double level,
bool is_temporary) {
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
}
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);
}
2017-01-18 13:58:20 +00:00
} // namespace atom