add zoom behaviour code from chromium and make pdf viewer use manual zoom behaviour to match chromium

This commit is contained in:
Heilig Benedek 2017-06-08 02:13:49 +02:00
parent a47fe715d1
commit 0f4341da42
4 changed files with 210 additions and 34 deletions

View file

@ -8,6 +8,9 @@
#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/values.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/stream_handle.h"
#include "content/public/browser/stream_info.h"
#include "content/public/browser/web_contents.h"
@ -90,15 +93,15 @@ void PdfViewerHandler::RegisterMessages() {
}
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)));
auto zoom_controller = WebContentsZoomController::FromWebContents(
web_ui()->GetWebContents());
zoom_controller->AddObserver(this);
}
void PdfViewerHandler::OnJavascriptDisallowed() {
host_zoom_map_subscription_.reset();
auto zoom_controller = WebContentsZoomController::FromWebContents(
web_ui()->GetWebContents());
zoom_controller->RemoveObserver(this);
}
void PdfViewerHandler::Initialize(const base::ListValue* args) {
@ -116,6 +119,11 @@ void PdfViewerHandler::Initialize(const base::ListValue* args) {
} else {
initialize_callback_id_ = callback_id->CreateDeepCopy();
}
auto zoom_controller = WebContentsZoomController::FromWebContents(
web_ui()->GetWebContents());
zoom_controller->SetZoomMode(WebContentsZoomController::ZOOM_MODE_MANUAL);
zoom_controller->SetZoomLevel(0);
}
void PdfViewerHandler::GetDefaultZoom(const base::ListValue* args) {
@ -125,9 +133,9 @@ void PdfViewerHandler::GetDefaultZoom(const base::ListValue* args) {
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();
auto zoom_controller = WebContentsZoomController::FromWebContents(
web_ui()->GetWebContents());
double zoom_level = zoom_controller->GetDefaultZoomLevel();
ResolveJavascriptCallback(
*callback_id,
base::Value(content::ZoomLevelToZoomFactor(zoom_level)));
@ -140,8 +148,9 @@ void PdfViewerHandler::GetInitialZoom(const base::ListValue* args) {
const base::Value* callback_id;
CHECK(args->Get(0, &callback_id));
double zoom_level =
content::HostZoomMap::GetZoomLevel(web_ui()->GetWebContents());
auto zoom_controller = WebContentsZoomController::FromWebContents(
web_ui()->GetWebContents());
double zoom_level = zoom_controller->GetZoomLevel();
ResolveJavascriptCallback(
*callback_id,
base::Value(content::ZoomLevelToZoomFactor(zoom_level)));
@ -156,8 +165,9 @@ void PdfViewerHandler::SetZoom(const base::ListValue* args) {
double zoom_level = 0.0;
CHECK(args->GetDouble(1, &zoom_level));
content::HostZoomMap::SetZoomLevel(web_ui()->GetWebContents(),
zoom_level);
auto zoom_controller = WebContentsZoomController::FromWebContents(
web_ui()->GetWebContents());
zoom_controller->SetZoomLevel(zoom_level);
ResolveJavascriptCallback(*callback_id, base::Value(zoom_level));
}
@ -198,13 +208,12 @@ void PdfViewerHandler::Reload(const base::ListValue* args) {
web_ui()->GetWebContents()->ReloadFocusedFrame(false);
}
void PdfViewerHandler::OnZoomLevelChanged(
const content::HostZoomMap::ZoomLevelChange& change) {
if (change.host == kPdfViewerUIHost) {
CallJavascriptFunction(
"cr.webUIListenerCallback", base::StringValue("onZoomLevelChanged"),
base::Value(
content::ZoomLevelToZoomFactor(change.zoom_level)));
void PdfViewerHandler::OnZoomLevelChanged(content::WebContents* web_contents,
double level, bool is_temporary) {
if (web_ui()->GetWebContents() == web_contents) {
CallJavascriptFunction("cr.webUIListenerCallback",
base::StringValue("onZoomLevelChanged"),
base::Value(content::ZoomLevelToZoomFactor(level)));
}
}

View file

@ -7,6 +7,7 @@
#include <string>
#include "atom/browser/web_contents_zoom_controller.h"
#include "base/macros.h"
#include "content/public/browser/host_zoom_map.h"
#include "content/public/browser/web_ui_message_handler.h"
@ -21,7 +22,8 @@ struct StreamInfo;
namespace atom {
class PdfViewerHandler : public content::WebUIMessageHandler {
class PdfViewerHandler : public content::WebUIMessageHandler,
public WebContentsZoomController::Observer {
public:
explicit PdfViewerHandler(const std::string& original_url);
~PdfViewerHandler() override;
@ -41,11 +43,9 @@ class PdfViewerHandler : public content::WebUIMessageHandler {
void SetZoom(const base::ListValue* args);
void GetStrings(const base::ListValue* args);
void Reload(const base::ListValue* args);
void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change);
void OnZoomLevelChanged(content::WebContents* web_contents, double level,
bool is_temporary);
// Keeps track of events related to zooming.
std::unique_ptr<content::HostZoomMap::Subscription>
host_zoom_map_subscription_;
std::unique_ptr<base::Value> initialize_callback_id_;
content::StreamInfo* stream_;
std::string original_url_;