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"
|
|
|
|
|
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-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-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)) {
|
|
|
|
base::StringValue* existing_string_value =
|
|
|
|
static_cast<base::StringValue*>(existing_value);
|
|
|
|
existing_string_value->GetString()->append(", ").append(header_value);
|
|
|
|
} else {
|
|
|
|
result->SetString(header_name, header_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-01-23 11:12:39 +00:00
|
|
|
PdfViewerHandler::PdfViewerHandler(const content::StreamInfo* stream,
|
|
|
|
const std::string& src)
|
|
|
|
: stream_(stream), original_url_(src) {}
|
2017-01-18 13:58:20 +00:00
|
|
|
|
|
|
|
PdfViewerHandler::~PdfViewerHandler() {}
|
|
|
|
|
|
|
|
void PdfViewerHandler::RegisterMessages() {
|
|
|
|
web_ui()->RegisterMessageCallback(
|
|
|
|
"initialize",
|
|
|
|
base::Bind(&PdfViewerHandler::Initialize, base::Unretained(this)));
|
|
|
|
web_ui()->RegisterMessageCallback(
|
2017-01-18 16:25:36 +00:00
|
|
|
"getDefaultZoom",
|
|
|
|
base::Bind(&PdfViewerHandler::GetInitialZoom, base::Unretained(this)));
|
|
|
|
web_ui()->RegisterMessageCallback(
|
|
|
|
"getInitialZoom",
|
|
|
|
base::Bind(&PdfViewerHandler::GetInitialZoom, base::Unretained(this)));
|
2017-01-22 14:50:38 +00:00
|
|
|
web_ui()->RegisterMessageCallback(
|
|
|
|
"getStrings",
|
|
|
|
base::Bind(&PdfViewerHandler::GetStrings, base::Unretained(this)));
|
2017-01-18 16:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PdfViewerHandler::OnJavascriptAllowed() {
|
|
|
|
auto host_zoom_map =
|
|
|
|
content::HostZoomMap::GetForWebContents(web_ui()->GetWebContents());
|
|
|
|
host_zoom_map_subscription_ =
|
|
|
|
host_zoom_map->AddZoomLevelChangedCallback(base::Bind(
|
|
|
|
&PdfViewerHandler::OnZoomLevelChanged, base::Unretained(this)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PdfViewerHandler::OnJavascriptDisallowed() {
|
|
|
|
host_zoom_map_subscription_.reset();
|
2017-01-18 13:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PdfViewerHandler::Initialize(const base::ListValue* args) {
|
|
|
|
AllowJavascript();
|
|
|
|
|
|
|
|
CHECK_EQ(1U, args->GetSize());
|
|
|
|
const base::Value* callback_id;
|
|
|
|
CHECK(args->Get(0, &callback_id));
|
|
|
|
std::unique_ptr<base::DictionaryValue> stream_info(new base::DictionaryValue);
|
2017-01-20 19:16:38 +00:00
|
|
|
std::unique_ptr<base::DictionaryValue> headers_dict(
|
|
|
|
new base::DictionaryValue);
|
2017-01-23 11:12:39 +00:00
|
|
|
std::string stream_url = original_url_;
|
|
|
|
if (stream_) {
|
|
|
|
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_);
|
2017-01-20 19:16:38 +00:00
|
|
|
stream_info->Set("responseHeaders", std::move(headers_dict));
|
2017-01-18 13:58:20 +00:00
|
|
|
ResolveJavascriptCallback(*callback_id, *stream_info);
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
auto host_zoom_map =
|
|
|
|
content::HostZoomMap::GetForWebContents(web_ui()->GetWebContents());
|
|
|
|
double zoom_level = host_zoom_map->GetDefaultZoomLevel();
|
|
|
|
ResolveJavascriptCallback(
|
|
|
|
*callback_id,
|
|
|
|
base::FundamentalValue(content::ZoomLevelToZoomFactor(zoom_level)));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
double zoom_level =
|
|
|
|
content::HostZoomMap::GetZoomLevel(web_ui()->GetWebContents());
|
|
|
|
ResolveJavascriptCallback(
|
|
|
|
*callback_id,
|
|
|
|
base::FundamentalValue(content::ZoomLevelToZoomFactor(zoom_level)));
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue);
|
|
|
|
// TODO(deepak1556): Generate strings from componenets/pdf_strings.grdp.
|
|
|
|
#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("tooltipPrint", "Print");
|
|
|
|
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(l10n_util::GetApplicationLocale(""),
|
|
|
|
result.get());
|
|
|
|
ResolveJavascriptCallback(*callback_id, *result);
|
|
|
|
}
|
|
|
|
|
2017-01-18 16:25:36 +00:00
|
|
|
void PdfViewerHandler::OnZoomLevelChanged(
|
|
|
|
const content::HostZoomMap::ZoomLevelChange& change) {
|
|
|
|
// TODO(deepak1556): This will work only if zoom level is changed through host
|
|
|
|
// zoom map.
|
|
|
|
if (change.scheme == "chrome" && change.host == "pdf-viewer") {
|
|
|
|
CallJavascriptFunction(
|
|
|
|
"cr.webUIListenerCallback", base::StringValue("onZoomLevelChanged"),
|
|
|
|
base::FundamentalValue(
|
|
|
|
content::ZoomLevelToZoomFactor(change.zoom_level)));
|
|
|
|
}
|
2017-01-18 13:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|