OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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/extensions/api/sync_file_system/extension_sync_event_ob server.h" | |
kinuko
2012/11/28 11:06:40
nit: please insert one empty line after this
calvinlo
2012/11/28 12:26:34
Done.
| |
6 #include "chrome/browser/extensions/event_names.h" | |
7 #include "chrome/browser/extensions/event_router.h" | |
8 #include "chrome/browser/extensions/extension_service.h" | |
9 #include "chrome/browser/extensions/extension_system.h" | |
10 #include "chrome/browser/sync_file_system/sync_event_observer.h" | |
11 #include "chrome/common/extensions/api/sync_file_system.h" | |
12 #include "webkit/fileapi/file_system_url.h" | |
13 #include "webkit/fileapi/syncable/sync_operation_type.h" | |
14 | |
15 using sync_file_system::SyncEventObserver; | |
16 | |
17 namespace extensions { | |
18 | |
19 namespace { | |
20 | |
21 api::sync_file_system::SyncStateStatus SyncServiceStateEnumToExtensionEnum( | |
22 SyncEventObserver::SyncServiceState state) { | |
23 switch(state) { | |
24 case SyncEventObserver::SYNC_SERVICE_RUNNING: | |
25 return api::sync_file_system::SYNC_FILE_SYSTEM_SYNC_STATE_STATUS_RUNNING; | |
26 case SyncEventObserver::SYNC_SERVICE_AUTHENTICATION_REQUIRED: | |
27 return api::sync_file_system:: | |
28 SYNC_FILE_SYSTEM_SYNC_STATE_STATUS_AUTHENTICATION_REQUIRED; | |
29 case SyncEventObserver::SYNC_SERVICE_TEMPORARY_UNAVAILABLE: | |
30 return api::sync_file_system:: | |
31 SYNC_FILE_SYSTEM_SYNC_STATE_STATUS_TEMPORARY_UNAVAILABLE; | |
32 case SyncEventObserver::SYNC_SERVICE_DISABLED: | |
33 return api::sync_file_system::SYNC_FILE_SYSTEM_SYNC_STATE_STATUS_DISABLED; | |
34 } | |
35 NOTREACHED(); | |
36 return api::sync_file_system::SYNC_FILE_SYSTEM_SYNC_STATE_STATUS_NONE; | |
37 } | |
38 | |
39 api::sync_file_system::SyncOperationType SyncOperationTypeToExtensionEnum( | |
40 fileapi::SyncOperationType operation_type) { | |
41 switch(operation_type) { | |
42 case fileapi::SYNC_OPERATION_NONE: | |
43 return api::sync_file_system:: | |
44 SYNC_FILE_SYSTEM_SYNC_OPERATION_TYPE_NONE; | |
45 case fileapi::SYNC_OPERATION_ADD: | |
46 return api::sync_file_system:: | |
47 SYNC_FILE_SYSTEM_SYNC_OPERATION_TYPE_FILE_ADDED; | |
48 case fileapi::SYNC_OPERATION_UPDATE: | |
49 return api::sync_file_system:: | |
50 SYNC_FILE_SYSTEM_SYNC_OPERATION_TYPE_FILE_UPDATED; | |
51 case fileapi::SYNC_OPERATION_DELETE: | |
52 return api::sync_file_system:: | |
53 SYNC_FILE_SYSTEM_SYNC_OPERATION_TYPE_FILE_DELETED; | |
54 } | |
55 NOTREACHED(); | |
56 return api::sync_file_system::SYNC_FILE_SYSTEM_SYNC_OPERATION_TYPE_NONE; | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 ExtensionSyncEventObserver::ExtensionSyncEventObserver( | |
62 Profile* profile, | |
63 const std::string& service_name) | |
64 : profile_(profile), | |
65 service_name_(service_name) {} | |
66 | |
67 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.
| |
68 const GURL& app_origin) { | |
69 const Extension* app = ExtensionSystem::Get(profile_)->extension_service()-> | |
70 GetInstalledApp(app_origin); | |
71 DCHECK(app); | |
72 return app->id(); | |
73 } | |
74 | |
75 void ExtensionSyncEventObserver::OnSyncStateUpdated( | |
76 const GURL& app_origin, | |
77 sync_file_system::SyncEventObserver::SyncServiceState state, | |
78 const std::string& description) { | |
79 // TODO(calvinlo): Check extension in set of extension_ids that are listening | |
80 // for events. If extension_id is not in the set, then ignore the event. | |
81 const std::string extension_id = GetExtensionId(app_origin); | |
82 | |
83 // Convert state and description into SyncState Object | |
84 api::sync_file_system::SyncState sync_state; | |
85 sync_state.service_name = service_name_; | |
86 sync_state.state = SyncServiceStateEnumToExtensionEnum(state); | |
87 sync_state.description = description; | |
88 scoped_ptr<base::ListValue> params( | |
89 api::sync_file_system::OnSyncStateChanged::Create(sync_state)); | |
90 | |
91 // Dispatch the event to the extension | |
92 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( | |
93 extension_id, event_names::kOnSyncStateChanged, params.Pass(), | |
94 profile_, GURL()); | |
95 } | |
96 | |
97 void ExtensionSyncEventObserver::OnFileSynced( | |
98 fileapi::SyncOperationType operation, | |
99 const fileapi::FileSystemURL& url) { | |
100 const std::string extension_id = GetExtensionId(url.origin()); | |
101 | |
102 // TODO(calvinlo):Convert filePath from string to Webkit FileEntry. | |
103 const api::sync_file_system::SyncOperationType sync_operation_type = | |
104 SyncOperationTypeToExtensionEnum(operation); | |
105 const std::string filePath = url.virtual_path().MaybeAsASCII(); | |
106 scoped_ptr<base::ListValue> params( | |
107 api::sync_file_system::OnFileSynced::Create(filePath, | |
108 sync_operation_type)); | |
109 | |
110 // Dispatch the event to the extension | |
111 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( | |
112 extension_id, event_names::kOnFileSynced, params.Pass(), | |
113 profile_, GURL()); | |
114 } | |
115 | |
116 } // namespace extensions | |
OLD | NEW |