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

Side by Side Diff: chrome/browser/ui/webui/local_state/local_state_ui.cc

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

Powered by Google App Engine
This is Rietveld 408576698