| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/public/browser/host_zoom_map.h" | 5 #include "content/public/browser/host_zoom_map.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/prefs/pref_service.h" | 17 #include "base/prefs/pref_service.h" |
| 18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "chrome/browser/chrome_page_zoom.h" | 20 #include "chrome/browser/chrome_page_zoom.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
| 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 24 #include "chrome/common/chrome_constants.h" | 24 #include "chrome/common/chrome_constants.h" |
| 25 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
| 26 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| 27 #include "chrome/test/base/in_process_browser_test.h" | 27 #include "chrome/test/base/in_process_browser_test.h" |
| 28 #include "chrome/test/base/testing_profile.h" | 28 #include "chrome/test/base/testing_profile.h" |
| 29 #include "chrome/test/base/ui_test_utils.h" | 29 #include "chrome/test/base/ui_test_utils.h" |
| 30 #include "content/public/browser/browser_context.h" |
| 31 #include "content/public/browser/storage_partition.h" |
| 30 #include "content/public/test/test_utils.h" | 32 #include "content/public/test/test_utils.h" |
| 31 #include "net/dns/mock_host_resolver.h" | 33 #include "net/dns/mock_host_resolver.h" |
| 32 #include "net/test/embedded_test_server/embedded_test_server.h" | 34 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 33 #include "net/test/embedded_test_server/http_response.h" | 35 #include "net/test/embedded_test_server/http_response.h" |
| 34 #include "testing/gmock/include/gmock/gmock.h" | 36 #include "testing/gmock/include/gmock/gmock.h" |
| 35 #include "url/gurl.h" | 37 #include "url/gurl.h" |
| 36 | 38 |
| 37 namespace { | 39 namespace { |
| 38 | 40 |
| 39 class ZoomLevelChangeObserver { | 41 class ZoomLevelChangeObserver { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 content::HostZoomMap::ZoomLevelVector zoom_levels = | 95 content::HostZoomMap::ZoomLevelVector zoom_levels = |
| 94 host_zoom_map->GetAllZoomLevels(); | 96 host_zoom_map->GetAllZoomLevels(); |
| 95 std::vector<std::string> results; | 97 std::vector<std::string> results; |
| 96 for (ZoomLevelVector::const_iterator it = zoom_levels.begin(); | 98 for (ZoomLevelVector::const_iterator it = zoom_levels.begin(); |
| 97 it != zoom_levels.end(); ++it) | 99 it != zoom_levels.end(); ++it) |
| 98 results.push_back(it->host); | 100 results.push_back(it->host); |
| 99 return results; | 101 return results; |
| 100 } | 102 } |
| 101 | 103 |
| 102 std::vector<std::string> GetHostsWithZoomLevelsFromPrefs() { | 104 std::vector<std::string> GetHostsWithZoomLevelsFromPrefs() { |
| 103 PrefService* prefs = browser()->profile()->GetPrefs(); | 105 PrefService* prefs = content::BrowserContext::GetDefaultStoragePartition( |
| 106 browser()->profile())->GetZoomLevelPrefs(); |
| 104 const base::DictionaryValue* values = | 107 const base::DictionaryValue* values = |
| 105 prefs->GetDictionary(prefs::kPerHostZoomLevels); | 108 prefs->GetDictionary(prefs::kPerHostZoomLevels); |
| 106 std::vector<std::string> results; | 109 std::vector<std::string> results; |
| 107 if (values) { | 110 if (values) { |
| 108 for (base::DictionaryValue::Iterator it(*values); | 111 for (base::DictionaryValue::Iterator it(*values); |
| 109 !it.IsAtEnd(); it.Advance()) | 112 !it.IsAtEnd(); it.Advance()) |
| 110 results.push_back(it.key()); | 113 results.push_back(it.key()); |
| 111 } | 114 } |
| 112 return results; | 115 return results; |
| 113 } | 116 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 139 public: | 142 public: |
| 140 HostZoomMapSanitizationBrowserTest() {} | 143 HostZoomMapSanitizationBrowserTest() {} |
| 141 | 144 |
| 142 private: | 145 private: |
| 143 // InProcessBrowserTest: | 146 // InProcessBrowserTest: |
| 144 virtual bool SetUpUserDataDirectory() OVERRIDE { | 147 virtual bool SetUpUserDataDirectory() OVERRIDE { |
| 145 // Zoom-related preferences demonstrating the two problems that could be | 148 // Zoom-related preferences demonstrating the two problems that could be |
| 146 // caused by the bug. They incorrectly contain a per-host zoom level for the | 149 // caused by the bug. They incorrectly contain a per-host zoom level for the |
| 147 // empty host; and a value for 'host1' that only differs from the default by | 150 // empty host; and a value for 'host1' that only differs from the default by |
| 148 // epsilon. Neither should have been persisted. | 151 // epsilon. Neither should have been persisted. |
| 152 const char kPrefs[] = |
| 153 "{'profile': {" |
| 154 " 'default_zoom_level': 1.2" |
| 155 "}}"; |
| 149 const char kBrokenPrefs[] = | 156 const char kBrokenPrefs[] = |
| 150 "{'profile': {" | 157 "{'profile': {" |
| 151 " 'default_zoom_level': 1.2," | |
| 152 " 'per_host_zoom_levels': {'': 1.1, 'host1': 1.20001, 'host2': 1.3}" | 158 " 'per_host_zoom_levels': {'': 1.1, 'host1': 1.20001, 'host2': 1.3}" |
| 153 "}}"; | 159 "}}"; |
| 160 std::string prefs(kPrefs); |
| 161 std::replace(prefs.begin(), prefs.end(), '\'', '\"'); |
| 154 std::string broken_prefs(kBrokenPrefs); | 162 std::string broken_prefs(kBrokenPrefs); |
| 155 std::replace(broken_prefs.begin(), broken_prefs.end(), '\'', '\"'); | 163 std::replace(broken_prefs.begin(), broken_prefs.end(), '\'', '\"'); |
| 156 | 164 |
| 157 base::FilePath user_data_directory, path_to_prefs; | 165 base::FilePath user_data_directory, path_to_prefs, path_to_broken_prefs; |
| 158 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory); | 166 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory); |
| 159 path_to_prefs = user_data_directory | 167 path_to_prefs = user_data_directory |
| 160 .AppendASCII(TestingProfile::kTestUserProfileDir) | 168 .AppendASCII(TestingProfile::kTestUserProfileDir) |
| 161 .Append(chrome::kPreferencesFilename); | 169 .Append(chrome::kPreferencesFilename); |
| 170 path_to_broken_prefs = user_data_directory |
| 171 .AppendASCII(TestingProfile::kTestUserProfileDir) |
| 172 .Append(chrome::kZoomLevelPreferencesFilename); |
| 162 base::CreateDirectory(path_to_prefs.DirName()); | 173 base::CreateDirectory(path_to_prefs.DirName()); |
| 163 base::WriteFile(path_to_prefs, broken_prefs.c_str(), broken_prefs.size()); | 174 base::WriteFile(path_to_prefs, prefs.c_str(), prefs.size()); |
| 175 base::WriteFile( |
| 176 path_to_broken_prefs, broken_prefs.c_str(), broken_prefs.size()); |
| 164 return true; | 177 return true; |
| 165 } | 178 } |
| 166 | 179 |
| 167 DISALLOW_COPY_AND_ASSIGN(HostZoomMapSanitizationBrowserTest); | 180 DISALLOW_COPY_AND_ASSIGN(HostZoomMapSanitizationBrowserTest); |
| 168 }; | 181 }; |
| 169 | 182 |
| 170 // Regression test for crbug.com/364399. | 183 // Regression test for crbug.com/364399. |
| 171 IN_PROC_BROWSER_TEST_F(HostZoomMapBrowserTest, ToggleDefaultZoomLevel) { | 184 IN_PROC_BROWSER_TEST_F(HostZoomMapBrowserTest, ToggleDefaultZoomLevel) { |
| 172 const double default_zoom_level = content::ZoomFactorToZoomLevel(1.5); | 185 const double default_zoom_level = content::ZoomFactorToZoomLevel(1.5); |
| 173 | 186 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 // any per-host values saved either to Pref, or internally in HostZoomMap. | 220 // any per-host values saved either to Pref, or internally in HostZoomMap. |
| 208 EXPECT_TRUE(GetHostsWithZoomLevels().empty()); | 221 EXPECT_TRUE(GetHostsWithZoomLevels().empty()); |
| 209 EXPECT_TRUE(GetHostsWithZoomLevelsFromPrefs().empty()); | 222 EXPECT_TRUE(GetHostsWithZoomLevelsFromPrefs().empty()); |
| 210 } | 223 } |
| 211 | 224 |
| 212 // Test that garbage data from crbug.com/364399 is cleared up on startup. | 225 // Test that garbage data from crbug.com/364399 is cleared up on startup. |
| 213 IN_PROC_BROWSER_TEST_F(HostZoomMapSanitizationBrowserTest, ClearOnStartup) { | 226 IN_PROC_BROWSER_TEST_F(HostZoomMapSanitizationBrowserTest, ClearOnStartup) { |
| 214 EXPECT_THAT(GetHostsWithZoomLevels(), testing::ElementsAre("host2")); | 227 EXPECT_THAT(GetHostsWithZoomLevels(), testing::ElementsAre("host2")); |
| 215 EXPECT_THAT(GetHostsWithZoomLevelsFromPrefs(), testing::ElementsAre("host2")); | 228 EXPECT_THAT(GetHostsWithZoomLevelsFromPrefs(), testing::ElementsAre("host2")); |
| 216 } | 229 } |
| OLD | NEW |