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

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

Powered by Google App Engine
This is Rietveld 408576698