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-20 19:16:38 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
|
|
|
#include "atom/browser/stream_manager.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
|
|
|
|
|
|
|
|
PdfViewerHandler::PdfViewerHandler(const std::string& view_id)
|
|
|
|
: view_id_(view_id) {}
|
2017-01-18 13:58:20 +00:00
|
|
|
|
|
|
|
PdfViewerHandler::~PdfViewerHandler() {}
|
|
|
|
|
|
|
|
void PdfViewerHandler::RegisterMessages() {
|
2017-01-20 19:16:38 +00:00
|
|
|
auto browser_context = static_cast<AtomBrowserContext*>(
|
|
|
|
web_ui()->GetWebContents()->GetBrowserContext());
|
|
|
|
auto stream_manager = browser_context->stream_manager();
|
|
|
|
stream_ = stream_manager->ReleaseStream(view_id_);
|
|
|
|
|
2017-01-18 13:58:20 +00:00
|
|
|
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
|
|
|
auto stream_url = stream_->handle->GetURL().spec();
|
|
|
|
auto original_url = stream_->original_url.spec();
|
|
|
|
stream_info->SetString("streamURL", stream_url);
|
|
|
|
stream_info->SetString("originalURL", original_url);
|
|
|
|
std::unique_ptr<base::DictionaryValue> headers_dict(
|
|
|
|
new base::DictionaryValue);
|
|
|
|
CreateResponseHeadersDictionary(stream_->response_headers.get(),
|
|
|
|
headers_dict.get());
|
|
|
|
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
|