use zoom factor webpreference option when required.

* When setzoomlevel is not called for the host.
* When there is no zoom preference for the host.
This commit is contained in:
deepak1556 2017-01-30 00:38:28 +05:30
parent b27c94368d
commit 07794a58aa
4 changed files with 41 additions and 21 deletions

View file

@ -68,6 +68,7 @@
#include "content/public/common/context_menu_params.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "net/base/url_util.h"
#include "net/url_request/url_request_context.h"
#include "third_party/WebKit/public/platform/WebInputEvent.h"
#include "third_party/WebKit/public/web/WebFindOptions.h"
@ -252,7 +253,8 @@ WebContents::WebContents(v8::Isolate* isolate,
type_(type),
request_id_(0),
background_throttling_(true),
enable_devtools_(true) {
enable_devtools_(true),
zoom_factor_(content::kMinimumZoomFactor) {
if (type == REMOTE) {
web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
@ -272,9 +274,11 @@ WebContents::WebContents(v8::Isolate* isolate,
type_(BROWSER_WINDOW),
request_id_(0),
background_throttling_(true),
enable_devtools_(true) {
enable_devtools_(true),
zoom_factor_(content::kMinimumZoomFactor) {
// Read options.
options.Get("backgroundThrottling", &background_throttling_);
options.Get("zoomFactor", &zoom_factor_);
// FIXME(zcbenz): We should read "type" parameter for better design, but
// on Windows we have encountered a compiler bug that if we read "type"
@ -734,6 +738,8 @@ void WebContents::DidFinishNavigation(
auto url = navigation_handle->GetURL();
bool is_in_page = navigation_handle->IsSamePage();
if (is_main_frame && !is_in_page) {
// Set initial zoom factor if needed.
SetZoomFactorIfNeeded(url);
Emit("did-navigate", url);
} else if (is_in_page) {
Emit("did-navigate-in-page", url, is_main_frame);
@ -1500,6 +1506,15 @@ void WebContents::Invalidate() {
}
void WebContents::SetZoomLevel(double level) {
auto factor = content::ZoomLevelToZoomFactor(level);
if (!content::ZoomValuesEqual(zoom_factor_, factor)) {
content::NavigationEntry* entry =
web_contents()->GetController().GetLastCommittedEntry();
if (entry) {
std::string host = net::GetHostOrSpecFromURL(entry->GetURL());
host_zoom_factor_[host] = factor;
}
}
content::HostZoomMap::SetZoomLevel(web_contents(), level);
}
@ -1517,6 +1532,22 @@ double WebContents::GetZoomFactor() {
return content::ZoomLevelToZoomFactor(level);
}
void WebContents::SetZoomFactorIfNeeded(const GURL& url) {
if (zoom_factor_ == content::kMinimumZoomFactor)
return;
std::string host = net::GetHostOrSpecFromURL(url);
double zoom_factor = zoom_factor_;
auto it = host_zoom_factor_.find(host);
if (it != host_zoom_factor_.end())
zoom_factor = it->second;
auto level = content::ZoomFactorToZoomLevel(zoom_factor);
if (content::ZoomValuesEqual(level, GetZoomLevel()))
return;
SetZoomLevel(level);
}
v8::Local<v8::Value> WebContents::GetWebPreferences(v8::Isolate* isolate) {
WebContentsPreferences* web_preferences =
WebContentsPreferences::FromWebContents(web_contents());

View file

@ -349,11 +349,16 @@ class WebContents : public mate::TrackableObject<WebContents>,
const base::ListValue& args,
IPC::Message* message);
// Called after committing a navigation, to set the zoom
// factor.
void SetZoomFactorIfNeeded(const GURL& url);
v8::Global<v8::Value> session_;
v8::Global<v8::Value> devtools_web_contents_;
v8::Global<v8::Value> debugger_;
std::unique_ptr<WebViewGuestDelegate> guest_delegate_;
std::map<std::string, double> host_zoom_factor_;
// The host webcontents that may contain this webcontents.
WebContents* embedder_;
@ -370,6 +375,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
// Whether to enable devtools.
bool enable_devtools_;
// Initial zoom factor.
double zoom_factor_;
DISALLOW_COPY_AND_ASSIGN(WebContents);
};

View file

@ -130,13 +130,6 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches(
if (web_preferences.GetString(options::kBackgroundColor, &color))
command_line->AppendSwitchASCII(switches::kBackgroundColor, color);
// The zoom factor.
double zoom_factor = 1.0;
if (web_preferences.GetDouble(options::kZoomFactor, &zoom_factor) &&
zoom_factor != 1.0)
command_line->AppendSwitchASCII(switches::kZoomFactor,
base::DoubleToString(zoom_factor));
// --guest-instance-id, which is used to identify guest WebContents.
int guest_instance_id = 0;
if (web_preferences.GetInteger(options::kGuestInstanceID, &guest_instance_id))

View file

@ -14,7 +14,6 @@
#include "atom/common/api/event_emitter_caller.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "atom/common/node_includes.h"
#include "atom/common/options_switches.h"
#include "atom/renderer/atom_renderer_client.h"
#include "base/command_line.h"
#include "base/strings/string_number_conversions.h"
@ -117,17 +116,6 @@ void AtomRenderViewObserver::EmitIPCEvent(blink::WebFrame* frame,
void AtomRenderViewObserver::DidCreateDocumentElement(
blink::WebLocalFrame* frame) {
document_created_ = true;
// Read --zoom-factor from command line.
std::string zoom_factor_str = base::CommandLine::ForCurrentProcess()->
GetSwitchValueASCII(switches::kZoomFactor);
if (zoom_factor_str.empty())
return;
double zoom_factor;
if (!base::StringToDouble(zoom_factor_str, &zoom_factor))
return;
double zoom_level = blink::WebView::zoomFactorToZoomLevel(zoom_factor);
frame->view()->setZoomLevel(zoom_level);
}
void AtomRenderViewObserver::DraggableRegionsChanged(blink::WebFrame* frame) {