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/profiles/profile.h" 14 #include "chrome/browser/sync_file_system/logger.h" 15 #include "chrome/browser/sync_file_system/sync_file_system_service.h" 16 #include "chrome/browser/sync_file_system/sync_file_system_service_factory.h" 17 #include "chrome/browser/sync_file_system/sync_service_state.h" 18 #include "chrome/common/extensions/api/sync_file_system.h" 19 #include "content/public/browser/storage_partition.h" 20 #include "content/public/browser/web_ui.h" 21 #include "google_apis/drive/time_util.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 "clearLogs", 55 base::Bind(&SyncFileSystemInternalsHandler::ClearLogs, 56 base::Unretained(this))); 57 web_ui()->RegisterMessageCallback( 58 "getNotificationSource", 59 base::Bind(&SyncFileSystemInternalsHandler::GetNotificationSource, 60 base::Unretained(this))); 61 } 62 63 void SyncFileSystemInternalsHandler::OnSyncStateUpdated( 64 const GURL& app_origin, 65 sync_file_system::SyncServiceState state, 66 const std::string& description) { 67 std::string state_string = extensions::api::sync_file_system::ToString( 68 extensions::SyncServiceStateToExtensionEnum(state)); 69 if (!description.empty()) 70 state_string += " (" + description + ")"; 71 72 // TODO(calvinlo): OnSyncStateUpdated should be updated to also provide the 73 // notification mechanism (XMPP or Polling). 74 web_ui()->CallJavascriptFunction("SyncService.onGetServiceStatus", 75 base::StringValue(state_string)); 76 } 77 78 void SyncFileSystemInternalsHandler::OnFileSynced( 79 const fileapi::FileSystemURL& url, 80 sync_file_system::SyncFileStatus status, 81 sync_file_system::SyncAction action, 82 sync_file_system::SyncDirection direction) {} 83 84 void SyncFileSystemInternalsHandler::GetServiceStatus( 85 const base::ListValue* args) { 86 SyncServiceState state_enum = SyncFileSystemServiceFactory::GetForProfile( 87 profile_)->GetSyncServiceState(); 88 std::string state_string = extensions::api::sync_file_system::ToString( 89 extensions::SyncServiceStateToExtensionEnum(state_enum)); 90 web_ui()->CallJavascriptFunction("SyncService.onGetServiceStatus", 91 base::StringValue(state_string)); 92 } 93 94 void SyncFileSystemInternalsHandler::GetNotificationSource( 95 const base::ListValue* args) { 96 drive::DriveNotificationManager* drive_notification_manager = 97 drive::DriveNotificationManagerFactory::GetForBrowserContext(profile_); 98 bool xmpp_enabled = drive_notification_manager->push_notification_enabled(); 99 std::string notification_source = xmpp_enabled ? "XMPP" : "Polling"; 100 web_ui()->CallJavascriptFunction("SyncService.onGetNotificationSource", 101 base::StringValue(notification_source)); 102 } 103 104 void SyncFileSystemInternalsHandler::GetLog( 105 const base::ListValue* args) { 106 const std::vector<EventLogger::Event> log = 107 sync_file_system::util::GetLogHistory(); 108 109 int last_log_id_sent; 110 if (!args->GetInteger(0, &last_log_id_sent)) 111 last_log_id_sent = -1; 112 113 // Collate events which haven't been sent to WebUI yet. 114 base::ListValue list; 115 for (std::vector<EventLogger::Event>::const_iterator log_entry = log.begin(); 116 log_entry != log.end(); 117 ++log_entry) { 118 if (log_entry->id <= last_log_id_sent) 119 continue; 120 121 base::DictionaryValue* dict = new DictionaryValue; 122 dict->SetInteger("id", log_entry->id); 123 dict->SetString("time", 124 google_apis::util::FormatTimeAsStringLocaltime(log_entry->when)); 125 dict->SetString("logEvent", log_entry->what); 126 list.Append(dict); 127 last_log_id_sent = log_entry->id; 128 } 129 if (list.empty()) 130 return; 131 132 web_ui()->CallJavascriptFunction("SyncService.onGetLog", list); 133 } 134 135 void SyncFileSystemInternalsHandler::ClearLogs(const base::ListValue* args) { 136 sync_file_system::util::ClearLog(); 137 } 138 139 } // namespace syncfs_internals 140