OLD | NEW |
---|---|
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/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/ui/browser_finder.h" | 10 #include "chrome/browser/ui/browser_finder.h" |
11 #include "chrome/browser/ui/zoom/zoom_event_manager.h" | |
11 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
12 #include "content/public/browser/host_zoom_map.h" | 13 #include "content/public/browser/host_zoom_map.h" |
13 #include "content/public/browser/navigation_entry.h" | 14 #include "content/public/browser/navigation_entry.h" |
14 #include "content/public/browser/notification_details.h" | 15 #include "content/public/browser/notification_details.h" |
15 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
16 #include "content/public/browser/notification_types.h" | 17 #include "content/public/browser/notification_types.h" |
18 #include "content/public/browser/render_process_host.h" | |
19 #include "content/public/browser/render_view_host.h" | |
17 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
18 #include "content/public/common/page_zoom.h" | 21 #include "content/public/common/page_zoom.h" |
19 #include "grit/theme_resources.h" | 22 #include "grit/theme_resources.h" |
20 #include "net/base/net_util.h" | 23 #include "net/base/net_util.h" |
21 | 24 |
22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); | 25 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); |
23 | 26 |
24 ZoomController::ZoomController(content::WebContents* web_contents) | 27 ZoomController::ZoomController(content::WebContents* web_contents) |
25 : content::WebContentsObserver(web_contents), | 28 : content::WebContentsObserver(web_contents), |
26 zoom_percent_(100), | 29 zoom_mode_(kZoomModeDefault), |
27 observer_(NULL), | 30 observer_(NULL), |
28 browser_context_(web_contents->GetBrowserContext()) { | 31 browser_context_(web_contents->GetBrowserContext()) { |
29 Profile* profile = | 32 Profile* profile = |
30 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 33 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
31 default_zoom_level_.Init(prefs::kDefaultZoomLevel, profile->GetPrefs(), | 34 default_zoom_level_.Init( |
32 base::Bind(&ZoomController::UpdateState, | 35 prefs::kDefaultZoomLevel, profile->GetPrefs(), |
33 base::Unretained(this), | 36 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.
| |
34 std::string())); | 37 const std::string&)>(&ZoomController::UpdateState), |
38 base::Unretained(this), | |
39 std::string())); | |
40 zoom_level_ = default_zoom_level_.GetValue(); | |
35 | 41 |
36 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext( | 42 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext( |
37 browser_context_)->AddZoomLevelChangedCallback( | 43 browser_context_)->AddZoomLevelChangedCallback( |
38 base::Bind(&ZoomController::OnZoomLevelChanged, | 44 base::Bind(&ZoomController::OnZoomLevelChanged, |
39 base::Unretained(this))); | 45 base::Unretained(this))); |
40 | 46 |
41 UpdateState(std::string()); | 47 UpdateState(std::string()); |
42 } | 48 } |
43 | 49 |
44 ZoomController::~ZoomController() {} | 50 ZoomController::~ZoomController() {} |
45 | 51 |
46 bool ZoomController::IsAtDefaultZoom() const { | 52 bool ZoomController::IsAtDefaultZoom() const { |
47 return content::ZoomValuesEqual( | 53 return content::ZoomValuesEqual(GetZoomLevel(), |
48 content::HostZoomMap::GetZoomLevel(web_contents()), | 54 default_zoom_level_.GetValue()); |
49 default_zoom_level_.GetValue()); | |
50 } | 55 } |
51 | 56 |
52 int ZoomController::GetResourceForZoomLevel() const { | 57 int ZoomController::GetResourceForZoomLevel() const { |
53 if (IsAtDefaultZoom()) | 58 if (IsAtDefaultZoom()) |
54 return IDR_ZOOM_NORMAL; | 59 return IDR_ZOOM_NORMAL; |
55 double zoom = content::HostZoomMap::GetZoomLevel(web_contents()); | 60 double zoom = GetZoomLevel(); |
56 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; | 61 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
| |
57 } | 62 } |
58 | 63 |
64 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.
| |
65 return last_extension_.get(); | |
66 } | |
67 | |
68 double ZoomController::GetZoomLevel() const { | |
69 if (zoom_mode_ == kZoomModeManual) | |
70 return zoom_level_; | |
71 else | |
72 return content::HostZoomMap::GetZoomLevel(web_contents()); | |
73 } | |
74 | |
75 int ZoomController::GetZoomPercent() const { | |
76 double zoom_factor = content::ZoomLevelToZoomFactor(GetZoomLevel()); | |
77 return static_cast<int>(zoom_factor * 100 + 0.5); | |
78 } | |
79 | |
80 bool ZoomController::SetZoomLevel(double zoom_level) { | |
81 scoped_refptr<const extensions::Extension> null_extension_pointer; | |
82 // An extension did not initiate this zoom change. | |
83 return SetZoomLevelByExtension(zoom_level, null_extension_pointer); | |
84 } | |
85 | |
86 bool ZoomController::SetZoomLevelByExtension( | |
87 double zoom_level, | |
88 scoped_refptr<const extensions::Extension> extension) { | |
89 // Cannot zoom in disabled mode. | |
90 if (zoom_mode_ == kZoomModeDisabled) | |
91 return false; | |
92 | |
93 if (zoom_mode_ == kZoomModeManual) { | |
94 double old_zoom_level = zoom_level_; | |
95 zoom_level_ = zoom_level; | |
96 | |
97 // TODO(wjmaclean) Do we care about filling in host/scheme here? | |
98 content::HostZoomMap::ZoomLevelChange change; | |
99 change.mode = content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; | |
100 change.zoom_level = zoom_level; | |
101 ZoomEventManager::GetForBrowserContext(web_contents()->GetBrowserContext()) | |
102 ->OnZoomLevelChanged(change); | |
103 | |
104 observer_->OnZoomChangeInitiated( | |
105 web_contents(), old_zoom_level, zoom_level, zoom_mode_); | |
106 | |
107 return true; | |
108 } | |
109 | |
110 // Store extension data so that |extension| can be attributed when the zoom | |
111 // change completes.. | |
Devlin
2014/06/16 19:04:50
Either 1 '.' or 3. Not two. :)
wjmaclean
2014/06/18 19:03:54
Done.
| |
112 extensions_.push(extension.get()); | |
113 | |
114 // Do not actually rescale the page in manual mode. | |
115 content::HostZoomMap* zoom_map = content::HostZoomMap::GetForBrowserContext( | |
116 web_contents()->GetBrowserContext()); | |
117 DCHECK(zoom_map); | |
118 double old_zoom_level = GetZoomLevel(); | |
119 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | |
120 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | |
121 if (zoom_mode_ == kZoomModeIsolated || | |
122 zoom_map->UsesTemporaryZoomLevel(render_process_id, render_view_id)) { | |
123 zoom_map->SetTemporaryZoomLevel( | |
124 render_process_id, render_view_id, zoom_level); | |
125 } else { | |
126 // TODO(wjmaclean) If there is no entry, do we really want to proceed? If | |
127 // not, then we shouldn't have pushed the extension above, nor should we | |
128 // 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.
| |
129 content::NavigationEntry* entry = | |
130 web_contents()->GetController().GetLastCommittedEntry(); | |
131 std::string host = | |
132 net::GetHostOrSpecFromURL(entry ? entry->GetURL() : GURL::EmptyGURL()); | |
133 zoom_map->SetZoomLevelForHost(host, zoom_level); | |
134 } | |
135 observer_->OnZoomChangeInitiated( | |
136 web_contents(), old_zoom_level, zoom_level, zoom_mode_); | |
137 | |
138 return true; | |
139 } | |
140 | |
141 void ZoomController::SetZoomMode(ZoomMode new_mode) { | |
142 if (new_mode == zoom_mode_) | |
143 return; | |
144 | |
145 content::HostZoomMap* zoom_map = content::HostZoomMap::GetForBrowserContext( | |
146 web_contents()->GetBrowserContext()); | |
147 DCHECK(zoom_map); | |
148 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | |
149 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | |
150 double original_zoom_level = GetZoomLevel(); | |
151 | |
152 switch (new_mode) { | |
153 case kZoomModeDefault: { | |
154 content::NavigationEntry* entry = | |
155 web_contents()->GetController().GetLastCommittedEntry(); | |
156 GURL url; | |
157 if (entry) | |
158 url = entry->GetURL(); | |
159 std::string host = net::GetHostOrSpecFromURL(url); | |
160 | |
161 if (zoom_map->HasZoomLevel(url.scheme(), host)) { | |
162 // All the other modes are per-tab, but setting this mode shouldn't | |
163 // affect the zoom level of other tabs; if there are other tabs with | |
164 // the same origin, then set this tab's zoom level to match theirs. | |
165 // 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
| |
166 double origin_zoom_level = | |
167 zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host); | |
168 zoom_map->SetTemporaryZoomLevel( | |
169 render_process_id, render_view_id, origin_zoom_level); | |
170 } else { | |
171 // The host will need a level prior to removing the temporary level. | |
172 zoom_map->SetZoomLevelForHost(host, original_zoom_level); | |
173 } | |
174 // Remove per-tab zoom data for this tab. | |
175 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id); | |
176 break; | |
177 } | |
178 case kZoomModeIsolated: { | |
179 // Unless the zoom mode was |kZoomModeDisabled| before this call, the page | |
180 // needs an initial isolated zoom back to the same level it was at in the | |
181 // other mode. | |
182 if (zoom_mode_ != kZoomModeDisabled) { | |
183 zoom_map->SetTemporaryZoomLevel( | |
184 render_process_id, render_view_id, original_zoom_level); | |
185 } | |
186 break; | |
187 } | |
188 case kZoomModeManual: { | |
189 // Unless the zoom mode was |kZoomModeDisabled| before this call, the page | |
190 // needs to be resized to the default zoom before calling SetZoomLevel() | |
191 // so that the page can be resized manually to the same zoom as before. | |
192 if (zoom_mode_ != kZoomModeDisabled) { | |
193 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0); | |
194 zoom_level_ = original_zoom_level; | |
195 } | |
196 break; | |
197 } | |
198 case kZoomModeDisabled: { | |
199 // The page needs to be zoomed back to default before disabling the zoom | |
200 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0); | |
201 break; | |
202 } | |
203 } | |
204 | |
205 zoom_mode_ = new_mode; | |
206 } | |
207 | |
59 void ZoomController::DidNavigateMainFrame( | 208 void ZoomController::DidNavigateMainFrame( |
60 const content::LoadCommittedDetails& details, | 209 const content::LoadCommittedDetails& details, |
61 const content::FrameNavigateParams& params) { | 210 const content::FrameNavigateParams& params) { |
62 // If the main frame's content has changed, the new page may have a different | 211 // If the main frame's content has changed, the new page may have a different |
63 // zoom level from the old one. | 212 // zoom level from the old one. |
64 UpdateState(std::string()); | 213 UpdateState(std::string()); |
65 } | 214 } |
66 | 215 |
67 void ZoomController::OnZoomLevelChanged( | 216 void ZoomController::OnZoomLevelChanged( |
68 const content::HostZoomMap::ZoomLevelChange& change) { | 217 const content::HostZoomMap::ZoomLevelChange& change) { |
69 UpdateState(change.host); | 218 UpdateState(change.host, |
219 change.mode == content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM); | |
70 } | 220 } |
71 | 221 |
72 void ZoomController::UpdateState(const std::string& host) { | 222 void ZoomController::UpdateState(const std::string& host) { |
223 UpdateState(host, false); | |
224 } | |
225 | |
226 void ZoomController::UpdateState(const std::string& host, | |
227 bool is_temporary_zoom) { | |
228 // Update |last_extension_|. | |
229 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
| |
230 last_extension_ = extensions_.front().get(); | |
231 extensions_.pop(); | |
232 } | |
233 | |
73 // If |host| is empty, all observers should be updated. | 234 // If |host| is empty, all observers should be updated. |
74 if (!host.empty()) { | 235 if (!host.empty()) { |
75 // Use the navigation entry's URL instead of the WebContents' so virtual | 236 // Use the navigation entry's URL instead of the WebContents' so virtual |
76 // URLs work (e.g. chrome://settings). http://crbug.com/153950 | 237 // URLs work (e.g. chrome://settings). http://crbug.com/153950 |
77 content::NavigationEntry* entry = | 238 content::NavigationEntry* entry = |
78 web_contents()->GetController().GetLastCommittedEntry(); | 239 web_contents()->GetController().GetLastCommittedEntry(); |
79 if (!entry || | 240 if (!entry || |
80 host != net::GetHostOrSpecFromURL(entry->GetURL())) { | 241 host != net::GetHostOrSpecFromURL(entry->GetURL())) { |
81 return; | 242 return; |
82 } | 243 } |
83 } | 244 } |
84 | 245 |
85 bool dummy; | 246 if (observer_) { |
86 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); | 247 // The zoom bubble can be shown for all normal, per-origin zoom changes |
248 // (where the host will not be empty and the zoom is not temporary), or any | |
249 // special zoom changes (where the zoom mode will not be "default"). | |
250 bool can_show_bubble = | |
251 zoom_mode_ != kZoomModeDefault || | |
252 (!host.empty() && !is_temporary_zoom); | |
253 observer_->OnZoomChanged(web_contents(), can_show_bubble); | |
254 } | |
87 | 255 |
88 if (observer_) | 256 last_extension_ = NULL; |
89 observer_->OnZoomChanged(web_contents(), !host.empty()); | |
90 } | 257 } |
258 | |
259 void ZoomController::ZoomCallback( | |
Devlin
2014/06/16 19:04:50
When is this used?
wjmaclean
2014/06/18 19:03:54
Removed.
| |
260 scoped_refptr<const extensions::Extension> extension, | |
261 const base::Callback<void(void)>& callback) { | |
262 extensions_.push(extension); | |
263 callback.Run(); | |
264 } | |
OLD | NEW |