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" | 7 #include "base/prefs/pref_service.h" |
8 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/ui/browser_finder.h" | 10 #include "chrome/browser/ui/browser_finder.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_details.h" | 14 #include "content/public/browser/notification_details.h" |
15 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
16 #include "content/public/browser/notification_types.h" | 16 #include "content/public/browser/notification_types.h" |
| 17 #include "content/public/browser/render_process_host.h" |
| 18 #include "content/public/browser/render_view_host.h" |
17 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
18 #include "content/public/common/page_zoom.h" | 20 #include "content/public/common/page_zoom.h" |
19 #include "grit/theme_resources.h" | 21 #include "grit/theme_resources.h" |
20 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
21 | 23 |
22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); | 24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ZoomController); |
23 | 25 |
24 ZoomController::ZoomController(content::WebContents* web_contents) | 26 ZoomController::ZoomController(content::WebContents* web_contents) |
25 : content::WebContentsObserver(web_contents), | 27 : content::WebContentsObserver(web_contents), |
26 zoom_percent_(100), | 28 zoom_percent_(100), |
27 observer_(NULL), | 29 observer_(NULL), |
28 browser_context_(web_contents->GetBrowserContext()) { | 30 browser_context_(web_contents->GetBrowserContext()) { |
29 Profile* profile = | 31 Profile* profile = |
30 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 32 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
31 default_zoom_level_.Init(prefs::kDefaultZoomLevel, profile->GetPrefs(), | 33 default_zoom_level_.Init( |
32 base::Bind(&ZoomController::UpdateState, | 34 prefs::kDefaultZoomLevel, profile->GetPrefs(), |
33 base::Unretained(this), | 35 base::Bind(static_cast<void(ZoomController::*)( |
34 std::string())); | 36 const std::string&)>(&ZoomController::UpdateState), |
| 37 base::Unretained(this), |
| 38 std::string())); |
35 | 39 |
36 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext( | 40 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext( |
37 browser_context_)->AddZoomLevelChangedCallback( | 41 browser_context_)->AddZoomLevelChangedCallback( |
38 base::Bind(&ZoomController::OnZoomLevelChanged, | 42 base::Bind(&ZoomController::OnZoomLevelChanged, |
39 base::Unretained(this))); | 43 base::Unretained(this))); |
40 | 44 |
41 UpdateState(std::string()); | 45 UpdateState(std::string()); |
42 } | 46 } |
43 | 47 |
44 ZoomController::~ZoomController() {} | 48 ZoomController::~ZoomController() {} |
45 | 49 |
46 bool ZoomController::IsAtDefaultZoom() const { | 50 bool ZoomController::IsAtDefaultZoom() const { |
47 return content::ZoomValuesEqual(web_contents()->GetZoomLevel(), | 51 return content::ZoomValuesEqual(web_contents()->GetZoomLevel(), |
48 default_zoom_level_.GetValue()); | 52 default_zoom_level_.GetValue()); |
49 } | 53 } |
50 | 54 |
51 int ZoomController::GetResourceForZoomLevel() const { | 55 int ZoomController::GetResourceForZoomLevel() const { |
52 if (IsAtDefaultZoom()) | 56 if (IsAtDefaultZoom()) |
53 return IDR_ZOOM_NORMAL; | 57 return IDR_ZOOM_NORMAL; |
54 double zoom = web_contents()->GetZoomLevel(); | 58 double zoom = web_contents()->GetZoomLevel(); |
55 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; |
56 } | 60 } |
57 | 61 |
| 62 const extensions::Extension* ZoomController::last_extension() const { |
| 63 return last_extension_.get(); |
| 64 } |
| 65 |
| 66 bool ZoomController::SetZoomLevel(double zoom_level) { |
| 67 // Cannot zoom in disabled mode. |
| 68 if (zoom_mode_ == kZoomModeDisabled) |
| 69 return false; |
| 70 |
| 71 // An extension did not initiate this zoom change. |
| 72 extensions_.push(NULL); |
| 73 |
| 74 // Do not actually rescale the page in manual mode. |
| 75 if (zoom_mode_ == kZoomModeManual) { |
| 76 content::HostZoomMap* zoom_map = content::HostZoomMap::GetForBrowserContext( |
| 77 web_contents()->GetBrowserContext()); |
| 78 DCHECK(zoom_map); |
| 79 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); |
| 80 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); |
| 81 std::string host = net::GetHostOrSpecFromURL( |
| 82 web_contents()->GetController().GetLastCommittedEntry()->GetURL()); |
| 83 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, |
| 84 host, zoom_level); |
| 85 } else { |
| 86 web_contents()->SetZoomLevel(zoom_level); |
| 87 } |
| 88 observer_->OnZoomChangeInitiated(web_contents(), |
| 89 web_contents()->GetZoomLevel(), |
| 90 zoom_level, |
| 91 zoom_mode_); |
| 92 |
| 93 return true; |
| 94 } |
| 95 |
| 96 bool ZoomController::SetZoomLevelByExtension( |
| 97 double zoom_level, |
| 98 scoped_refptr<const extensions::Extension> extension) { |
| 99 if (!SetZoomLevel(zoom_level)) |
| 100 return false; |
| 101 |
| 102 // Store extension data so that |extension| can be attributed when the zoom |
| 103 // change completes.. |
| 104 extensions_.front() = extension.get(); |
| 105 return true; |
| 106 } |
| 107 |
| 108 void ZoomController::SetZoomMode(ZoomMode new_mode) { |
| 109 if (new_mode == zoom_mode_) |
| 110 return; |
| 111 |
| 112 content::HostZoomMap* zoom_map = content::HostZoomMap::GetForBrowserContext( |
| 113 web_contents()->GetBrowserContext()); |
| 114 DCHECK(zoom_map); |
| 115 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); |
| 116 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); |
| 117 std::string host = net::GetHostOrSpecFromURL( |
| 118 web_contents()->GetController().GetLastCommittedEntry()->GetURL()); |
| 119 double original_zoom_level = web_contents()->GetZoomLevel(); |
| 120 |
| 121 switch (new_mode) { |
| 122 case kZoomModeDefault: { |
| 123 zoom_map->SetZoomLevelForHost(host, original_zoom_level); |
| 124 // Remove per-tab zoom data for this tab. |
| 125 zoom_map->EraseTemporaryZoomLevel(render_process_id, render_view_id); |
| 126 break; |
| 127 } |
| 128 case kZoomModeIsolated: { |
| 129 // Unless the zoom mode was |kZoomModeDisabled| before this call, the page |
| 130 // needs an initial isolated zoom back to the same level it was at in the |
| 131 // other mode. |
| 132 if (zoom_mode_ != kZoomModeDisabled) { |
| 133 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, |
| 134 host, original_zoom_level); |
| 135 } |
| 136 break; |
| 137 } |
| 138 case kZoomModeManual: { |
| 139 // Unless the zoom mode was |kZoomModeDisabled| before this call, the page |
| 140 // needs to be resized to the default zoom before calling SetZoomLevel() |
| 141 // so that the page can be resized manually to the same zoom as before. |
| 142 if (zoom_mode_ != kZoomModeDisabled) { |
| 143 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, |
| 144 host, 0); |
| 145 zoom_mode_ = new_mode; |
| 146 SetZoomLevel(original_zoom_level); |
| 147 } |
| 148 break; |
| 149 } |
| 150 case kZoomModeDisabled: { |
| 151 // The page needs to be zoomed back to default before disabling the zoom |
| 152 // (unless it was just in |kZoomModeManual| mode, in which case this is |
| 153 // already done). |
| 154 if (zoom_mode_ != kZoomModeManual) |
| 155 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, |
| 156 host, 0); |
| 157 break; |
| 158 } |
| 159 } |
| 160 |
| 161 zoom_mode_ = new_mode; |
| 162 web_contents()->SetTemporaryZoomSettings(new_mode != kZoomModeDefault); |
| 163 } |
| 164 |
58 void ZoomController::DidNavigateMainFrame( | 165 void ZoomController::DidNavigateMainFrame( |
59 const content::LoadCommittedDetails& details, | 166 const content::LoadCommittedDetails& details, |
60 const content::FrameNavigateParams& params) { | 167 const content::FrameNavigateParams& params) { |
61 // If the main frame's content has changed, the new page may have a different | 168 // If the main frame's content has changed, the new page may have a different |
62 // zoom level from the old one. | 169 // zoom level from the old one. |
63 UpdateState(std::string()); | 170 UpdateState(std::string()); |
64 } | 171 } |
65 | 172 |
66 void ZoomController::OnZoomLevelChanged( | 173 void ZoomController::OnZoomLevelChanged( |
67 const content::HostZoomMap::ZoomLevelChange& change) { | 174 const content::HostZoomMap::ZoomLevelChange& change) { |
68 UpdateState(change.host); | 175 UpdateState(change.host, |
| 176 change.mode == content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM); |
69 } | 177 } |
70 | 178 |
71 void ZoomController::UpdateState(const std::string& host) { | 179 void ZoomController::UpdateState(const std::string& host) { |
| 180 UpdateState(host, false); |
| 181 } |
| 182 |
| 183 void ZoomController::UpdateState(const std::string& host, |
| 184 bool is_temporary_zoom) { |
| 185 // Update |last_extension_|. |
| 186 DCHECK(!extensions_.empty()); |
| 187 last_extension_ = extensions_.front().get(); |
| 188 extensions_.pop(); |
| 189 |
72 // If |host| is empty, all observers should be updated. | 190 // If |host| is empty, all observers should be updated. |
73 if (!host.empty()) { | 191 if (!host.empty()) { |
74 // Use the navigation entry's URL instead of the WebContents' so virtual | 192 // Use the navigation entry's URL instead of the WebContents' so virtual |
75 // URLs work (e.g. chrome://settings). http://crbug.com/153950 | 193 // URLs work (e.g. chrome://settings). http://crbug.com/153950 |
76 content::NavigationEntry* entry = | 194 content::NavigationEntry* entry = |
77 web_contents()->GetController().GetLastCommittedEntry(); | 195 web_contents()->GetController().GetLastCommittedEntry(); |
78 if (!entry || | 196 if (!entry || |
79 host != net::GetHostOrSpecFromURL(entry->GetURL())) { | 197 host != net::GetHostOrSpecFromURL(entry->GetURL())) { |
80 return; | 198 return; |
81 } | 199 } |
82 } | 200 } |
83 | |
84 bool dummy; | 201 bool dummy; |
85 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); | 202 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); |
86 | 203 |
87 if (observer_) | 204 if (observer_) { |
88 observer_->OnZoomChanged(web_contents(), !host.empty()); | 205 // The zoom bubble can be shown for all normal, per-origin zoom changes |
| 206 // (where the host will not be empty and the zoom is not temporary), or any |
| 207 // special zoom changes (where the zoom mode will not be "default"). |
| 208 bool can_show_bubble = |
| 209 zoom_mode_ != kZoomModeDefault || |
| 210 (!host.empty() && !is_temporary_zoom); |
| 211 observer_->OnZoomChanged(web_contents(), can_show_bubble); |
| 212 } |
| 213 |
| 214 last_extension_ = NULL; |
89 } | 215 } |
OLD | NEW |