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

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

Powered by Google App Engine
This is Rietveld 408576698