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

Side by Side Diff: chrome/browser/ui/webui/sync_internals_message_handler.cc

Issue 2872023002: [Sync] Add a simple UI to sync-internals to create UserEvents. (Closed)
Patch Set: Self review. Created 3 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/sync_internals_message_handler.h" 5 #include "chrome/browser/ui/webui/sync_internals_message_handler.h"
6 6
7 #include <stdint.h>
8
7 #include <utility> 9 #include <utility>
8 #include <vector> 10 #include <vector>
9 11
12 #include "base/feature_list.h"
10 #include "base/logging.h" 13 #include "base/logging.h"
11 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/strings/string_number_conversions.h"
12 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/sync/profile_sync_service_factory.h" 17 #include "chrome/browser/sync/profile_sync_service_factory.h"
18 #include "chrome/browser/sync/user_event_service_factory.h"
14 #include "chrome/common/channel_info.h" 19 #include "chrome/common/channel_info.h"
15 #include "components/browser_sync/profile_sync_service.h" 20 #include "components/browser_sync/profile_sync_service.h"
16 #include "components/signin/core/browser/signin_manager_base.h" 21 #include "components/signin/core/browser/signin_manager_base.h"
17 #include "components/sync/base/weak_handle.h" 22 #include "components/sync/base/weak_handle.h"
18 #include "components/sync/driver/about_sync_util.h" 23 #include "components/sync/driver/about_sync_util.h"
24 #include "components/sync/driver/sync_driver_switches.h"
19 #include "components/sync/driver/sync_service.h" 25 #include "components/sync/driver/sync_service.h"
20 #include "components/sync/engine/cycle/commit_counters.h" 26 #include "components/sync/engine/cycle/commit_counters.h"
21 #include "components/sync/engine/cycle/status_counters.h" 27 #include "components/sync/engine/cycle/status_counters.h"
22 #include "components/sync/engine/cycle/update_counters.h" 28 #include "components/sync/engine/cycle/update_counters.h"
23 #include "components/sync/engine/events/protocol_event.h" 29 #include "components/sync/engine/events/protocol_event.h"
24 #include "components/sync/js/js_event_details.h" 30 #include "components/sync/js/js_event_details.h"
31 #include "components/sync/protocol/sync.pb.h"
32 #include "components/sync/user_events/user_event_service.h"
25 #include "content/public/browser/browser_thread.h" 33 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/web_ui.h" 34 #include "content/public/browser/web_ui.h"
27 35
36 using base::Value;
28 using browser_sync::ProfileSyncService; 37 using browser_sync::ProfileSyncService;
29 using syncer::JsEventDetails; 38 using syncer::JsEventDetails;
30 using syncer::ModelTypeSet; 39 using syncer::ModelTypeSet;
31 using syncer::WeakHandle; 40 using syncer::WeakHandle;
32 41
33 namespace { 42 namespace {
43
44 // Converts the string at |index| in |list| to an int, defaulting to 0 on error.
45 int64_t IndexToInt64(const base::ListValue* list, int index) {
Patrick Noland 2017/05/09 23:46:23 From the name I'd expect this to convert an index,
skym 2017/05/10 00:06:46 Done.
46 std::string str;
47 if (list->GetString(index, &str)) {
48 int64_t integer = 0;
49 if (base::StringToInt64(str, &integer)) {
50 return integer;
51 }
52 }
53 return 0;
Patrick Noland 2017/05/09 23:46:23 So if you fat-finger one of the inputs the value w
skym 2017/05/10 00:06:46 I think feedback in the UI that they've entered a
54 }
55
34 class UtilAboutSyncDataExtractor : public AboutSyncDataExtractor { 56 class UtilAboutSyncDataExtractor : public AboutSyncDataExtractor {
35 public: 57 public:
36 std::unique_ptr<base::DictionaryValue> ConstructAboutInformation( 58 std::unique_ptr<base::DictionaryValue> ConstructAboutInformation(
37 syncer::SyncService* service, 59 syncer::SyncService* service,
38 SigninManagerBase* signin) override { 60 SigninManagerBase* signin) override {
39 return syncer::sync_ui_util::ConstructAboutInformation( 61 return syncer::sync_ui_util::ConstructAboutInformation(
40 service, signin, chrome::GetChannel()); 62 service, signin, chrome::GetChannel());
41 } 63 }
42 }; 64 };
65
43 } // namespace 66 } // namespace
44 67
45 SyncInternalsMessageHandler::SyncInternalsMessageHandler() 68 SyncInternalsMessageHandler::SyncInternalsMessageHandler()
46 : SyncInternalsMessageHandler( 69 : SyncInternalsMessageHandler(
47 base::MakeUnique<UtilAboutSyncDataExtractor>()) {} 70 base::MakeUnique<UtilAboutSyncDataExtractor>()) {}
48 71
49 SyncInternalsMessageHandler::SyncInternalsMessageHandler( 72 SyncInternalsMessageHandler::SyncInternalsMessageHandler(
50 std::unique_ptr<AboutSyncDataExtractor> about_sync_data_extractor) 73 std::unique_ptr<AboutSyncDataExtractor> about_sync_data_extractor)
51 : about_sync_data_extractor_(std::move(about_sync_data_extractor)), 74 : about_sync_data_extractor_(std::move(about_sync_data_extractor)),
52 weak_ptr_factory_(this) {} 75 weak_ptr_factory_(this) {}
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 109
87 web_ui()->RegisterMessageCallback( 110 web_ui()->RegisterMessageCallback(
88 syncer::sync_ui_util::kRequestListOfTypes, 111 syncer::sync_ui_util::kRequestListOfTypes,
89 base::Bind(&SyncInternalsMessageHandler::HandleRequestListOfTypes, 112 base::Bind(&SyncInternalsMessageHandler::HandleRequestListOfTypes,
90 base::Unretained(this))); 113 base::Unretained(this)));
91 114
92 web_ui()->RegisterMessageCallback( 115 web_ui()->RegisterMessageCallback(
93 syncer::sync_ui_util::kGetAllNodes, 116 syncer::sync_ui_util::kGetAllNodes,
94 base::Bind(&SyncInternalsMessageHandler::HandleGetAllNodes, 117 base::Bind(&SyncInternalsMessageHandler::HandleGetAllNodes,
95 base::Unretained(this))); 118 base::Unretained(this)));
119
120 web_ui()->RegisterMessageCallback(
121 syncer::sync_ui_util::kRequestUserEventsVisibility,
122 base::Bind(
123 &SyncInternalsMessageHandler::HandleRequestUserEventsVisibility,
124 base::Unretained(this)));
125
126 web_ui()->RegisterMessageCallback(
127 syncer::sync_ui_util::kWriteUserEvent,
128 base::Bind(&SyncInternalsMessageHandler::HandleWriteUserEvent,
129 base::Unretained(this)));
96 } 130 }
97 131
98 void SyncInternalsMessageHandler::HandleRegisterForEvents( 132 void SyncInternalsMessageHandler::HandleRegisterForEvents(
99 const base::ListValue* args) { 133 const base::ListValue* args) {
100 DCHECK(args->empty()); 134 DCHECK(args->empty());
101 135
102 // is_registered_ flag protects us from double-registering. This could 136 // is_registered_ flag protects us from double-registering. This could
103 // happen on a page refresh, where the JavaScript gets re-run but the 137 // happen on a page refresh, where the JavaScript gets re-run but the
104 // message handler remains unchanged. 138 // message handler remains unchanged.
105 ProfileSyncService* service = GetProfileSyncService(); 139 ProfileSyncService* service = GetProfileSyncService();
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 base::DictionaryValue event_details; 174 base::DictionaryValue event_details;
141 std::unique_ptr<base::ListValue> type_list(new base::ListValue()); 175 std::unique_ptr<base::ListValue> type_list(new base::ListValue());
142 ModelTypeSet protocol_types = syncer::ProtocolTypes(); 176 ModelTypeSet protocol_types = syncer::ProtocolTypes();
143 for (ModelTypeSet::Iterator it = protocol_types.First(); 177 for (ModelTypeSet::Iterator it = protocol_types.First();
144 it.Good(); it.Inc()) { 178 it.Good(); it.Inc()) {
145 type_list->AppendString(ModelTypeToString(it.Get())); 179 type_list->AppendString(ModelTypeToString(it.Get()));
146 } 180 }
147 event_details.Set(syncer::sync_ui_util::kTypes, type_list.release()); 181 event_details.Set(syncer::sync_ui_util::kTypes, type_list.release());
148 web_ui()->CallJavascriptFunctionUnsafe( 182 web_ui()->CallJavascriptFunctionUnsafe(
149 syncer::sync_ui_util::kDispatchEvent, 183 syncer::sync_ui_util::kDispatchEvent,
150 base::Value(syncer::sync_ui_util::kOnReceivedListOfTypes), event_details); 184 Value(syncer::sync_ui_util::kOnReceivedListOfTypes), event_details);
151 } 185 }
152 186
153 void SyncInternalsMessageHandler::HandleGetAllNodes( 187 void SyncInternalsMessageHandler::HandleGetAllNodes(
154 const base::ListValue* args) { 188 const base::ListValue* args) {
155 DCHECK_EQ(1U, args->GetSize()); 189 DCHECK_EQ(1U, args->GetSize());
156 int request_id = 0; 190 int request_id = 0;
157 bool success = ExtractIntegerValue(args, &request_id); 191 bool success = ExtractIntegerValue(args, &request_id);
158 DCHECK(success); 192 DCHECK(success);
159 193
160 ProfileSyncService* service = GetProfileSyncService(); 194 ProfileSyncService* service = GetProfileSyncService();
161 if (service) { 195 if (service) {
162 service->GetAllNodes( 196 service->GetAllNodes(
163 base::Bind(&SyncInternalsMessageHandler::OnReceivedAllNodes, 197 base::Bind(&SyncInternalsMessageHandler::OnReceivedAllNodes,
164 weak_ptr_factory_.GetWeakPtr(), request_id)); 198 weak_ptr_factory_.GetWeakPtr(), request_id));
165 } 199 }
166 } 200 }
167 201
202 void SyncInternalsMessageHandler::HandleRequestUserEventsVisibility(
203 const base::ListValue* args) {
204 DCHECK(args->empty());
205 web_ui()->CallJavascriptFunctionUnsafe(
206 syncer::sync_ui_util::kUserEventsVisibilityCallback,
207 Value(base::FeatureList::IsEnabled(switches::kSyncUserEvents)));
208 }
209
210 void SyncInternalsMessageHandler::HandleWriteUserEvent(
211 const base::ListValue* args) {
212 DCHECK_EQ(2U, args->GetSize());
213
214 Profile* profile = Profile::FromWebUI(web_ui());
215 syncer::UserEventService* user_event_service =
216 browser_sync::UserEventServiceFactory::GetForProfile(
217 profile->GetOriginalProfile());
218
219 sync_pb::UserEventSpecifics event_specifics;
220 event_specifics.set_event_time_usec(IndexToInt64(args, 0));
221 event_specifics.set_navigation_id(IndexToInt64(args, 1));
222 user_event_service->RecordUserEvent(event_specifics);
223 }
224
168 void SyncInternalsMessageHandler::OnReceivedAllNodes( 225 void SyncInternalsMessageHandler::OnReceivedAllNodes(
169 int request_id, 226 int request_id,
170 std::unique_ptr<base::ListValue> nodes) { 227 std::unique_ptr<base::ListValue> nodes) {
171 base::Value id(request_id); 228 Value id(request_id);
172 web_ui()->CallJavascriptFunctionUnsafe( 229 web_ui()->CallJavascriptFunctionUnsafe(
173 syncer::sync_ui_util::kGetAllNodesCallback, id, *nodes); 230 syncer::sync_ui_util::kGetAllNodesCallback, id, *nodes);
174 } 231 }
175 232
176 void SyncInternalsMessageHandler::OnStateChanged(syncer::SyncService* sync) { 233 void SyncInternalsMessageHandler::OnStateChanged(syncer::SyncService* sync) {
177 SendAboutInfo(); 234 SendAboutInfo();
178 } 235 }
179 236
180 void SyncInternalsMessageHandler::OnProtocolEvent( 237 void SyncInternalsMessageHandler::OnProtocolEvent(
181 const syncer::ProtocolEvent& event) { 238 const syncer::ProtocolEvent& event) {
182 std::unique_ptr<base::DictionaryValue> value( 239 std::unique_ptr<base::DictionaryValue> value(
183 syncer::ProtocolEvent::ToValue(event)); 240 syncer::ProtocolEvent::ToValue(event));
184 web_ui()->CallJavascriptFunctionUnsafe( 241 web_ui()->CallJavascriptFunctionUnsafe(
185 syncer::sync_ui_util::kDispatchEvent, 242 syncer::sync_ui_util::kDispatchEvent,
186 base::Value(syncer::sync_ui_util::kOnProtocolEvent), *value); 243 Value(syncer::sync_ui_util::kOnProtocolEvent), *value);
187 } 244 }
188 245
189 void SyncInternalsMessageHandler::OnCommitCountersUpdated( 246 void SyncInternalsMessageHandler::OnCommitCountersUpdated(
190 syncer::ModelType type, 247 syncer::ModelType type,
191 const syncer::CommitCounters& counters) { 248 const syncer::CommitCounters& counters) {
192 EmitCounterUpdate(type, syncer::sync_ui_util::kCommit, counters.ToValue()); 249 EmitCounterUpdate(type, syncer::sync_ui_util::kCommit, counters.ToValue());
193 } 250 }
194 251
195 void SyncInternalsMessageHandler::OnUpdateCountersUpdated( 252 void SyncInternalsMessageHandler::OnUpdateCountersUpdated(
196 syncer::ModelType type, 253 syncer::ModelType type,
(...skipping 10 matching lines...) Expand all
207 void SyncInternalsMessageHandler::EmitCounterUpdate( 264 void SyncInternalsMessageHandler::EmitCounterUpdate(
208 syncer::ModelType type, 265 syncer::ModelType type,
209 const std::string& counter_type, 266 const std::string& counter_type,
210 std::unique_ptr<base::DictionaryValue> value) { 267 std::unique_ptr<base::DictionaryValue> value) {
211 std::unique_ptr<base::DictionaryValue> details(new base::DictionaryValue()); 268 std::unique_ptr<base::DictionaryValue> details(new base::DictionaryValue());
212 details->SetString(syncer::sync_ui_util::kModelType, ModelTypeToString(type)); 269 details->SetString(syncer::sync_ui_util::kModelType, ModelTypeToString(type));
213 details->SetString(syncer::sync_ui_util::kCounterType, counter_type); 270 details->SetString(syncer::sync_ui_util::kCounterType, counter_type);
214 details->Set(syncer::sync_ui_util::kCounters, value.release()); 271 details->Set(syncer::sync_ui_util::kCounters, value.release());
215 web_ui()->CallJavascriptFunctionUnsafe( 272 web_ui()->CallJavascriptFunctionUnsafe(
216 syncer::sync_ui_util::kDispatchEvent, 273 syncer::sync_ui_util::kDispatchEvent,
217 base::Value(syncer::sync_ui_util::kOnCountersUpdated), *details); 274 Value(syncer::sync_ui_util::kOnCountersUpdated), *details);
218 } 275 }
219 276
220 void SyncInternalsMessageHandler::HandleJsEvent( 277 void SyncInternalsMessageHandler::HandleJsEvent(
221 const std::string& name, 278 const std::string& name,
222 const JsEventDetails& details) { 279 const JsEventDetails& details) {
223 DVLOG(1) << "Handling event: " << name 280 DVLOG(1) << "Handling event: " << name
224 << " with details " << details.ToString(); 281 << " with details " << details.ToString();
225 web_ui()->CallJavascriptFunctionUnsafe(syncer::sync_ui_util::kDispatchEvent, 282 web_ui()->CallJavascriptFunctionUnsafe(syncer::sync_ui_util::kDispatchEvent,
226 base::Value(name), details.Get()); 283 Value(name), details.Get());
227 } 284 }
228 285
229 void SyncInternalsMessageHandler::SendAboutInfo() { 286 void SyncInternalsMessageHandler::SendAboutInfo() {
230 ProfileSyncService* sync_service = GetProfileSyncService(); 287 ProfileSyncService* sync_service = GetProfileSyncService();
231 SigninManagerBase* signin = sync_service ? sync_service->signin() : nullptr; 288 SigninManagerBase* signin = sync_service ? sync_service->signin() : nullptr;
232 std::unique_ptr<base::DictionaryValue> value = 289 std::unique_ptr<base::DictionaryValue> value =
233 about_sync_data_extractor_->ConstructAboutInformation(sync_service, 290 about_sync_data_extractor_->ConstructAboutInformation(sync_service,
234 signin); 291 signin);
235 web_ui()->CallJavascriptFunctionUnsafe( 292 web_ui()->CallJavascriptFunctionUnsafe(
236 syncer::sync_ui_util::kDispatchEvent, 293 syncer::sync_ui_util::kDispatchEvent,
237 base::Value(syncer::sync_ui_util::kOnAboutInfoUpdated), *value); 294 Value(syncer::sync_ui_util::kOnAboutInfoUpdated), *value);
238 } 295 }
239 296
240 // Gets the ProfileSyncService of the underlying original profile. 297 // Gets the ProfileSyncService of the underlying original profile.
241 // May return NULL (e.g., if sync is disabled on the command line). 298 // May return NULL (e.g., if sync is disabled on the command line).
242 ProfileSyncService* SyncInternalsMessageHandler::GetProfileSyncService() { 299 ProfileSyncService* SyncInternalsMessageHandler::GetProfileSyncService() {
243 Profile* profile = Profile::FromWebUI(web_ui()); 300 Profile* profile = Profile::FromWebUI(web_ui());
244 return ProfileSyncServiceFactory::GetForProfile( 301 return ProfileSyncServiceFactory::GetForProfile(
245 profile->GetOriginalProfile()); 302 profile->GetOriginalProfile());
246 } 303 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698