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 // TODO(wjmaclean) Make calls to HostZoomMap::GetDefaultForBrowserContext() |
35 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 28 // refer to the webcontents-specific HostZoomMap when that becomes available. |
36 default_zoom_level_.Init( | 29 content::HostZoomMap* host_zoom_map = |
37 prefs::kDefaultZoomLevel, | 30 content::HostZoomMap::GetDefaultForBrowserContext(browser_context_); |
38 profile->GetPrefs(), | 31 zoom_level_ = host_zoom_map->GetDefaultZoomLevel(); |
39 base::Bind( | |
40 &ZoomController::UpdateState, base::Unretained(this), std::string())); | |
41 zoom_level_ = default_zoom_level_.GetValue(); | |
42 | 32 |
43 zoom_subscription_ = content::HostZoomMap::GetDefaultForBrowserContext( | 33 zoom_subscription_ = host_zoom_map->AddZoomLevelChangedCallback( |
44 browser_context_)->AddZoomLevelChangedCallback( | 34 base::Bind(&ZoomController::OnZoomLevelChanged, base::Unretained(this))); |
45 base::Bind(&ZoomController::OnZoomLevelChanged, | |
46 base::Unretained(this))); | |
47 | 35 |
48 UpdateState(std::string()); | 36 UpdateState(std::string()); |
49 } | 37 } |
50 | 38 |
51 ZoomController::~ZoomController() {} | 39 ZoomController::~ZoomController() {} |
52 | 40 |
53 bool ZoomController::IsAtDefaultZoom() const { | 41 bool ZoomController::IsAtDefaultZoom() const { |
54 return content::ZoomValuesEqual(GetZoomLevel(), | 42 return content::ZoomValuesEqual(GetZoomLevel(), GetDefaultZoomLevel()); |
55 default_zoom_level_.GetValue()); | |
56 } | 43 } |
57 | 44 |
58 int ZoomController::GetResourceForZoomLevel() const { | 45 int ZoomController::GetResourceForZoomLevel() const { |
59 if (IsAtDefaultZoom()) | 46 if (IsAtDefaultZoom()) |
60 return IDR_ZOOM_NORMAL; | 47 return IDR_ZOOM_NORMAL; |
61 return GetZoomLevel() > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS | 48 return GetZoomLevel() > GetDefaultZoomLevel() ? IDR_ZOOM_PLUS |
62 : IDR_ZOOM_MINUS; | 49 : IDR_ZOOM_MINUS; |
63 } | 50 } |
64 | 51 |
65 void ZoomController::AddObserver(ZoomObserver* observer) { | 52 void ZoomController::AddObserver(ZoomObserver* observer) { |
66 observers_.AddObserver(observer); | 53 observers_.AddObserver(observer); |
67 } | 54 } |
68 | 55 |
69 void ZoomController::RemoveObserver(ZoomObserver* observer) { | 56 void ZoomController::RemoveObserver(ZoomObserver* observer) { |
70 observers_.RemoveObserver(observer); | 57 observers_.RemoveObserver(observer); |
71 } | 58 } |
72 | 59 |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 event_data_.reset(); | 207 event_data_.reset(); |
221 } | 208 } |
222 break; | 209 break; |
223 } | 210 } |
224 case ZOOM_MODE_MANUAL: { | 211 case ZOOM_MODE_MANUAL: { |
225 // Unless the zoom mode was |ZOOM_MODE_DISABLED| before this call, the | 212 // 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, | 213 // page needs to be resized to the default zoom. While in manual mode, |
227 // the zoom level is handled independently. | 214 // the zoom level is handled independently. |
228 if (zoom_mode_ != ZOOM_MODE_DISABLED) { | 215 if (zoom_mode_ != ZOOM_MODE_DISABLED) { |
229 zoom_map->SetTemporaryZoomLevel( | 216 zoom_map->SetTemporaryZoomLevel( |
230 render_process_id, render_view_id, default_zoom_level_.GetValue()); | 217 render_process_id, render_view_id, GetDefaultZoomLevel()); |
231 zoom_level_ = original_zoom_level; | 218 zoom_level_ = original_zoom_level; |
232 } else { | 219 } else { |
233 // When we don't call any HostZoomMap set functions, we send the event | 220 // When we don't call any HostZoomMap set functions, we send the event |
234 // manually. | 221 // manually. |
235 FOR_EACH_OBSERVER( | 222 FOR_EACH_OBSERVER( |
236 ZoomObserver, observers_, OnZoomChanged(*event_data_)); | 223 ZoomObserver, observers_, OnZoomChanged(*event_data_)); |
237 event_data_.reset(); | 224 event_data_.reset(); |
238 } | 225 } |
239 break; | 226 break; |
240 } | 227 } |
241 case ZOOM_MODE_DISABLED: { | 228 case ZOOM_MODE_DISABLED: { |
242 // The page needs to be zoomed back to default before disabling the zoom | 229 // The page needs to be zoomed back to default before disabling the zoom |
243 zoom_map->SetTemporaryZoomLevel( | 230 zoom_map->SetTemporaryZoomLevel( |
244 render_process_id, render_view_id, default_zoom_level_.GetValue()); | 231 render_process_id, render_view_id, GetDefaultZoomLevel()); |
245 break; | 232 break; |
246 } | 233 } |
247 } | 234 } |
248 // Any event data we've stored should have been consumed by this point. | 235 // Any event data we've stored should have been consumed by this point. |
249 DCHECK(!event_data_); | 236 DCHECK(!event_data_); |
250 | 237 |
251 zoom_mode_ = new_mode; | 238 zoom_mode_ = new_mode; |
252 } | 239 } |
253 | 240 |
254 void ZoomController::DidNavigateMainFrame( | 241 void ZoomController::DidNavigateMainFrame( |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 } else { | 285 } else { |
299 // TODO(wjmaclean) Should we consider having HostZoomMap send both old and | 286 // TODO(wjmaclean) Should we consider having HostZoomMap send both old and |
300 // new zoom levels here? | 287 // new zoom levels here? |
301 double zoom_level = GetZoomLevel(); | 288 double zoom_level = GetZoomLevel(); |
302 ZoomChangedEventData zoom_change_data( | 289 ZoomChangedEventData zoom_change_data( |
303 web_contents(), zoom_level, zoom_level, zoom_mode_, can_show_bubble); | 290 web_contents(), zoom_level, zoom_level, zoom_mode_, can_show_bubble); |
304 FOR_EACH_OBSERVER( | 291 FOR_EACH_OBSERVER( |
305 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); | 292 ZoomObserver, observers_, OnZoomChanged(zoom_change_data)); |
306 } | 293 } |
307 } | 294 } |
OLD | NEW |