Chromium Code Reviews| 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 "chrome/browser/ui/webui/local_state/local_state_ui.h" | 5 #include "chrome/browser/ui/webui/local_state/local_state_ui.h" |
| 6 | 6 |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/json/json_string_value_serializer.h" | 7 #include "base/json/json_string_value_serializer.h" |
| 10 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/values.h" | |
| 11 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 12 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
| 15 #include "components/prefs/pref_service.h" | 14 #include "components/prefs/pref_service.h" |
| 16 #include "content/public/browser/web_ui.h" | 15 #include "content/public/browser/web_ui.h" |
| 17 #include "content/public/browser/web_ui_controller.h" | 16 #include "content/public/browser/web_ui_controller.h" |
| 18 #include "content/public/browser/web_ui_data_source.h" | 17 #include "content/public/browser/web_ui_data_source.h" |
| 19 #include "content/public/browser/web_ui_message_handler.h" | 18 #include "content/public/browser/web_ui_message_handler.h" |
| 20 #include "grit/browser_resources.h" | 19 #include "grit/browser_resources.h" |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| 23 | 22 |
| 23 // On ChromeOS, the local state file contains some information about other | |
| 24 // user accounts which we don't want to expose to other users. Use a whitelist | |
| 25 // to only show variations and UMA related fields which don't contain PII. | |
| 26 #if defined(OS_CHROMEOS) | |
| 27 #define ENABLE_FILTERING true | |
| 28 #else | |
| 29 #define ENABLE_FILTERING false | |
| 30 #endif // defined(OS_CHROMEOS) | |
| 31 | |
| 24 // UI Handler for chrome://local-state. Displays the Local State file as JSON. | 32 // UI Handler for chrome://local-state. Displays the Local State file as JSON. |
| 25 class LocalStateUIHandler : public content::WebUIMessageHandler { | 33 class LocalStateUIHandler : public content::WebUIMessageHandler { |
| 26 public: | 34 public: |
| 27 LocalStateUIHandler(); | 35 LocalStateUIHandler(); |
| 28 ~LocalStateUIHandler() override; | 36 ~LocalStateUIHandler() override; |
| 29 | 37 |
| 30 // content::WebUIMessageHandler: | 38 // content::WebUIMessageHandler: |
| 31 void RegisterMessages() override; | 39 void RegisterMessages() override; |
| 32 | 40 |
| 33 private: | 41 private: |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 44 LocalStateUIHandler::~LocalStateUIHandler() { | 52 LocalStateUIHandler::~LocalStateUIHandler() { |
| 45 } | 53 } |
| 46 | 54 |
| 47 void LocalStateUIHandler::RegisterMessages() { | 55 void LocalStateUIHandler::RegisterMessages() { |
| 48 web_ui()->RegisterMessageCallback( | 56 web_ui()->RegisterMessageCallback( |
| 49 "requestJson", base::Bind(&LocalStateUIHandler::HandleRequestJson, | 57 "requestJson", base::Bind(&LocalStateUIHandler::HandleRequestJson, |
| 50 base::Unretained(this))); | 58 base::Unretained(this))); |
| 51 } | 59 } |
| 52 | 60 |
| 53 void LocalStateUIHandler::HandleRequestJson(const base::ListValue* args) { | 61 void LocalStateUIHandler::HandleRequestJson(const base::ListValue* args) { |
| 54 #if !defined(OS_CHROMEOS) | |
| 55 std::unique_ptr<base::DictionaryValue> local_state_values( | 62 std::unique_ptr<base::DictionaryValue> local_state_values( |
| 56 g_browser_process->local_state()->GetPreferenceValuesOmitDefaults()); | 63 g_browser_process->local_state()->GetPreferenceValuesOmitDefaults()); |
| 57 | 64 if (ENABLE_FILTERING) { |
| 65 std::vector<std::string> whitelisted_prefixes = { | |
| 66 "variations", "user_experience_metrics", "uninstall_metrics"}; | |
| 67 internal::FilterPrefs(whitelisted_prefixes, local_state_values.get()); | |
| 68 } | |
| 58 std::string json; | 69 std::string json; |
| 59 JSONStringValueSerializer serializer(&json); | 70 JSONStringValueSerializer serializer(&json); |
| 60 serializer.set_pretty_print(true); | 71 serializer.set_pretty_print(true); |
| 61 bool result = serializer.Serialize(*local_state_values); | 72 bool result = serializer.Serialize(*local_state_values); |
| 62 if (!result) | 73 if (!result) |
| 63 json = "Error loading Local State file."; | 74 json = "Error loading Local State file."; |
| 64 | 75 |
| 65 web_ui()->CallJavascriptFunction("localState.setLocalState", | 76 web_ui()->CallJavascriptFunction("localState.setLocalState", |
| 66 base::StringValue(json)); | 77 base::StringValue(json)); |
| 67 #endif // !defined(OS_CHROMEOS) | 78 } |
| 79 | |
| 80 // Returns true if |pref_name| starts with one of the |valid_prefixes|. | |
| 81 bool HasValidPrefix(const std::string& pref_name, | |
| 82 const std::vector<std::string> valid_prefixes) { | |
| 83 for (const std::string& prefix : valid_prefixes) { | |
| 84 if (pref_name.compare(0, prefix.size(), prefix) == 0) | |
|
Alexei Svitkine (slow)
2016/04/26 17:46:35
Oops, missed this earlier. Can you change this to
hamelphi
2016/04/26 18:14:54
Done.
| |
| 85 return true; | |
| 86 } | |
| 87 return false; | |
| 68 } | 88 } |
| 69 | 89 |
| 70 } // namespace | 90 } // namespace |
| 71 | 91 |
| 92 namespace internal { | |
| 93 | |
| 94 void FilterPrefs(const std::vector<std::string>& valid_prefixes, | |
| 95 base::DictionaryValue* prefs) { | |
| 96 std::vector<std::string> prefs_to_remove; | |
| 97 for (base::DictionaryValue::Iterator it(*prefs); !it.IsAtEnd(); | |
| 98 it.Advance()) { | |
| 99 if (!HasValidPrefix(it.key(), valid_prefixes)) | |
| 100 prefs_to_remove.push_back(it.key()); | |
| 101 } | |
| 102 for (const std::string& pref_to_remove : prefs_to_remove) { | |
| 103 std::unique_ptr<base::Value> removed_value; | |
| 104 bool successfully_removed = prefs->Remove(pref_to_remove, &removed_value); | |
| 105 DCHECK(successfully_removed); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 } // namespace internal | |
| 110 | |
| 72 LocalStateUI::LocalStateUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 111 LocalStateUI::LocalStateUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
| 73 // Set up the chrome://local-state source. | 112 // Set up the chrome://local-state source. |
| 74 content::WebUIDataSource* html_source = | 113 content::WebUIDataSource* html_source = |
| 75 content::WebUIDataSource::Create(chrome::kChromeUILocalStateHost); | 114 content::WebUIDataSource::Create(chrome::kChromeUILocalStateHost); |
| 76 html_source->SetDefaultResource(IDR_LOCAL_STATE_HTML); | 115 html_source->SetDefaultResource(IDR_LOCAL_STATE_HTML); |
| 77 html_source->AddResourcePath("local_state.js", IDR_LOCAL_STATE_JS); | 116 html_source->AddResourcePath("local_state.js", IDR_LOCAL_STATE_JS); |
| 78 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source); | 117 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source); |
| 79 | 118 |
| 80 // AddMessageHandler takes ownership of LocalStateUIHandler. | 119 // AddMessageHandler takes ownership of LocalStateUIHandler. |
| 81 web_ui->AddMessageHandler(new LocalStateUIHandler); | 120 web_ui->AddMessageHandler(new LocalStateUIHandler); |
| 82 } | 121 } |
| 83 | 122 |
| 84 LocalStateUI::~LocalStateUI() { | 123 LocalStateUI::~LocalStateUI() { |
| 85 } | 124 } |
| OLD | NEW |