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" |
22 #include "extensions/common/extension.h" | |
19 #include "grit/theme_resources.h" | 23 #include "grit/theme_resources.h" |
20 #include "net/base/net_util.h" | 24 #include "net/base/net_util.h" |
21 | 25 |
22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); | 26 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); |
23 | 27 |
24 ZoomController::ZoomController(content::WebContents* web_contents) | 28 ZoomController::ZoomController(content::WebContents* web_contents) |
25 : content::WebContentsObserver(web_contents), | 29 : content::WebContentsObserver(web_contents), |
26 zoom_percent_(100), | 30 zoom_mode_(kZoomModeDefault), |
27 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, |
33 base::Unretained(this), | 36 profile->GetPrefs(), |
34 std::string())); | 37 base::Bind( |
38 &ZoomController::UpdateState, base::Unretained(this), std::string())); | |
39 zoom_level_ = default_zoom_level_.GetValue(); | |
35 | 40 |
36 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext( | 41 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext( |
37 browser_context_)->AddZoomLevelChangedCallback( | 42 browser_context_)->AddZoomLevelChangedCallback( |
38 base::Bind(&ZoomController::OnZoomLevelChanged, | 43 base::Bind(&ZoomController::OnZoomLevelChanged, |
39 base::Unretained(this))); | 44 base::Unretained(this))); |
40 | 45 |
41 UpdateState(std::string()); | 46 UpdateState(std::string()); |
42 } | 47 } |
43 | 48 |
44 ZoomController::~ZoomController() {} | 49 ZoomController::~ZoomController() {} |
45 | 50 |
46 bool ZoomController::IsAtDefaultZoom() const { | 51 bool ZoomController::IsAtDefaultZoom() const { |
47 return content::ZoomValuesEqual( | 52 return content::ZoomValuesEqual(GetZoomLevel(), |
48 content::HostZoomMap::GetZoomLevel(web_contents()), | 53 default_zoom_level_.GetValue()); |
49 default_zoom_level_.GetValue()); | |
50 } | 54 } |
51 | 55 |
52 int ZoomController::GetResourceForZoomLevel() const { | 56 int ZoomController::GetResourceForZoomLevel() const { |
53 if (IsAtDefaultZoom()) | 57 if (IsAtDefaultZoom()) |
54 return IDR_ZOOM_NORMAL; | 58 return IDR_ZOOM_NORMAL; |
55 double zoom = content::HostZoomMap::GetZoomLevel(web_contents()); | 59 return GetZoomLevel() > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS |
56 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; | 60 : IDR_ZOOM_MINUS; |
61 } | |
62 | |
63 double ZoomController::GetZoomLevel() const { | |
64 if (zoom_mode_ == kZoomModeManual) | |
Devlin
2014/06/19 21:15:30
nit: Ternary if, please.
wjmaclean
2014/06/20 22:01:33
Done.
| |
65 return zoom_level_; | |
66 else | |
67 return content::HostZoomMap::GetZoomLevel(web_contents()); | |
68 } | |
69 | |
70 int ZoomController::GetZoomPercent() const { | |
71 double zoom_factor = content::ZoomLevelToZoomFactor(GetZoomLevel()); | |
72 return static_cast<int>(zoom_factor * 100 + 0.5); | |
73 } | |
74 | |
75 bool ZoomController::SetZoomLevel(double zoom_level) { | |
76 scoped_refptr<const extensions::Extension> null_extension_pointer; | |
77 // An extension did not initiate this zoom change. | |
78 return SetZoomLevelByExtension(zoom_level, null_extension_pointer); | |
Devlin
2014/06/19 21:15:29
nit: You can just do
return SetZoomLevelByExtensio
wjmaclean
2014/06/20 22:01:33
Done.
| |
79 } | |
80 | |
81 bool ZoomController::SetZoomLevelByExtension( | |
82 double zoom_level, | |
83 scoped_refptr<const extensions::Extension> extension) { | |
84 // Cannot zoom in disabled mode. | |
85 if (zoom_mode_ == kZoomModeDisabled) | |
86 return false; | |
87 | |
88 if (zoom_mode_ == kZoomModeManual) { | |
Devlin
2014/06/19 21:15:30
Do we not care about the extension in manual mode?
wjmaclean
2014/06/20 22:01:34
It's probably true that consumers of OnZoomChanged
| |
89 double old_zoom_level = zoom_level_; | |
90 zoom_level_ = zoom_level; | |
91 | |
92 // TODO(wjmaclean) Do we care about filling in host/scheme here? | |
93 content::HostZoomMap::ZoomLevelChange change; | |
94 change.mode = content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; | |
95 change.zoom_level = zoom_level; | |
96 ZoomEventManager::GetForBrowserContext(web_contents()->GetBrowserContext()) | |
97 ->OnZoomLevelChanged(change); | |
98 | |
99 for (size_t i = 0; i < observers_.size(); ++i) | |
Devlin
2014/06/19 21:15:30
From observer_list:
FOR_EACH_OBSERVER(Observer,
wjmaclean
2014/06/20 22:01:34
Done.
| |
100 observers_[i]->OnZoomChangeInitiated( | |
101 web_contents(), old_zoom_level, zoom_level, zoom_mode_); | |
102 | |
103 return true; | |
104 } | |
105 | |
106 // Store extension data so that |extension| can be attributed when the zoom | |
107 // change completes. We expect that by the time this function returns that | |
108 // any observers that require this information will have requested it. | |
109 last_extension_ = extension; | |
110 | |
111 // Do not actually rescale the page in manual mode. | |
112 content::HostZoomMap* zoom_map = content::HostZoomMap::GetForBrowserContext( | |
113 web_contents()->GetBrowserContext()); | |
114 DCHECK(zoom_map); | |
115 double old_zoom_level = GetZoomLevel(); | |
116 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | |
117 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | |
118 if (zoom_mode_ == kZoomModeIsolated || | |
119 zoom_map->UsesTemporaryZoomLevel(render_process_id, render_view_id)) { | |
120 zoom_map->SetTemporaryZoomLevel( | |
121 render_process_id, render_view_id, zoom_level); | |
122 } else { | |
123 content::NavigationEntry* entry = | |
124 web_contents()->GetController().GetLastCommittedEntry(); | |
125 // We allow empty |host| values for data urls. | |
126 std::string host = | |
127 net::GetHostOrSpecFromURL(entry ? entry->GetURL() : GURL::EmptyGURL()); | |
128 zoom_map->SetZoomLevelForHost(host, zoom_level); | |
129 } | |
130 for (size_t i = 0; i < observers_.size(); ++i) | |
131 observers_[i]->OnZoomChangeInitiated( | |
Devlin
2014/06/19 21:15:29
Same FOR_EACH_OBSERVER
wjmaclean
2014/06/20 22:01:33
Done.
| |
132 web_contents(), old_zoom_level, zoom_level, zoom_mode_); | |
133 last_extension_ = NULL; | |
Devlin
2014/06/19 21:15:29
To me, it seems that if we only have last_extensio
wjmaclean
2014/06/20 22:01:33
The only consumer of this information at present i
Devlin
2014/06/23 22:56:07
I'm confused. It looks like last_extension_ is be
wjmaclean
2014/06/24 15:38:16
Update state gets called from the observer notific
| |
134 | |
135 return true; | |
136 } | |
137 | |
138 void ZoomController::SetZoomMode(ZoomMode new_mode) { | |
139 if (new_mode == zoom_mode_) | |
140 return; | |
141 | |
142 content::HostZoomMap* zoom_map = content::HostZoomMap::GetForBrowserContext( | |
143 web_contents()->GetBrowserContext()); | |
144 DCHECK(zoom_map); | |
145 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | |
146 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | |
147 double original_zoom_level = GetZoomLevel(); | |
148 | |
149 switch (new_mode) { | |
150 case kZoomModeDefault: { | |
151 content::NavigationEntry* entry = | |
152 web_contents()->GetController().GetLastCommittedEntry(); | |
153 GURL url; | |
154 if (entry) | |
155 url = entry->GetURL(); | |
156 std::string host = net::GetHostOrSpecFromURL(url); | |
157 | |
158 if (zoom_map->HasZoomLevel(url.scheme(), host)) { | |
159 // If there are other tabs with the same origin, then set this tab's | |
160 // zoom level to match theirs. The temporary zoom level will be cleared | |
161 // below, but this call will make sure this tab re-draws at the correct | |
162 // zoom level. | |
163 double origin_zoom_level = | |
164 zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host); | |
165 zoom_map->SetTemporaryZoomLevel( | |
166 render_process_id, render_view_id, origin_zoom_level); | |
167 } else { | |
168 // The host will need a level prior to removing the temporary level. | |
169 // We don't want the zoom level to change just because we entered | |
170 // default mode. | |
171 zoom_map->SetZoomLevelForHost(host, original_zoom_level); | |
172 } | |
173 // Remove per-tab zoom data for this tab. | |
174 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id); | |
175 break; | |
176 } | |
177 case kZoomModeIsolated: { | |
178 // Unless the zoom mode was |kZoomModeDisabled| before this call, the page | |
179 // needs an initial isolated zoom back to the same level it was at in the | |
180 // other mode. | |
181 if (zoom_mode_ != kZoomModeDisabled) { | |
182 zoom_map->SetTemporaryZoomLevel( | |
183 render_process_id, render_view_id, original_zoom_level); | |
184 } | |
185 break; | |
186 } | |
187 case kZoomModeManual: { | |
188 // Unless the zoom mode was |kZoomModeDisabled| before this call, the page | |
189 // needs to be resized to the default zoom before calling SetZoomLevel() | |
190 // so that the page can be resized manually to the same zoom as before. | |
191 if (zoom_mode_ != kZoomModeDisabled) { | |
192 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0); | |
193 zoom_level_ = original_zoom_level; | |
194 } | |
195 break; | |
196 } | |
197 case kZoomModeDisabled: { | |
198 // The page needs to be zoomed back to default before disabling the zoom | |
199 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0); | |
200 break; | |
201 } | |
202 } | |
203 | |
204 zoom_mode_ = new_mode; | |
57 } | 205 } |
58 | 206 |
59 void ZoomController::DidNavigateMainFrame( | 207 void ZoomController::DidNavigateMainFrame( |
60 const content::LoadCommittedDetails& details, | 208 const content::LoadCommittedDetails& details, |
61 const content::FrameNavigateParams& params) { | 209 const content::FrameNavigateParams& params) { |
62 // If the main frame's content has changed, the new page may have a different | 210 // If the main frame's content has changed, the new page may have a different |
63 // zoom level from the old one. | 211 // zoom level from the old one. |
64 UpdateState(std::string()); | 212 UpdateState(std::string()); |
65 } | 213 } |
66 | 214 |
67 void ZoomController::OnZoomLevelChanged( | 215 void ZoomController::OnZoomLevelChanged( |
68 const content::HostZoomMap::ZoomLevelChange& change) { | 216 const content::HostZoomMap::ZoomLevelChange& change) { |
69 UpdateState(change.host); | 217 UpdateStateIncludingTemporary( |
218 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 UpdateStateIncludingTemporary(host, false); | |
224 } | |
225 | |
226 void ZoomController::UpdateStateIncludingTemporary(const std::string& host, | |
227 bool is_temporary_zoom) { | |
73 // If |host| is empty, all observers should be updated. | 228 // If |host| is empty, all observers should be updated. |
74 if (!host.empty()) { | 229 if (!host.empty()) { |
75 // Use the navigation entry's URL instead of the WebContents' so virtual | 230 // Use the navigation entry's URL instead of the WebContents' so virtual |
76 // URLs work (e.g. chrome://settings). http://crbug.com/153950 | 231 // URLs work (e.g. chrome://settings). http://crbug.com/153950 |
77 content::NavigationEntry* entry = | 232 content::NavigationEntry* entry = |
78 web_contents()->GetController().GetLastCommittedEntry(); | 233 web_contents()->GetController().GetLastCommittedEntry(); |
79 if (!entry || | 234 if (!entry || |
80 host != net::GetHostOrSpecFromURL(entry->GetURL())) { | 235 host != net::GetHostOrSpecFromURL(entry->GetURL())) { |
81 return; | 236 return; |
82 } | 237 } |
83 } | 238 } |
84 | 239 |
85 bool dummy; | 240 // The zoom bubble can be shown for all normal, per-origin zoom changes |
86 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); | 241 // (where the host will not be empty and the zoom is not temporary), or any |
87 | 242 // special zoom changes (where the zoom mode will not be "default"). |
88 if (observer_) | 243 bool can_show_bubble = |
89 observer_->OnZoomChanged(web_contents(), !host.empty()); | 244 zoom_mode_ != kZoomModeDefault || (!host.empty() && !is_temporary_zoom); |
245 for (size_t i = 0; i < observers_.size(); ++i) | |
Devlin
2014/06/19 21:15:29
FOR_EACH_OBSERVER
wjmaclean
2014/06/20 22:01:34
Done.
| |
246 observers_[i]->OnZoomChanged(web_contents(), can_show_bubble); | |
90 } | 247 } |
OLD | NEW |