Merge pull request #8864 from electron/zoom_controller_webview_patch

browser: track origin zoom with HostZoomMap
This commit is contained in:
Kevin Sawicki 2017-03-09 11:27:34 -08:00 committed by GitHub
commit 14e8e463b3
4 changed files with 45 additions and 44 deletions

View file

@ -26,8 +26,6 @@ WebContentsZoomController::WebContentsZoomController(
embedder_zoom_controller_(nullptr) {
default_zoom_factor_ = content::kEpsilon;
host_zoom_map_ = content::HostZoomMap::GetForWebContents(web_contents);
zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind(
&WebContentsZoomController::OnZoomLevelChanged, base::Unretained(this)));
}
WebContentsZoomController::~WebContentsZoomController() {}
@ -59,19 +57,10 @@ void WebContentsZoomController::SetZoomLevel(double level) {
host_zoom_map_->ClearTemporaryZoomLevel(render_process_id, render_view_id);
}
auto new_zoom_factor = content::ZoomLevelToZoomFactor(level);
content::NavigationEntry* entry =
web_contents()->GetController().GetLastCommittedEntry();
if (entry) {
std::string host = net::GetHostOrSpecFromURL(entry->GetURL());
// When new zoom level varies from kZoomFactor, it takes preference.
if (!content::ZoomValuesEqual(GetDefaultZoomFactor(), new_zoom_factor))
host_zoom_factor_[host] = new_zoom_factor;
content::HostZoomMap::SetZoomLevel(web_contents(), level);
// Notify observers of zoom level changes.
for (Observer& observer : observers_)
observer.OnZoomLevelChanged(web_contents(), level, false);
}
content::HostZoomMap::SetZoomLevel(web_contents(), level);
// Notify observers of zoom level changes.
for (Observer& observer : observers_)
observer.OnZoomLevelChanged(web_contents(), level, false);
}
double WebContentsZoomController::GetZoomLevel() {
@ -117,7 +106,6 @@ void WebContentsZoomController::DidFinishNavigation(
void WebContentsZoomController::WebContentsDestroyed() {
observers_.Clear();
host_zoom_factor_.clear();
embedder_zoom_controller_ = nullptr;
}
@ -131,8 +119,6 @@ void WebContentsZoomController::RenderFrameHostChanged(
return;
host_zoom_map_ = new_host_zoom_map;
zoom_subscription_ = host_zoom_map_->AddZoomLevelChangedCallback(base::Bind(
&WebContentsZoomController::OnZoomLevelChanged, base::Unretained(this)));
}
void WebContentsZoomController::SetZoomFactorOnNavigationIfNeeded(
@ -156,27 +142,16 @@ void WebContentsZoomController::SetZoomFactorOnNavigationIfNeeded(
// then it takes precendence.
// pref store < kZoomFactor < setZoomLevel
std::string host = net::GetHostOrSpecFromURL(url);
std::string scheme = url.scheme();
double zoom_factor = GetDefaultZoomFactor();
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()))
double zoom_level = content::ZoomFactorToZoomLevel(zoom_factor);
if (host_zoom_map_->HasZoomLevel(scheme, host)) {
zoom_level = host_zoom_map_->GetZoomLevelForHostAndScheme(scheme, host);
}
if (content::ZoomValuesEqual(zoom_level, GetZoomLevel()))
return;
SetZoomLevel(level);
}
void WebContentsZoomController::OnZoomLevelChanged(
const content::HostZoomMap::ZoomLevelChange& change) {
if (change.mode == content::HostZoomMap::ZOOM_CHANGED_FOR_HOST) {
auto it = host_zoom_factor_.find(change.host);
if (it == host_zoom_factor_.end())
return;
host_zoom_factor_.insert(
it, std::make_pair(change.host,
content::ZoomLevelToZoomFactor(change.zoom_level)));
}
SetZoomLevel(zoom_level);
}
} // namespace atom

View file

@ -58,9 +58,6 @@ class WebContentsZoomController
// Called after a navigation has committed to set default zoom factor.
void SetZoomFactorOnNavigationIfNeeded(const GURL& url);
// Track zoom changes of a host in other instances of a partition.
void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change);
// kZoomFactor.
double default_zoom_factor_;
double temporary_zoom_level_;
@ -70,15 +67,10 @@ class WebContentsZoomController
WebContentsZoomController* embedder_zoom_controller_;
// Map between zoom factor and hosts in this webContent.
std::map<std::string, double> host_zoom_factor_;
base::ObserverList<Observer> observers_;
content::HostZoomMap* host_zoom_map_;
std::unique_ptr<content::HostZoomMap::Subscription> zoom_subscription_;
DISALLOW_COPY_AND_ASSIGN(WebContentsZoomController);
};

View file

@ -0,0 +1,20 @@
<html>
<body>
<webview nodeintegration src="zoom://host1" id="view" partition="webview-temp"/>
</body>
<script>
const {ipcRenderer} = require('electron')
const view = document.getElementById('view')
const view2 = document.createElement('webview')
view.addEventListener('dom-ready', () => {
view.setZoomLevel(2.0)
view2.src = "zoom://host1"
view2.partition = "webview-temp"
document.body.appendChild(view2)
})
view2.addEventListener('dom-ready', () => {
view2.getZoomLevel((level) => {
ipcRenderer.send('webview-origin-zoom-level', level)
})
})
</script>

View file

@ -1601,5 +1601,19 @@ describe('<webview> tag', function () {
})
w.loadURL(`file://${fixtures}/pages/webview-in-page-navigate.html`)
})
it('inherits zoom level for the origin when available', (done) => {
w = new BrowserWindow({
show: false,
webPreferences: {
zoomFactor: 1.2
}
})
ipcMain.once('webview-origin-zoom-level', (event, zoomLevel) => {
assert.equal(zoomLevel, 2.0)
done()
})
w.loadURL(`file://${fixtures}/pages/webview-origin-zoom-level.html`)
})
})
})