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 zoom_callback_(base::Bind(&ZoomController::OnZoomLevelChanged, | |
29 base::Unretained(this))) { | |
28 Profile* profile = | 30 Profile* profile = |
29 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 31 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
30 default_zoom_level_.Init(prefs::kDefaultZoomLevel, profile->GetPrefs(), | 32 default_zoom_level_.Init(prefs::kDefaultZoomLevel, profile->GetPrefs(), |
31 base::Bind(&ZoomController::UpdateState, | 33 base::Bind(&ZoomController::UpdateState, |
32 base::Unretained(this), | 34 base::Unretained(this), |
33 std::string())); | 35 std::string())); |
34 | 36 |
35 content::HostZoomMap* zoom_map = | 37 content::HostZoomMap::GetForBrowserContext( |
36 content::HostZoomMap::GetForBrowserContext(profile); | 38 web_contents->GetBrowserContext())->AddZoomLevelChangedCallback( |
37 registrar_.Add(this, content::NOTIFICATION_ZOOM_LEVEL_CHANGED, | 39 zoom_callback_); |
38 content::Source<content::HostZoomMap>(zoom_map)); | |
39 | 40 |
40 UpdateState(std::string()); | 41 UpdateState(std::string()); |
41 } | 42 } |
42 | 43 |
43 ZoomController::~ZoomController() {} | 44 ZoomController::~ZoomController() { |
45 if (!web_contents()) | |
46 return; | |
Avi (use Gerrit)
2013/01/31 18:46:55
This looks like a very, very bad idea.
Yes, as a
Paweł Hajdan Jr.
2013/02/01 12:36:11
Thanks for catching that!
| |
47 | |
48 content::HostZoomMap::GetForBrowserContext( | |
49 web_contents()->GetBrowserContext())->RemoveZoomLevelChangedCallback( | |
50 zoom_callback_); | |
51 } | |
44 | 52 |
45 bool ZoomController::IsAtDefaultZoom() const { | 53 bool ZoomController::IsAtDefaultZoom() const { |
46 if (!web_contents()) | 54 if (!web_contents()) |
47 return true; | 55 return true; |
48 | 56 |
49 return content::ZoomValuesEqual(web_contents()->GetZoomLevel(), | 57 return content::ZoomValuesEqual(web_contents()->GetZoomLevel(), |
50 default_zoom_level_.GetValue()); | 58 default_zoom_level_.GetValue()); |
51 } | 59 } |
52 | 60 |
53 int ZoomController::GetResourceForZoomLevel() const { | 61 int ZoomController::GetResourceForZoomLevel() const { |
54 if (!web_contents()) | 62 if (!web_contents()) |
55 return IDR_ZOOM_MINUS; | 63 return IDR_ZOOM_MINUS; |
56 | 64 |
57 DCHECK(!IsAtDefaultZoom()); | 65 DCHECK(!IsAtDefaultZoom()); |
58 double zoom = web_contents()->GetZoomLevel(); | 66 double zoom = web_contents()->GetZoomLevel(); |
59 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; | 67 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; |
60 } | 68 } |
61 | 69 |
62 void ZoomController::DidNavigateMainFrame( | 70 void ZoomController::DidNavigateMainFrame( |
63 const content::LoadCommittedDetails& details, | 71 const content::LoadCommittedDetails& details, |
64 const content::FrameNavigateParams& params) { | 72 const content::FrameNavigateParams& params) { |
65 // If the main frame's content has changed, the new page may have a different | 73 // If the main frame's content has changed, the new page may have a different |
66 // zoom level from the old one. | 74 // zoom level from the old one. |
67 UpdateState(std::string()); | 75 UpdateState(std::string()); |
68 } | 76 } |
69 | 77 |
70 void ZoomController::Observe(int type, | 78 void ZoomController::OnZoomLevelChanged(const std::string& host) { |
71 const content::NotificationSource& source, | 79 UpdateState(host); |
72 const content::NotificationDetails& details) { | |
73 DCHECK_EQ(content::NOTIFICATION_ZOOM_LEVEL_CHANGED, type); | |
74 UpdateState(*content::Details<std::string>(details).ptr()); | |
75 } | 80 } |
76 | 81 |
77 void ZoomController::UpdateState(const std::string& host) { | 82 void ZoomController::UpdateState(const std::string& host) { |
78 // TODO(dbeam): I'm not totally sure why this is happening, and there's been a | 83 // TODO(dbeam): I'm not totally sure why this is happening, and there's been a |
79 // bit of effort to understand with no tangible results yet. It's possible | 84 // bit of effort to understand with no tangible results yet. It's possible |
80 // that WebContents is NULL as it's being destroyed or some other random | 85 // that WebContents is NULL as it's being destroyed or some other random |
81 // reason that I haven't found yet. Applying band-aid. http://crbug.com/144879 | 86 // reason that I haven't found yet. Applying band-aid. http://crbug.com/144879 |
82 if (!web_contents()) | 87 if (!web_contents()) |
83 return; | 88 return; |
84 | 89 |
85 // If |host| is empty, all observers should be updated. | 90 // If |host| is empty, all observers should be updated. |
86 if (!host.empty()) { | 91 if (!host.empty()) { |
87 // Use the active navigation entry's URL instead of the WebContents' so | 92 // Use the active navigation entry's URL instead of the WebContents' so |
88 // virtual URLs work (e.g. chrome://settings). http://crbug.com/153950 | 93 // virtual URLs work (e.g. chrome://settings). http://crbug.com/153950 |
89 content::NavigationEntry* active_entry = | 94 content::NavigationEntry* active_entry = |
90 web_contents()->GetController().GetActiveEntry(); | 95 web_contents()->GetController().GetActiveEntry(); |
91 if (!active_entry || | 96 if (!active_entry || |
92 host != net::GetHostOrSpecFromURL(active_entry->GetURL())) { | 97 host != net::GetHostOrSpecFromURL(active_entry->GetURL())) { |
93 return; | 98 return; |
94 } | 99 } |
95 } | 100 } |
96 | 101 |
97 bool dummy; | 102 bool dummy; |
98 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); | 103 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); |
99 | 104 |
100 if (observer_) | 105 if (observer_) |
101 observer_->OnZoomChanged(web_contents(), !host.empty()); | 106 observer_->OnZoomChanged(web_contents(), !host.empty()); |
102 } | 107 } |
OLD | NEW |