| 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 { |
| 40 public: | 42 public: |
| 41 explicit ZoomLevelChangeObserver(Profile* profile) | 43 explicit ZoomLevelChangeObserver(Profile* profile) |
| 42 : message_loop_runner_(new content::MessageLoopRunner) { | 44 : message_loop_runner_(new content::MessageLoopRunner) { |
| 43 content::HostZoomMap* host_zoom_map = static_cast<content::HostZoomMap*>( | 45 content::HostZoomMap* host_zoom_map = static_cast<content::HostZoomMap*>( |
| 44 content::HostZoomMap::GetForBrowserContext(profile)); | 46 content::HostZoomMap::GetDefaultForBrowserContext(profile)); |
| 45 subscription_ = host_zoom_map->AddZoomLevelChangedCallback(base::Bind( | 47 subscription_ = host_zoom_map->AddZoomLevelChangedCallback(base::Bind( |
| 46 &ZoomLevelChangeObserver::OnZoomLevelChanged, base::Unretained(this))); | 48 &ZoomLevelChangeObserver::OnZoomLevelChanged, base::Unretained(this))); |
| 47 } | 49 } |
| 48 | 50 |
| 49 void BlockUntilZoomLevelForHostHasChanged(const std::string& host) { | 51 void BlockUntilZoomLevelForHostHasChanged(const std::string& host) { |
| 50 while (!std::count(changed_hosts_.begin(), changed_hosts_.end(), host)) { | 52 while (!std::count(changed_hosts_.begin(), changed_hosts_.end(), host)) { |
| 51 message_loop_runner_->Run(); | 53 message_loop_runner_->Run(); |
| 52 message_loop_runner_ = new content::MessageLoopRunner; | 54 message_loop_runner_ = new content::MessageLoopRunner; |
| 53 } | 55 } |
| 54 changed_hosts_.clear(); | 56 changed_hosts_.clear(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 74 HostZoomMapBrowserTest() {} | 76 HostZoomMapBrowserTest() {} |
| 75 | 77 |
| 76 protected: | 78 protected: |
| 77 void SetDefaultZoomLevel(double level) { | 79 void SetDefaultZoomLevel(double level) { |
| 78 browser()->profile()->GetPrefs()->SetDouble( | 80 browser()->profile()->GetPrefs()->SetDouble( |
| 79 prefs::kDefaultZoomLevel, level); | 81 prefs::kDefaultZoomLevel, level); |
| 80 } | 82 } |
| 81 | 83 |
| 82 double GetZoomLevel(const GURL& url) { | 84 double GetZoomLevel(const GURL& url) { |
| 83 content::HostZoomMap* host_zoom_map = static_cast<content::HostZoomMap*>( | 85 content::HostZoomMap* host_zoom_map = static_cast<content::HostZoomMap*>( |
| 84 content::HostZoomMap::GetForBrowserContext(browser()->profile())); | 86 content::HostZoomMap::GetDefaultForBrowserContext( |
| 87 browser()->profile())); |
| 85 return host_zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), | 88 return host_zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), |
| 86 url.host()); | 89 url.host()); |
| 87 } | 90 } |
| 88 | 91 |
| 89 std::vector<std::string> GetHostsWithZoomLevels() { | 92 std::vector<std::string> GetHostsWithZoomLevels() { |
| 90 typedef content::HostZoomMap::ZoomLevelVector ZoomLevelVector; | 93 typedef content::HostZoomMap::ZoomLevelVector ZoomLevelVector; |
| 91 content::HostZoomMap* host_zoom_map = static_cast<content::HostZoomMap*>( | 94 content::HostZoomMap* host_zoom_map = static_cast<content::HostZoomMap*>( |
| 92 content::HostZoomMap::GetForBrowserContext(browser()->profile())); | 95 content::HostZoomMap::GetDefaultForBrowserContext( |
| 96 browser()->profile())); |
| 93 content::HostZoomMap::ZoomLevelVector zoom_levels = | 97 content::HostZoomMap::ZoomLevelVector zoom_levels = |
| 94 host_zoom_map->GetAllZoomLevels(); | 98 host_zoom_map->GetAllZoomLevels(); |
| 95 std::vector<std::string> results; | 99 std::vector<std::string> results; |
| 96 for (ZoomLevelVector::const_iterator it = zoom_levels.begin(); | 100 for (ZoomLevelVector::const_iterator it = zoom_levels.begin(); |
| 97 it != zoom_levels.end(); ++it) | 101 it != zoom_levels.end(); ++it) |
| 98 results.push_back(it->host); | 102 results.push_back(it->host); |
| 99 return results; | 103 return results; |
| 100 } | 104 } |
| 101 | 105 |
| 102 std::vector<std::string> GetHostsWithZoomLevelsFromPrefs() { | 106 std::vector<std::string> GetHostsWithZoomLevelsFromPrefs() { |
| 103 PrefService* prefs = browser()->profile()->GetPrefs(); | 107 PrefService* prefs = content::BrowserContext::GetDefaultStoragePartition( |
| 108 browser()->profile())->GetZoomLevelPrefs(); |
| 104 const base::DictionaryValue* values = | 109 const base::DictionaryValue* values = |
| 105 prefs->GetDictionary(prefs::kPerHostZoomLevels); | 110 prefs->GetDictionary(prefs::kPerHostZoomLevels); |
| 106 std::vector<std::string> results; | 111 std::vector<std::string> results; |
| 107 if (values) { | 112 if (values) { |
| 108 for (base::DictionaryValue::Iterator it(*values); | 113 for (base::DictionaryValue::Iterator it(*values); |
| 109 !it.IsAtEnd(); it.Advance()) | 114 !it.IsAtEnd(); it.Advance()) |
| 110 results.push_back(it.key()); | 115 results.push_back(it.key()); |
| 111 } | 116 } |
| 112 return results; | 117 return results; |
| 113 } | 118 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 139 public: | 144 public: |
| 140 HostZoomMapSanitizationBrowserTest() {} | 145 HostZoomMapSanitizationBrowserTest() {} |
| 141 | 146 |
| 142 private: | 147 private: |
| 143 // InProcessBrowserTest: | 148 // InProcessBrowserTest: |
| 144 virtual bool SetUpUserDataDirectory() OVERRIDE { | 149 virtual bool SetUpUserDataDirectory() OVERRIDE { |
| 145 // Zoom-related preferences demonstrating the two problems that could be | 150 // 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 | 151 // 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 | 152 // empty host; and a value for 'host1' that only differs from the default by |
| 148 // epsilon. Neither should have been persisted. | 153 // epsilon. Neither should have been persisted. |
| 154 const char kPrefs[] = |
| 155 "{'profile': {" |
| 156 " 'default_zoom_level': 1.2" |
| 157 "}}"; |
| 149 const char kBrokenPrefs[] = | 158 const char kBrokenPrefs[] = |
| 150 "{'profile': {" | 159 "{'profile': {" |
| 151 " 'default_zoom_level': 1.2," | |
| 152 " 'per_host_zoom_levels': {'': 1.1, 'host1': 1.20001, 'host2': 1.3}" | 160 " 'per_host_zoom_levels': {'': 1.1, 'host1': 1.20001, 'host2': 1.3}" |
| 153 "}}"; | 161 "}}"; |
| 162 std::string prefs(kPrefs); |
| 163 std::replace(prefs.begin(), prefs.end(), '\'', '\"'); |
| 154 std::string broken_prefs(kBrokenPrefs); | 164 std::string broken_prefs(kBrokenPrefs); |
| 155 std::replace(broken_prefs.begin(), broken_prefs.end(), '\'', '\"'); | 165 std::replace(broken_prefs.begin(), broken_prefs.end(), '\'', '\"'); |
| 156 | 166 |
| 157 base::FilePath user_data_directory, path_to_prefs; | 167 base::FilePath user_data_directory, path_to_prefs, path_to_broken_prefs; |
| 158 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory); | 168 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory); |
| 159 path_to_prefs = user_data_directory | 169 path_to_prefs = user_data_directory |
| 160 .AppendASCII(TestingProfile::kTestUserProfileDir) | 170 .AppendASCII(TestingProfile::kTestUserProfileDir) |
| 161 .Append(chrome::kPreferencesFilename); | 171 .Append(chrome::kPreferencesFilename); |
| 172 path_to_broken_prefs = user_data_directory |
| 173 .AppendASCII(TestingProfile::kTestUserProfileDir) |
| 174 .Append(chrome::kZoomLevelPreferencesFilename); |
| 162 base::CreateDirectory(path_to_prefs.DirName()); | 175 base::CreateDirectory(path_to_prefs.DirName()); |
| 163 base::WriteFile(path_to_prefs, broken_prefs.c_str(), broken_prefs.size()); | 176 base::WriteFile(path_to_prefs, prefs.c_str(), prefs.size()); |
| 177 base::WriteFile( |
| 178 path_to_broken_prefs, broken_prefs.c_str(), broken_prefs.size()); |
| 164 return true; | 179 return true; |
| 165 } | 180 } |
| 166 | 181 |
| 167 DISALLOW_COPY_AND_ASSIGN(HostZoomMapSanitizationBrowserTest); | 182 DISALLOW_COPY_AND_ASSIGN(HostZoomMapSanitizationBrowserTest); |
| 168 }; | 183 }; |
| 169 | 184 |
| 170 // Regression test for crbug.com/364399. | 185 // Regression test for crbug.com/364399. |
| 171 IN_PROC_BROWSER_TEST_F(HostZoomMapBrowserTest, ToggleDefaultZoomLevel) { | 186 IN_PROC_BROWSER_TEST_F(HostZoomMapBrowserTest, ToggleDefaultZoomLevel) { |
| 172 const double default_zoom_level = content::ZoomFactorToZoomLevel(1.5); | 187 const double default_zoom_level = content::ZoomFactorToZoomLevel(1.5); |
| 173 | 188 |
| (...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. | 222 // any per-host values saved either to Pref, or internally in HostZoomMap. |
| 208 EXPECT_TRUE(GetHostsWithZoomLevels().empty()); | 223 EXPECT_TRUE(GetHostsWithZoomLevels().empty()); |
| 209 EXPECT_TRUE(GetHostsWithZoomLevelsFromPrefs().empty()); | 224 EXPECT_TRUE(GetHostsWithZoomLevelsFromPrefs().empty()); |
| 210 } | 225 } |
| 211 | 226 |
| 212 // Test that garbage data from crbug.com/364399 is cleared up on startup. | 227 // Test that garbage data from crbug.com/364399 is cleared up on startup. |
| 213 IN_PROC_BROWSER_TEST_F(HostZoomMapSanitizationBrowserTest, ClearOnStartup) { | 228 IN_PROC_BROWSER_TEST_F(HostZoomMapSanitizationBrowserTest, ClearOnStartup) { |
| 214 EXPECT_THAT(GetHostsWithZoomLevels(), testing::ElementsAre("host2")); | 229 EXPECT_THAT(GetHostsWithZoomLevels(), testing::ElementsAre("host2")); |
| 215 EXPECT_THAT(GetHostsWithZoomLevelsFromPrefs(), testing::ElementsAre("host2")); | 230 EXPECT_THAT(GetHostsWithZoomLevelsFromPrefs(), testing::ElementsAre("host2")); |
| 216 } | 231 } |
| OLD | NEW |