Chromium Code Reviews| 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" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 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 Profile* profile = | 28 Profile* profile = |
| 29 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 29 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 30 default_zoom_level_.Init(prefs::kDefaultZoomLevel, profile->GetPrefs(), | 30 default_zoom_level_.Init(prefs::kDefaultZoomLevel, profile->GetPrefs(), |
| 31 base::Bind(&ZoomController::UpdateState, | 31 base::Bind(&ZoomController::UpdateState, |
| 32 base::Unretained(this), | 32 base::Unretained(this), |
| 33 std::string())); | 33 std::string())); |
| 34 | 34 |
| 35 content::HostZoomMap* zoom_map = | 35 content::HostZoomMap::GetForBrowserContext( |
| 36 content::HostZoomMap::GetForBrowserContext(profile); | 36 web_contents->GetBrowserContext())->AddZoomLevelChangedCallback( |
| 37 registrar_.Add(this, content::NOTIFICATION_ZOOM_LEVEL_CHANGED, | 37 base::Bind(&ZoomController::OnZoomLevelChanged, |
| 38 content::Source<content::HostZoomMap>(zoom_map)); | 38 base::Unretained(this))); |
| 39 | 39 |
| 40 UpdateState(std::string()); | 40 UpdateState(std::string()); |
| 41 } | 41 } |
| 42 | 42 |
| 43 ZoomController::~ZoomController() {} | 43 ZoomController::~ZoomController() { |
| 44 if (!web_contents()) | |
| 45 return; | |
| 46 | |
| 47 content::HostZoomMap::GetForBrowserContext( | |
| 48 web_contents()->GetBrowserContext())->RemoveZoomLevelChangedCallback( | |
| 49 base::Bind(&ZoomController::OnZoomLevelChanged, | |
| 50 base::Unretained(this))); | |
|
jam
2013/01/30 17:50:44
this doesn't work because the new callback you cre
| |
| 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 |
| 70 void ZoomController::OnZoomLevelChanged(const std::string& host) { | |
| 71 UpdateState(host); | |
| 72 } | |
| 73 | |
| 62 void ZoomController::DidNavigateMainFrame( | 74 void ZoomController::DidNavigateMainFrame( |
| 63 const content::LoadCommittedDetails& details, | 75 const content::LoadCommittedDetails& details, |
| 64 const content::FrameNavigateParams& params) { | 76 const content::FrameNavigateParams& params) { |
| 65 // If the main frame's content has changed, the new page may have a different | 77 // If the main frame's content has changed, the new page may have a different |
| 66 // zoom level from the old one. | 78 // zoom level from the old one. |
| 67 UpdateState(std::string()); | 79 UpdateState(std::string()); |
| 68 } | 80 } |
| 69 | 81 |
| 70 void ZoomController::Observe(int type, | |
| 71 const content::NotificationSource& source, | |
| 72 const content::NotificationDetails& details) { | |
| 73 DCHECK_EQ(content::NOTIFICATION_ZOOM_LEVEL_CHANGED, type); | |
| 74 UpdateState(*content::Details<std::string>(details).ptr()); | |
| 75 } | |
| 76 | |
| 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 |