Commit 57106083 authored by Marc Treib's avatar Marc Treib Committed by Commit Bot

Update iOS version of SyncInternalsMessageHandler

This adds message handlers for a few things that were added
semi-recently to the non-iOS version.
Also includes a few tiny cleanups to the non-iOS message handler.

Bug: 898439
Change-Id: I38dc6d643c1b9ad6fd3608d8a4ed799385d7d946
Reviewed-on: https://chromium-review.googlesource.com/c/1315209
Commit-Queue: Marc Treib <treib@chromium.org>
Reviewed-by: default avatarMohamed Amir Yosef <mamir@chromium.org>
Cr-Commit-Position: refs/heads/master@{#605308}
parent ed34d0a0
......@@ -4,11 +4,10 @@
#include "chrome/browser/ui/webui/sync_internals_message_handler.h"
#include <stdint.h>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
......@@ -17,9 +16,7 @@
#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/sync/base/enum_set.h"
#include "components/sync/base/model_type.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"
......@@ -36,11 +33,7 @@
using base::DictionaryValue;
using base::ListValue;
using base::Value;
using browser_sync::ProfileSyncService;
using syncer::JsEventDetails;
using syncer::ModelTypeSet;
using syncer::SyncService;
using syncer::WeakHandle;
namespace {
......@@ -224,7 +217,7 @@ void SyncInternalsMessageHandler::HandleRequestListOfTypes(
DictionaryValue event_details;
auto type_list = std::make_unique<ListValue>();
ModelTypeSet protocol_types = syncer::ProtocolTypes();
syncer::ModelTypeSet protocol_types = syncer::ProtocolTypes();
for (syncer::ModelType type : protocol_types) {
type_list->AppendString(ModelTypeToString(type));
}
......@@ -404,7 +397,7 @@ void SyncInternalsMessageHandler::EmitCounterUpdate(
void SyncInternalsMessageHandler::HandleJsEvent(
const std::string& name,
const JsEventDetails& details) {
const syncer::JsEventDetails& details) {
DVLOG(1) << "Handling event: " << name
<< " with details " << details.ToString();
DispatchEvent(name, details.Get());
......
......@@ -8,10 +8,8 @@
#include <memory>
#include <string>
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observer.h"
#include "base/values.h"
#include "components/sync/driver/sync_service_observer.h"
#include "components/sync/engine/cycle/type_debug_info_observer.h"
......@@ -21,10 +19,6 @@
#include "components/version_info/channel.h"
#include "content/public/browser/web_ui_message_handler.h"
namespace browser_sync {
class ProfileSyncService;
} // namespace browser_sync
namespace syncer {
class SyncService;
} // namespace syncer
......
......@@ -7,11 +7,13 @@
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/logging.h"
#include "base/values.h"
#include "components/browser_sync/profile_sync_service.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"
......@@ -24,18 +26,25 @@
#include "ios/web/public/web_thread.h"
#include "ios/web/public/webui/web_ui_ios.h"
using syncer::JsEventDetails;
using syncer::ModelTypeSet;
using syncer::WeakHandle;
namespace {
// Returns the initial state of the "include specifics" flag, based on whether
// or not the corresponding command-line switch is set.
bool GetIncludeSpecificsInitialState() {
return base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kSyncIncludeSpecificsInProtocolLog);
}
} // namespace
SyncInternalsMessageHandler::SyncInternalsMessageHandler()
: is_registered_(false),
is_registered_for_counters_(false),
: include_specifics_(GetIncludeSpecificsInitialState()),
weak_ptr_factory_(this) {}
SyncInternalsMessageHandler::~SyncInternalsMessageHandler() {
if (js_controller_)
if (js_controller_) {
js_controller_->RemoveJsEventHandler(this);
}
syncer::SyncService* service = GetSyncService();
if (service && service->HasObserver(this)) {
......@@ -75,8 +84,9 @@ void SyncInternalsMessageHandler::RegisterMessages() {
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
syncer::sync_ui_util::kGetAllNodes,
base::BindRepeating(&SyncInternalsMessageHandler::HandleGetAllNodes,
syncer::sync_ui_util::kRequestIncludeSpecificsInitialState,
base::BindRepeating(&SyncInternalsMessageHandler::
HandleRequestIncludeSpecificsInitialState,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
......@@ -84,6 +94,33 @@ void SyncInternalsMessageHandler::RegisterMessages() {
base::BindRepeating(
&SyncInternalsMessageHandler::HandleSetIncludeSpecifics,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
syncer::sync_ui_util::kRequestStart,
base::BindRepeating(&SyncInternalsMessageHandler::HandleRequestStart,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
syncer::sync_ui_util::kRequestStopKeepData,
base::BindRepeating(
&SyncInternalsMessageHandler::HandleRequestStopKeepData,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
syncer::sync_ui_util::kRequestStopClearData,
base::BindRepeating(
&SyncInternalsMessageHandler::HandleRequestStopClearData,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
syncer::sync_ui_util::kTriggerRefresh,
base::BindRepeating(&SyncInternalsMessageHandler::HandleTriggerRefresh,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
syncer::sync_ui_util::kGetAllNodes,
base::BindRepeating(&SyncInternalsMessageHandler::HandleGetAllNodes,
base::Unretained(this)));
}
void SyncInternalsMessageHandler::HandleRegisterForEvents(
......@@ -129,15 +166,25 @@ void SyncInternalsMessageHandler::HandleRequestListOfTypes(
const base::ListValue* args) {
DCHECK(args->empty());
base::DictionaryValue event_details;
std::unique_ptr<base::ListValue> type_list(new base::ListValue());
ModelTypeSet protocol_types = syncer::ProtocolTypes();
auto type_list = std::make_unique<base::ListValue>();
syncer::ModelTypeSet protocol_types = syncer::ProtocolTypes();
for (syncer::ModelType type : protocol_types) {
type_list->AppendString(ModelTypeToString(type));
}
event_details.Set(syncer::sync_ui_util::kTypes, std::move(type_list));
web_ui()->CallJavascriptFunction(
syncer::sync_ui_util::kDispatchEvent,
base::Value(syncer::sync_ui_util::kOnReceivedListOfTypes), event_details);
DispatchEvent(syncer::sync_ui_util::kOnReceivedListOfTypes, event_details);
}
void SyncInternalsMessageHandler::HandleRequestIncludeSpecificsInitialState(
const base::ListValue* args) {
DCHECK(args->empty());
base::DictionaryValue value;
value.SetBoolean(syncer::sync_ui_util::kIncludeSpecifics,
GetIncludeSpecificsInitialState());
DispatchEvent(syncer::sync_ui_util::kOnReceivedIncludeSpecificsInitialState,
value);
}
void SyncInternalsMessageHandler::HandleGetAllNodes(
......@@ -161,6 +208,59 @@ void SyncInternalsMessageHandler::HandleSetIncludeSpecifics(
include_specifics_ = args->GetList()[0].GetBool();
}
void SyncInternalsMessageHandler::HandleRequestStart(
const base::ListValue* args) {
DCHECK_EQ(0U, args->GetSize());
syncer::SyncService* service = GetSyncService();
if (!service) {
return;
}
service->RequestStart();
// If the service was previously stopped with CLEAR_DATA, then the
// "first-setup-complete" bit was also cleared, and now the service wouldn't
// fully start up. So set that too.
service->SetFirstSetupComplete();
}
void SyncInternalsMessageHandler::HandleRequestStopKeepData(
const base::ListValue* args) {
DCHECK_EQ(0U, args->GetSize());
syncer::SyncService* service = GetSyncService();
if (!service) {
return;
}
service->RequestStop(syncer::SyncService::KEEP_DATA);
}
void SyncInternalsMessageHandler::HandleRequestStopClearData(
const base::ListValue* args) {
DCHECK_EQ(0U, args->GetSize());
syncer::SyncService* service = GetSyncService();
if (!service) {
return;
}
service->RequestStop(syncer::SyncService::CLEAR_DATA);
}
void SyncInternalsMessageHandler::HandleTriggerRefresh(
const base::ListValue* args) {
syncer::SyncService* service = GetSyncService();
if (!service) {
return;
}
// Only allowed to trigger refresh/schedule nudges for protocol types, things
// like PROXY_TABS are not allowed.
service->TriggerRefresh(syncer::Intersection(service->GetActiveDataTypes(),
syncer::ProtocolTypes()));
}
void SyncInternalsMessageHandler::OnReceivedAllNodes(
int request_id,
std::unique_ptr<base::ListValue> nodes) {
......@@ -177,9 +277,7 @@ void SyncInternalsMessageHandler::OnProtocolEvent(
const syncer::ProtocolEvent& event) {
std::unique_ptr<base::DictionaryValue> value(
syncer::ProtocolEvent::ToValue(event, include_specifics_));
web_ui()->CallJavascriptFunction(
syncer::sync_ui_util::kDispatchEvent,
base::Value(syncer::sync_ui_util::kOnProtocolEvent), *value);
DispatchEvent(syncer::sync_ui_util::kOnProtocolEvent, *value);
}
void SyncInternalsMessageHandler::OnCommitCountersUpdated(
......@@ -208,17 +306,15 @@ 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, std::move(value));
web_ui()->CallJavascriptFunction(
syncer::sync_ui_util::kDispatchEvent,
base::Value(syncer::sync_ui_util::kOnCountersUpdated), *details);
DispatchEvent(syncer::sync_ui_util::kOnCountersUpdated, *details);
}
void SyncInternalsMessageHandler::HandleJsEvent(const std::string& name,
const JsEventDetails& details) {
void SyncInternalsMessageHandler::HandleJsEvent(
const std::string& name,
const syncer::JsEventDetails& details) {
DVLOG(1) << "Handling event: " << name << " with details "
<< details.ToString();
web_ui()->CallJavascriptFunction(syncer::sync_ui_util::kDispatchEvent,
base::Value(name), details.Get());
DispatchEvent(name, details.Get());
}
void SyncInternalsMessageHandler::SendAboutInfo() {
......@@ -226,9 +322,7 @@ void SyncInternalsMessageHandler::SendAboutInfo() {
std::unique_ptr<base::DictionaryValue> value =
syncer::sync_ui_util::ConstructAboutInformation(sync_service,
GetChannel());
web_ui()->CallJavascriptFunction(
syncer::sync_ui_util::kDispatchEvent,
base::Value(syncer::sync_ui_util::kOnAboutInfoUpdated), *value);
DispatchEvent(syncer::sync_ui_util::kOnAboutInfoUpdated, *value);
}
// Gets the SyncService of the underlying original profile. May return null.
......@@ -238,3 +332,10 @@ syncer::SyncService* SyncInternalsMessageHandler::GetSyncService() {
return ProfileSyncServiceFactory::GetForBrowserState(
browser_state->GetOriginalChromeBrowserState());
}
void SyncInternalsMessageHandler::DispatchEvent(
const std::string& name,
const base::Value& details_value) {
web_ui()->CallJavascriptFunction(syncer::sync_ui_util::kDispatchEvent,
base::Value(name), details_value);
}
......@@ -10,6 +10,7 @@
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "components/sync/driver/sync_service_observer.h"
#include "components/sync/engine/cycle/type_debug_info_observer.h"
#include "components/sync/engine/events/protocol_event_observer.h"
......@@ -17,10 +18,6 @@
#include "components/sync/js/js_event_handler.h"
#include "ios/web/public/webui/web_ui_ios_message_handler.h"
namespace base {
class DictionaryValue;
} // namespace base
namespace syncer {
class SyncService;
} // namespace syncer
......@@ -46,9 +43,12 @@ class SyncInternalsMessageHandler : public web::WebUIIOSMessageHandler,
// Fires an event to send updated info back to the page.
void HandleRequestUpdatedAboutInfo(const base::ListValue* args);
// Fires and event to send the list of types back to the page.
// Fires an event to send the list of types back to the page.
void HandleRequestListOfTypes(const base::ListValue* args);
// Fires an event to send the initial state of the "include specifics" flag.
void HandleRequestIncludeSpecificsInitialState(const base::ListValue* args);
// Handler for getAllNodes message. Needs a |request_id| argument.
void HandleGetAllNodes(const base::ListValue* args);
......@@ -56,6 +56,18 @@ class SyncInternalsMessageHandler : public web::WebUIIOSMessageHandler,
// protocol events when sent to be displayed.
void HandleSetIncludeSpecifics(const base::ListValue* args);
// Handler for requestStart message.
void HandleRequestStart(const base::ListValue* args);
// Handler for requestStopKeepData message.
void HandleRequestStopKeepData(const base::ListValue* args);
// Handler for requestStopClearData message.
void HandleRequestStopClearData(const base::ListValue* args);
// Handler for triggerRefresh message.
void HandleTriggerRefresh(const base::ListValue* args);
// syncer::JsEventHandler implementation.
void HandleJsEvent(const std::string& name,
const syncer::JsEventDetails& details) override;
......@@ -94,14 +106,16 @@ class SyncInternalsMessageHandler : public web::WebUIIOSMessageHandler,
syncer::SyncService* GetSyncService();
void DispatchEvent(const std::string& name, const base::Value& details_value);
base::WeakPtr<syncer::JsController> js_controller_;
// A flag used to prevent double-registration with ProfileSyncService.
bool is_registered_;
bool is_registered_ = false;
// A flag used to prevent double-registration as TypeDebugInfoObserver with
// ProfileSyncService.
bool is_registered_for_counters_;
bool is_registered_for_counters_ = false;
// Whether specifics should be included when converting protocol events to a
// human readable format.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment