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

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

Issue 162283002: Move towards event-driven JS on about:sync (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Another reupload Created 6 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/ui/webui/sync_internals_message_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <vector> 7 #include <vector>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
(...skipping 16 matching lines...) Expand all
27 : weak_ptr_factory_(this) {} 27 : weak_ptr_factory_(this) {}
28 28
29 SyncInternalsMessageHandler::~SyncInternalsMessageHandler() { 29 SyncInternalsMessageHandler::~SyncInternalsMessageHandler() {
30 if (js_controller_) 30 if (js_controller_)
31 js_controller_->RemoveJsEventHandler(this); 31 js_controller_->RemoveJsEventHandler(this);
32 } 32 }
33 33
34 void SyncInternalsMessageHandler::RegisterMessages() { 34 void SyncInternalsMessageHandler::RegisterMessages() {
35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
36 36
37 // Init our link to the JsController. 37 // Register for ProfileSyncService events.
38 ProfileSyncService* service = GetProfileSyncService(); 38 ProfileSyncService* service = GetProfileSyncService();
39 if (service) 39 if (service) {
40 service->AddObserver(this);
40 js_controller_ = service->GetJsController(); 41 js_controller_ = service->GetJsController();
41 if (js_controller_)
42 js_controller_->AddJsEventHandler(this); 42 js_controller_->AddJsEventHandler(this);
43 }
43 44
44 web_ui()->RegisterMessageCallback( 45 web_ui()->RegisterMessageCallback(
45 "getAboutInfo", 46 "requestUpdatedAboutInfo",
46 base::Bind(&SyncInternalsMessageHandler::OnGetAboutInfo, 47 base::Bind(&SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo,
47 base::Unretained(this))); 48 base::Unretained(this)));
48 49
49 web_ui()->RegisterMessageCallback( 50 web_ui()->RegisterMessageCallback(
50 "getListOfTypes", 51 "requestListOfTypes",
51 base::Bind(&SyncInternalsMessageHandler::OnGetListOfTypes, 52 base::Bind(&SyncInternalsMessageHandler::HandleRequestListOfTypes,
52 base::Unretained(this))); 53 base::Unretained(this)));
53 54
54 RegisterJsControllerCallback("getNotificationState"); 55 RegisterJsControllerCallback("getNotificationState");
55 RegisterJsControllerCallback("getNotificationInfo"); 56 RegisterJsControllerCallback("getNotificationInfo");
56 RegisterJsControllerCallback("getAllNodes"); 57 RegisterJsControllerCallback("getAllNodes");
57 RegisterJsControllerCallback("getClientServerTraffic"); 58 RegisterJsControllerCallback("getClientServerTraffic");
58 } 59 }
59 60
60 void SyncInternalsMessageHandler::OnGetAboutInfo(const base::ListValue* args) { 61 void SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo(
61 // TODO(rlarocque): We should DCHECK(!args) here. See crbug.com/334431. 62 const base::ListValue* args) {
62 scoped_ptr<base::DictionaryValue> value = 63 DCHECK(args->empty());
63 sync_ui_util::ConstructAboutInformation(GetProfileSyncService()); 64 SendAboutInfo();
64 web_ui()->CallJavascriptFunction(
65 "chrome.sync.getAboutInfo.handleReply",
66 *value);
67 } 65 }
68 66
69 void SyncInternalsMessageHandler::OnGetListOfTypes( 67 void SyncInternalsMessageHandler::HandleRequestListOfTypes(
70 const base::ListValue* args) { 68 const base::ListValue* args) {
71 // TODO(rlarocque): We should DCHECK(!args) here. See crbug.com/334431. 69 DCHECK(args->empty());
72 base::ListValue type_list; 70 base::DictionaryValue event_details;
71 scoped_ptr<base::ListValue> type_list(new base::ListValue());
73 ModelTypeSet protocol_types = syncer::ProtocolTypes(); 72 ModelTypeSet protocol_types = syncer::ProtocolTypes();
74 for (ModelTypeSet::Iterator it = protocol_types.First(); 73 for (ModelTypeSet::Iterator it = protocol_types.First();
75 it.Good(); it.Inc()) { 74 it.Good(); it.Inc()) {
76 type_list.Append(new base::StringValue(ModelTypeToString(it.Get()))); 75 type_list->Append(new base::StringValue(ModelTypeToString(it.Get())));
77 } 76 }
77 event_details.Set("types", type_list.release());
78 web_ui()->CallJavascriptFunction( 78 web_ui()->CallJavascriptFunction(
79 "chrome.sync.getListOfTypes.handleReply", 79 "chrome.sync.dispatchEvent",
80 type_list); 80 base::StringValue("onReceivedListOfTypes"),
81 event_details);
81 } 82 }
82 83
83 void SyncInternalsMessageHandler::HandleJsReply( 84 void SyncInternalsMessageHandler::HandleJsReply(
84 const std::string& name, const JsArgList& args) { 85 const std::string& name, const JsArgList& args) {
85 DVLOG(1) << "Handling reply for " << name << " message" 86 DVLOG(1) << "Handling reply for " << name << " message"
86 << " with args " << args.ToString(); 87 << " with args " << args.ToString();
87 const std::string& reply_handler = "chrome.sync." + name + ".handleReply"; 88 const std::string& reply_handler = "chrome.sync." + name + ".handleReply";
88 std::vector<const base::Value*> arg_list(args.Get().begin(), 89 std::vector<const base::Value*> arg_list(args.Get().begin(),
89 args.Get().end()); 90 args.Get().end());
90 web_ui()->CallJavascriptFunction(reply_handler, arg_list); 91 web_ui()->CallJavascriptFunction(reply_handler, arg_list);
91 } 92 }
92 93
94 void SyncInternalsMessageHandler::OnStateChanged() {
95 SendAboutInfo();
96 }
97
93 void SyncInternalsMessageHandler::HandleJsEvent( 98 void SyncInternalsMessageHandler::HandleJsEvent(
94 const std::string& name, 99 const std::string& name,
95 const JsEventDetails& details) { 100 const JsEventDetails& details) {
96 DVLOG(1) << "Handling event: " << name 101 DVLOG(1) << "Handling event: " << name
97 << " with details " << details.ToString(); 102 << " with details " << details.ToString();
98 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent", 103 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent",
99 base::StringValue(name), 104 base::StringValue(name),
100 details.Get()); 105 details.Get());
101 } 106 }
102 107
103 void SyncInternalsMessageHandler::RegisterJsControllerCallback( 108 void SyncInternalsMessageHandler::RegisterJsControllerCallback(
104 const std::string& name) { 109 const std::string& name) {
105 web_ui()->RegisterMessageCallback( 110 web_ui()->RegisterMessageCallback(
106 name, 111 name,
107 base::Bind(&SyncInternalsMessageHandler::ForwardToJsController, 112 base::Bind(&SyncInternalsMessageHandler::ForwardToJsController,
108 base::Unretained(this), 113 base::Unretained(this),
109 name)); 114 name));
110 } 115 }
111 116
117 void SyncInternalsMessageHandler::SendAboutInfo() {
118 scoped_ptr<base::DictionaryValue> value =
119 sync_ui_util::ConstructAboutInformation(GetProfileSyncService());
120 web_ui()->CallJavascriptFunction(
121 "chrome.sync.dispatchEvent",
122 base::StringValue("onAboutInfoUpdated"),
123 *value);
124 }
125
112 void SyncInternalsMessageHandler::ForwardToJsController( 126 void SyncInternalsMessageHandler::ForwardToJsController(
113 const std::string& name, 127 const std::string& name,
114 const base::ListValue* args) { 128 const base::ListValue* args) {
115 if (js_controller_) { 129 if (js_controller_) {
116 scoped_ptr<base::ListValue> args_copy(args->DeepCopy()); 130 scoped_ptr<base::ListValue> args_copy(args->DeepCopy());
117 JsArgList js_arg_list(args_copy.get()); 131 JsArgList js_arg_list(args_copy.get());
118 js_controller_->ProcessJsMessage( 132 js_controller_->ProcessJsMessage(
119 name, js_arg_list, 133 name, js_arg_list,
120 MakeWeakHandle(weak_ptr_factory_.GetWeakPtr())); 134 MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()));
121 } else { 135 } else {
122 DLOG(WARNING) << "No sync service; dropping message " << name; 136 DLOG(WARNING) << "No sync service; dropping message " << name;
123 } 137 }
124 } 138 }
125 139
126 // Gets the ProfileSyncService of the underlying original profile. 140 // Gets the ProfileSyncService of the underlying original profile.
127 // May return NULL (e.g., if sync is disabled on the command line). 141 // May return NULL (e.g., if sync is disabled on the command line).
128 ProfileSyncService* SyncInternalsMessageHandler::GetProfileSyncService() { 142 ProfileSyncService* SyncInternalsMessageHandler::GetProfileSyncService() {
129 Profile* profile = Profile::FromWebUI(web_ui()); 143 Profile* profile = Profile::FromWebUI(web_ui());
130 ProfileSyncServiceFactory* factory = ProfileSyncServiceFactory::GetInstance(); 144 ProfileSyncServiceFactory* factory = ProfileSyncServiceFactory::GetInstance();
131 return factory->GetForProfile(profile->GetOriginalProfile()); 145 return factory->GetForProfile(profile->GetOriginalProfile());
132 } 146 }
133 147
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/sync_internals_message_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698