| 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 "chrome/browser/ui/sad_tab.h" | 7 #include "chrome/browser/ui/sad_tab.h" |
| 8 #include "chrome/browser/ui/zoom/zoom_event_manager.h" | 8 #include "chrome/browser/ui/zoom/zoom_event_manager.h" |
| 9 #include "chrome/browser/ui/zoom/zoom_observer.h" | 9 #include "chrome/browser/ui/zoom/zoom_observer.h" |
| 10 #include "content/public/browser/host_zoom_map.h" | 10 #include "content/public/browser/host_zoom_map.h" |
| 11 #include "content/public/browser/navigation_details.h" |
| 11 #include "content/public/browser/navigation_entry.h" | 12 #include "content/public/browser/navigation_entry.h" |
| 12 #include "content/public/browser/render_process_host.h" | 13 #include "content/public/browser/render_process_host.h" |
| 13 #include "content/public/browser/render_view_host.h" | 14 #include "content/public/browser/render_view_host.h" |
| 14 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/common/page_type.h" | 16 #include "content/public/common/page_type.h" |
| 16 #include "content/public/common/page_zoom.h" | 17 #include "content/public/common/page_zoom.h" |
| 17 #include "extensions/common/extension.h" | 18 #include "extensions/common/extension.h" |
| 18 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
| 19 #include "net/base/net_util.h" | 20 #include "net/base/net_util.h" |
| 20 | 21 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 bool ZoomController::SetZoomLevel(double zoom_level) { | 75 bool ZoomController::SetZoomLevel(double zoom_level) { |
| 75 // An extension did not initiate this zoom change. | 76 // An extension did not initiate this zoom change. |
| 76 return SetZoomLevelByExtension(zoom_level, NULL); | 77 return SetZoomLevelByExtension(zoom_level, NULL); |
| 77 } | 78 } |
| 78 | 79 |
| 79 bool ZoomController::SetZoomLevelByExtension( | 80 bool ZoomController::SetZoomLevelByExtension( |
| 80 double zoom_level, | 81 double zoom_level, |
| 81 const scoped_refptr<const extensions::Extension>& extension) { | 82 const scoped_refptr<const extensions::Extension>& extension) { |
| 82 content::NavigationEntry* entry = | 83 content::NavigationEntry* entry = |
| 83 web_contents()->GetController().GetLastCommittedEntry(); | 84 web_contents()->GetController().GetLastCommittedEntry(); |
| 84 bool is_normal_page = | |
| 85 entry && entry->GetPageType() == content::PAGE_TYPE_NORMAL; | |
| 86 // Cannot zoom in disabled mode. Also, don't allow changing zoom level on | 85 // Cannot zoom in disabled mode. Also, don't allow changing zoom level on |
| 87 // a crashed tab, an error page or an interstitial page. | 86 // a crashed tab, an error page or an interstitial page. |
| 88 if (zoom_mode_ == ZOOM_MODE_DISABLED || | 87 if (zoom_mode_ == ZOOM_MODE_DISABLED || |
| 89 !web_contents()->GetRenderViewHost()->IsRenderViewLive() || | 88 !web_contents()->GetRenderViewHost()->IsRenderViewLive()) |
| 90 !is_normal_page) | |
| 91 return false; | 89 return false; |
| 92 | 90 |
| 93 // Store extension data so that |extension| can be attributed when the zoom | 91 // Store extension data so that |extension| can be attributed when the zoom |
| 94 // change completes. We expect that by the time this function returns that | 92 // change completes. We expect that by the time this function returns that |
| 95 // any observers that require this information will have requested it. | 93 // any observers that require this information will have requested it. |
| 96 last_extension_ = extension; | 94 last_extension_ = extension; |
| 97 | 95 |
| 98 // Do not actually rescale the page in manual mode. | 96 // Do not actually rescale the page in manual mode. |
| 99 if (zoom_mode_ == ZOOM_MODE_MANUAL) { | 97 if (zoom_mode_ == ZOOM_MODE_MANUAL) { |
| 100 double old_zoom_level = zoom_level_; | 98 double old_zoom_level = zoom_level_; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | 130 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); |
| 133 if (zoom_mode_ == ZOOM_MODE_ISOLATED || | 131 if (zoom_mode_ == ZOOM_MODE_ISOLATED || |
| 134 zoom_map->UsesTemporaryZoomLevel(render_process_id, render_view_id)) { | 132 zoom_map->UsesTemporaryZoomLevel(render_process_id, render_view_id)) { |
| 135 zoom_map->SetTemporaryZoomLevel( | 133 zoom_map->SetTemporaryZoomLevel( |
| 136 render_process_id, render_view_id, zoom_level); | 134 render_process_id, render_view_id, zoom_level); |
| 137 } else { | 135 } else { |
| 138 if (!entry) { | 136 if (!entry) { |
| 139 last_extension_ = NULL; | 137 last_extension_ = NULL; |
| 140 return false; | 138 return false; |
| 141 } | 139 } |
| 142 std::string host = net::GetHostOrSpecFromURL(entry->GetURL()); | 140 std::string host = |
| 141 net::GetHostOrSpecFromURL(content::HostZoomMap::GetURLFromEntry(entry)); |
| 143 zoom_map->SetZoomLevelForHost(host, zoom_level); | 142 zoom_map->SetZoomLevelForHost(host, zoom_level); |
| 144 } | 143 } |
| 145 | 144 |
| 146 DCHECK(!event_data_); | 145 DCHECK(!event_data_); |
| 147 last_extension_ = NULL; | 146 last_extension_ = NULL; |
| 148 return true; | 147 return true; |
| 149 } | 148 } |
| 150 | 149 |
| 151 void ZoomController::SetZoomMode(ZoomMode new_mode) { | 150 void ZoomController::SetZoomMode(ZoomMode new_mode) { |
| 152 if (new_mode == zoom_mode_) | 151 if (new_mode == zoom_mode_) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 165 original_zoom_level, | 164 original_zoom_level, |
| 166 new_mode, | 165 new_mode, |
| 167 new_mode != ZOOM_MODE_DEFAULT)); | 166 new_mode != ZOOM_MODE_DEFAULT)); |
| 168 | 167 |
| 169 switch (new_mode) { | 168 switch (new_mode) { |
| 170 case ZOOM_MODE_DEFAULT: { | 169 case ZOOM_MODE_DEFAULT: { |
| 171 content::NavigationEntry* entry = | 170 content::NavigationEntry* entry = |
| 172 web_contents()->GetController().GetLastCommittedEntry(); | 171 web_contents()->GetController().GetLastCommittedEntry(); |
| 173 | 172 |
| 174 if (entry) { | 173 if (entry) { |
| 175 GURL url = entry->GetURL(); | 174 GURL url = content::HostZoomMap::GetURLFromEntry(entry); |
| 176 std::string host = net::GetHostOrSpecFromURL(url); | 175 std::string host = net::GetHostOrSpecFromURL(url); |
| 177 | 176 |
| 178 if (zoom_map->HasZoomLevel(url.scheme(), host)) { | 177 if (zoom_map->HasZoomLevel(url.scheme(), host)) { |
| 179 // If there are other tabs with the same origin, then set this tab's | 178 // If there are other tabs with the same origin, then set this tab's |
| 180 // zoom level to match theirs. The temporary zoom level will be | 179 // zoom level to match theirs. The temporary zoom level will be |
| 181 // cleared below, but this call will make sure this tab re-draws at | 180 // cleared below, but this call will make sure this tab re-draws at |
| 182 // the correct zoom level. | 181 // the correct zoom level. |
| 183 double origin_zoom_level = | 182 double origin_zoom_level = |
| 184 zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host); | 183 zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host); |
| 185 event_data_->new_zoom_level = origin_zoom_level; | 184 event_data_->new_zoom_level = origin_zoom_level; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 } | 237 } |
| 239 // Any event data we've stored should have been consumed by this point. | 238 // Any event data we've stored should have been consumed by this point. |
| 240 DCHECK(!event_data_); | 239 DCHECK(!event_data_); |
| 241 | 240 |
| 242 zoom_mode_ = new_mode; | 241 zoom_mode_ = new_mode; |
| 243 } | 242 } |
| 244 | 243 |
| 245 void ZoomController::DidNavigateMainFrame( | 244 void ZoomController::DidNavigateMainFrame( |
| 246 const content::LoadCommittedDetails& details, | 245 const content::LoadCommittedDetails& details, |
| 247 const content::FrameNavigateParams& params) { | 246 const content::FrameNavigateParams& params) { |
| 247 if (details.entry && details.entry->GetPageType() == content::PAGE_TYPE_ERROR) |
| 248 content::HostZoomMap::SendErrorPageZoomLevelRefresh(web_contents()); |
| 249 |
| 248 // If the main frame's content has changed, the new page may have a different | 250 // If the main frame's content has changed, the new page may have a different |
| 249 // zoom level from the old one. | 251 // zoom level from the old one. |
| 250 UpdateState(std::string()); | 252 UpdateState(std::string()); |
| 251 } | 253 } |
| 252 | 254 |
| 253 void ZoomController::WebContentsDestroyed() { | 255 void ZoomController::WebContentsDestroyed() { |
| 254 // At this point we should no longer be sending any zoom events with this | 256 // At this point we should no longer be sending any zoom events with this |
| 255 // WebContents. | 257 // WebContents. |
| 256 observers_.Clear(); | 258 observers_.Clear(); |
| 257 } | 259 } |
| 258 | 260 |
| 259 void ZoomController::OnZoomLevelChanged( | 261 void ZoomController::OnZoomLevelChanged( |
| 260 const content::HostZoomMap::ZoomLevelChange& change) { | 262 const content::HostZoomMap::ZoomLevelChange& change) { |
| 261 UpdateState(change.host); | 263 UpdateState(change.host); |
| 262 } | 264 } |
| 263 | 265 |
| 264 void ZoomController::UpdateState(const std::string& host) { | 266 void ZoomController::UpdateState(const std::string& host) { |
| 265 // If |host| is empty, all observers should be updated. | 267 // If |host| is empty, all observers should be updated. |
| 266 if (!host.empty()) { | 268 if (!host.empty()) { |
| 267 // Use the navigation entry's URL instead of the WebContents' so virtual | 269 // Use the navigation entry's URL instead of the WebContents' so virtual |
| 268 // URLs work (e.g. chrome://settings). http://crbug.com/153950 | 270 // URLs work (e.g. chrome://settings). http://crbug.com/153950 |
| 269 content::NavigationEntry* entry = | 271 content::NavigationEntry* entry = |
| 270 web_contents()->GetController().GetLastCommittedEntry(); | 272 web_contents()->GetController().GetLastCommittedEntry(); |
| 271 if (!entry || | 273 if (!entry || |
| 272 host != net::GetHostOrSpecFromURL(entry->GetURL())) { | 274 host != net::GetHostOrSpecFromURL( |
| 275 content::HostZoomMap::GetURLFromEntry(entry))) { |
| 273 return; | 276 return; |
| 274 } | 277 } |
| 275 } | 278 } |
| 276 | 279 |
| 277 // The zoom bubble should not be shown for zoom changes where the host is | 280 // The zoom bubble should not be shown for zoom changes where the host is |
| 278 // empty. | 281 // empty. |
| 279 bool can_show_bubble = can_show_bubble_ && !host.empty(); | 282 bool can_show_bubble = can_show_bubble_ && !host.empty(); |
| 280 | 283 |
| 281 if (event_data_) { | 284 if (event_data_) { |
| 282 // For state changes initiated within the ZoomController, information about | 285 // For state changes initiated within the ZoomController, information about |
| 283 // the change should be sent. | 286 // the change should be sent. |
| 284 ZoomChangedEventData zoom_change_data = *event_data_; | 287 ZoomChangedEventData zoom_change_data = *event_data_; |
| 285 event_data_.reset(); | 288 event_data_.reset(); |
| 286 zoom_change_data.can_show_bubble = can_show_bubble; | 289 zoom_change_data.can_show_bubble = can_show_bubble; |
| 287 FOR_EACH_OBSERVER( | 290 FOR_EACH_OBSERVER( |
| 288 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); | 291 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); |
| 289 } else { | 292 } else { |
| 290 // TODO(wjmaclean) Should we consider having HostZoomMap send both old and | 293 // TODO(wjmaclean) Should we consider having HostZoomMap send both old and |
| 291 // new zoom levels here? | 294 // new zoom levels here? |
| 292 double zoom_level = GetZoomLevel(); | 295 double zoom_level = GetZoomLevel(); |
| 293 ZoomChangedEventData zoom_change_data( | 296 ZoomChangedEventData zoom_change_data( |
| 294 web_contents(), zoom_level, zoom_level, zoom_mode_, can_show_bubble); | 297 web_contents(), zoom_level, zoom_level, zoom_mode_, can_show_bubble); |
| 295 FOR_EACH_OBSERVER( | 298 FOR_EACH_OBSERVER( |
| 296 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); | 299 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); |
| 297 } | 300 } |
| 298 } | 301 } |
| OLD | NEW |