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

Side by Side 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: Address dbeam@'s comments. Created 6 years, 5 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/zoom/zoom_controller.h" 5 #include "chrome/browser/ui/zoom/zoom_controller.h"
6 6
7 #include "base/prefs/pref_service.h" 7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/chrome_notification_types.h" 8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/extensions/api/tabs/tabs_event_router.h"
10 #include "chrome/browser/extensions/api/tabs/tabs_windows_api.h"
9 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser_finder.h" 12 #include "chrome/browser/ui/browser_finder.h"
13 #include "chrome/browser/ui/zoom/zoom_event_manager.h"
11 #include "chrome/common/pref_names.h" 14 #include "chrome/common/pref_names.h"
12 #include "content/public/browser/host_zoom_map.h" 15 #include "content/public/browser/host_zoom_map.h"
13 #include "content/public/browser/navigation_entry.h" 16 #include "content/public/browser/navigation_entry.h"
14 #include "content/public/browser/notification_details.h" 17 #include "content/public/browser/notification_details.h"
15 #include "content/public/browser/notification_service.h" 18 #include "content/public/browser/notification_service.h"
16 #include "content/public/browser/notification_types.h" 19 #include "content/public/browser/notification_types.h"
20 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/page_zoom.h" 23 #include "content/public/common/page_zoom.h"
24 #include "extensions/common/extension.h"
19 #include "grit/theme_resources.h" 25 #include "grit/theme_resources.h"
20 #include "net/base/net_util.h" 26 #include "net/base/net_util.h"
21 27
22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); 28 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController);
23 29
24 ZoomController::ZoomController(content::WebContents* web_contents) 30 ZoomController::ZoomController(content::WebContents* web_contents)
25 : content::WebContentsObserver(web_contents), 31 : content::WebContentsObserver(web_contents),
26 zoom_percent_(100), 32 zoom_mode_(ZOOM_MODE_DEFAULT),
27 observer_(NULL), 33 zoom_level_(1.0),
28 browser_context_(web_contents->GetBrowserContext()) { 34 browser_context_(web_contents->GetBrowserContext()) {
29 Profile* profile = 35 Profile* profile =
30 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 36 Profile::FromBrowserContext(web_contents->GetBrowserContext());
31 default_zoom_level_.Init(prefs::kDefaultZoomLevel, profile->GetPrefs(), 37 default_zoom_level_.Init(
32 base::Bind(&ZoomController::UpdateState, 38 prefs::kDefaultZoomLevel,
33 base::Unretained(this), 39 profile->GetPrefs(),
34 std::string())); 40 base::Bind(
41 &ZoomController::UpdateState, base::Unretained(this), std::string()));
42 zoom_level_ = default_zoom_level_.GetValue();
35 43
36 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext( 44 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext(
37 browser_context_)->AddZoomLevelChangedCallback( 45 browser_context_)->AddZoomLevelChangedCallback(
38 base::Bind(&ZoomController::OnZoomLevelChanged, 46 base::Bind(&ZoomController::OnZoomLevelChanged,
39 base::Unretained(this))); 47 base::Unretained(this)));
40 48
41 UpdateState(std::string()); 49 UpdateState(std::string());
42 } 50 }
43 51
44 ZoomController::~ZoomController() {} 52 ZoomController::~ZoomController() {}
45 53
46 bool ZoomController::IsAtDefaultZoom() const { 54 bool ZoomController::IsAtDefaultZoom() const {
47 return content::ZoomValuesEqual( 55 return content::ZoomValuesEqual(GetZoomLevel(),
48 content::HostZoomMap::GetZoomLevel(web_contents()), 56 default_zoom_level_.GetValue());
49 default_zoom_level_.GetValue());
50 } 57 }
51 58
52 int ZoomController::GetResourceForZoomLevel() const { 59 int ZoomController::GetResourceForZoomLevel() const {
53 if (IsAtDefaultZoom()) 60 if (IsAtDefaultZoom())
54 return IDR_ZOOM_NORMAL; 61 return IDR_ZOOM_NORMAL;
55 double zoom = content::HostZoomMap::GetZoomLevel(web_contents()); 62 return GetZoomLevel() > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS
56 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; 63 : IDR_ZOOM_MINUS;
64 }
65
66 void ZoomController::AddObserver(ZoomObserver* observer) {
67 observers_.AddObserver(observer);
68 }
69
70 void ZoomController::RemoveObserver(ZoomObserver* observer) {
71 observers_.RemoveObserver(observer);
72 }
73
74 double ZoomController::GetZoomLevel() const {
75 return zoom_mode_ == ZOOM_MODE_MANUAL ?
76 zoom_level_:
77 content::HostZoomMap::GetZoomLevel(web_contents());
78 }
79
80 int ZoomController::GetZoomPercent() const {
81 double zoom_factor = content::ZoomLevelToZoomFactor(GetZoomLevel());
82 // Round double for return.
83 return static_cast<int>(zoom_factor * 100 + 0.5);
84 }
85
86 bool ZoomController::SetZoomLevel(double zoom_level) {
87 // An extension did not initiate this zoom change.
88 return SetZoomLevelByExtension(zoom_level, NULL);
89 }
90
91 bool ZoomController::SetZoomLevelByExtension(
92 double zoom_level,
93 const scoped_refptr<const extensions::Extension>& extension) {
94 // Cannot zoom in disabled mode.
95 if (zoom_mode_ == ZOOM_MODE_DISABLED)
96 return false;
97
98 // Store extension data so that |extension| can be attributed when the zoom
99 // change completes. We expect that by the time this function returns that
100 // any observers that require this information will have requested it.
101 last_extension_ = extension;
102
103 if (zoom_mode_ == ZOOM_MODE_MANUAL) {
104 double old_zoom_level = zoom_level_;
105 zoom_level_ = zoom_level;
106
107 // TODO(wjmaclean) Do we care about filling in host/scheme here?
108 content::HostZoomMap::ZoomLevelChange change;
109 change.mode = content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM;
110 change.zoom_level = zoom_level;
111 ZoomEventManager::GetForBrowserContext(browser_context_)
112 ->OnZoomLevelChanged(change);
Dan Beam 2014/06/26 22:16:38 nit: -> at end of L111
wjmaclean 2014/07/02 22:24:59 Done.
113
114 FOR_EACH_OBSERVER(
115 ZoomObserver,
116 observers_,
117 OnZoomChanged(web_contents(), true /* can_show_bubble */));
118 FOR_EACH_OBSERVER(
119 ZoomObserver,
120 observers_,
121 OnZoomChangeInitiated(
122 web_contents(), old_zoom_level, zoom_level, zoom_mode_));
Dan Beam 2014/06/26 22:16:38 ^ shouldn't this happen *before* OnZoomChanged()?
wjmaclean 2014/07/02 22:24:59 Hmm, this might just be an unfortunate choice of n
123
124 last_extension_ = NULL;
125 return true;
126 }
127
128 // Do not actually rescale the page in manual mode.
129 content::HostZoomMap* zoom_map =
130 content::HostZoomMap::GetForBrowserContext(browser_context_);
131 DCHECK(zoom_map);
132 double old_zoom_level = GetZoomLevel();
133 int render_process_id = web_contents()->GetRenderProcessHost()->GetID();
134 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
135 if (zoom_mode_ == ZOOM_MODE_ISOLATED ||
136 zoom_map->UsesTemporaryZoomLevel(render_process_id, render_view_id)) {
137 zoom_map->SetTemporaryZoomLevel(
138 render_process_id, render_view_id, zoom_level);
139 } else {
140 content::NavigationEntry* entry =
141 web_contents()->GetController().GetLastCommittedEntry();
142 // We allow empty |host| values for data urls.
143 std::string host =
144 net::GetHostOrSpecFromURL(entry ? entry->GetURL() : GURL::EmptyGURL());
Dan Beam 2014/06/26 22:16:38 if there's no entry, shouldn't we just ask the web
wjmaclean 2014/07/02 22:24:59 I don't think so ... zoom levels don't correspond
145 zoom_map->SetZoomLevelForHost(host, zoom_level);
Dan Beam 2014/06/26 22:16:38 should we be calling SetZoomLevelForHost() with an
wjmaclean 2014/07/02 22:24:59 Hmmm, no, I don't think so (esp. to be consistent
146 }
147 FOR_EACH_OBSERVER(
148 ZoomObserver,
149 observers_,
150 OnZoomChangeInitiated(
151 web_contents(), old_zoom_level, zoom_level, zoom_mode_));
152
153 last_extension_ = NULL;
154 return true;
155 }
156
157 void ZoomController::SetZoomMode(ZoomMode new_mode) {
158 if (new_mode == zoom_mode_)
159 return;
160
161 content::HostZoomMap* zoom_map =
162 content::HostZoomMap::GetForBrowserContext(browser_context_);
163 DCHECK(zoom_map);
164 int render_process_id = web_contents()->GetRenderProcessHost()->GetID();
165 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
166 double original_zoom_level = GetZoomLevel();
167
168 switch (new_mode) {
169 case ZOOM_MODE_DEFAULT: {
170 content::NavigationEntry* entry =
171 web_contents()->GetController().GetLastCommittedEntry();
172 GURL url;
173 if (entry)
174 url = entry->GetURL();
Dan Beam 2014/06/26 22:16:38 same re: empty GURL or !entry handling
wjmaclean 2014/07/02 22:24:59 Done.
175 std::string host = net::GetHostOrSpecFromURL(url);
176
177 if (zoom_map->HasZoomLevel(url.scheme(), host)) {
178 // If there are other tabs with the same origin, then set this tab's
179 // zoom level to match theirs. The temporary zoom level will be cleared
180 // below, but this call will make sure this tab re-draws at the correct
181 // zoom level.
182 double origin_zoom_level =
183 zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host);
184 zoom_map->SetTemporaryZoomLevel(
185 render_process_id, render_view_id, origin_zoom_level);
186 } else {
187 // The host will need a level prior to removing the temporary level.
188 // We don't want the zoom level to change just because we entered
189 // default mode.
190 zoom_map->SetZoomLevelForHost(host, original_zoom_level);
191 }
192 // Remove per-tab zoom data for this tab.
193 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id);
194 break;
195 }
196 case ZOOM_MODE_ISOLATED: {
197 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the
198 // page needs an initial isolated zoom back to the same level it was at
199 // in the other mode.
200 if (zoom_mode_ != ZOOM_MODE_DISABLED) {
201 zoom_map->SetTemporaryZoomLevel(
202 render_process_id, render_view_id, original_zoom_level);
203 }
204 break;
205 }
206 case ZOOM_MODE_MANUAL: {
207 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the
208 // page needs to be resized to the default zoom. While in manual mode,
209 // the zoom level is handled independently.
210 if (zoom_mode_ != ZOOM_MODE_DISABLED) {
211 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0);
212 zoom_level_ = original_zoom_level;
213 }
214 break;
215 }
216 case ZOOM_MODE_DISABLED: {
217 // The page needs to be zoomed back to default before disabling the zoom
218 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0);
219 break;
220 }
221 }
222
223 zoom_mode_ = new_mode;
57 } 224 }
58 225
59 void ZoomController::DidNavigateMainFrame( 226 void ZoomController::DidNavigateMainFrame(
60 const content::LoadCommittedDetails& details, 227 const content::LoadCommittedDetails& details,
61 const content::FrameNavigateParams& params) { 228 const content::FrameNavigateParams& params) {
62 // If the main frame's content has changed, the new page may have a different 229 // If the main frame's content has changed, the new page may have a different
63 // zoom level from the old one. 230 // zoom level from the old one.
64 UpdateState(std::string()); 231 UpdateState(std::string());
65 } 232 }
66 233
67 void ZoomController::OnZoomLevelChanged( 234 void ZoomController::OnZoomLevelChanged(
68 const content::HostZoomMap::ZoomLevelChange& change) { 235 const content::HostZoomMap::ZoomLevelChange& change) {
69 UpdateState(change.host); 236 UpdateStateIncludingTemporary(
237 change.host,
238 change.mode == content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM);
70 } 239 }
71 240
72 void ZoomController::UpdateState(const std::string& host) { 241 void ZoomController::UpdateState(const std::string& host) {
242 UpdateStateIncludingTemporary(host, false);
243 }
244
245 void ZoomController::UpdateStateIncludingTemporary(const std::string& host,
246 bool is_temporary_zoom) {
73 // If |host| is empty, all observers should be updated. 247 // If |host| is empty, all observers should be updated.
74 if (!host.empty()) { 248 if (!host.empty()) {
75 // Use the navigation entry's URL instead of the WebContents' so virtual 249 // Use the navigation entry's URL instead of the WebContents' so virtual
76 // URLs work (e.g. chrome://settings). http://crbug.com/153950 250 // URLs work (e.g. chrome://settings). http://crbug.com/153950
77 content::NavigationEntry* entry = 251 content::NavigationEntry* entry =
78 web_contents()->GetController().GetLastCommittedEntry(); 252 web_contents()->GetController().GetLastCommittedEntry();
79 if (!entry || 253 if (!entry ||
80 host != net::GetHostOrSpecFromURL(entry->GetURL())) { 254 host != net::GetHostOrSpecFromURL(entry->GetURL())) {
81 return; 255 return;
82 } 256 }
83 } 257 }
84 258
85 bool dummy; 259 // The zoom bubble can be shown for all normal, per-origin zoom changes
86 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); 260 // (where the host will not be empty and the zoom is not temporary), or any
87 261 // special zoom changes (where the zoom mode will not be "default").
88 if (observer_) 262 bool can_show_bubble =
89 observer_->OnZoomChanged(web_contents(), !host.empty()); 263 zoom_mode_ != ZOOM_MODE_DEFAULT || (!host.empty() && !is_temporary_zoom);
264 FOR_EACH_OBSERVER(ZoomObserver,
265 observers_,
266 OnZoomChanged(web_contents(), can_show_bubble));
90 } 267 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698