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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/invalidations_message_handler.h"
8 #include "chrome/browser/invalidation/invalidation_logger.h"
9 #include "chrome/browser/invalidation/invalidation_service.h"
10 #include "chrome/browser/invalidation/invalidation_service_factory.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "content/public/browser/web_ui.h"
13 #include "sync/notifier/invalidation_handler.h"
15 namespace invalidation
{
16 class InvalidationLogger
;
17 } // namespace invalidation
20 class ObjectIdInvalidationMap
;
23 InvalidationsMessageHandler::InvalidationsMessageHandler()
24 : logger_(NULL
), weak_ptr_factory_(this) {}
26 InvalidationsMessageHandler::~InvalidationsMessageHandler() {
28 logger_
->UnregisterObserver(this);
31 void InvalidationsMessageHandler::RegisterMessages() {
32 web_ui()->RegisterMessageCallback(
34 base::Bind(&InvalidationsMessageHandler::UIReady
,
35 base::Unretained(this)));
36 web_ui()->RegisterMessageCallback(
37 "requestDetailedStatus",
38 base::Bind(&InvalidationsMessageHandler::HandleRequestDetailedStatus
,
39 base::Unretained(this)));
42 void InvalidationsMessageHandler::UIReady(const base::ListValue
* args
) {
43 invalidation::InvalidationService
* invalidation_service
=
44 invalidation::InvalidationServiceFactory::GetForProfile(
45 Profile::FromWebUI(web_ui()));
46 if (invalidation_service
)
47 logger_
= invalidation_service
->GetInvalidationLogger();
48 if (logger_
&& !logger_
->IsObserverRegistered(this))
49 logger_
->RegisterObserver(this);
53 void InvalidationsMessageHandler::HandleRequestDetailedStatus(
54 const base::ListValue
* args
) {
55 invalidation::InvalidationService
* invalidation_service
=
56 invalidation::InvalidationServiceFactory::GetForProfile(
57 Profile::FromWebUI(web_ui()));
58 if (invalidation_service
) {
59 invalidation_service
->RequestDetailedStatus(base::Bind(
60 &InvalidationsMessageHandler::OnDetailedStatus
,
61 weak_ptr_factory_
.GetWeakPtr()));
65 void InvalidationsMessageHandler::UpdateContent(const base::ListValue
* args
) {
67 logger_
->EmitContent();
70 void InvalidationsMessageHandler::OnRegistrationChange(
71 const std::multiset
<std::string
>& registered_handlers
) {
72 base::ListValue list_of_handlers
;
73 for (std::multiset
<std::string
>::const_iterator it
=
74 registered_handlers
.begin();
75 it
!= registered_handlers
.end(); ++it
) {
76 list_of_handlers
.Append(new base::StringValue(*it
));
78 web_ui()->CallJavascriptFunction("chrome.invalidations.updateHandlers",
82 void InvalidationsMessageHandler::OnStateChange(
83 const syncer::InvalidatorState
& new_state
,
84 const base::Time
& last_changed_timestamp
) {
85 std::string
state(syncer::InvalidatorStateToString(new_state
));
86 web_ui()->CallJavascriptFunction(
87 "chrome.invalidations.updateInvalidatorState", base::StringValue(state
),
88 base::FundamentalValue(last_changed_timestamp
.ToJsTime()));
91 void InvalidationsMessageHandler::OnUpdateIds(
92 const std::string
& handler_name
,
93 const syncer::ObjectIdCountMap
& ids
) {
94 base::ListValue list_of_objects
;
95 for (syncer::ObjectIdCountMap::const_iterator it
= ids
.begin();
98 scoped_ptr
<base::DictionaryValue
> dic(new base::DictionaryValue());
99 dic
->SetString("name", (it
->first
).name());
100 dic
->SetInteger("source", (it
->first
).source());
101 dic
->SetInteger("totalCount", it
->second
);
102 list_of_objects
.Append(dic
.release());
104 web_ui()->CallJavascriptFunction("chrome.invalidations.updateIds",
105 base::StringValue(handler_name
),
108 void InvalidationsMessageHandler::OnDebugMessage(
109 const base::DictionaryValue
& details
) {}
111 void InvalidationsMessageHandler::OnInvalidation(
112 const syncer::ObjectIdInvalidationMap
& new_invalidations
) {
113 scoped_ptr
<base::ListValue
> invalidations_list
= new_invalidations
.ToValue();
114 web_ui()->CallJavascriptFunction("chrome.invalidations.logInvalidations",
115 *invalidations_list
);
118 void InvalidationsMessageHandler::OnDetailedStatus(
119 const base::DictionaryValue
& network_details
) {
120 web_ui()->CallJavascriptFunction("chrome.invalidations.updateDetailedStatus",