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

Unified Diff: chrome/browser/extensions/api/sync_file_system/extension_sync_event_observer.cc

Issue 11316133: Added implementation of SyncEventObserver to route events to Javascript Extension. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Kinuko review #2 (after little fixups) Created 8 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/sync_file_system/extension_sync_event_observer.cc
diff --git a/chrome/browser/extensions/api/sync_file_system/extension_sync_event_observer.cc b/chrome/browser/extensions/api/sync_file_system/extension_sync_event_observer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3936ee6e066c1eb75e6744bdfbe23bb5e86aeb6b
--- /dev/null
+++ b/chrome/browser/extensions/api/sync_file_system/extension_sync_event_observer.cc
@@ -0,0 +1,116 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/extensions/api/sync_file_system/extension_sync_event_observer.h"
kinuko 2012/11/28 11:06:40 nit: please insert one empty line after this
calvinlo 2012/11/28 12:26:34 Done.
+#include "chrome/browser/extensions/event_names.h"
+#include "chrome/browser/extensions/event_router.h"
+#include "chrome/browser/extensions/extension_service.h"
+#include "chrome/browser/extensions/extension_system.h"
+#include "chrome/browser/sync_file_system/sync_event_observer.h"
+#include "chrome/common/extensions/api/sync_file_system.h"
+#include "webkit/fileapi/file_system_url.h"
+#include "webkit/fileapi/syncable/sync_operation_type.h"
+
+using sync_file_system::SyncEventObserver;
+
+namespace extensions {
+
+namespace {
+
+api::sync_file_system::SyncStateStatus SyncServiceStateEnumToExtensionEnum(
+ SyncEventObserver::SyncServiceState state) {
+ switch(state) {
+ case SyncEventObserver::SYNC_SERVICE_RUNNING:
+ return api::sync_file_system::SYNC_FILE_SYSTEM_SYNC_STATE_STATUS_RUNNING;
+ case SyncEventObserver::SYNC_SERVICE_AUTHENTICATION_REQUIRED:
+ return api::sync_file_system::
+ SYNC_FILE_SYSTEM_SYNC_STATE_STATUS_AUTHENTICATION_REQUIRED;
+ case SyncEventObserver::SYNC_SERVICE_TEMPORARY_UNAVAILABLE:
+ return api::sync_file_system::
+ SYNC_FILE_SYSTEM_SYNC_STATE_STATUS_TEMPORARY_UNAVAILABLE;
+ case SyncEventObserver::SYNC_SERVICE_DISABLED:
+ return api::sync_file_system::SYNC_FILE_SYSTEM_SYNC_STATE_STATUS_DISABLED;
+ }
+ NOTREACHED();
+ return api::sync_file_system::SYNC_FILE_SYSTEM_SYNC_STATE_STATUS_NONE;
+}
+
+api::sync_file_system::SyncOperationType SyncOperationTypeToExtensionEnum(
+ fileapi::SyncOperationType operation_type) {
+ switch(operation_type) {
+ case fileapi::SYNC_OPERATION_NONE:
+ return api::sync_file_system::
+ SYNC_FILE_SYSTEM_SYNC_OPERATION_TYPE_NONE;
+ case fileapi::SYNC_OPERATION_ADD:
+ return api::sync_file_system::
+ SYNC_FILE_SYSTEM_SYNC_OPERATION_TYPE_FILE_ADDED;
+ case fileapi::SYNC_OPERATION_UPDATE:
+ return api::sync_file_system::
+ SYNC_FILE_SYSTEM_SYNC_OPERATION_TYPE_FILE_UPDATED;
+ case fileapi::SYNC_OPERATION_DELETE:
+ return api::sync_file_system::
+ SYNC_FILE_SYSTEM_SYNC_OPERATION_TYPE_FILE_DELETED;
+ }
+ NOTREACHED();
+ return api::sync_file_system::SYNC_FILE_SYSTEM_SYNC_OPERATION_TYPE_NONE;
+}
+
+} // namespace
+
+ExtensionSyncEventObserver::ExtensionSyncEventObserver(
+ Profile* profile,
+ const std::string& service_name)
+ : profile_(profile),
+ service_name_(service_name) {}
+
+const std::string ExtensionSyncEventObserver::GetExtensionId(
kinuko 2012/11/28 11:06:40 you probably meant const std::string& ? (as Extens
calvinlo 2012/11/28 12:26:34 Done.
+ const GURL& app_origin) {
+ const Extension* app = ExtensionSystem::Get(profile_)->extension_service()->
+ GetInstalledApp(app_origin);
+ DCHECK(app);
+ return app->id();
+}
+
+void ExtensionSyncEventObserver::OnSyncStateUpdated(
+ const GURL& app_origin,
+ sync_file_system::SyncEventObserver::SyncServiceState state,
+ const std::string& description) {
+ // TODO(calvinlo): Check extension in set of extension_ids that are listening
+ // for events. If extension_id is not in the set, then ignore the event.
+ const std::string extension_id = GetExtensionId(app_origin);
+
+ // Convert state and description into SyncState Object
+ api::sync_file_system::SyncState sync_state;
+ sync_state.service_name = service_name_;
+ sync_state.state = SyncServiceStateEnumToExtensionEnum(state);
+ sync_state.description = description;
+ scoped_ptr<base::ListValue> params(
+ api::sync_file_system::OnSyncStateChanged::Create(sync_state));
+
+ // Dispatch the event to the extension
+ ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension(
+ extension_id, event_names::kOnSyncStateChanged, params.Pass(),
+ profile_, GURL());
+}
+
+void ExtensionSyncEventObserver::OnFileSynced(
+ fileapi::SyncOperationType operation,
+ const fileapi::FileSystemURL& url) {
+ const std::string extension_id = GetExtensionId(url.origin());
+
+ // TODO(calvinlo):Convert filePath from string to Webkit FileEntry.
+ const api::sync_file_system::SyncOperationType sync_operation_type =
+ SyncOperationTypeToExtensionEnum(operation);
+ const std::string filePath = url.virtual_path().MaybeAsASCII();
+ scoped_ptr<base::ListValue> params(
+ api::sync_file_system::OnFileSynced::Create(filePath,
+ sync_operation_type));
+
+ // Dispatch the event to the extension
+ ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension(
+ extension_id, event_names::kOnFileSynced, params.Pass(),
+ profile_, GURL());
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698