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

Side by Side Diff: chrome/browser/extensions/api/api_resource_event_notifier.cc

Issue 10224009: Experimental USB API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 months 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" 5 #include "chrome/browser/extensions/api/api_resource_event_notifier.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/json/json_writer.h" 8 #include "base/json/json_writer.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "chrome/browser/extensions/extension_event_router.h" 10 #include "chrome/browser/extensions/extension_event_router.h"
11 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
12 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
13 13
14 using content::BrowserThread; 14 using content::BrowserThread;
15 15
16 namespace events { 16 namespace events {
17 // TODO(miket): This should be generic, but at the moment only socket sends 17 const char kExperimentalSocketOnEvent[] = "experimental.socket.onEvent";
18 // onEvent events. We'll fix this when serial becomes nonblocking. 18 const char kExperimentalUsbOnEvent[] = "experimental.usb.onEvent";
19 const char kOnAPIResourceEvent[] = "experimental.socket.onEvent";
20 }; 19 };
21 20
22 namespace extensions { 21 namespace extensions {
23 22
24 const char kEventTypeKey[] = "type"; 23 const char kEventTypeKey[] = "type";
24
asargent_no_longer_on_chrome 2012/04/26 21:21:45 nit: I have a slight preference to not have this e
25 const char kEventTypeConnectComplete[] = "connectComplete"; 25 const char kEventTypeConnectComplete[] = "connectComplete";
26 const char kEventTypeDataRead[] = "dataRead"; 26 const char kEventTypeDataRead[] = "dataRead";
27 const char kEventTypeWriteComplete[] = "writeComplete"; 27 const char kEventTypeWriteComplete[] = "writeComplete";
28 28
29 const char kEventTypeTransferComplete[] = "transferComplete";
asargent_no_longer_on_chrome 2012/04/26 21:21:45 how about moving this up 1 line to be directly und
Garret Kelly 2012/04/27 00:32:34 I had separated the groups based on the extension
30
29 const char kSrcIdKey[] = "srcId"; 31 const char kSrcIdKey[] = "srcId";
30 const char kIsFinalEventKey[] = "isFinalEvent"; 32 const char kIsFinalEventKey[] = "isFinalEvent";
31 33
32 const char kResultCodeKey[] = "resultCode"; 34 const char kResultCodeKey[] = "resultCode";
33 const char kDataKey[] = "data"; 35 const char kDataKey[] = "data";
34 const char kAddressKey[] = "address"; 36 const char kAddressKey[] = "address";
35 const char kPortKey[] = "port"; 37 const char kPortKey[] = "port";
36 38
37 APIResourceEventNotifier::APIResourceEventNotifier( 39 APIResourceEventNotifier::APIResourceEventNotifier(
38 ExtensionEventRouter* router, 40 ExtensionEventRouter* router,
39 Profile* profile, 41 Profile* profile,
40 const std::string& src_extension_id, 42 const std::string& src_extension_id,
41 int src_id, 43 int src_id,
42 const GURL& src_url) 44 const GURL& src_url)
43 : router_(router), 45 : router_(router),
44 profile_(profile), 46 profile_(profile),
45 src_extension_id_(src_extension_id), 47 src_extension_id_(src_extension_id),
46 src_id_(src_id), 48 src_id_(src_id),
47 src_url_(src_url) {} 49 src_url_(src_url) {}
48 50
49 APIResourceEventNotifier::~APIResourceEventNotifier() {} 51 APIResourceEventNotifier::~APIResourceEventNotifier() {}
50 52
51 void APIResourceEventNotifier::OnConnectComplete(int result_code) { 53 void APIResourceEventNotifier::OnConnectComplete(int result_code) {
52 SendEventWithResultCode(API_RESOURCE_EVENT_CONNECT_COMPLETE, result_code); 54 SendEventWithResultCode(events::kExperimentalSocketOnEvent,
55 API_RESOURCE_EVENT_CONNECT_COMPLETE, result_code);
53 } 56 }
54 57
55 void APIResourceEventNotifier::OnDataRead(int result_code, 58 void APIResourceEventNotifier::OnDataRead(int result_code,
56 base::ListValue* data, 59 base::ListValue* data,
57 const std::string& address, 60 const std::string& address,
58 int port) { 61 int port) {
59 // Do we have a destination for this event? There will be one if a source id 62 // Do we have a destination for this event? There will be one if a source id
60 // was injected by the request handler for the resource's create method in 63 // was injected by the request handler for the resource's create method in
61 // schema_generated_bindings.js, which will in turn be the case if the caller 64 // schema_generated_bindings.js, which will in turn be the case if the caller
62 // of the create method provided an onEvent closure. 65 // of the create method provided an onEvent closure.
63 if (src_id_ < 0) { 66 if (src_id_ < 0) {
64 delete data; 67 delete data;
65 return; 68 return;
66 } 69 }
67 70
68 DictionaryValue* event = CreateAPIResourceEvent( 71 DictionaryValue* event = CreateAPIResourceEvent(
69 API_RESOURCE_EVENT_DATA_READ); 72 API_RESOURCE_EVENT_DATA_READ);
70 event->SetInteger(kResultCodeKey, result_code); 73 event->SetInteger(kResultCodeKey, result_code);
71 event->Set(kDataKey, data); 74 event->Set(kDataKey, data);
72 event->SetString(kAddressKey, address); 75 event->SetString(kAddressKey, address);
73 event->SetInteger(kPortKey, port); 76 event->SetInteger(kPortKey, port);
74 DispatchEvent(event); 77 DispatchEvent(events::kExperimentalSocketOnEvent, event);
75 } 78 }
76 79
77 void APIResourceEventNotifier::OnWriteComplete(int result_code) { 80 void APIResourceEventNotifier::OnWriteComplete(int result_code) {
78 SendEventWithResultCode(API_RESOURCE_EVENT_WRITE_COMPLETE, result_code); 81 SendEventWithResultCode(events::kExperimentalSocketOnEvent,
82 API_RESOURCE_EVENT_WRITE_COMPLETE, result_code);
83 }
84
85 void APIResourceEventNotifier::OnTransferComplete(int result_code,
86 base::ListValue* data) {
87 if (src_id_ < 0) {
88 delete data;
89 return;
90 }
91
92 DictionaryValue* event = CreateAPIResourceEvent(
93 API_RESOURCE_EVENT_TRANSFER_COMPLETE);
94 event->SetInteger(kResultCodeKey, result_code);
95 event->Set(kDataKey, data);
96 DispatchEvent(events::kExperimentalUsbOnEvent, event);
79 } 97 }
80 98
81 void APIResourceEventNotifier::SendEventWithResultCode( 99 void APIResourceEventNotifier::SendEventWithResultCode(
100 const std::string &extension,
82 APIResourceEventType event_type, 101 APIResourceEventType event_type,
83 int result_code) { 102 int result_code) {
84 if (src_id_ < 0) 103 if (src_id_ < 0)
85 return; 104 return;
86 105
87 DictionaryValue* event = CreateAPIResourceEvent(event_type); 106 DictionaryValue* event = CreateAPIResourceEvent(event_type);
88 event->SetInteger(kResultCodeKey, result_code); 107 event->SetInteger(kResultCodeKey, result_code);
89 DispatchEvent(event); 108 DispatchEvent(extension, event);
90 } 109 }
91 110
92 void APIResourceEventNotifier::DispatchEvent(DictionaryValue* event) { 111 void APIResourceEventNotifier::DispatchEvent(const std::string &extension,
112 DictionaryValue* event) {
93 BrowserThread::PostTask( 113 BrowserThread::PostTask(
94 BrowserThread::UI, FROM_HERE, 114 BrowserThread::UI, FROM_HERE,
95 base::Bind( 115 base::Bind(
96 &APIResourceEventNotifier::DispatchEventOnUIThread, this, event)); 116 &APIResourceEventNotifier::DispatchEventOnUIThread, this, extension,
117 event));
97 } 118 }
98 119
99 void APIResourceEventNotifier::DispatchEventOnUIThread( 120 void APIResourceEventNotifier::DispatchEventOnUIThread(
100 DictionaryValue* event) { 121 const std::string &extension, DictionaryValue* event) {
101 ListValue args; 122 ListValue args;
102 123
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
104 125
105 args.Set(0, event); 126 args.Set(0, event);
106 std::string json_args; 127 std::string json_args;
107 base::JSONWriter::Write(&args, &json_args); 128 base::JSONWriter::Write(&args, &json_args);
108 router_->DispatchEventToExtension(src_extension_id_, 129 router_->DispatchEventToExtension(src_extension_id_, extension, json_args,
109 events::kOnAPIResourceEvent, 130 profile_, src_url_);
110 json_args, profile_, src_url_);
111 } 131 }
112 132
113 DictionaryValue* APIResourceEventNotifier::CreateAPIResourceEvent( 133 DictionaryValue* APIResourceEventNotifier::CreateAPIResourceEvent(
114 APIResourceEventType event_type) { 134 APIResourceEventType event_type) {
115 DictionaryValue* event = new DictionaryValue(); 135 DictionaryValue* event = new DictionaryValue();
116 event->SetString(kEventTypeKey, APIResourceEventTypeToString(event_type)); 136 event->SetString(kEventTypeKey, APIResourceEventTypeToString(event_type));
117 event->SetInteger(kSrcIdKey, src_id_); 137 event->SetInteger(kSrcIdKey, src_id_);
118 138
119 // TODO(miket): Signal that it's OK to clean up onEvent listeners. This is 139 // TODO(miket): Signal that it's OK to clean up onEvent listeners. This is
120 // the framework we'll use, but we need to start using it. 140 // the framework we'll use, but we need to start using it.
121 event->SetBoolean(kIsFinalEventKey, false); 141 event->SetBoolean(kIsFinalEventKey, false);
122 142
123 // The caller owns the created event, which typically is then given to a 143 // The caller owns the created event, which typically is then given to a
124 // ListValue to dispose of. 144 // ListValue to dispose of.
125 return event; 145 return event;
126 } 146 }
127 147
128 // static 148 // static
129 std::string APIResourceEventNotifier::APIResourceEventTypeToString( 149 std::string APIResourceEventNotifier::APIResourceEventTypeToString(
130 APIResourceEventType event_type) { 150 APIResourceEventType event_type) {
131 switch (event_type) { 151 switch (event_type) {
132 case API_RESOURCE_EVENT_CONNECT_COMPLETE: 152 case API_RESOURCE_EVENT_CONNECT_COMPLETE:
133 return kEventTypeConnectComplete; 153 return kEventTypeConnectComplete;
134 case API_RESOURCE_EVENT_DATA_READ: 154 case API_RESOURCE_EVENT_DATA_READ:
135 return kEventTypeDataRead; 155 return kEventTypeDataRead;
136 case API_RESOURCE_EVENT_WRITE_COMPLETE: 156 case API_RESOURCE_EVENT_WRITE_COMPLETE:
137 return kEventTypeWriteComplete; 157 return kEventTypeWriteComplete;
158 case API_RESOURCE_EVENT_TRANSFER_COMPLETE:
159 return kEventTypeTransferComplete;
138 } 160 }
139 161
140 NOTREACHED(); 162 NOTREACHED();
141 return std::string(); 163 return std::string();
142 } 164 }
143 165
144 } // namespace extensions 166 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698