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

Unified 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: Fixed unittests. 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/webui/sync_internals_message_handler.cc
diff --git a/chrome/browser/ui/webui/sync_internals_message_handler.cc b/chrome/browser/ui/webui/sync_internals_message_handler.cc
index eab283ce9c0900b839ff0dc880bc56ffa0c1c0f0..5aee0a628ffb7d93bc2e00178fd70e47d2db5919 100644
--- a/chrome/browser/ui/webui/sync_internals_message_handler.cc
+++ b/chrome/browser/ui/webui/sync_internals_message_handler.cc
@@ -4,33 +4,55 @@
#include "chrome/browser/ui/webui/sync_internals_message_handler.h"
+#include <stdint.h>
+
#include <utility>
#include <vector>
+#include "base/feature_list.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
+#include "base/strings/string_number_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
+#include "chrome/browser/sync/user_event_service_factory.h"
#include "chrome/common/channel_info.h"
#include "components/browser_sync/profile_sync_service.h"
#include "components/signin/core/browser/signin_manager_base.h"
#include "components/sync/base/weak_handle.h"
#include "components/sync/driver/about_sync_util.h"
+#include "components/sync/driver/sync_driver_switches.h"
#include "components/sync/driver/sync_service.h"
#include "components/sync/engine/cycle/commit_counters.h"
#include "components/sync/engine/cycle/status_counters.h"
#include "components/sync/engine/cycle/update_counters.h"
#include "components/sync/engine/events/protocol_event.h"
#include "components/sync/js/js_event_details.h"
+#include "components/sync/protocol/sync.pb.h"
+#include "components/sync/user_events/user_event_service.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_ui.h"
+using base::Value;
using browser_sync::ProfileSyncService;
using syncer::JsEventDetails;
using syncer::ModelTypeSet;
using syncer::WeakHandle;
namespace {
+
+// Converts the string at |index| in |list| to an int, defaulting to 0 on error.
+int64_t StringAtIndexToInt64(const base::ListValue* list, int index) {
+ std::string str;
+ if (list->GetString(index, &str)) {
+ int64_t integer = 0;
+ if (base::StringToInt64(str, &integer)) {
+ return integer;
+ }
Dan Beam 2017/05/16 22:40:54 no curlies
skym 2017/05/22 21:55:48 How strongly do you feel about this? It seems that
+ }
+ return 0;
+}
+
class UtilAboutSyncDataExtractor : public AboutSyncDataExtractor {
public:
std::unique_ptr<base::DictionaryValue> ConstructAboutInformation(
@@ -40,6 +62,7 @@ class UtilAboutSyncDataExtractor : public AboutSyncDataExtractor {
service, signin, chrome::GetChannel());
}
};
+
} // namespace
SyncInternalsMessageHandler::SyncInternalsMessageHandler()
@@ -93,11 +116,23 @@ void SyncInternalsMessageHandler::RegisterMessages() {
syncer::sync_ui_util::kGetAllNodes,
base::Bind(&SyncInternalsMessageHandler::HandleGetAllNodes,
base::Unretained(this)));
+
+ web_ui()->RegisterMessageCallback(
+ syncer::sync_ui_util::kRequestUserEventsVisibility,
+ base::Bind(
+ &SyncInternalsMessageHandler::HandleRequestUserEventsVisibility,
+ base::Unretained(this)));
+
+ web_ui()->RegisterMessageCallback(
+ syncer::sync_ui_util::kWriteUserEvent,
+ base::Bind(&SyncInternalsMessageHandler::HandleWriteUserEvent,
+ base::Unretained(this)));
}
void SyncInternalsMessageHandler::HandleRegisterForEvents(
const base::ListValue* args) {
DCHECK(args->empty());
+ AllowJavascript();
// is_registered_ flag protects us from double-registering. This could
// happen on a page refresh, where the JavaScript gets re-run but the
@@ -115,6 +150,7 @@ void SyncInternalsMessageHandler::HandleRegisterForEvents(
void SyncInternalsMessageHandler::HandleRegisterForPerTypeCounters(
const base::ListValue* args) {
DCHECK(args->empty());
+ AllowJavascript();
if (ProfileSyncService* service = GetProfileSyncService()) {
if (!is_registered_for_counters_) {
@@ -131,12 +167,15 @@ void SyncInternalsMessageHandler::HandleRegisterForPerTypeCounters(
void SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo(
const base::ListValue* args) {
DCHECK(args->empty());
+ AllowJavascript();
SendAboutInfo();
}
void SyncInternalsMessageHandler::HandleRequestListOfTypes(
const base::ListValue* args) {
DCHECK(args->empty());
+ AllowJavascript();
+
base::DictionaryValue event_details;
std::unique_ptr<base::ListValue> type_list(new base::ListValue());
ModelTypeSet protocol_types = syncer::ProtocolTypes();
@@ -145,14 +184,15 @@ void SyncInternalsMessageHandler::HandleRequestListOfTypes(
type_list->AppendString(ModelTypeToString(it.Get()));
}
event_details.Set(syncer::sync_ui_util::kTypes, type_list.release());
- web_ui()->CallJavascriptFunctionUnsafe(
- syncer::sync_ui_util::kDispatchEvent,
- base::Value(syncer::sync_ui_util::kOnReceivedListOfTypes), event_details);
+ TryCallDispatch(Value(syncer::sync_ui_util::kOnReceivedListOfTypes),
+ event_details);
}
void SyncInternalsMessageHandler::HandleGetAllNodes(
const base::ListValue* args) {
DCHECK_EQ(1U, args->GetSize());
+ AllowJavascript();
+
int request_id = 0;
bool success = ExtractIntegerValue(args, &request_id);
DCHECK(success);
@@ -165,12 +205,37 @@ void SyncInternalsMessageHandler::HandleGetAllNodes(
}
}
+void SyncInternalsMessageHandler::HandleRequestUserEventsVisibility(
+ const base::ListValue* args) {
+ DCHECK(args->empty());
+ AllowJavascript();
+ CallJavascriptFunction(
+ syncer::sync_ui_util::kUserEventsVisibilityCallback,
+ Value(base::FeatureList::IsEnabled(switches::kSyncUserEvents)));
+}
+
+void SyncInternalsMessageHandler::HandleWriteUserEvent(
+ const base::ListValue* args) {
+ DCHECK_EQ(2U, args->GetSize());
+ AllowJavascript();
+
+ Profile* profile = Profile::FromWebUI(web_ui());
+ syncer::UserEventService* user_event_service =
+ browser_sync::UserEventServiceFactory::GetForProfile(
+ profile->GetOriginalProfile());
+
+ sync_pb::UserEventSpecifics event_specifics;
+ event_specifics.set_event_time_usec(StringAtIndexToInt64(args, 0));
+ event_specifics.set_navigation_id(StringAtIndexToInt64(args, 1));
+ user_event_service->RecordUserEvent(event_specifics);
+}
+
void SyncInternalsMessageHandler::OnReceivedAllNodes(
int request_id,
std::unique_ptr<base::ListValue> nodes) {
- base::Value id(request_id);
- web_ui()->CallJavascriptFunctionUnsafe(
- syncer::sync_ui_util::kGetAllNodesCallback, id, *nodes);
+ Value id(request_id);
+ CallJavascriptFunction(syncer::sync_ui_util::kGetAllNodesCallback, id,
+ *nodes);
}
void SyncInternalsMessageHandler::OnStateChanged(syncer::SyncService* sync) {
@@ -181,9 +246,7 @@ void SyncInternalsMessageHandler::OnProtocolEvent(
const syncer::ProtocolEvent& event) {
std::unique_ptr<base::DictionaryValue> value(
syncer::ProtocolEvent::ToValue(event));
- web_ui()->CallJavascriptFunctionUnsafe(
- syncer::sync_ui_util::kDispatchEvent,
- base::Value(syncer::sync_ui_util::kOnProtocolEvent), *value);
+ TryCallDispatch(Value(syncer::sync_ui_util::kOnProtocolEvent), *value);
}
void SyncInternalsMessageHandler::OnCommitCountersUpdated(
@@ -212,18 +275,17 @@ void SyncInternalsMessageHandler::EmitCounterUpdate(
details->SetString(syncer::sync_ui_util::kModelType, ModelTypeToString(type));
details->SetString(syncer::sync_ui_util::kCounterType, counter_type);
details->Set(syncer::sync_ui_util::kCounters, value.release());
- web_ui()->CallJavascriptFunctionUnsafe(
- syncer::sync_ui_util::kDispatchEvent,
- base::Value(syncer::sync_ui_util::kOnCountersUpdated), *details);
+ TryCallDispatch(Value(syncer::sync_ui_util::kOnCountersUpdated), *details);
}
void SyncInternalsMessageHandler::HandleJsEvent(
const std::string& name,
const JsEventDetails& details) {
+ AllowJavascript();
+
DVLOG(1) << "Handling event: " << name
<< " with details " << details.ToString();
- web_ui()->CallJavascriptFunctionUnsafe(syncer::sync_ui_util::kDispatchEvent,
- base::Value(name), details.Get());
+ TryCallDispatch(Value(name), details.Get());
Dan Beam 2017/05/16 22:40:54 i don't really see how doing AllowJavascript() the
skym 2017/05/22 21:55:48 Whoops, that was sloppy of me, thanks for catching
}
void SyncInternalsMessageHandler::SendAboutInfo() {
@@ -232,9 +294,7 @@ void SyncInternalsMessageHandler::SendAboutInfo() {
std::unique_ptr<base::DictionaryValue> value =
about_sync_data_extractor_->ConstructAboutInformation(sync_service,
signin);
- web_ui()->CallJavascriptFunctionUnsafe(
- syncer::sync_ui_util::kDispatchEvent,
- base::Value(syncer::sync_ui_util::kOnAboutInfoUpdated), *value);
+ TryCallDispatch(Value(syncer::sync_ui_util::kOnAboutInfoUpdated), *value);
}
// Gets the ProfileSyncService of the underlying original profile.
@@ -244,3 +304,11 @@ ProfileSyncService* SyncInternalsMessageHandler::GetProfileSyncService() {
return ProfileSyncServiceFactory::GetForProfile(
profile->GetOriginalProfile());
}
+
+void SyncInternalsMessageHandler::TryCallDispatch(const Value& name_value,
+ const Value& details_value) {
+ if (IsJavascriptAllowed()) {
Dan Beam 2017/05/16 22:40:54 this does work but maybe it'd be easier just to di
skym 2017/05/22 21:55:48 Done in https://codereview.chromium.org/2898723003
+ CallJavascriptFunction(syncer::sync_ui_util::kDispatchEvent, name_value,
+ details_value);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698