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_(ZOOM_MODE_DEFAULT), |
27 observer_(NULL), | 31 zoom_level_(1.0), |
28 browser_context_(web_contents->GetBrowserContext()) { | 32 browser_context_(web_contents->GetBrowserContext()) { |
29 Profile* profile = | 33 Profile* profile = |
30 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 34 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
31 default_zoom_level_.Init(prefs::kDefaultZoomLevel, profile->GetPrefs(), | 35 default_zoom_level_.Init( |
32 base::Bind(&ZoomController::UpdateState, | 36 prefs::kDefaultZoomLevel, |
33 base::Unretained(this), | 37 profile->GetPrefs(), |
34 std::string())); | 38 base::Bind( |
| 39 &ZoomController::UpdateState, base::Unretained(this), 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 return GetZoomLevel() > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS |
56 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; | 61 : IDR_ZOOM_MINUS; |
| 62 } |
| 63 |
| 64 void ZoomController::AddObserver(ZoomObserver* observer) { |
| 65 observers_.AddObserver(observer); |
| 66 } |
| 67 |
| 68 void ZoomController::RemoveObserver(ZoomObserver* observer) { |
| 69 observers_.RemoveObserver(observer); |
| 70 } |
| 71 |
| 72 double ZoomController::GetZoomLevel() const { |
| 73 return zoom_mode_ == ZOOM_MODE_MANUAL ? |
| 74 zoom_level_: |
| 75 content::HostZoomMap::GetZoomLevel(web_contents()); |
| 76 } |
| 77 |
| 78 int ZoomController::GetZoomPercent() const { |
| 79 double zoom_factor = content::ZoomLevelToZoomFactor(GetZoomLevel()); |
| 80 // Round double for return. |
| 81 return static_cast<int>(zoom_factor * 100 + 0.5); |
| 82 } |
| 83 |
| 84 bool ZoomController::SetZoomLevel(double zoom_level) { |
| 85 // An extension did not initiate this zoom change. |
| 86 return SetZoomLevelByExtension(zoom_level, NULL); |
| 87 } |
| 88 |
| 89 bool ZoomController::SetZoomLevelByExtension( |
| 90 double zoom_level, |
| 91 const scoped_refptr<const extensions::Extension>& extension) { |
| 92 // Cannot zoom in disabled mode. |
| 93 if (zoom_mode_ == ZOOM_MODE_DISABLED) |
| 94 return false; |
| 95 |
| 96 // Store extension data so that |extension| can be attributed when the zoom |
| 97 // change completes. We expect that by the time this function returns that |
| 98 // any observers that require this information will have requested it. |
| 99 last_extension_ = extension; |
| 100 |
| 101 // Do not actually rescale the page in manual mode. |
| 102 if (zoom_mode_ == ZOOM_MODE_MANUAL) { |
| 103 double old_zoom_level = zoom_level_; |
| 104 zoom_level_ = zoom_level; |
| 105 |
| 106 // TODO(wjmaclean) Do we care about filling in host/scheme here? |
| 107 content::HostZoomMap::ZoomLevelChange change; |
| 108 change.mode = content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; |
| 109 change.zoom_level = zoom_level; |
| 110 ZoomEventManager::GetForBrowserContext(browser_context_)-> |
| 111 OnZoomLevelChanged(change); |
| 112 |
| 113 ZoomChangedEventData zoom_change_data(web_contents(), |
| 114 old_zoom_level, |
| 115 zoom_level_, |
| 116 zoom_mode_, |
| 117 false /* can_show_bubble */); |
| 118 FOR_EACH_OBSERVER( |
| 119 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); |
| 120 |
| 121 last_extension_ = NULL; |
| 122 return true; |
| 123 } |
| 124 |
| 125 content::HostZoomMap* zoom_map = |
| 126 content::HostZoomMap::GetForBrowserContext(browser_context_); |
| 127 DCHECK(zoom_map); |
| 128 DCHECK(event_data_.empty()); |
| 129 event_data_.push_back(ZoomChangedEventData(web_contents(), |
| 130 GetZoomLevel(), |
| 131 zoom_level, |
| 132 zoom_mode_, |
| 133 false /* can_show_bubble */)); |
| 134 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); |
| 135 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); |
| 136 if (zoom_mode_ == ZOOM_MODE_ISOLATED || |
| 137 zoom_map->UsesTemporaryZoomLevel(render_process_id, render_view_id)) { |
| 138 zoom_map->SetTemporaryZoomLevel( |
| 139 render_process_id, render_view_id, zoom_level); |
| 140 } else { |
| 141 content::NavigationEntry* entry = |
| 142 web_contents()->GetController().GetLastCommittedEntry(); |
| 143 |
| 144 if (!entry) { |
| 145 last_extension_ = NULL; |
| 146 return false; |
| 147 } |
| 148 std::string host = net::GetHostOrSpecFromURL(entry->GetURL()); |
| 149 zoom_map->SetZoomLevelForHost(host, zoom_level); |
| 150 } |
| 151 |
| 152 DCHECK(event_data_.empty()); |
| 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 ZoomChangedEventData zoom_change_data(web_contents(), |
| 169 original_zoom_level, |
| 170 original_zoom_level, |
| 171 new_mode, |
| 172 new_mode != ZOOM_MODE_DEFAULT); |
| 173 DCHECK(event_data_.empty()); |
| 174 event_data_.push_back(zoom_change_data); |
| 175 |
| 176 switch (new_mode) { |
| 177 case ZOOM_MODE_DEFAULT: { |
| 178 content::NavigationEntry* entry = |
| 179 web_contents()->GetController().GetLastCommittedEntry(); |
| 180 |
| 181 if (entry) { |
| 182 GURL url = entry->GetURL(); |
| 183 std::string host = net::GetHostOrSpecFromURL(url); |
| 184 |
| 185 if (zoom_map->HasZoomLevel(url.scheme(), host)) { |
| 186 // If there are other tabs with the same origin, then set this tab's |
| 187 // zoom level to match theirs. The temporary zoom level will be |
| 188 // cleared below, but this call will make sure this tab re-draws at |
| 189 // the correct zoom level. |
| 190 double origin_zoom_level = |
| 191 zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host); |
| 192 event_data_.back().new_zoom_level = origin_zoom_level; |
| 193 zoom_map->SetTemporaryZoomLevel( |
| 194 render_process_id, render_view_id, origin_zoom_level); |
| 195 } else { |
| 196 // The host will need a level prior to removing the temporary level. |
| 197 // We don't want the zoom level to change just because we entered |
| 198 // default mode. |
| 199 zoom_map->SetZoomLevelForHost(host, original_zoom_level); |
| 200 } |
| 201 } |
| 202 // Remove per-tab zoom data for this tab. No event callback expected. |
| 203 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id); |
| 204 break; |
| 205 } |
| 206 case ZOOM_MODE_ISOLATED: { |
| 207 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the |
| 208 // page needs an initial isolated zoom back to the same level it was at |
| 209 // in the other mode. |
| 210 if (zoom_mode_ != ZOOM_MODE_DISABLED) { |
| 211 zoom_map->SetTemporaryZoomLevel( |
| 212 render_process_id, render_view_id, original_zoom_level); |
| 213 } else { |
| 214 // When we don't call any HostZoomMap set functions, we send the event |
| 215 // manually. |
| 216 FOR_EACH_OBSERVER( |
| 217 ZoomObserver, observers_, OnZoomChanged(event_data_.back())); |
| 218 event_data_.pop_back(); |
| 219 } |
| 220 break; |
| 221 } |
| 222 case ZOOM_MODE_MANUAL: { |
| 223 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the |
| 224 // page needs to be resized to the default zoom. While in manual mode, |
| 225 // the zoom level is handled independently. |
| 226 if (zoom_mode_ != ZOOM_MODE_DISABLED) { |
| 227 zoom_map->SetTemporaryZoomLevel( |
| 228 render_process_id, render_view_id, default_zoom_level_.GetValue()); |
| 229 zoom_level_ = original_zoom_level; |
| 230 } else { |
| 231 // When we don't call any HostZoomMap set functions, we send the event |
| 232 // manually. |
| 233 FOR_EACH_OBSERVER( |
| 234 ZoomObserver, observers_, OnZoomChanged(event_data_.back())); |
| 235 event_data_.pop_back(); |
| 236 } |
| 237 break; |
| 238 } |
| 239 case ZOOM_MODE_DISABLED: { |
| 240 // The page needs to be zoomed back to default before disabling the zoom |
| 241 zoom_map->SetTemporaryZoomLevel( |
| 242 render_process_id, render_view_id, default_zoom_level_.GetValue()); |
| 243 break; |
| 244 } |
| 245 } |
| 246 // Any event data we've stored should have been consumed by this point. |
| 247 DCHECK(event_data_.empty()); |
| 248 |
| 249 zoom_mode_ = new_mode; |
57 } | 250 } |
58 | 251 |
59 void ZoomController::DidNavigateMainFrame( | 252 void ZoomController::DidNavigateMainFrame( |
60 const content::LoadCommittedDetails& details, | 253 const content::LoadCommittedDetails& details, |
61 const content::FrameNavigateParams& params) { | 254 const content::FrameNavigateParams& params) { |
62 // If the main frame's content has changed, the new page may have a different | 255 // If the main frame's content has changed, the new page may have a different |
63 // zoom level from the old one. | 256 // zoom level from the old one. |
64 UpdateState(std::string()); | 257 UpdateState(std::string()); |
65 } | 258 } |
66 | 259 |
67 void ZoomController::OnZoomLevelChanged( | 260 void ZoomController::OnZoomLevelChanged( |
68 const content::HostZoomMap::ZoomLevelChange& change) { | 261 const content::HostZoomMap::ZoomLevelChange& change) { |
69 UpdateState(change.host); | 262 UpdateStateIncludingTemporary( |
| 263 change.host, |
| 264 change.mode == content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM); |
70 } | 265 } |
71 | 266 |
72 void ZoomController::UpdateState(const std::string& host) { | 267 void ZoomController::UpdateState(const std::string& host) { |
| 268 UpdateStateIncludingTemporary(host, false); |
| 269 } |
| 270 |
| 271 void ZoomController::UpdateStateIncludingTemporary(const std::string& host, |
| 272 bool is_temporary_zoom) { |
73 // If |host| is empty, all observers should be updated. | 273 // If |host| is empty, all observers should be updated. |
74 if (!host.empty()) { | 274 if (!host.empty()) { |
75 // Use the navigation entry's URL instead of the WebContents' so virtual | 275 // Use the navigation entry's URL instead of the WebContents' so virtual |
76 // URLs work (e.g. chrome://settings). http://crbug.com/153950 | 276 // URLs work (e.g. chrome://settings). http://crbug.com/153950 |
77 content::NavigationEntry* entry = | 277 content::NavigationEntry* entry = |
78 web_contents()->GetController().GetLastCommittedEntry(); | 278 web_contents()->GetController().GetLastCommittedEntry(); |
79 if (!entry || | 279 if (!entry || |
80 host != net::GetHostOrSpecFromURL(entry->GetURL())) { | 280 host != net::GetHostOrSpecFromURL(entry->GetURL())) { |
81 return; | 281 return; |
82 } | 282 } |
83 } | 283 } |
84 | 284 |
85 bool dummy; | 285 // The zoom bubble can be shown for all normal, per-origin zoom changes |
86 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); | 286 // (where the host will not be empty and the zoom is not temporary), or any |
| 287 // special zoom changes (where the zoom mode will not be "default"). |
| 288 bool can_show_bubble = |
| 289 zoom_mode_ != ZOOM_MODE_DEFAULT || (!host.empty() && !is_temporary_zoom); |
87 | 290 |
88 if (observer_) | 291 if (!event_data_.empty()) { |
89 observer_->OnZoomChanged(web_contents(), !host.empty()); | 292 // For state changes initiated within the ZoomController, information about |
| 293 // the change should be sent. |
| 294 ZoomChangedEventData zoom_change_data = event_data_.back(); |
| 295 event_data_.pop_back(); |
| 296 zoom_change_data.can_show_bubble |= can_show_bubble; |
| 297 FOR_EACH_OBSERVER( |
| 298 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); |
| 299 } else { |
| 300 // TODO(wjmaclean) Should we consider having HostZoomMap send both old and |
| 301 // new zoom levels here? |
| 302 double zoom_level = GetZoomLevel(); |
| 303 ZoomChangedEventData zoom_change_data = ZoomChangedEventData( |
| 304 web_contents(), zoom_level, zoom_level, zoom_mode_, can_show_bubble); |
| 305 FOR_EACH_OBSERVER( |
| 306 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); |
| 307 } |
90 } | 308 } |
OLD | NEW |