- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / sync_file_system_internals / sync_file_system_internals_handler.cc
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.
4
5 #include "chrome/browser/ui/webui/sync_file_system_internals/sync_file_system_internals_handler.h"
6
7 #include "base/bind.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"
22
23 using drive::EventLogger;
24 using sync_file_system::SyncFileSystemServiceFactory;
25 using sync_file_system::SyncServiceState;
26
27 namespace syncfs_internals {
28
29 SyncFileSystemInternalsHandler::SyncFileSystemInternalsHandler(Profile* profile)
30     : profile_(profile) {
31   sync_file_system::SyncFileSystemService* sync_service =
32       SyncFileSystemServiceFactory::GetForProfile(profile);
33   DCHECK(sync_service);
34   sync_service->AddSyncEventObserver(this);
35 }
36
37 SyncFileSystemInternalsHandler::~SyncFileSystemInternalsHandler() {
38   sync_file_system::SyncFileSystemService* sync_service =
39       SyncFileSystemServiceFactory::GetForProfile(profile_);
40   if (sync_service != NULL)
41     sync_service->RemoveSyncEventObserver(this);
42 }
43
44 void SyncFileSystemInternalsHandler::RegisterMessages() {
45   web_ui()->RegisterMessageCallback(
46       "getServiceStatus",
47       base::Bind(&SyncFileSystemInternalsHandler::GetServiceStatus,
48                  base::Unretained(this)));
49   web_ui()->RegisterMessageCallback(
50       "getLog",
51       base::Bind(&SyncFileSystemInternalsHandler::GetLog,
52                  base::Unretained(this)));
53   web_ui()->RegisterMessageCallback(
54       "getNotificationSource",
55       base::Bind(&SyncFileSystemInternalsHandler::GetNotificationSource,
56                  base::Unretained(this)));
57 }
58
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 + ")";
67
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));
72 }
73
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) {}
79
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));
88 }
89
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));
98 }
99
100 void SyncFileSystemInternalsHandler::GetLog(
101     const base::ListValue* args) {
102   const std::vector<EventLogger::Event> log =
103       sync_file_system::util::GetLogHistory();
104
105   int last_log_id_sent;
106   if (!args->GetInteger(0, &last_log_id_sent))
107     last_log_id_sent = -1;
108
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();
113        ++log_entry) {
114     if (log_entry->id <= last_log_id_sent)
115       continue;
116
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);
122     list.Append(dict);
123     last_log_id_sent = log_entry->id;
124   }
125   if (list.empty())
126     return;
127
128   web_ui()->CallJavascriptFunction("SyncService.onGetLog", list);
129 }
130
131 }  // namespace syncfs_internals