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/prefs/pref_service.h" | 7 #include "chrome/browser/prefs/pref_service.h" |
8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/ui/browser_finder.h" | 9 #include "chrome/browser/ui/browser_finder.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/host_zoom_map.h" | 12 #include "content/public/browser/host_zoom_map.h" |
13 #include "content/public/browser/navigation_entry.h" | 13 #include "content/public/browser/navigation_entry.h" |
14 #include "content/public/browser/notification_types.h" | 14 #include "content/public/browser/notification_types.h" |
15 #include "content/public/browser/notification_details.h" | 15 #include "content/public/browser/notification_details.h" |
16 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
18 #include "content/public/common/page_zoom.h" | 18 #include "content/public/common/page_zoom.h" |
19 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
20 #include "net/base/net_util.h" | 20 #include "net/base/net_util.h" |
21 | 21 |
22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); | 22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); |
23 | 23 |
24 ZoomController::ZoomController(content::WebContents* web_contents) | 24 ZoomController::ZoomController(content::WebContents* web_contents) |
25 : content::WebContentsObserver(web_contents), | 25 : content::WebContentsObserver(web_contents), |
26 zoom_percent_(100), | 26 zoom_percent_(100), |
27 observer_(NULL) { | 27 observer_(NULL), |
| 28 browser_context_(web_contents->GetBrowserContext()), |
| 29 zoom_callback_(base::Bind(&ZoomController::OnZoomLevelChanged, |
| 30 base::Unretained(this))) { |
28 Profile* profile = | 31 Profile* profile = |
29 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 32 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
30 default_zoom_level_.Init(prefs::kDefaultZoomLevel, profile->GetPrefs(), | 33 default_zoom_level_.Init(prefs::kDefaultZoomLevel, profile->GetPrefs(), |
31 base::Bind(&ZoomController::UpdateState, | 34 base::Bind(&ZoomController::UpdateState, |
32 base::Unretained(this), | 35 base::Unretained(this), |
33 std::string())); | 36 std::string())); |
34 | 37 |
35 content::HostZoomMap* zoom_map = | 38 content::HostZoomMap::GetForBrowserContext( |
36 content::HostZoomMap::GetForBrowserContext(profile); | 39 browser_context_)->AddZoomLevelChangedCallback( |
37 registrar_.Add(this, content::NOTIFICATION_ZOOM_LEVEL_CHANGED, | 40 zoom_callback_); |
38 content::Source<content::HostZoomMap>(zoom_map)); | |
39 | 41 |
40 UpdateState(std::string()); | 42 UpdateState(std::string()); |
41 } | 43 } |
42 | 44 |
43 ZoomController::~ZoomController() {} | 45 ZoomController::~ZoomController() { |
| 46 content::HostZoomMap::GetForBrowserContext( |
| 47 browser_context_)->RemoveZoomLevelChangedCallback( |
| 48 zoom_callback_); |
| 49 } |
44 | 50 |
45 bool ZoomController::IsAtDefaultZoom() const { | 51 bool ZoomController::IsAtDefaultZoom() const { |
46 return content::ZoomValuesEqual(web_contents()->GetZoomLevel(), | 52 return content::ZoomValuesEqual(web_contents()->GetZoomLevel(), |
47 default_zoom_level_.GetValue()); | 53 default_zoom_level_.GetValue()); |
48 } | 54 } |
49 | 55 |
50 int ZoomController::GetResourceForZoomLevel() const { | 56 int ZoomController::GetResourceForZoomLevel() const { |
51 DCHECK(!IsAtDefaultZoom()); | 57 DCHECK(!IsAtDefaultZoom()); |
52 double zoom = web_contents()->GetZoomLevel(); | 58 double zoom = web_contents()->GetZoomLevel(); |
53 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; | 59 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; |
54 } | 60 } |
55 | 61 |
56 void ZoomController::DidNavigateMainFrame( | 62 void ZoomController::DidNavigateMainFrame( |
57 const content::LoadCommittedDetails& details, | 63 const content::LoadCommittedDetails& details, |
58 const content::FrameNavigateParams& params) { | 64 const content::FrameNavigateParams& params) { |
59 // If the main frame's content has changed, the new page may have a different | 65 // If the main frame's content has changed, the new page may have a different |
60 // zoom level from the old one. | 66 // zoom level from the old one. |
61 UpdateState(std::string()); | 67 UpdateState(std::string()); |
62 } | 68 } |
63 | 69 |
64 void ZoomController::Observe(int type, | 70 void ZoomController::OnZoomLevelChanged(const std::string& host) { |
65 const content::NotificationSource& source, | 71 UpdateState(host); |
66 const content::NotificationDetails& details) { | |
67 DCHECK_EQ(content::NOTIFICATION_ZOOM_LEVEL_CHANGED, type); | |
68 UpdateState(*content::Details<std::string>(details).ptr()); | |
69 } | 72 } |
70 | 73 |
71 void ZoomController::UpdateState(const std::string& host) { | 74 void ZoomController::UpdateState(const std::string& host) { |
72 // If |host| is empty, all observers should be updated. | 75 // If |host| is empty, all observers should be updated. |
73 if (!host.empty()) { | 76 if (!host.empty()) { |
74 // Use the active navigation entry's URL instead of the WebContents' so | 77 // Use the active navigation entry's URL instead of the WebContents' so |
75 // virtual URLs work (e.g. chrome://settings). http://crbug.com/153950 | 78 // virtual URLs work (e.g. chrome://settings). http://crbug.com/153950 |
76 content::NavigationEntry* active_entry = | 79 content::NavigationEntry* active_entry = |
77 web_contents()->GetController().GetActiveEntry(); | 80 web_contents()->GetController().GetActiveEntry(); |
78 if (!active_entry || | 81 if (!active_entry || |
79 host != net::GetHostOrSpecFromURL(active_entry->GetURL())) { | 82 host != net::GetHostOrSpecFromURL(active_entry->GetURL())) { |
80 return; | 83 return; |
81 } | 84 } |
82 } | 85 } |
83 | 86 |
84 bool dummy; | 87 bool dummy; |
85 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); | 88 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); |
86 | 89 |
87 if (observer_) | 90 if (observer_) |
88 observer_->OnZoomChanged(web_contents(), !host.empty()); | 91 observer_->OnZoomChanged(web_contents(), !host.empty()); |
89 } | 92 } |
OLD | NEW |