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