1 // Copyright 2013 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/sync_file_system_internals/sync_file_system_internals_handler.h"
8 #include "base/bind_helpers.h"
9 #include "base/values.h"
10 #include "chrome/browser/drive/drive_notification_manager.h"
11 #include "chrome/browser/drive/drive_notification_manager_factory.h"
12 #include "chrome/browser/extensions/api/sync_file_system/sync_file_system_api_helpers.h"
13 #include "chrome/browser/google_apis/time_util.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/sync_file_system/logger.h"
16 #include "chrome/browser/sync_file_system/sync_file_system_service.h"
17 #include "chrome/browser/sync_file_system/sync_file_system_service_factory.h"
18 #include "chrome/browser/sync_file_system/sync_service_state.h"
19 #include "chrome/common/extensions/api/sync_file_system.h"
20 #include "content/public/browser/storage_partition.h"
21 #include "content/public/browser/web_ui.h"
23 using drive::EventLogger;
24 using sync_file_system::SyncFileSystemServiceFactory;
25 using sync_file_system::SyncServiceState;
27 namespace syncfs_internals {
29 SyncFileSystemInternalsHandler::SyncFileSystemInternalsHandler(Profile* profile)
31 sync_file_system::SyncFileSystemService* sync_service =
32 SyncFileSystemServiceFactory::GetForProfile(profile);
34 sync_service->AddSyncEventObserver(this);
37 SyncFileSystemInternalsHandler::~SyncFileSystemInternalsHandler() {
38 sync_file_system::SyncFileSystemService* sync_service =
39 SyncFileSystemServiceFactory::GetForProfile(profile_);
40 if (sync_service != NULL)
41 sync_service->RemoveSyncEventObserver(this);
44 void SyncFileSystemInternalsHandler::RegisterMessages() {
45 web_ui()->RegisterMessageCallback(
47 base::Bind(&SyncFileSystemInternalsHandler::GetServiceStatus,
48 base::Unretained(this)));
49 web_ui()->RegisterMessageCallback(
51 base::Bind(&SyncFileSystemInternalsHandler::GetLog,
52 base::Unretained(this)));
53 web_ui()->RegisterMessageCallback(
54 "getNotificationSource",
55 base::Bind(&SyncFileSystemInternalsHandler::GetNotificationSource,
56 base::Unretained(this)));
59 void SyncFileSystemInternalsHandler::OnSyncStateUpdated(
60 const GURL& app_origin,
61 sync_file_system::SyncServiceState state,
62 const std::string& description) {
63 std::string state_string = extensions::api::sync_file_system::ToString(
64 extensions::SyncServiceStateToExtensionEnum(state));
65 if (!description.empty())
66 state_string += " (" + description + ")";
68 // TODO(calvinlo): OnSyncStateUpdated should be updated to also provide the
69 // notification mechanism (XMPP or Polling).
70 web_ui()->CallJavascriptFunction("SyncService.onGetServiceStatus",
71 base::StringValue(state_string));
74 void SyncFileSystemInternalsHandler::OnFileSynced(
75 const fileapi::FileSystemURL& url,
76 sync_file_system::SyncFileStatus status,
77 sync_file_system::SyncAction action,
78 sync_file_system::SyncDirection direction) {}
80 void SyncFileSystemInternalsHandler::GetServiceStatus(
81 const base::ListValue* args) {
82 SyncServiceState state_enum = SyncFileSystemServiceFactory::GetForProfile(
83 profile_)->GetSyncServiceState();
84 std::string state_string = extensions::api::sync_file_system::ToString(
85 extensions::SyncServiceStateToExtensionEnum(state_enum));
86 web_ui()->CallJavascriptFunction("SyncService.onGetServiceStatus",
87 base::StringValue(state_string));
90 void SyncFileSystemInternalsHandler::GetNotificationSource(
91 const base::ListValue* args) {
92 drive::DriveNotificationManager* drive_notification_manager =
93 drive::DriveNotificationManagerFactory::GetForBrowserContext(profile_);
94 bool xmpp_enabled = drive_notification_manager->push_notification_enabled();
95 std::string notification_source = xmpp_enabled ? "XMPP" : "Polling";
96 web_ui()->CallJavascriptFunction("SyncService.onGetNotificationSource",
97 base::StringValue(notification_source));
100 void SyncFileSystemInternalsHandler::GetLog(
101 const base::ListValue* args) {
102 const std::vector<EventLogger::Event> log =
103 sync_file_system::util::GetLogHistory();
105 int last_log_id_sent;
106 if (!args->GetInteger(0, &last_log_id_sent))
107 last_log_id_sent = -1;
109 // Collate events which haven't been sent to WebUI yet.
110 base::ListValue list;
111 for (std::vector<EventLogger::Event>::const_iterator log_entry = log.begin();
112 log_entry != log.end();
114 if (log_entry->id <= last_log_id_sent)
117 base::DictionaryValue* dict = new DictionaryValue;
118 dict->SetInteger("id", log_entry->id);
119 dict->SetString("time",
120 google_apis::util::FormatTimeAsStringLocaltime(log_entry->when));
121 dict->SetString("logEvent", log_entry->what);
123 last_log_id_sent = log_entry->id;
128 web_ui()->CallJavascriptFunction("SyncService.onGetLog", list);
131 } // namespace syncfs_internals