summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/sync_internals_message_handler.cc
blob: 88d7e2904ad1125f7aefa6219a4bf299087ac344 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/sync_internals_message_handler.h"

#include <vector>

#include "base/logging.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/about_sync_util.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "content/public/browser/web_ui.h"
#include "sync/internal_api/public/util/weak_handle.h"
#include "sync/js/js_arg_list.h"
#include "sync/js/js_event_details.h"

using syncer::JsArgList;
using syncer::JsEventDetails;
using syncer::JsReplyHandler;
using syncer::WeakHandle;

SyncInternalsMessageHandler::SyncInternalsMessageHandler()
    : weak_ptr_factory_(this) {}

SyncInternalsMessageHandler::~SyncInternalsMessageHandler() {
  if (js_controller_)
    js_controller_->RemoveJsEventHandler(this);
}

void SyncInternalsMessageHandler::RegisterMessages() {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));

  // Init our link to the JsController.
  ProfileSyncService* service = GetProfileSyncService();
  if (service)
    js_controller_ = service->GetJsController();
  if (js_controller_)
    js_controller_->AddJsEventHandler(this);

  web_ui()->RegisterMessageCallback(
      "getAboutInfo",
      base::Bind(&SyncInternalsMessageHandler::OnGetAboutInfo,
                 base::Unretained(this)));

  RegisterJsControllerCallback("getNotificationState");
  RegisterJsControllerCallback("getNotificationInfo");
  RegisterJsControllerCallback("getRootNodeDetails");
  RegisterJsControllerCallback("getNodeSummariesById");
  RegisterJsControllerCallback("getNodeDetailsById");
  RegisterJsControllerCallback("getAllNodes");
  RegisterJsControllerCallback("getChildNodeIds");
  RegisterJsControllerCallback("getClientServerTraffic");
}

void SyncInternalsMessageHandler::OnGetAboutInfo(const base::ListValue* args) {
  // TODO(rlarocque): We should DCHECK(!args) here.
  scoped_ptr<base::DictionaryValue> value =
      sync_ui_util::ConstructAboutInformation(GetProfileSyncService());
  web_ui()->CallJavascriptFunction(
      "chrome.sync.getAboutInfo.handleReply",
      *value);
}

void SyncInternalsMessageHandler::HandleJsReply(
    const std::string& name, const JsArgList& args) {
  DVLOG(1) << "Handling reply for " << name << " message"
           << " with args " << args.ToString();
  const std::string& reply_handler = "chrome.sync." + name + ".handleReply";
  std::vector<const base::Value*> arg_list(args.Get().begin(),
                                           args.Get().end());
  web_ui()->CallJavascriptFunction(reply_handler, arg_list);
}

void SyncInternalsMessageHandler::HandleJsEvent(
    const std::string& name,
    const JsEventDetails& details) {
  DVLOG(1) << "Handling event: " << name
           << " with details " << details.ToString();
  const std::string& event_handler = "chrome.sync." + name + ".fire";
  std::vector<const base::Value*> arg_list(1, &details.Get());
  web_ui()->CallJavascriptFunction(event_handler, arg_list);
}

void SyncInternalsMessageHandler::RegisterJsControllerCallback(
    const std::string& name) {
  web_ui()->RegisterMessageCallback(
      name,
      base::Bind(&SyncInternalsMessageHandler::ForwardToJsController,
                 base::Unretained(this),
                 name));
}

void SyncInternalsMessageHandler::ForwardToJsController(
    const std::string& name,
    const base::ListValue* args) {
  if (js_controller_) {
    scoped_ptr<base::ListValue> args_copy(args->DeepCopy());
    JsArgList js_arg_list(args_copy.get());
    js_controller_->ProcessJsMessage(
        name, js_arg_list,
        MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()));
  } else {
    DLOG(WARNING) << "No sync service; dropping message " << name;
  }
}

// Gets the ProfileSyncService of the underlying original profile.
// May return NULL (e.g., if sync is disabled on the command line).
ProfileSyncService* SyncInternalsMessageHandler::GetProfileSyncService() {
  Profile* profile = Profile::FromWebUI(web_ui());
  ProfileSyncServiceFactory* factory = ProfileSyncServiceFactory::GetInstance();
  return factory->GetForProfile(profile->GetOriginalProfile());
}