| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/zoom/zoom_controller.h" | |
| 6 | |
| 7 #include "chrome/browser/ui/sad_tab.h" | |
| 8 #include "chrome/browser/ui/zoom/zoom_event_manager.h" | |
| 9 #include "chrome/browser/ui/zoom/zoom_observer.h" | |
| 10 #include "content/public/browser/host_zoom_map.h" | |
| 11 #include "content/public/browser/navigation_details.h" | |
| 12 #include "content/public/browser/navigation_entry.h" | |
| 13 #include "content/public/browser/render_process_host.h" | |
| 14 #include "content/public/browser/render_view_host.h" | |
| 15 #include "content/public/browser/web_contents.h" | |
| 16 #include "content/public/common/page_type.h" | |
| 17 #include "content/public/common/page_zoom.h" | |
| 18 #include "extensions/common/extension.h" | |
| 19 #include "grit/theme_resources.h" | |
| 20 #include "net/base/net_util.h" | |
| 21 | |
| 22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); | |
| 23 | |
| 24 ZoomController::ZoomController(content::WebContents* web_contents) | |
| 25 : content::WebContentsObserver(web_contents), | |
| 26 can_show_bubble_(true), | |
| 27 zoom_mode_(ZOOM_MODE_DEFAULT), | |
| 28 zoom_level_(1.0), | |
| 29 browser_context_(web_contents->GetBrowserContext()) { | |
| 30 content::HostZoomMap* host_zoom_map = | |
| 31 content::HostZoomMap::GetForWebContents(web_contents); | |
| 32 zoom_level_ = host_zoom_map->GetDefaultZoomLevel(); | |
| 33 | |
| 34 zoom_subscription_ = host_zoom_map->AddZoomLevelChangedCallback( | |
| 35 base::Bind(&ZoomController::OnZoomLevelChanged, base::Unretained(this))); | |
| 36 | |
| 37 UpdateState(std::string()); | |
| 38 } | |
| 39 | |
| 40 ZoomController::~ZoomController() {} | |
| 41 | |
| 42 bool ZoomController::IsAtDefaultZoom() const { | |
| 43 return content::ZoomValuesEqual(GetZoomLevel(), GetDefaultZoomLevel()); | |
| 44 } | |
| 45 | |
| 46 int ZoomController::GetResourceForZoomLevel() const { | |
| 47 if (IsAtDefaultZoom()) | |
| 48 return IDR_ZOOM_NORMAL; | |
| 49 return GetZoomLevel() > GetDefaultZoomLevel() ? IDR_ZOOM_PLUS | |
| 50 : IDR_ZOOM_MINUS; | |
| 51 } | |
| 52 | |
| 53 void ZoomController::AddObserver(ZoomObserver* observer) { | |
| 54 observers_.AddObserver(observer); | |
| 55 } | |
| 56 | |
| 57 void ZoomController::RemoveObserver(ZoomObserver* observer) { | |
| 58 observers_.RemoveObserver(observer); | |
| 59 } | |
| 60 | |
| 61 double ZoomController::GetZoomLevel() const { | |
| 62 return zoom_mode_ == ZOOM_MODE_MANUAL ? | |
| 63 zoom_level_: | |
| 64 content::HostZoomMap::GetZoomLevel(web_contents()); | |
| 65 } | |
| 66 | |
| 67 int ZoomController::GetZoomPercent() const { | |
| 68 double zoom_factor = content::ZoomLevelToZoomFactor(GetZoomLevel()); | |
| 69 // Round double for return. | |
| 70 return static_cast<int>(zoom_factor * 100 + 0.5); | |
| 71 } | |
| 72 | |
| 73 bool ZoomController::SetZoomLevel(double zoom_level) { | |
| 74 // An extension did not initiate this zoom change. | |
| 75 return SetZoomLevelByExtension(zoom_level, NULL); | |
| 76 } | |
| 77 | |
| 78 bool ZoomController::SetZoomLevelByExtension( | |
| 79 double zoom_level, | |
| 80 const scoped_refptr<const extensions::Extension>& extension) { | |
| 81 content::NavigationEntry* entry = | |
| 82 web_contents()->GetController().GetLastCommittedEntry(); | |
| 83 // Cannot zoom in disabled mode. Also, don't allow changing zoom level on | |
| 84 // a crashed tab, an error page or an interstitial page. | |
| 85 if (zoom_mode_ == ZOOM_MODE_DISABLED || | |
| 86 !web_contents()->GetRenderViewHost()->IsRenderViewLive()) | |
| 87 return false; | |
| 88 | |
| 89 // Store extension data so that |extension| can be attributed when the zoom | |
| 90 // change completes. We expect that by the time this function returns that | |
| 91 // any observers that require this information will have requested it. | |
| 92 last_extension_ = extension; | |
| 93 | |
| 94 // Do not actually rescale the page in manual mode. | |
| 95 if (zoom_mode_ == ZOOM_MODE_MANUAL) { | |
| 96 double old_zoom_level = zoom_level_; | |
| 97 zoom_level_ = zoom_level; | |
| 98 | |
| 99 // TODO(wjmaclean) Do we care about filling in host/scheme here? | |
| 100 content::HostZoomMap::ZoomLevelChange change; | |
| 101 change.mode = content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; | |
| 102 change.zoom_level = zoom_level; | |
| 103 ZoomEventManager::GetForBrowserContext(browser_context_)-> | |
| 104 OnZoomLevelChanged(change); | |
| 105 | |
| 106 ZoomChangedEventData zoom_change_data(web_contents(), | |
| 107 old_zoom_level, | |
| 108 zoom_level_, | |
| 109 zoom_mode_, | |
| 110 false /* can_show_bubble */); | |
| 111 FOR_EACH_OBSERVER( | |
| 112 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); | |
| 113 | |
| 114 last_extension_ = NULL; | |
| 115 return true; | |
| 116 } | |
| 117 | |
| 118 content::HostZoomMap* zoom_map = | |
| 119 content::HostZoomMap::GetForWebContents(web_contents()); | |
| 120 DCHECK(zoom_map); | |
| 121 DCHECK(!event_data_); | |
| 122 event_data_.reset(new ZoomChangedEventData(web_contents(), | |
| 123 GetZoomLevel(), | |
| 124 zoom_level, | |
| 125 zoom_mode_, | |
| 126 false /* can_show_bubble */)); | |
| 127 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | |
| 128 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | |
| 129 if (zoom_mode_ == ZOOM_MODE_ISOLATED || | |
| 130 zoom_map->UsesTemporaryZoomLevel(render_process_id, render_view_id)) { | |
| 131 zoom_map->SetTemporaryZoomLevel( | |
| 132 render_process_id, render_view_id, zoom_level); | |
| 133 } else { | |
| 134 if (!entry) { | |
| 135 last_extension_ = NULL; | |
| 136 return false; | |
| 137 } | |
| 138 std::string host = | |
| 139 net::GetHostOrSpecFromURL(content::HostZoomMap::GetURLFromEntry(entry)); | |
| 140 zoom_map->SetZoomLevelForHost(host, zoom_level); | |
| 141 } | |
| 142 | |
| 143 DCHECK(!event_data_); | |
| 144 last_extension_ = NULL; | |
| 145 return true; | |
| 146 } | |
| 147 | |
| 148 void ZoomController::SetZoomMode(ZoomMode new_mode) { | |
| 149 if (new_mode == zoom_mode_) | |
| 150 return; | |
| 151 | |
| 152 content::HostZoomMap* zoom_map = | |
| 153 content::HostZoomMap::GetForWebContents(web_contents()); | |
| 154 DCHECK(zoom_map); | |
| 155 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | |
| 156 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | |
| 157 double original_zoom_level = GetZoomLevel(); | |
| 158 | |
| 159 DCHECK(!event_data_); | |
| 160 event_data_.reset(new ZoomChangedEventData(web_contents(), | |
| 161 original_zoom_level, | |
| 162 original_zoom_level, | |
| 163 new_mode, | |
| 164 new_mode != ZOOM_MODE_DEFAULT)); | |
| 165 | |
| 166 switch (new_mode) { | |
| 167 case ZOOM_MODE_DEFAULT: { | |
| 168 content::NavigationEntry* entry = | |
| 169 web_contents()->GetController().GetLastCommittedEntry(); | |
| 170 | |
| 171 if (entry) { | |
| 172 GURL url = content::HostZoomMap::GetURLFromEntry(entry); | |
| 173 std::string host = net::GetHostOrSpecFromURL(url); | |
| 174 | |
| 175 if (zoom_map->HasZoomLevel(url.scheme(), host)) { | |
| 176 // If there are other tabs with the same origin, then set this tab's | |
| 177 // zoom level to match theirs. The temporary zoom level will be | |
| 178 // cleared below, but this call will make sure this tab re-draws at | |
| 179 // the correct zoom level. | |
| 180 double origin_zoom_level = | |
| 181 zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host); | |
| 182 event_data_->new_zoom_level = origin_zoom_level; | |
| 183 zoom_map->SetTemporaryZoomLevel( | |
| 184 render_process_id, render_view_id, origin_zoom_level); | |
| 185 } else { | |
| 186 // The host will need a level prior to removing the temporary level. | |
| 187 // We don't want the zoom level to change just because we entered | |
| 188 // default mode. | |
| 189 zoom_map->SetZoomLevelForHost(host, original_zoom_level); | |
| 190 } | |
| 191 } | |
| 192 // Remove per-tab zoom data for this tab. No event callback expected. | |
| 193 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id); | |
| 194 break; | |
| 195 } | |
| 196 case ZOOM_MODE_ISOLATED: { | |
| 197 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the | |
| 198 // page needs an initial isolated zoom back to the same level it was at | |
| 199 // in the other mode. | |
| 200 if (zoom_mode_ != ZOOM_MODE_DISABLED) { | |
| 201 zoom_map->SetTemporaryZoomLevel( | |
| 202 render_process_id, render_view_id, original_zoom_level); | |
| 203 } else { | |
| 204 // When we don't call any HostZoomMap set functions, we send the event | |
| 205 // manually. | |
| 206 FOR_EACH_OBSERVER( | |
| 207 ZoomObserver, observers_, OnZoomChanged(*event_data_)); | |
| 208 event_data_.reset(); | |
| 209 } | |
| 210 break; | |
| 211 } | |
| 212 case ZOOM_MODE_MANUAL: { | |
| 213 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the | |
| 214 // page needs to be resized to the default zoom. While in manual mode, | |
| 215 // the zoom level is handled independently. | |
| 216 if (zoom_mode_ != ZOOM_MODE_DISABLED) { | |
| 217 zoom_map->SetTemporaryZoomLevel( | |
| 218 render_process_id, render_view_id, GetDefaultZoomLevel()); | |
| 219 zoom_level_ = original_zoom_level; | |
| 220 } else { | |
| 221 // When we don't call any HostZoomMap set functions, we send the event | |
| 222 // manually. | |
| 223 FOR_EACH_OBSERVER( | |
| 224 ZoomObserver, observers_, OnZoomChanged(*event_data_)); | |
| 225 event_data_.reset(); | |
| 226 } | |
| 227 break; | |
| 228 } | |
| 229 case ZOOM_MODE_DISABLED: { | |
| 230 // The page needs to be zoomed back to default before disabling the zoom | |
| 231 zoom_map->SetTemporaryZoomLevel( | |
| 232 render_process_id, render_view_id, GetDefaultZoomLevel()); | |
| 233 break; | |
| 234 } | |
| 235 } | |
| 236 // Any event data we've stored should have been consumed by this point. | |
| 237 DCHECK(!event_data_); | |
| 238 | |
| 239 zoom_mode_ = new_mode; | |
| 240 } | |
| 241 | |
| 242 void ZoomController::DidNavigateMainFrame( | |
| 243 const content::LoadCommittedDetails& details, | |
| 244 const content::FrameNavigateParams& params) { | |
| 245 if (details.entry && details.entry->GetPageType() == content::PAGE_TYPE_ERROR) | |
| 246 content::HostZoomMap::SendErrorPageZoomLevelRefresh(web_contents()); | |
| 247 | |
| 248 // If the main frame's content has changed, the new page may have a different | |
| 249 // zoom level from the old one. | |
| 250 UpdateState(std::string()); | |
| 251 } | |
| 252 | |
| 253 void ZoomController::WebContentsDestroyed() { | |
| 254 // At this point we should no longer be sending any zoom events with this | |
| 255 // WebContents. | |
| 256 observers_.Clear(); | |
| 257 } | |
| 258 | |
| 259 void ZoomController::OnZoomLevelChanged( | |
| 260 const content::HostZoomMap::ZoomLevelChange& change) { | |
| 261 UpdateState(change.host); | |
| 262 } | |
| 263 | |
| 264 void ZoomController::UpdateState(const std::string& host) { | |
| 265 // If |host| is empty, all observers should be updated. | |
| 266 if (!host.empty()) { | |
| 267 // Use the navigation entry's URL instead of the WebContents' so virtual | |
| 268 // URLs work (e.g. chrome://settings). http://crbug.com/153950 | |
| 269 content::NavigationEntry* entry = | |
| 270 web_contents()->GetController().GetLastCommittedEntry(); | |
| 271 if (!entry || | |
| 272 host != net::GetHostOrSpecFromURL( | |
| 273 content::HostZoomMap::GetURLFromEntry(entry))) { | |
| 274 return; | |
| 275 } | |
| 276 } | |
| 277 | |
| 278 // The zoom bubble should not be shown for zoom changes where the host is | |
| 279 // empty. | |
| 280 bool can_show_bubble = can_show_bubble_ && !host.empty(); | |
| 281 | |
| 282 if (event_data_) { | |
| 283 // For state changes initiated within the ZoomController, information about | |
| 284 // the change should be sent. | |
| 285 ZoomChangedEventData zoom_change_data = *event_data_; | |
| 286 event_data_.reset(); | |
| 287 zoom_change_data.can_show_bubble = can_show_bubble; | |
| 288 FOR_EACH_OBSERVER( | |
| 289 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); | |
| 290 } else { | |
| 291 // TODO(wjmaclean) Should we consider having HostZoomMap send both old and | |
| 292 // new zoom levels here? | |
| 293 double zoom_level = GetZoomLevel(); | |
| 294 ZoomChangedEventData zoom_change_data( | |
| 295 web_contents(), zoom_level, zoom_level, zoom_mode_, can_show_bubble); | |
| 296 FOR_EACH_OBSERVER( | |
| 297 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); | |
| 298 } | |
| 299 } | |
| OLD | NEW |