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

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, 8 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/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 #if defined(OS_CHROMEOS)
Alexei Svitkine (slow) 2016/04/26 15:33:35 Nit: Add a comment above this block. e.g.: // On
hamelphi1 2016/04/26 17:42:40 Done.
24 #define ENABLE_FILTERING true
25 #else
26 #define ENABLE_FILTERING false
27 #endif // defined(OS_CHROMEOS)
28
24 // UI Handler for chrome://local-state. Displays the Local State file as JSON. 29 // UI Handler for chrome://local-state. Displays the Local State file as JSON.
25 class LocalStateUIHandler : public content::WebUIMessageHandler { 30 class LocalStateUIHandler : public content::WebUIMessageHandler {
26 public: 31 public:
27 LocalStateUIHandler(); 32 LocalStateUIHandler();
28 ~LocalStateUIHandler() override; 33 ~LocalStateUIHandler() override;
29 34
30 // content::WebUIMessageHandler: 35 // content::WebUIMessageHandler:
31 void RegisterMessages() override; 36 void RegisterMessages() override;
32 37
33 private: 38 private:
(...skipping 10 matching lines...) Expand all
44 LocalStateUIHandler::~LocalStateUIHandler() { 49 LocalStateUIHandler::~LocalStateUIHandler() {
45 } 50 }
46 51
47 void LocalStateUIHandler::RegisterMessages() { 52 void LocalStateUIHandler::RegisterMessages() {
48 web_ui()->RegisterMessageCallback( 53 web_ui()->RegisterMessageCallback(
49 "requestJson", base::Bind(&LocalStateUIHandler::HandleRequestJson, 54 "requestJson", base::Bind(&LocalStateUIHandler::HandleRequestJson,
50 base::Unretained(this))); 55 base::Unretained(this)));
51 } 56 }
52 57
53 void LocalStateUIHandler::HandleRequestJson(const base::ListValue* args) { 58 void LocalStateUIHandler::HandleRequestJson(const base::ListValue* args) {
54 #if !defined(OS_CHROMEOS)
55 std::unique_ptr<base::DictionaryValue> local_state_values( 59 std::unique_ptr<base::DictionaryValue> local_state_values(
56 g_browser_process->local_state()->GetPreferenceValuesOmitDefaults()); 60 g_browser_process->local_state()->GetPreferenceValuesOmitDefaults());
57 61 if (ENABLE_FILTERING) {
62 std::vector<std::string> whitelisted_prefixes = {
63 "variations", "user_experience_metrics", "uninstall_metrics"};
64 internal::FilterPrefs(whitelisted_prefixes, local_state_values.get());
65 }
58 std::string json; 66 std::string json;
59 JSONStringValueSerializer serializer(&json); 67 JSONStringValueSerializer serializer(&json);
60 serializer.set_pretty_print(true); 68 serializer.set_pretty_print(true);
61 bool result = serializer.Serialize(*local_state_values); 69 bool result = serializer.Serialize(*local_state_values);
62 if (!result) 70 if (!result)
63 json = "Error loading Local State file."; 71 json = "Error loading Local State file.";
64 72
65 web_ui()->CallJavascriptFunction("localState.setLocalState", 73 web_ui()->CallJavascriptFunction("localState.setLocalState",
66 base::StringValue(json)); 74 base::StringValue(json));
67 #endif // !defined(OS_CHROMEOS) 75 }
76
77 // Returns true if |pref_name| starts with one of the |valid_prefixes|.
78 bool HasValidPrefix(const std::string& pref_name,
79 const std::vector<std::string> valid_prefixes) {
80 for (const std::string& prefix : valid_prefixes) {
81 if (pref_name.compare(0, prefix.size(), prefix) == 0)
82 return true;
83 }
84 return false;
68 } 85 }
69 86
70 } // namespace 87 } // namespace
71 88
89 namespace internal {
90
91 void FilterPrefs(const std::vector<std::string>& valid_prefixes,
92 base::DictionaryValue* prefs) {
93 std::vector<std::string> prefs_to_remove;
94 for (base::DictionaryValue::Iterator it(*prefs); !it.IsAtEnd();
95 it.Advance()) {
96 if (!HasValidPrefix(it.key(), valid_prefixes))
97 prefs_to_remove.push_back(it.key());
98 }
99 for (const std::string& pref_to_remove : prefs_to_remove) {
100 std::unique_ptr<base::Value> removed_value;
101 bool successfully_removed = prefs->Remove(pref_to_remove, &removed_value);
102 DCHECK(successfully_removed);
103 }
104 }
105
106 } // namespace internal
107
72 LocalStateUI::LocalStateUI(content::WebUI* web_ui) : WebUIController(web_ui) { 108 LocalStateUI::LocalStateUI(content::WebUI* web_ui) : WebUIController(web_ui) {
73 // Set up the chrome://local-state source. 109 // Set up the chrome://local-state source.
74 content::WebUIDataSource* html_source = 110 content::WebUIDataSource* html_source =
75 content::WebUIDataSource::Create(chrome::kChromeUILocalStateHost); 111 content::WebUIDataSource::Create(chrome::kChromeUILocalStateHost);
76 html_source->SetDefaultResource(IDR_LOCAL_STATE_HTML); 112 html_source->SetDefaultResource(IDR_LOCAL_STATE_HTML);
77 html_source->AddResourcePath("local_state.js", IDR_LOCAL_STATE_JS); 113 html_source->AddResourcePath("local_state.js", IDR_LOCAL_STATE_JS);
78 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source); 114 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source);
79 115
80 // AddMessageHandler takes ownership of LocalStateUIHandler. 116 // AddMessageHandler takes ownership of LocalStateUIHandler.
81 web_ui->AddMessageHandler(new LocalStateUIHandler); 117 web_ui->AddMessageHandler(new LocalStateUIHandler);
82 } 118 }
83 119
84 LocalStateUI::~LocalStateUI() { 120 LocalStateUI::~LocalStateUI() {
85 } 121 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698