Chromium Code Reviews| 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" | |
| 8 #include "chrome/browser/chrome_notification_types.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/browser/ui/zoom/zoom_event_manager.h" | 7 #include "chrome/browser/ui/zoom/zoom_event_manager.h" |
| 11 #include "chrome/browser/ui/zoom/zoom_observer.h" | 8 #include "chrome/browser/ui/zoom/zoom_observer.h" |
| 12 #include "chrome/common/pref_names.h" | |
| 13 #include "content/public/browser/host_zoom_map.h" | 9 #include "content/public/browser/host_zoom_map.h" |
| 14 #include "content/public/browser/navigation_entry.h" | 10 #include "content/public/browser/navigation_entry.h" |
| 15 #include "content/public/browser/notification_details.h" | |
| 16 #include "content/public/browser/notification_service.h" | |
| 17 #include "content/public/browser/notification_types.h" | |
| 18 #include "content/public/browser/render_process_host.h" | 11 #include "content/public/browser/render_process_host.h" |
| 19 #include "content/public/browser/render_view_host.h" | 12 #include "content/public/browser/render_view_host.h" |
| 20 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/common/page_zoom.h" | 14 #include "content/public/common/page_zoom.h" |
| 22 #include "extensions/common/extension.h" | 15 #include "extensions/common/extension.h" |
| 23 #include "grit/theme_resources.h" | 16 #include "grit/theme_resources.h" |
| 24 #include "net/base/net_util.h" | 17 #include "net/base/net_util.h" |
| 25 | 18 |
| 26 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); | 19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); |
| 27 | 20 |
| 28 ZoomController::ZoomController(content::WebContents* web_contents) | 21 ZoomController::ZoomController(content::WebContents* web_contents) |
| 29 : content::WebContentsObserver(web_contents), | 22 : content::WebContentsObserver(web_contents), |
| 30 can_show_bubble_(true), | 23 can_show_bubble_(true), |
| 31 zoom_mode_(ZOOM_MODE_DEFAULT), | 24 zoom_mode_(ZOOM_MODE_DEFAULT), |
| 32 zoom_level_(1.0), | 25 zoom_level_(1.0), |
| 33 browser_context_(web_contents->GetBrowserContext()) { | 26 browser_context_(web_contents->GetBrowserContext()) { |
| 34 Profile* profile = | 27 content::HostZoomMap* host_zoom_map = |
| 35 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 28 content::HostZoomMap::GetForWebContents(web_contents); |
| 36 default_zoom_level_.Init( | 29 zoom_level_ = host_zoom_map->GetDefaultZoomLevel(); |
| 37 prefs::kDefaultZoomLevel, | |
| 38 profile->GetPrefs(), | |
| 39 base::Bind( | |
| 40 &ZoomController::UpdateState, base::Unretained(this), std::string())); | |
| 41 zoom_level_ = default_zoom_level_.GetValue(); | |
| 42 | 30 |
| 43 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext( | 31 zoom_subscription_ = host_zoom_map->AddZoomLevelChangedCallback( |
| 44 browser_context_)->AddZoomLevelChangedCallback( | 32 base::Bind(&ZoomController::OnZoomLevelChanged, base::Unretained(this))); |
| 45 base::Bind(&ZoomController::OnZoomLevelChanged, | |
| 46 base::Unretained(this))); | |
| 47 | 33 |
| 48 UpdateState(std::string()); | 34 UpdateState(std::string()); |
| 49 } | 35 } |
| 50 | 36 |
| 51 ZoomController::~ZoomController() {} | 37 ZoomController::~ZoomController() {} |
| 52 | 38 |
| 53 bool ZoomController::IsAtDefaultZoom() const { | 39 bool ZoomController::IsAtDefaultZoom() const { |
| 54 return content::ZoomValuesEqual(GetZoomLevel(), | 40 return content::ZoomValuesEqual(GetZoomLevel(), GetDefaultZoomLevel()); |
| 55 default_zoom_level_.GetValue()); | |
| 56 } | 41 } |
| 57 | 42 |
| 58 int ZoomController::GetResourceForZoomLevel() const { | 43 int ZoomController::GetResourceForZoomLevel() const { |
| 59 if (IsAtDefaultZoom()) | 44 if (IsAtDefaultZoom()) |
| 60 return IDR_ZOOM_NORMAL; | 45 return IDR_ZOOM_NORMAL; |
| 61 return GetZoomLevel() > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS | 46 return GetZoomLevel() > GetDefaultZoomLevel() ? IDR_ZOOM_PLUS |
| 62 : IDR_ZOOM_MINUS; | 47 : IDR_ZOOM_MINUS; |
| 63 } | 48 } |
| 64 | 49 |
| 65 void ZoomController::AddObserver(ZoomObserver* observer) { | 50 void ZoomController::AddObserver(ZoomObserver* observer) { |
| 66 observers_.AddObserver(observer); | 51 observers_.AddObserver(observer); |
| 67 } | 52 } |
| 68 | 53 |
| 69 void ZoomController::RemoveObserver(ZoomObserver* observer) { | 54 void ZoomController::RemoveObserver(ZoomObserver* observer) { |
| 70 observers_.RemoveObserver(observer); | 55 observers_.RemoveObserver(observer); |
| 71 } | 56 } |
| 72 | 57 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 zoom_mode_, | 104 zoom_mode_, |
| 120 false /* can_show_bubble */); | 105 false /* can_show_bubble */); |
| 121 FOR_EACH_OBSERVER( | 106 FOR_EACH_OBSERVER( |
| 122 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); | 107 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); |
| 123 | 108 |
| 124 last_extension_ = NULL; | 109 last_extension_ = NULL; |
| 125 return true; | 110 return true; |
| 126 } | 111 } |
| 127 | 112 |
| 128 content::HostZoomMap* zoom_map = | 113 content::HostZoomMap* zoom_map = |
| 129 content::HostZoomMap::GetForBrowserContext(browser_context_); | 114 content::HostZoomMap::GetForWebContents(web_contents()); |
| 130 DCHECK(zoom_map); | 115 DCHECK(zoom_map); |
| 131 DCHECK(!event_data_); | 116 DCHECK(!event_data_); |
| 132 event_data_.reset(new ZoomChangedEventData(web_contents(), | 117 event_data_.reset(new ZoomChangedEventData(web_contents(), |
| 133 GetZoomLevel(), | 118 GetZoomLevel(), |
| 134 zoom_level, | 119 zoom_level, |
| 135 zoom_mode_, | 120 zoom_mode_, |
| 136 false /* can_show_bubble */)); | 121 false /* can_show_bubble */)); |
| 137 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | 122 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); |
| 138 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | 123 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); |
| 139 if (zoom_mode_ == ZOOM_MODE_ISOLATED || | 124 if (zoom_mode_ == ZOOM_MODE_ISOLATED || |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 155 DCHECK(!event_data_); | 140 DCHECK(!event_data_); |
| 156 last_extension_ = NULL; | 141 last_extension_ = NULL; |
| 157 return true; | 142 return true; |
| 158 } | 143 } |
| 159 | 144 |
| 160 void ZoomController::SetZoomMode(ZoomMode new_mode) { | 145 void ZoomController::SetZoomMode(ZoomMode new_mode) { |
| 161 if (new_mode == zoom_mode_) | 146 if (new_mode == zoom_mode_) |
| 162 return; | 147 return; |
| 163 | 148 |
| 164 content::HostZoomMap* zoom_map = | 149 content::HostZoomMap* zoom_map = |
| 165 content::HostZoomMap::GetForBrowserContext(browser_context_); | 150 content::HostZoomMap::GetForWebContents(web_contents()); |
| 166 DCHECK(zoom_map); | 151 DCHECK(zoom_map); |
| 167 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | 152 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); |
| 168 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | 153 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); |
| 169 double original_zoom_level = GetZoomLevel(); | 154 double original_zoom_level = GetZoomLevel(); |
| 170 | 155 |
| 171 DCHECK(!event_data_); | 156 DCHECK(!event_data_); |
| 172 event_data_.reset(new ZoomChangedEventData(web_contents(), | 157 event_data_.reset(new ZoomChangedEventData(web_contents(), |
| 173 original_zoom_level, | 158 original_zoom_level, |
| 174 original_zoom_level, | 159 original_zoom_level, |
| 175 new_mode, | 160 new_mode, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 event_data_.reset(); | 205 event_data_.reset(); |
| 221 } | 206 } |
| 222 break; | 207 break; |
| 223 } | 208 } |
| 224 case ZOOM_MODE_MANUAL: { | 209 case ZOOM_MODE_MANUAL: { |
| 225 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the | 210 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the |
| 226 // page needs to be resized to the default zoom. While in manual mode, | 211 // page needs to be resized to the default zoom. While in manual mode, |
| 227 // the zoom level is handled independently. | 212 // the zoom level is handled independently. |
| 228 if (zoom_mode_ != ZOOM_MODE_DISABLED) { | 213 if (zoom_mode_ != ZOOM_MODE_DISABLED) { |
| 229 zoom_map->SetTemporaryZoomLevel( | 214 zoom_map->SetTemporaryZoomLevel( |
| 230 render_process_id, render_view_id, default_zoom_level_.GetValue()); | 215 render_process_id, render_view_id, GetDefaultZoomLevel()); |
| 231 zoom_level_ = original_zoom_level; | 216 zoom_level_ = original_zoom_level; |
| 232 } else { | 217 } else { |
| 233 // When we don't call any HostZoomMap set functions, we send the event | 218 // When we don't call any HostZoomMap set functions, we send the event |
| 234 // manually. | 219 // manually. |
| 235 FOR_EACH_OBSERVER( | 220 FOR_EACH_OBSERVER( |
| 236 ZoomObserver, observers_, OnZoomChanged(*event_data_)); | 221 ZoomObserver, observers_, OnZoomChanged(*event_data_)); |
| 237 event_data_.reset(); | 222 event_data_.reset(); |
| 238 } | 223 } |
| 239 break; | 224 break; |
| 240 } | 225 } |
| 241 case ZOOM_MODE_DISABLED: { | 226 case ZOOM_MODE_DISABLED: { |
| 242 // The page needs to be zoomed back to default before disabling the zoom | 227 // The page needs to be zoomed back to default before disabling the zoom |
| 243 zoom_map->SetTemporaryZoomLevel( | 228 zoom_map->SetTemporaryZoomLevel( |
| 244 render_process_id, render_view_id, default_zoom_level_.GetValue()); | 229 render_process_id, render_view_id, GetDefaultZoomLevel()); |
| 245 break; | 230 break; |
| 246 } | 231 } |
| 247 } | 232 } |
| 248 // Any event data we've stored should have been consumed by this point. | 233 // Any event data we've stored should have been consumed by this point. |
| 249 DCHECK(!event_data_); | 234 DCHECK(!event_data_); |
| 250 | 235 |
| 251 zoom_mode_ = new_mode; | 236 zoom_mode_ = new_mode; |
| 252 } | 237 } |
| 253 | 238 |
| 254 void ZoomController::DidNavigateMainFrame( | 239 void ZoomController::DidNavigateMainFrame( |
| 255 const content::LoadCommittedDetails& details, | 240 const content::LoadCommittedDetails& details, |
| 256 const content::FrameNavigateParams& params) { | 241 const content::FrameNavigateParams& params) { |
| 257 // If the main frame's content has changed, the new page may have a different | 242 // If the main frame's content has changed, the new page may have a different |
| 258 // zoom level from the old one. | 243 // zoom level from the old one. |
| 259 UpdateState(std::string()); | 244 UpdateState(std::string()); |
| 260 } | 245 } |
| 261 | 246 |
| 262 void ZoomController::WebContentsDestroyed() { | 247 void ZoomController::WebContentsDestroyed() { |
| 263 // At this point we should no longer be sending any zoom events with this | 248 // At this point we should no longer be sending any zoom events with this |
| 264 // WebContents. | 249 // WebContents. |
| 265 observers_.Clear(); | 250 observers_.Clear(); |
| 266 } | 251 } |
| 267 | 252 |
| 268 void ZoomController::OnZoomLevelChanged( | 253 void ZoomController::OnZoomLevelChanged( |
| 269 const content::HostZoomMap::ZoomLevelChange& change) { | 254 const content::HostZoomMap::ZoomLevelChange& change) { |
| 270 UpdateState(change.host); | 255 if (change.mode == content::HostZoomMap::ZOOM_CHANGED_DEFAULT_ZOOM_LEVEL) { |
| 256 // TODO(wjmaclean) I don't think it should be possible for a change in the | |
| 257 // default zoom level to interrupt an in-flight zoom change, but if it can | |
| 258 // then we should notify observers here without calling update state. | |
|
awong
2014/08/20 19:56:58
If this shouldn't be possible, shouldn't we bias o
wjmaclean
2014/08/20 20:15:45
Actually, this code is gone in my current working
| |
| 259 DCHECK(!event_data_); | |
| 260 double zoom_level = GetZoomLevel(); | |
| 261 // Only relay the event if our web contents is affected by it. | |
| 262 if (zoom_mode_ == ZOOM_MODE_DEFAULT && change.zoom_level == zoom_level) { | |
| 263 ZoomChangedEventData zoom_change_data( | |
| 264 web_contents(), zoom_level, zoom_level, zoom_mode_, false); | |
| 265 FOR_EACH_OBSERVER( | |
| 266 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); | |
| 267 } | |
| 268 } else { | |
| 269 UpdateState(change.host); | |
| 270 } | |
| 271 } | 271 } |
| 272 | 272 |
| 273 void ZoomController::UpdateState(const std::string& host) { | 273 void ZoomController::UpdateState(const std::string& host) { |
| 274 // If |host| is empty, all observers should be updated. | 274 // If |host| is empty, all observers should be updated. |
| 275 if (!host.empty()) { | 275 if (!host.empty()) { |
| 276 // Use the navigation entry's URL instead of the WebContents' so virtual | 276 // Use the navigation entry's URL instead of the WebContents' so virtual |
| 277 // URLs work (e.g. chrome://settings). http://crbug.com/153950 | 277 // URLs work (e.g. chrome://settings). http://crbug.com/153950 |
| 278 content::NavigationEntry* entry = | 278 content::NavigationEntry* entry = |
| 279 web_contents()->GetController().GetLastCommittedEntry(); | 279 web_contents()->GetController().GetLastCommittedEntry(); |
| 280 if (!entry || | 280 if (!entry || |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 298 } else { | 298 } else { |
| 299 // TODO(wjmaclean) Should we consider having HostZoomMap send both old and | 299 // TODO(wjmaclean) Should we consider having HostZoomMap send both old and |
| 300 // new zoom levels here? | 300 // new zoom levels here? |
| 301 double zoom_level = GetZoomLevel(); | 301 double zoom_level = GetZoomLevel(); |
| 302 ZoomChangedEventData zoom_change_data( | 302 ZoomChangedEventData zoom_change_data( |
| 303 web_contents(), zoom_level, zoom_level, zoom_mode_, can_show_bubble); | 303 web_contents(), zoom_level, zoom_level, zoom_mode_, can_show_bubble); |
| 304 FOR_EACH_OBSERVER( | 304 FOR_EACH_OBSERVER( |
| 305 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); | 305 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); |
| 306 } | 306 } |
| 307 } | 307 } |
| OLD | NEW |