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

Side by Side Diff: chrome/browser/ui/webui/settings/site_settings_handler.cc

Issue 2323693002: Site Settings Desktop: Implement Zoom Levels category. (Closed)
Patch Set: Created 4 years, 3 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/webui/settings/site_settings_handler.h" 5 #include "chrome/browser/ui/webui/settings/site_settings_handler.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/i18n/number_formatting.h"
12 #include "base/macros.h" 13 #include "base/macros.h"
13 #include "base/values.h" 14 #include "base/values.h"
14 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h" 15 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h"
15 #include "chrome/browser/chrome_notification_types.h" 16 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 17 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
17 #include "chrome/browser/permissions/chooser_context_base.h" 18 #include "chrome/browser/permissions/chooser_context_base.h"
18 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/webui/site_settings_helper.h" 20 #include "chrome/browser/ui/webui/site_settings_helper.h"
20 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" 21 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
22 #include "chrome/grit/generated_resources.h"
21 #include "components/content_settings/core/browser/host_content_settings_map.h" 23 #include "components/content_settings/core/browser/host_content_settings_map.h"
22 #include "components/content_settings/core/common/content_settings_types.h" 24 #include "components/content_settings/core/common/content_settings_types.h"
23 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/notification_service.h" 26 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/web_contents.cc"
25 #include "content/public/browser/web_ui.h" 28 #include "content/public/browser/web_ui.h"
29 #include "content/public/common/page_zoom.h"
30 #include "content/public/common/url_constants.h"
26 #include "extensions/browser/extension_registry.h" 31 #include "extensions/browser/extension_registry.h"
27 #include "extensions/common/permissions/api_permission.h" 32 #include "extensions/common/permissions/api_permission.h"
28 #include "extensions/common/permissions/permissions_data.h" 33 #include "extensions/common/permissions/permissions_data.h"
29 #include "storage/browser/quota/quota_manager.h" 34 #include "storage/browser/quota/quota_manager.h"
30 #include "storage/common/quota/quota_status_code.h" 35 #include "storage/common/quota/quota_status_code.h"
36 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/text/bytes_formatting.h" 37 #include "ui/base/text/bytes_formatting.h"
32 38
33 namespace settings { 39 namespace settings {
34 40
35 namespace { 41 namespace {
36 42
37 const char kAppName[] = "appName"; 43 const char kAppName[] = "appName";
38 const char kAppId[] = "appId"; 44 const char kAppId[] = "appId";
45 const char kZoom[] = "zoom";
39 46
40 // Return an appropriate API Permission ID for the given string name. 47 // Return an appropriate API Permission ID for the given string name.
41 extensions::APIPermission::APIPermission::ID APIPermissionFromGroupName( 48 extensions::APIPermission::APIPermission::ID APIPermissionFromGroupName(
42 std::string type) { 49 std::string type) {
43 // Once there are more than two groups to consider, this should be changed to 50 // Once there are more than two groups to consider, this should be changed to
44 // something better than if's. 51 // something better than if's.
45 52
46 if (site_settings::ContentSettingsTypeFromGroupName(type) == 53 if (site_settings::ContentSettingsTypeFromGroupName(type) ==
47 CONTENT_SETTINGS_TYPE_GEOLOCATION) 54 CONTENT_SETTINGS_TYPE_GEOLOCATION)
48 return extensions::APIPermission::APIPermission::kGeolocation; 55 return extensions::APIPermission::APIPermission::kGeolocation;
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 base::Bind(&SiteSettingsHandler::HandleSetCategoryPermissionForOrigin, 158 base::Bind(&SiteSettingsHandler::HandleSetCategoryPermissionForOrigin,
152 base::Unretained(this))); 159 base::Unretained(this)));
153 web_ui()->RegisterMessageCallback( 160 web_ui()->RegisterMessageCallback(
154 "isPatternValid", 161 "isPatternValid",
155 base::Bind(&SiteSettingsHandler::HandleIsPatternValid, 162 base::Bind(&SiteSettingsHandler::HandleIsPatternValid,
156 base::Unretained(this))); 163 base::Unretained(this)));
157 web_ui()->RegisterMessageCallback( 164 web_ui()->RegisterMessageCallback(
158 "updateIncognitoStatus", 165 "updateIncognitoStatus",
159 base::Bind(&SiteSettingsHandler::HandleUpdateIncognitoStatus, 166 base::Bind(&SiteSettingsHandler::HandleUpdateIncognitoStatus,
160 base::Unretained(this))); 167 base::Unretained(this)));
168 web_ui()->RegisterMessageCallback(
169 "fetchZoomLevels",
170 base::Bind(&SiteSettingsHandler::HandleFetchZoomLevels,
171 base::Unretained(this)));
172 web_ui()->RegisterMessageCallback(
173 "removeZoomLevel",
174 base::Bind(&SiteSettingsHandler::HandleRemoveZoomLevel,
175 base::Unretained(this)));
176
177 // Here we only subscribe to the HostZoomMap for the default storage partition
178 // since we don't allow the user to manage the zoom levels for apps.
179 // We're only interested in zoom-levels that are persisted, since the user
180 // is given the opportunity to view/delete these in the content-settings page.
181 host_zoom_map_subscription_ =
182 content::HostZoomMap::GetDefaultForBrowserContext(
183 web_ui()->GetWebContents()->GetBrowserContext())
184 ->AddZoomLevelChangedCallback(
185 base::Bind(&SiteSettingsHandler::OnZoomLevelChanged,
186 base::Unretained(this)));
161 } 187 }
162 188
163 void SiteSettingsHandler::OnJavascriptAllowed() { 189 void SiteSettingsHandler::OnJavascriptAllowed() {
164 observer_.Add(HostContentSettingsMapFactory::GetForProfile(profile_)); 190 observer_.Add(HostContentSettingsMapFactory::GetForProfile(profile_));
165 if (profile_->HasOffTheRecordProfile()) { 191 if (profile_->HasOffTheRecordProfile()) {
166 auto* map = HostContentSettingsMapFactory::GetForProfile( 192 auto* map = HostContentSettingsMapFactory::GetForProfile(
167 profile_->GetOffTheRecordProfile()); 193 profile_->GetOffTheRecordProfile());
168 if (!observer_.IsObserving(map)) 194 if (!observer_.IsObserving(map))
169 observer_.Add(map); 195 observer_.Add(map);
170 } 196 }
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 if (!profile_->IsSameProfile(profile)) 284 if (!profile_->IsSameProfile(profile))
259 break; 285 break;
260 SendIncognitoStatus(profile, /*was_destroyed=*/ false); 286 SendIncognitoStatus(profile, /*was_destroyed=*/ false);
261 287
262 observer_.Add(HostContentSettingsMapFactory::GetForProfile(profile)); 288 observer_.Add(HostContentSettingsMapFactory::GetForProfile(profile));
263 break; 289 break;
264 } 290 }
265 } 291 }
266 } 292 }
267 293
294 void SiteSettingsHandler::OnZoomLevelChanged(
295 const content::HostZoomMap::ZoomLevelChange& change) {
296 SendZoomLevels();
297 }
298
268 void SiteSettingsHandler::HandleFetchUsageTotal( 299 void SiteSettingsHandler::HandleFetchUsageTotal(
269 const base::ListValue* args) { 300 const base::ListValue* args) {
270 AllowJavascript(); 301 AllowJavascript();
271 302
272 CHECK_EQ(1U, args->GetSize()); 303 CHECK_EQ(1U, args->GetSize());
273 std::string host; 304 std::string host;
274 CHECK(args->GetString(0, &host)); 305 CHECK(args->GetString(0, &host));
275 usage_host_ = host; 306 usage_host_ = host;
276 307
277 scoped_refptr<StorageInfoFetcher> storage_info_fetcher 308 scoped_refptr<StorageInfoFetcher> storage_info_fetcher
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
521 // message before destroying, so HasOffTheRecordProfile for profile_ won't 552 // message before destroying, so HasOffTheRecordProfile for profile_ won't
522 // return false until after the profile actually been destroyed. 553 // return false until after the profile actually been destroyed.
523 bool incognito_enabled = profile_->HasOffTheRecordProfile() && 554 bool incognito_enabled = profile_->HasOffTheRecordProfile() &&
524 !(was_destroyed && profile == profile_->GetOffTheRecordProfile()); 555 !(was_destroyed && profile == profile_->GetOffTheRecordProfile());
525 556
526 CallJavascriptFunction("cr.webUIListenerCallback", 557 CallJavascriptFunction("cr.webUIListenerCallback",
527 base::StringValue("onIncognitoStatusChanged"), 558 base::StringValue("onIncognitoStatusChanged"),
528 base::FundamentalValue(incognito_enabled)); 559 base::FundamentalValue(incognito_enabled));
529 } 560 }
530 561
562 void SiteSettingsHandler::HandleFetchZoomLevels(const base::ListValue* args) {
563 AllowJavascript();
564 SendZoomLevels();
565 }
566
567 void SiteSettingsHandler::SendZoomLevels() {
568 if (!IsJavascriptAllowed())
569 return;
570
571 base::ListValue zoom_levels_exceptions;
572
573 content::HostZoomMap* host_zoom_map =
574 content::HostZoomMap::GetDefaultForBrowserContext(profile_);
575 content::HostZoomMap::ZoomLevelVector zoom_levels(
576 host_zoom_map->GetAllZoomLevels());
577
578 // Sort ZoomLevelChanges by host and scheme
579 // (a.com < http://a.com < https://a.com < b.com).
580 std::sort(zoom_levels.begin(), zoom_levels.end(),
581 [](const content::HostZoomMap::ZoomLevelChange& a,
582 const content::HostZoomMap::ZoomLevelChange& b) {
583 return a.host == b.host ? a.scheme < b.scheme : a.host < b.host;
584 });
585
586 for (content::HostZoomMap::ZoomLevelVector::const_iterator i =
587 zoom_levels.begin();
588 i != zoom_levels.end();
589 ++i) {
590 std::unique_ptr<base::DictionaryValue> exception(new base::DictionaryValue);
591 switch (i->mode) {
592 case content::HostZoomMap::ZOOM_CHANGED_FOR_HOST: {
593 exception->SetString(site_settings::kOrigin, i->host);
594 std::string host = i->host;
595 if (host == content::kUnreachableWebDataURL) {
596 host =
597 l10n_util::GetStringUTF8(IDS_ZOOMLEVELS_CHROME_ERROR_PAGES_LABEL);
598 }
599 exception->SetString(site_settings::kOrigin, host);
600 break;
601 }
602 case content::HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST:
603 // These are not stored in preferences and get cleared on next browser
604 // start. Therefore, we don't care for them.
605 continue;
606 case content::HostZoomMap::PAGE_SCALE_IS_ONE_CHANGED:
607 continue;
608 case content::HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM:
609 NOTREACHED();
610 }
611
612 std::string setting_string =
613 content_settings::ContentSettingToString(CONTENT_SETTING_DEFAULT);
614 DCHECK(!setting_string.empty());
615
616 exception->SetString(site_settings::kSetting, setting_string);
617
618 // Calculate the zoom percent from the factor. Round up to the nearest whole
619 // number.
620 int zoom_percent = static_cast<int>(
621 content::ZoomLevelToZoomFactor(i->zoom_level) * 100 + 0.5);
622 exception->SetString(kZoom, base::FormatPercent(zoom_percent));
623 exception->SetString(
624 site_settings::kSource, site_settings::kPreferencesSource);
625 // Append the new entry to the list and map.
626 zoom_levels_exceptions.Append(std::move(exception));
627 }
628
629 CallJavascriptFunction("cr.webUIListenerCallback",
630 base::StringValue("onZoomLevelsChanged"),
631 zoom_levels_exceptions);
632 }
633
634 void SiteSettingsHandler::HandleRemoveZoomLevel(const base::ListValue* args) {
635 CHECK_EQ(1U, args->GetSize());
636
637 std::string origin;
638 CHECK(args->GetString(0, &origin));
639
640 if (origin ==
641 l10n_util::GetStringUTF8(IDS_ZOOMLEVELS_CHROME_ERROR_PAGES_LABEL)) {
642 origin = content::kUnreachableWebDataURL;
643 }
644
645 content::HostZoomMap* host_zoom_map;
646 host_zoom_map = content::HostZoomMap::GetDefaultForBrowserContext(profile_);
647 double default_level = host_zoom_map->GetDefaultZoomLevel();
648 host_zoom_map->SetZoomLevelForHost(origin, default_level);
649 }
650
531 } // namespace settings 651 } // namespace settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698