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

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: Rebase to r279487. 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 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
49 AddObserver(
50 extensions::TabsWindowsAPI::Get(browser_context_)->tabs_event_router());
51
41 UpdateState(std::string()); 52 UpdateState(std::string());
42 } 53 }
43 54
44 ZoomController::~ZoomController() {} 55 ZoomController::~ZoomController() {}
45 56
46 bool ZoomController::IsAtDefaultZoom() const { 57 bool ZoomController::IsAtDefaultZoom() const {
47 return content::ZoomValuesEqual( 58 return content::ZoomValuesEqual(GetZoomLevel(),
48 content::HostZoomMap::GetZoomLevel(web_contents()), 59 default_zoom_level_.GetValue());
49 default_zoom_level_.GetValue());
50 } 60 }
51 61
52 int ZoomController::GetResourceForZoomLevel() const { 62 int ZoomController::GetResourceForZoomLevel() const {
53 if (IsAtDefaultZoom()) 63 if (IsAtDefaultZoom())
54 return IDR_ZOOM_NORMAL; 64 return IDR_ZOOM_NORMAL;
55 double zoom = content::HostZoomMap::GetZoomLevel(web_contents()); 65 return GetZoomLevel() > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS
56 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; 66 : IDR_ZOOM_MINUS;
67 }
68
69 void ZoomController::AddObserver(ZoomObserver* observer) {
70 observers_.AddObserver(observer);
71 }
72
73 void ZoomController::RemoveObserver(ZoomObserver* observer) {
74 observers_.RemoveObserver(observer);
75 }
76
77 double ZoomController::GetZoomLevel() const {
78 return zoom_mode_ == ZOOM_MODE_MANUAL
79 ? zoom_level_
80 : content::HostZoomMap::GetZoomLevel(web_contents());
81 }
82
83 int ZoomController::GetZoomPercent() const {
84 double zoom_factor = content::ZoomLevelToZoomFactor(GetZoomLevel());
85 return static_cast<int>(zoom_factor * 100 + 0.5);
86 }
87
88 bool ZoomController::SetZoomLevel(double zoom_level) {
89 // An extension did not initiate this zoom change.
90 return SetZoomLevelByExtension(zoom_level,
91 scoped_refptr<const extensions::Extension>());
92 }
93
94 bool ZoomController::SetZoomLevelByExtension(
95 double zoom_level,
96 scoped_refptr<const extensions::Extension> extension) {
97 // Cannot zoom in disabled mode.
98 if (zoom_mode_ == ZOOM_MODE_DISABLED)
99 return false;
100
101 // Store extension data so that |extension| can be attributed when the zoom
102 // change completes. We expect that by the time this function returns that
103 // any observers that require this information will have requested it.
104 last_extension_ = extension;
105
106 if (zoom_mode_ == ZOOM_MODE_MANUAL) {
107 double old_zoom_level = zoom_level_;
108 zoom_level_ = zoom_level;
109
110 // TODO(wjmaclean) Do we care about filling in host/scheme here?
111 content::HostZoomMap::ZoomLevelChange change;
112 change.mode = content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM;
113 change.zoom_level = zoom_level;
114 ZoomEventManager::GetForBrowserContext(browser_context_)
115 ->OnZoomLevelChanged(change);
116
117 FOR_EACH_OBSERVER(
118 ZoomObserver,
119 observers_,
120 OnZoomChanged(web_contents(), true /* can_show_bubble */));
121 FOR_EACH_OBSERVER(
122 ZoomObserver,
123 observers_,
124 OnZoomChangeInitiated(
125 web_contents(), old_zoom_level, zoom_level, zoom_mode_));
126
127 last_extension_ = NULL;
128 return true;
129 }
130
131 // Do not actually rescale the page in manual mode.
132 content::HostZoomMap* zoom_map =
133 content::HostZoomMap::GetForBrowserContext(browser_context_);
134 DCHECK(zoom_map);
135 double old_zoom_level = GetZoomLevel();
136 int render_process_id = web_contents()->GetRenderProcessHost()->GetID();
137 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
138 if (zoom_mode_ == ZOOM_MODE_ISOLATED ||
139 zoom_map->UsesTemporaryZoomLevel(render_process_id, render_view_id)) {
140 zoom_map->SetTemporaryZoomLevel(
141 render_process_id, render_view_id, zoom_level);
142 } else {
143 content::NavigationEntry* entry =
144 web_contents()->GetController().GetLastCommittedEntry();
145 // We allow empty |host| values for data urls.
146 std::string host =
147 net::GetHostOrSpecFromURL(entry ? entry->GetURL() : GURL::EmptyGURL());
148 zoom_map->SetZoomLevelForHost(host, zoom_level);
149 }
150 FOR_EACH_OBSERVER(
151 ZoomObserver,
152 observers_,
153 OnZoomChangeInitiated(
154 web_contents(), old_zoom_level, zoom_level, zoom_mode_));
155
156 last_extension_ = NULL;
157 return true;
158 }
159
160 void ZoomController::SetZoomMode(ZoomMode new_mode) {
161 if (new_mode == zoom_mode_)
162 return;
163
164 content::HostZoomMap* zoom_map =
165 content::HostZoomMap::GetForBrowserContext(browser_context_);
166 DCHECK(zoom_map);
167 int render_process_id = web_contents()->GetRenderProcessHost()->GetID();
168 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
169 double original_zoom_level = GetZoomLevel();
170
171 switch (new_mode) {
172 case ZOOM_MODE_DEFAULT: {
173 content::NavigationEntry* entry =
174 web_contents()->GetController().GetLastCommittedEntry();
175 GURL url;
176 if (entry)
177 url = entry->GetURL();
178 std::string host = net::GetHostOrSpecFromURL(url);
179
180 if (zoom_map->HasZoomLevel(url.scheme(), host)) {
181 // If there are other tabs with the same origin, then set this tab's
182 // zoom level to match theirs. The temporary zoom level will be cleared
183 // below, but this call will make sure this tab re-draws at the correct
184 // zoom level.
185 double origin_zoom_level =
186 zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host);
187 zoom_map->SetTemporaryZoomLevel(
188 render_process_id, render_view_id, origin_zoom_level);
189 } else {
190 // The host will need a level prior to removing the temporary level.
191 // We don't want the zoom level to change just because we entered
192 // default mode.
193 zoom_map->SetZoomLevelForHost(host, original_zoom_level);
194 }
195 // Remove per-tab zoom data for this tab.
196 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id);
197 break;
198 }
199 case ZOOM_MODE_ISOLATED: {
200 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the
201 // page needs an initial isolated zoom back to the same level it was at
202 // in the other mode.
203 if (zoom_mode_ != ZOOM_MODE_DISABLED) {
204 zoom_map->SetTemporaryZoomLevel(
205 render_process_id, render_view_id, original_zoom_level);
206 }
207 break;
208 }
209 case ZOOM_MODE_MANUAL: {
210 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the
211 // page needs to be resized to the default zoom before calling
212 // SetZoomLevel() so that the page can be resized manually to the same
213 // zoom as before.
214 if (zoom_mode_ != ZOOM_MODE_DISABLED) {
215 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0);
216 zoom_level_ = original_zoom_level;
217 }
218 break;
219 }
220 case ZOOM_MODE_DISABLED: {
221 // The page needs to be zoomed back to default before disabling the zoom
222 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0);
223 break;
224 }
225 }
226
227 zoom_mode_ = new_mode;
57 } 228 }
58 229
59 void ZoomController::DidNavigateMainFrame( 230 void ZoomController::DidNavigateMainFrame(
60 const content::LoadCommittedDetails& details, 231 const content::LoadCommittedDetails& details,
61 const content::FrameNavigateParams& params) { 232 const content::FrameNavigateParams& params) {
62 // If the main frame's content has changed, the new page may have a different 233 // If the main frame's content has changed, the new page may have a different
63 // zoom level from the old one. 234 // zoom level from the old one.
64 UpdateState(std::string()); 235 UpdateState(std::string());
65 } 236 }
66 237
67 void ZoomController::OnZoomLevelChanged( 238 void ZoomController::OnZoomLevelChanged(
68 const content::HostZoomMap::ZoomLevelChange& change) { 239 const content::HostZoomMap::ZoomLevelChange& change) {
69 UpdateState(change.host); 240 UpdateStateIncludingTemporary(
241 change.host,
242 change.mode == content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM);
70 } 243 }
71 244
72 void ZoomController::UpdateState(const std::string& host) { 245 void ZoomController::UpdateState(const std::string& host) {
246 UpdateStateIncludingTemporary(host, false);
247 }
248
249 void ZoomController::UpdateStateIncludingTemporary(const std::string& host,
250 bool is_temporary_zoom) {
73 // If |host| is empty, all observers should be updated. 251 // If |host| is empty, all observers should be updated.
74 if (!host.empty()) { 252 if (!host.empty()) {
75 // Use the navigation entry's URL instead of the WebContents' so virtual 253 // Use the navigation entry's URL instead of the WebContents' so virtual
76 // URLs work (e.g. chrome://settings). http://crbug.com/153950 254 // URLs work (e.g. chrome://settings). http://crbug.com/153950
77 content::NavigationEntry* entry = 255 content::NavigationEntry* entry =
78 web_contents()->GetController().GetLastCommittedEntry(); 256 web_contents()->GetController().GetLastCommittedEntry();
79 if (!entry || 257 if (!entry ||
80 host != net::GetHostOrSpecFromURL(entry->GetURL())) { 258 host != net::GetHostOrSpecFromURL(entry->GetURL())) {
81 return; 259 return;
82 } 260 }
83 } 261 }
84 262
85 bool dummy; 263 // The zoom bubble can be shown for all normal, per-origin zoom changes
86 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); 264 // (where the host will not be empty and the zoom is not temporary), or any
87 265 // special zoom changes (where the zoom mode will not be "default").
88 if (observer_) 266 bool can_show_bubble =
89 observer_->OnZoomChanged(web_contents(), !host.empty()); 267 zoom_mode_ != ZOOM_MODE_DEFAULT || (!host.empty() && !is_temporary_zoom);
268 FOR_EACH_OBSERVER(ZoomObserver,
269 observers_,
270 OnZoomChanged(web_contents(), can_show_bubble));
90 } 271 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698