| 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" |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 FOR_EACH_OBSERVER( | 118 FOR_EACH_OBSERVER( |
| 119 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); | 119 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); |
| 120 | 120 |
| 121 last_extension_ = NULL; | 121 last_extension_ = NULL; |
| 122 return true; | 122 return true; |
| 123 } | 123 } |
| 124 | 124 |
| 125 content::HostZoomMap* zoom_map = | 125 content::HostZoomMap* zoom_map = |
| 126 content::HostZoomMap::GetForBrowserContext(browser_context_); | 126 content::HostZoomMap::GetForBrowserContext(browser_context_); |
| 127 DCHECK(zoom_map); | 127 DCHECK(zoom_map); |
| 128 DCHECK(event_data_.empty()); | 128 DCHECK(!event_data_); |
| 129 event_data_.push_back(ZoomChangedEventData(web_contents(), | 129 event_data_.reset(new ZoomChangedEventData(web_contents(), |
| 130 GetZoomLevel(), | 130 GetZoomLevel(), |
| 131 zoom_level, | 131 zoom_level, |
| 132 zoom_mode_, | 132 zoom_mode_, |
| 133 false /* can_show_bubble */)); | 133 false /* can_show_bubble */)); |
| 134 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | 134 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); |
| 135 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | 135 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); |
| 136 if (zoom_mode_ == ZOOM_MODE_ISOLATED || | 136 if (zoom_mode_ == ZOOM_MODE_ISOLATED || |
| 137 zoom_map->UsesTemporaryZoomLevel(render_process_id, render_view_id)) { | 137 zoom_map->UsesTemporaryZoomLevel(render_process_id, render_view_id)) { |
| 138 zoom_map->SetTemporaryZoomLevel( | 138 zoom_map->SetTemporaryZoomLevel( |
| 139 render_process_id, render_view_id, zoom_level); | 139 render_process_id, render_view_id, zoom_level); |
| 140 } else { | 140 } else { |
| 141 content::NavigationEntry* entry = | 141 content::NavigationEntry* entry = |
| 142 web_contents()->GetController().GetLastCommittedEntry(); | 142 web_contents()->GetController().GetLastCommittedEntry(); |
| 143 | 143 |
| 144 if (!entry) { | 144 if (!entry) { |
| 145 last_extension_ = NULL; | 145 last_extension_ = NULL; |
| 146 return false; | 146 return false; |
| 147 } | 147 } |
| 148 std::string host = net::GetHostOrSpecFromURL(entry->GetURL()); | 148 std::string host = net::GetHostOrSpecFromURL(entry->GetURL()); |
| 149 zoom_map->SetZoomLevelForHost(host, zoom_level); | 149 zoom_map->SetZoomLevelForHost(host, zoom_level); |
| 150 } | 150 } |
| 151 | 151 |
| 152 DCHECK(event_data_.empty()); | 152 DCHECK(!event_data_); |
| 153 last_extension_ = NULL; | 153 last_extension_ = NULL; |
| 154 return true; | 154 return true; |
| 155 } | 155 } |
| 156 | 156 |
| 157 void ZoomController::SetZoomMode(ZoomMode new_mode) { | 157 void ZoomController::SetZoomMode(ZoomMode new_mode) { |
| 158 if (new_mode == zoom_mode_) | 158 if (new_mode == zoom_mode_) |
| 159 return; | 159 return; |
| 160 | 160 |
| 161 content::HostZoomMap* zoom_map = | 161 content::HostZoomMap* zoom_map = |
| 162 content::HostZoomMap::GetForBrowserContext(browser_context_); | 162 content::HostZoomMap::GetForBrowserContext(browser_context_); |
| 163 DCHECK(zoom_map); | 163 DCHECK(zoom_map); |
| 164 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); | 164 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); |
| 165 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); | 165 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); |
| 166 double original_zoom_level = GetZoomLevel(); | 166 double original_zoom_level = GetZoomLevel(); |
| 167 | 167 |
| 168 ZoomChangedEventData zoom_change_data(web_contents(), | 168 DCHECK(!event_data_); |
| 169 original_zoom_level, | 169 event_data_.reset(new ZoomChangedEventData(web_contents(), |
| 170 original_zoom_level, | 170 original_zoom_level, |
| 171 new_mode, | 171 original_zoom_level, |
| 172 new_mode != ZOOM_MODE_DEFAULT); | 172 new_mode, |
| 173 DCHECK(event_data_.empty()); | 173 new_mode != ZOOM_MODE_DEFAULT)); |
| 174 event_data_.push_back(zoom_change_data); | |
| 175 | 174 |
| 176 switch (new_mode) { | 175 switch (new_mode) { |
| 177 case ZOOM_MODE_DEFAULT: { | 176 case ZOOM_MODE_DEFAULT: { |
| 178 content::NavigationEntry* entry = | 177 content::NavigationEntry* entry = |
| 179 web_contents()->GetController().GetLastCommittedEntry(); | 178 web_contents()->GetController().GetLastCommittedEntry(); |
| 180 | 179 |
| 181 if (entry) { | 180 if (entry) { |
| 182 GURL url = entry->GetURL(); | 181 GURL url = entry->GetURL(); |
| 183 std::string host = net::GetHostOrSpecFromURL(url); | 182 std::string host = net::GetHostOrSpecFromURL(url); |
| 184 | 183 |
| 185 if (zoom_map->HasZoomLevel(url.scheme(), host)) { | 184 if (zoom_map->HasZoomLevel(url.scheme(), host)) { |
| 186 // If there are other tabs with the same origin, then set this tab's | 185 // 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 | 186 // 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 | 187 // cleared below, but this call will make sure this tab re-draws at |
| 189 // the correct zoom level. | 188 // the correct zoom level. |
| 190 double origin_zoom_level = | 189 double origin_zoom_level = |
| 191 zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host); | 190 zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host); |
| 192 event_data_.back().new_zoom_level = origin_zoom_level; | 191 event_data_->new_zoom_level = origin_zoom_level; |
| 193 zoom_map->SetTemporaryZoomLevel( | 192 zoom_map->SetTemporaryZoomLevel( |
| 194 render_process_id, render_view_id, origin_zoom_level); | 193 render_process_id, render_view_id, origin_zoom_level); |
| 195 } else { | 194 } else { |
| 196 // The host will need a level prior to removing the temporary level. | 195 // 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 | 196 // We don't want the zoom level to change just because we entered |
| 198 // default mode. | 197 // default mode. |
| 199 zoom_map->SetZoomLevelForHost(host, original_zoom_level); | 198 zoom_map->SetZoomLevelForHost(host, original_zoom_level); |
| 200 } | 199 } |
| 201 } | 200 } |
| 202 // Remove per-tab zoom data for this tab. No event callback expected. | 201 // Remove per-tab zoom data for this tab. No event callback expected. |
| 203 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id); | 202 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id); |
| 204 break; | 203 break; |
| 205 } | 204 } |
| 206 case ZOOM_MODE_ISOLATED: { | 205 case ZOOM_MODE_ISOLATED: { |
| 207 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the | 206 // 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 | 207 // page needs an initial isolated zoom back to the same level it was at |
| 209 // in the other mode. | 208 // in the other mode. |
| 210 if (zoom_mode_ != ZOOM_MODE_DISABLED) { | 209 if (zoom_mode_ != ZOOM_MODE_DISABLED) { |
| 211 zoom_map->SetTemporaryZoomLevel( | 210 zoom_map->SetTemporaryZoomLevel( |
| 212 render_process_id, render_view_id, original_zoom_level); | 211 render_process_id, render_view_id, original_zoom_level); |
| 213 } else { | 212 } else { |
| 214 // When we don't call any HostZoomMap set functions, we send the event | 213 // When we don't call any HostZoomMap set functions, we send the event |
| 215 // manually. | 214 // manually. |
| 216 FOR_EACH_OBSERVER( | 215 FOR_EACH_OBSERVER( |
| 217 ZoomObserver, observers_, OnZoomChanged(event_data_.back())); | 216 ZoomObserver, observers_, OnZoomChanged(*event_data_)); |
| 218 event_data_.pop_back(); | 217 event_data_.reset(); |
| 219 } | 218 } |
| 220 break; | 219 break; |
| 221 } | 220 } |
| 222 case ZOOM_MODE_MANUAL: { | 221 case ZOOM_MODE_MANUAL: { |
| 223 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the | 222 // 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, | 223 // page needs to be resized to the default zoom. While in manual mode, |
| 225 // the zoom level is handled independently. | 224 // the zoom level is handled independently. |
| 226 if (zoom_mode_ != ZOOM_MODE_DISABLED) { | 225 if (zoom_mode_ != ZOOM_MODE_DISABLED) { |
| 227 zoom_map->SetTemporaryZoomLevel( | 226 zoom_map->SetTemporaryZoomLevel( |
| 228 render_process_id, render_view_id, default_zoom_level_.GetValue()); | 227 render_process_id, render_view_id, default_zoom_level_.GetValue()); |
| 229 zoom_level_ = original_zoom_level; | 228 zoom_level_ = original_zoom_level; |
| 230 } else { | 229 } else { |
| 231 // When we don't call any HostZoomMap set functions, we send the event | 230 // When we don't call any HostZoomMap set functions, we send the event |
| 232 // manually. | 231 // manually. |
| 233 FOR_EACH_OBSERVER( | 232 FOR_EACH_OBSERVER( |
| 234 ZoomObserver, observers_, OnZoomChanged(event_data_.back())); | 233 ZoomObserver, observers_, OnZoomChanged(*event_data_)); |
| 235 event_data_.pop_back(); | 234 event_data_.reset(); |
| 236 } | 235 } |
| 237 break; | 236 break; |
| 238 } | 237 } |
| 239 case ZOOM_MODE_DISABLED: { | 238 case ZOOM_MODE_DISABLED: { |
| 240 // The page needs to be zoomed back to default before disabling the zoom | 239 // The page needs to be zoomed back to default before disabling the zoom |
| 241 zoom_map->SetTemporaryZoomLevel( | 240 zoom_map->SetTemporaryZoomLevel( |
| 242 render_process_id, render_view_id, default_zoom_level_.GetValue()); | 241 render_process_id, render_view_id, default_zoom_level_.GetValue()); |
| 243 break; | 242 break; |
| 244 } | 243 } |
| 245 } | 244 } |
| 246 // Any event data we've stored should have been consumed by this point. | 245 // Any event data we've stored should have been consumed by this point. |
| 247 DCHECK(event_data_.empty()); | 246 DCHECK(!event_data_); |
| 248 | 247 |
| 249 zoom_mode_ = new_mode; | 248 zoom_mode_ = new_mode; |
| 250 } | 249 } |
| 251 | 250 |
| 252 void ZoomController::DidNavigateMainFrame( | 251 void ZoomController::DidNavigateMainFrame( |
| 253 const content::LoadCommittedDetails& details, | 252 const content::LoadCommittedDetails& details, |
| 254 const content::FrameNavigateParams& params) { | 253 const content::FrameNavigateParams& params) { |
| 255 // If the main frame's content has changed, the new page may have a different | 254 // If the main frame's content has changed, the new page may have a different |
| 256 // zoom level from the old one. | 255 // zoom level from the old one. |
| 257 UpdateState(std::string()); | 256 UpdateState(std::string()); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 281 return; | 280 return; |
| 282 } | 281 } |
| 283 } | 282 } |
| 284 | 283 |
| 285 // The zoom bubble can be shown for all normal, per-origin zoom changes | 284 // The zoom bubble can be shown for all normal, per-origin zoom changes |
| 286 // (where the host will not be empty and the zoom is not temporary), or any | 285 // (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"). | 286 // special zoom changes (where the zoom mode will not be "default"). |
| 288 bool can_show_bubble = | 287 bool can_show_bubble = |
| 289 zoom_mode_ != ZOOM_MODE_DEFAULT || (!host.empty() && !is_temporary_zoom); | 288 zoom_mode_ != ZOOM_MODE_DEFAULT || (!host.empty() && !is_temporary_zoom); |
| 290 | 289 |
| 291 if (!event_data_.empty()) { | 290 if (event_data_) { |
| 292 // For state changes initiated within the ZoomController, information about | 291 // For state changes initiated within the ZoomController, information about |
| 293 // the change should be sent. | 292 // the change should be sent. |
| 294 ZoomChangedEventData zoom_change_data = event_data_.back(); | 293 ZoomChangedEventData zoom_change_data = *event_data_; |
| 295 event_data_.pop_back(); | 294 event_data_.reset(); |
| 296 zoom_change_data.can_show_bubble |= can_show_bubble; | 295 zoom_change_data.can_show_bubble |= can_show_bubble; |
| 297 FOR_EACH_OBSERVER( | 296 FOR_EACH_OBSERVER( |
| 298 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); | 297 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); |
| 299 } else { | 298 } else { |
| 300 // TODO(wjmaclean) Should we consider having HostZoomMap send both old and | 299 // TODO(wjmaclean) Should we consider having HostZoomMap send both old and |
| 301 // new zoom levels here? | 300 // new zoom levels here? |
| 302 double zoom_level = GetZoomLevel(); | 301 double zoom_level = GetZoomLevel(); |
| 303 ZoomChangedEventData zoom_change_data = ZoomChangedEventData( | 302 ZoomChangedEventData zoom_change_data( |
| 304 web_contents(), zoom_level, zoom_level, zoom_mode_, can_show_bubble); | 303 web_contents(), zoom_level, zoom_level, zoom_mode_, can_show_bubble); |
| 305 FOR_EACH_OBSERVER( | 304 FOR_EACH_OBSERVER( |
| 306 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); | 305 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); |
| 307 } | 306 } |
| 308 } | 307 } |
| OLD | NEW |