Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1868)

Unified Diff: chrome/browser/ui/zoom/zoom_controller.cc

Issue 301733006: Zoom Extension API (chrome) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add event manager for manual zoom events. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/zoom/zoom_controller.cc
diff --git a/chrome/browser/ui/zoom/zoom_controller.cc b/chrome/browser/ui/zoom/zoom_controller.cc
index 701c0dd24ad34d1fe66524ff1602021d9486933a..258bf3fa95e4b6a96da96481cfd8aa24aa571603 100644
--- a/chrome/browser/ui/zoom/zoom_controller.cc
+++ b/chrome/browser/ui/zoom/zoom_controller.cc
@@ -8,12 +8,15 @@
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_finder.h"
+#include "chrome/browser/ui/zoom/zoom_event_manager.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/host_zoom_map.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
+#include "content/public/browser/render_process_host.h"
+#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/page_zoom.h"
#include "grit/theme_resources.h"
@@ -23,15 +26,18 @@ DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController);
ZoomController::ZoomController(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
- zoom_percent_(100),
+ zoom_mode_(kZoomModeDefault),
observer_(NULL),
browser_context_(web_contents->GetBrowserContext()) {
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
- default_zoom_level_.Init(prefs::kDefaultZoomLevel, profile->GetPrefs(),
- base::Bind(&ZoomController::UpdateState,
- base::Unretained(this),
- std::string()));
+ default_zoom_level_.Init(
+ prefs::kDefaultZoomLevel, profile->GetPrefs(),
+ base::Bind(static_cast<void(ZoomController::*)(
Devlin 2014/06/16 19:04:49 Is this all just because you added the other metho
wjmaclean 2014/06/18 19:03:55 Done.
+ const std::string&)>(&ZoomController::UpdateState),
+ base::Unretained(this),
+ std::string()));
+ zoom_level_ = default_zoom_level_.GetValue();
zoom_subscription_ = content::HostZoomMap::GetForBrowserContext(
browser_context_)->AddZoomLevelChangedCallback(
@@ -44,18 +50,161 @@ ZoomController::ZoomController(content::WebContents* web_contents)
ZoomController::~ZoomController() {}
bool ZoomController::IsAtDefaultZoom() const {
- return content::ZoomValuesEqual(
- content::HostZoomMap::GetZoomLevel(web_contents()),
- default_zoom_level_.GetValue());
+ return content::ZoomValuesEqual(GetZoomLevel(),
+ default_zoom_level_.GetValue());
}
int ZoomController::GetResourceForZoomLevel() const {
if (IsAtDefaultZoom())
return IDR_ZOOM_NORMAL;
- double zoom = content::HostZoomMap::GetZoomLevel(web_contents());
+ double zoom = GetZoomLevel();
return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS;
Devlin 2014/06/16 19:04:50 nit: inline double zoom's value.
wjmaclean 2014/06/18 19:03:54 Done. Let me know if this is what you were lookin
}
+const extensions::Extension* ZoomController::last_extension() const {
Devlin 2014/06/16 19:04:49 why not defined in the header?
wjmaclean 2014/06/18 19:03:55 Done.
+ return last_extension_.get();
+}
+
+double ZoomController::GetZoomLevel() const {
+ if (zoom_mode_ == kZoomModeManual)
+ return zoom_level_;
+ else
+ return content::HostZoomMap::GetZoomLevel(web_contents());
+}
+
+int ZoomController::GetZoomPercent() const {
+ double zoom_factor = content::ZoomLevelToZoomFactor(GetZoomLevel());
+ return static_cast<int>(zoom_factor * 100 + 0.5);
+}
+
+bool ZoomController::SetZoomLevel(double zoom_level) {
+ scoped_refptr<const extensions::Extension> null_extension_pointer;
+ // An extension did not initiate this zoom change.
+ return SetZoomLevelByExtension(zoom_level, null_extension_pointer);
+}
+
+bool ZoomController::SetZoomLevelByExtension(
+ double zoom_level,
+ scoped_refptr<const extensions::Extension> extension) {
+ // Cannot zoom in disabled mode.
+ if (zoom_mode_ == kZoomModeDisabled)
+ return false;
+
+ if (zoom_mode_ == kZoomModeManual) {
+ double old_zoom_level = zoom_level_;
+ zoom_level_ = zoom_level;
+
+ // TODO(wjmaclean) Do we care about filling in host/scheme here?
+ content::HostZoomMap::ZoomLevelChange change;
+ change.mode = content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM;
+ change.zoom_level = zoom_level;
+ ZoomEventManager::GetForBrowserContext(web_contents()->GetBrowserContext())
+ ->OnZoomLevelChanged(change);
+
+ observer_->OnZoomChangeInitiated(
+ web_contents(), old_zoom_level, zoom_level, zoom_mode_);
+
+ return true;
+ }
+
+ // Store extension data so that |extension| can be attributed when the zoom
+ // change completes..
Devlin 2014/06/16 19:04:50 Either 1 '.' or 3. Not two. :)
wjmaclean 2014/06/18 19:03:54 Done.
+ extensions_.push(extension.get());
+
+ // Do not actually rescale the page in manual mode.
+ content::HostZoomMap* zoom_map = content::HostZoomMap::GetForBrowserContext(
+ web_contents()->GetBrowserContext());
+ DCHECK(zoom_map);
+ double old_zoom_level = GetZoomLevel();
+ int render_process_id = web_contents()->GetRenderProcessHost()->GetID();
+ int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
+ if (zoom_mode_ == kZoomModeIsolated ||
+ zoom_map->UsesTemporaryZoomLevel(render_process_id, render_view_id)) {
+ zoom_map->SetTemporaryZoomLevel(
+ render_process_id, render_view_id, zoom_level);
+ } else {
+ // TODO(wjmaclean) If there is no entry, do we really want to proceed? If
+ // not, then we shouldn't have pushed the extension above, nor should we
+ // notify the observer below. Re-write this before submitting.
Devlin 2014/06/16 19:04:49 Address this.
wjmaclean 2014/06/18 19:03:55 Done.
+ content::NavigationEntry* entry =
+ web_contents()->GetController().GetLastCommittedEntry();
+ std::string host =
+ net::GetHostOrSpecFromURL(entry ? entry->GetURL() : GURL::EmptyGURL());
+ zoom_map->SetZoomLevelForHost(host, zoom_level);
+ }
+ observer_->OnZoomChangeInitiated(
+ web_contents(), old_zoom_level, zoom_level, zoom_mode_);
+
+ return true;
+}
+
+void ZoomController::SetZoomMode(ZoomMode new_mode) {
+ if (new_mode == zoom_mode_)
+ return;
+
+ content::HostZoomMap* zoom_map = content::HostZoomMap::GetForBrowserContext(
+ web_contents()->GetBrowserContext());
+ DCHECK(zoom_map);
+ int render_process_id = web_contents()->GetRenderProcessHost()->GetID();
+ int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
+ double original_zoom_level = GetZoomLevel();
+
+ switch (new_mode) {
+ case kZoomModeDefault: {
+ content::NavigationEntry* entry =
+ web_contents()->GetController().GetLastCommittedEntry();
+ GURL url;
+ if (entry)
+ url = entry->GetURL();
+ std::string host = net::GetHostOrSpecFromURL(url);
+
+ if (zoom_map->HasZoomLevel(url.scheme(), host)) {
+ // All the other modes are per-tab, but setting this mode shouldn't
+ // affect the zoom level of other tabs; if there are other tabs with
+ // the same origin, then set this tab's zoom level to match theirs.
+ // TODO(wjmaclean) How to convert this call?
Devlin 2014/06/16 19:04:49 Address this.
wjmaclean 2014/06/18 19:03:55 Done. I'm not sure what I had in mind when I wrot
+ double origin_zoom_level =
+ zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host);
+ zoom_map->SetTemporaryZoomLevel(
+ render_process_id, render_view_id, origin_zoom_level);
+ } else {
+ // The host will need a level prior to removing the temporary level.
+ zoom_map->SetZoomLevelForHost(host, original_zoom_level);
+ }
+ // Remove per-tab zoom data for this tab.
+ zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id);
+ break;
+ }
+ case kZoomModeIsolated: {
+ // Unless the zoom mode was |kZoomModeDisabled| before this call, the page
+ // needs an initial isolated zoom back to the same level it was at in the
+ // other mode.
+ if (zoom_mode_ != kZoomModeDisabled) {
+ zoom_map->SetTemporaryZoomLevel(
+ render_process_id, render_view_id, original_zoom_level);
+ }
+ break;
+ }
+ case kZoomModeManual: {
+ // Unless the zoom mode was |kZoomModeDisabled| before this call, the page
+ // needs to be resized to the default zoom before calling SetZoomLevel()
+ // so that the page can be resized manually to the same zoom as before.
+ if (zoom_mode_ != kZoomModeDisabled) {
+ zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0);
+ zoom_level_ = original_zoom_level;
+ }
+ break;
+ }
+ case kZoomModeDisabled: {
+ // The page needs to be zoomed back to default before disabling the zoom
+ zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0);
+ break;
+ }
+ }
+
+ zoom_mode_ = new_mode;
+}
+
void ZoomController::DidNavigateMainFrame(
const content::LoadCommittedDetails& details,
const content::FrameNavigateParams& params) {
@@ -66,10 +215,22 @@ void ZoomController::DidNavigateMainFrame(
void ZoomController::OnZoomLevelChanged(
const content::HostZoomMap::ZoomLevelChange& change) {
- UpdateState(change.host);
+ UpdateState(change.host,
+ change.mode == content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM);
}
void ZoomController::UpdateState(const std::string& host) {
+ UpdateState(host, false);
+}
+
+void ZoomController::UpdateState(const std::string& host,
+ bool is_temporary_zoom) {
+ // Update |last_extension_|.
+ if (!extensions_.empty()) {
Devlin 2014/06/16 19:04:50 This seems odd for a few reasons. 1. If we are c
wjmaclean 2014/06/18 19:03:54 Agreed, the code as it stands in this CL is convol
+ last_extension_ = extensions_.front().get();
+ extensions_.pop();
+ }
+
// If |host| is empty, all observers should be updated.
if (!host.empty()) {
// Use the navigation entry's URL instead of the WebContents' so virtual
@@ -82,9 +243,22 @@ void ZoomController::UpdateState(const std::string& host) {
}
}
- bool dummy;
- zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy);
+ if (observer_) {
+ // The zoom bubble can be shown for all normal, per-origin zoom changes
+ // (where the host will not be empty and the zoom is not temporary), or any
+ // special zoom changes (where the zoom mode will not be "default").
+ bool can_show_bubble =
+ zoom_mode_ != kZoomModeDefault ||
+ (!host.empty() && !is_temporary_zoom);
+ observer_->OnZoomChanged(web_contents(), can_show_bubble);
+ }
+
+ last_extension_ = NULL;
+}
- if (observer_)
- observer_->OnZoomChanged(web_contents(), !host.empty());
+void ZoomController::ZoomCallback(
Devlin 2014/06/16 19:04:50 When is this used?
wjmaclean 2014/06/18 19:03:54 Removed.
+ scoped_refptr<const extensions::Extension> extension,
+ const base::Callback<void(void)>& callback) {
+ extensions_.push(extension);
+ callback.Run();
}

Powered by Google App Engine
This is Rietveld 408576698