Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: chrome/browser/ui/zoom/zoom_controller.cc

Issue 301733006: Zoom Extension API (chrome) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update to match changes in content/ CL. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_mode_(kZoomModeDefault),
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),
Fady Samuel 2014/06/12 19:38:58 Funky cast. Is this necessary?
wjmaclean 2014/06/12 19:45:25 Not sure ... I inherited this chuck of code and ha
37 base::Unretained(this),
38 std::string()));
39 zoom_level_ = default_zoom_level_.GetValue();
35 40
36 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext( 41 zoom_subscription_ = content::HostZoomMap::GetForBrowserContext(
37 browser_context_)->AddZoomLevelChangedCallback( 42 browser_context_)->AddZoomLevelChangedCallback(
38 base::Bind(&ZoomController::OnZoomLevelChanged, 43 base::Bind(&ZoomController::OnZoomLevelChanged,
39 base::Unretained(this))); 44 base::Unretained(this)));
40 45
41 UpdateState(std::string()); 46 UpdateState(std::string());
42 } 47 }
43 48
44 ZoomController::~ZoomController() {} 49 ZoomController::~ZoomController() {}
45 50
46 bool ZoomController::IsAtDefaultZoom() const { 51 bool ZoomController::IsAtDefaultZoom() const {
47 return content::ZoomValuesEqual( 52 return content::ZoomValuesEqual(GetZoomLevel(),
48 content::HostZoomMap::GetZoomLevel(web_contents()), 53 default_zoom_level_.GetValue());
49 default_zoom_level_.GetValue());
50 } 54 }
51 55
52 int ZoomController::GetResourceForZoomLevel() const { 56 int ZoomController::GetResourceForZoomLevel() const {
53 if (IsAtDefaultZoom()) 57 if (IsAtDefaultZoom())
54 return IDR_ZOOM_NORMAL; 58 return IDR_ZOOM_NORMAL;
55 double zoom = content::HostZoomMap::GetZoomLevel(web_contents()); 59 double zoom = GetZoomLevel();
56 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS; 60 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS;
57 } 61 }
58 62
63 const extensions::Extension* ZoomController::last_extension() const {
64 return last_extension_.get();
65 }
66
67 double ZoomController::GetZoomLevel() const {
68 if (zoom_mode_ == kZoomModeManual)
69 return zoom_level_;
70 else
71 return content::HostZoomMap::GetZoomLevel(web_contents());
72 }
73
74 int ZoomController::GetZoomPercent() const {
75 double zoom_factor = content::ZoomLevelToZoomFactor(GetZoomLevel());
76 return static_cast<int>(zoom_factor * 100 + 0.5);
77 }
78
79 bool ZoomController::SetZoomLevel(double zoom_level) {
80 scoped_refptr<const extensions::Extension> null_extension_pointer;
81 // An extension did not initiate this zoom change.
82 return SetZoomLevelByExtension(zoom_level, null_extension_pointer);
83 }
84
85 bool ZoomController::SetZoomLevelByExtension(
86 double zoom_level,
87 scoped_refptr<const extensions::Extension> extension) {
88 // Cannot zoom in disabled mode.
89 if (zoom_mode_ == kZoomModeDisabled)
90 return false;
91
92 if (zoom_mode_ == kZoomModeManual) {
93 double old_zoom_level = zoom_level_;
94 zoom_level_ = zoom_level;
95
96 observer_->OnZoomChangeInitiated(
97 web_contents(), old_zoom_level, zoom_level, zoom_mode_);
98
99 return true;
100 }
101
102 // Store extension data so that |extension| can be attributed when the zoom
103 // change completes..
104 extensions_.push(extension.get());
105
106 // Do not actually rescale the page in manual mode.
107 content::HostZoomMap* zoom_map = content::HostZoomMap::GetForBrowserContext(
108 web_contents()->GetBrowserContext());
109 DCHECK(zoom_map);
110 content::NavigationEntry* entry =
111 web_contents()->GetController().GetLastCommittedEntry();
112 std::string host =
Fady Samuel 2014/06/12 19:38:58 Move this to the else clause?
wjmaclean 2014/06/12 19:45:25 Done.
113 net::GetHostOrSpecFromURL(entry ? entry->GetURL() : GURL::EmptyGURL());
114 double old_zoom_level = GetZoomLevel();
115 int render_process_id = web_contents()->GetRenderProcessHost()->GetID();
116 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
117 if (zoom_mode_ == kZoomModeIsolated ||
118 zoom_map->UsesTemporaryZoomLevel(render_process_id, render_view_id)) {
119 zoom_map->SetTemporaryZoomLevel(
120 render_process_id, render_view_id, zoom_level);
121 } else {
122 zoom_map->SetZoomLevelForHost(host, zoom_level);
123 }
124 observer_->OnZoomChangeInitiated(
125 web_contents(), old_zoom_level, zoom_level, zoom_mode_);
126
127 return true;
128 }
129
130 void ZoomController::SetZoomMode(ZoomMode new_mode) {
131 if (new_mode == zoom_mode_)
132 return;
133
134 content::HostZoomMap* zoom_map = content::HostZoomMap::GetForBrowserContext(
135 web_contents()->GetBrowserContext());
136 DCHECK(zoom_map);
137 int render_process_id = web_contents()->GetRenderProcessHost()->GetID();
138 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID();
139 content::NavigationEntry* entry =
140 web_contents()->GetController().GetLastCommittedEntry();
141 GURL url;
142 if (entry)
143 url = entry->GetURL();
144 std::string host = net::GetHostOrSpecFromURL(url);
Fady Samuel 2014/06/12 19:38:58 Move this and its dependencies to the case kZoomMo
wjmaclean 2014/06/12 19:45:25 Done.
145 double original_zoom_level = GetZoomLevel();
146
147 switch (new_mode) {
148 case kZoomModeDefault: {
149 if (zoom_map->HasZoomLevel(url.scheme(), host)) {
150 // All the other modes are per-tab, but setting this mode shouldn't
151 // affect the zoom level of other tabs; if there are other tabs with
152 // the same origin, then set this tab's zoom level to match theirs.
153 // TODO(wjmaclean) How to convert this call?
154 double origin_zoom_level =
155 zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), host);
156 zoom_map->SetTemporaryZoomLevel(
157 render_process_id, render_view_id, origin_zoom_level);
158 } else {
159 // The host will need a level prior to removing the temporary level.
160 zoom_map->SetZoomLevelForHost(host, original_zoom_level);
161 }
162 // Remove per-tab zoom data for this tab.
163 zoom_map->ClearTemporaryZoomLevel(render_process_id, render_view_id);
164 break;
165 }
166 case kZoomModeIsolated: {
167 // Unless the zoom mode was |kZoomModeDisabled| before this call, the page
168 // needs an initial isolated zoom back to the same level it was at in the
169 // other mode.
170 if (zoom_mode_ != kZoomModeDisabled) {
171 zoom_map->SetTemporaryZoomLevel(
172 render_process_id, render_view_id, original_zoom_level);
173 }
174 break;
175 }
176 case kZoomModeManual: {
177 // Unless the zoom mode was |kZoomModeDisabled| before this call, the page
178 // needs to be resized to the default zoom before calling SetZoomLevel()
179 // so that the page can be resized manually to the same zoom as before.
180 if (zoom_mode_ != kZoomModeDisabled) {
181 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0);
182 zoom_mode_ = new_mode;
183 zoom_level_ = original_zoom_level;
184 }
185 break;
186 }
187 case kZoomModeDisabled: {
188 // The page needs to be zoomed back to default before disabling the zoom
189 zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0);
190 break;
191 }
192 }
193
194 zoom_mode_ = new_mode;
195 }
196
59 void ZoomController::DidNavigateMainFrame( 197 void ZoomController::DidNavigateMainFrame(
60 const content::LoadCommittedDetails& details, 198 const content::LoadCommittedDetails& details,
61 const content::FrameNavigateParams& params) { 199 const content::FrameNavigateParams& params) {
62 // If the main frame's content has changed, the new page may have a different 200 // If the main frame's content has changed, the new page may have a different
63 // zoom level from the old one. 201 // zoom level from the old one.
64 UpdateState(std::string()); 202 UpdateState(std::string());
65 } 203 }
66 204
67 void ZoomController::OnZoomLevelChanged( 205 void ZoomController::OnZoomLevelChanged(
68 const content::HostZoomMap::ZoomLevelChange& change) { 206 const content::HostZoomMap::ZoomLevelChange& change) {
69 UpdateState(change.host); 207 UpdateState(change.host,
208 change.mode == content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM);
70 } 209 }
71 210
72 void ZoomController::UpdateState(const std::string& host) { 211 void ZoomController::UpdateState(const std::string& host) {
212 UpdateState(host, false);
213 }
214
215 void ZoomController::UpdateState(const std::string& host,
216 bool is_temporary_zoom) {
217 // Update |last_extension_|.
218 if (!extensions_.empty()) {
219 last_extension_ = extensions_.front().get();
220 extensions_.pop();
221 }
222
73 // If |host| is empty, all observers should be updated. 223 // If |host| is empty, all observers should be updated.
74 if (!host.empty()) { 224 if (!host.empty()) {
75 // Use the navigation entry's URL instead of the WebContents' so virtual 225 // Use the navigation entry's URL instead of the WebContents' so virtual
76 // URLs work (e.g. chrome://settings). http://crbug.com/153950 226 // URLs work (e.g. chrome://settings). http://crbug.com/153950
77 content::NavigationEntry* entry = 227 content::NavigationEntry* entry =
78 web_contents()->GetController().GetLastCommittedEntry(); 228 web_contents()->GetController().GetLastCommittedEntry();
79 if (!entry || 229 if (!entry ||
80 host != net::GetHostOrSpecFromURL(entry->GetURL())) { 230 host != net::GetHostOrSpecFromURL(entry->GetURL())) {
81 return; 231 return;
82 } 232 }
83 } 233 }
84 234
85 bool dummy; 235 if (observer_) {
86 zoom_percent_ = web_contents()->GetZoomPercent(&dummy, &dummy); 236 // The zoom bubble can be shown for all normal, per-origin zoom changes
237 // (where the host will not be empty and the zoom is not temporary), or any
238 // special zoom changes (where the zoom mode will not be "default").
239 bool can_show_bubble =
240 zoom_mode_ != kZoomModeDefault ||
241 (!host.empty() && !is_temporary_zoom);
242 observer_->OnZoomChanged(web_contents(), can_show_bubble);
243 }
87 244
88 if (observer_) 245 last_extension_ = NULL;
89 observer_->OnZoomChanged(web_contents(), !host.empty());
90 } 246 }
247
248 void ZoomController::ZoomCallback(
249 scoped_refptr<const extensions::Extension> extension,
250 const base::Callback<void(void)>& callback) {
251 extensions_.push(extension);
252 callback.Run();
253 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698