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/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
8 #include "chrome/browser/ui/browser_finder.h" | 8 #include "chrome/browser/ui/browser_finder.h" |
9 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 9 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
10 #include "chrome/common/chrome_notification_types.h" | 10 #include "chrome/common/chrome_notification_types.h" |
11 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
12 #include "content/public/browser/notification_types.h" | 12 #include "content/public/browser/notification_types.h" |
13 #include "content/public/browser/notification_details.h" | 13 #include "content/public/browser/notification_details.h" |
14 #include "content/public/browser/notification_service.h" | 14 #include "content/public/browser/notification_service.h" |
15 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
16 #include "content/public/common/page_zoom.h" | 16 #include "content/public/common/page_zoom.h" |
17 #include "grit/theme_resources.h" | |
18 | 17 |
19 ZoomController::ZoomController(TabContents* tab_contents) | 18 ZoomController::ZoomController(TabContents* tab_contents) |
20 : content::WebContentsObserver(tab_contents->web_contents()), | 19 : content::WebContentsObserver(tab_contents->web_contents()), |
| 20 zoom_icon_state_(NONE), |
21 zoom_percent_(100), | 21 zoom_percent_(100), |
22 tab_contents_(tab_contents), | 22 tab_contents_(tab_contents), |
23 observer_(NULL) { | 23 observer_(NULL) { |
24 default_zoom_level_.Init(prefs::kDefaultZoomLevel, | 24 default_zoom_level_.Init(prefs::kDefaultZoomLevel, |
25 tab_contents->profile()->GetPrefs(), this); | 25 tab_contents->profile()->GetPrefs(), this); |
26 registrar_.Add(this, content::NOTIFICATION_ZOOM_LEVEL_CHANGED, | 26 registrar_.Add(this, content::NOTIFICATION_ZOOM_LEVEL_CHANGED, |
27 content::NotificationService::AllBrowserContextsAndSources()); | 27 content::NotificationService::AllBrowserContextsAndSources()); |
28 | 28 |
29 UpdateState(false); | 29 UpdateState(false); |
30 } | 30 } |
31 | 31 |
32 ZoomController::~ZoomController() { | 32 ZoomController::~ZoomController() { |
33 default_zoom_level_.Destroy(); | 33 default_zoom_level_.Destroy(); |
34 registrar_.RemoveAll(); | 34 registrar_.RemoveAll(); |
35 } | 35 } |
36 | 36 |
37 bool ZoomController::IsAtDefaultZoom() const { | |
38 return content::ZoomValuesEqual(tab_contents_->web_contents()->GetZoomLevel(), | |
39 default_zoom_level_.GetValue()); | |
40 } | |
41 | |
42 int ZoomController::GetResourceForZoomLevel() const { | |
43 DCHECK(!IsAtDefaultZoom()); | |
44 double zoom = tab_contents_->web_contents()->GetZoomLevel(); | |
45 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; | |
46 } | |
47 | |
48 void ZoomController::DidNavigateMainFrame( | 37 void ZoomController::DidNavigateMainFrame( |
49 const content::LoadCommittedDetails& details, | 38 const content::LoadCommittedDetails& details, |
50 const content::FrameNavigateParams& params) { | 39 const content::FrameNavigateParams& params) { |
51 // If the main frame's content has changed, the new page may have a different | 40 // If the main frame's content has changed, the new page may have a different |
52 // zoom level from the old one. | 41 // zoom level from the old one. |
53 UpdateState(false); | 42 UpdateState(false); |
54 } | 43 } |
55 | 44 |
56 void ZoomController::Observe(int type, | 45 void ZoomController::Observe(int type, |
57 const content::NotificationSource& source, | 46 const content::NotificationSource& source, |
58 const content::NotificationDetails& details) { | 47 const content::NotificationDetails& details) { |
59 switch (type) { | 48 switch (type) { |
60 case chrome::NOTIFICATION_PREF_CHANGED: { | 49 case chrome::NOTIFICATION_PREF_CHANGED: { |
61 std::string* pref_name = content::Details<std::string>(details).ptr(); | 50 std::string* pref_name = content::Details<std::string>(details).ptr(); |
62 DCHECK(pref_name && *pref_name == prefs::kDefaultZoomLevel); | 51 DCHECK(pref_name && *pref_name == prefs::kDefaultZoomLevel); |
63 UpdateState(false); | 52 UpdateState(false); |
64 break; | 53 break; |
65 } | 54 } |
66 case content::NOTIFICATION_ZOOM_LEVEL_CHANGED: | 55 case content::NOTIFICATION_ZOOM_LEVEL_CHANGED: |
67 UpdateState(!content::Details<std::string>(details)->empty()); | 56 UpdateState(!content::Details<std::string>(details)->empty()); |
68 break; | 57 break; |
69 default: | 58 default: |
70 NOTREACHED(); | 59 NOTREACHED(); |
71 } | 60 } |
72 } | 61 } |
73 | 62 |
74 void ZoomController::UpdateState(bool can_show_bubble) { | 63 void ZoomController::UpdateState(bool can_show_bubble) { |
| 64 double current_zoom_level = tab_contents_->web_contents()->GetZoomLevel(); |
| 65 double default_zoom_level = default_zoom_level_.GetValue(); |
| 66 |
| 67 ZoomIconState state; |
| 68 if (content::ZoomValuesEqual(current_zoom_level, default_zoom_level)) |
| 69 state = NONE; |
| 70 else if (current_zoom_level > default_zoom_level) |
| 71 state = ZOOM_PLUS_ICON; |
| 72 else |
| 73 state = ZOOM_MINUS_ICON; |
| 74 |
75 bool dummy; | 75 bool dummy; |
76 zoom_percent_ = tab_contents_->web_contents()->GetZoomPercent(&dummy, &dummy); | 76 int zoom_percent = tab_contents_->web_contents()-> |
| 77 GetZoomPercent(&dummy, &dummy); |
77 | 78 |
78 if (observer_) | 79 if (state != zoom_icon_state_) { |
79 observer_->OnZoomChanged(tab_contents_, can_show_bubble); | 80 zoom_icon_state_ = state; |
| 81 if (observer_) |
| 82 observer_->OnZoomIconChanged(tab_contents_, state); |
| 83 } |
| 84 |
| 85 if (zoom_percent != zoom_percent_) { |
| 86 zoom_percent_ = zoom_percent; |
| 87 if (observer_) |
| 88 observer_->OnZoomChanged(tab_contents_, |
| 89 zoom_percent, |
| 90 can_show_bubble && state != NONE); |
| 91 } |
80 } | 92 } |
OLD | NEW |