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

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: Rebase. 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
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";
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 51
50 void APIResourceEventNotifier::OnConnectComplete(int result_code) { 52 void APIResourceEventNotifier::OnConnectComplete(int result_code) {
51 SendEventWithResultCode(API_RESOURCE_EVENT_CONNECT_COMPLETE, result_code); 53 SendEventWithResultCode(events::kExperimentalSocketOnEvent,
54 API_RESOURCE_EVENT_CONNECT_COMPLETE, result_code);
52 } 55 }
53 56
54 void APIResourceEventNotifier::OnDataRead(int result_code, 57 void APIResourceEventNotifier::OnDataRead(int result_code,
55 base::ListValue* data, 58 base::ListValue* data,
56 const std::string& address, 59 const std::string& address,
57 int port) { 60 int port) {
58 // Do we have a destination for this event? There will be one if a source id 61 // Do we have a destination for this event? There will be one if a source id
59 // was injected by the request handler for the resource's create method in 62 // was injected by the request handler for the resource's create method in
60 // schema_generated_bindings.js, which will in turn be the case if the caller 63 // schema_generated_bindings.js, which will in turn be the case if the caller
61 // of the create method provided an onEvent closure. 64 // of the create method provided an onEvent closure.
62 if (src_id_ < 0) { 65 if (src_id_ < 0) {
63 delete data; 66 delete data;
64 return; 67 return;
65 } 68 }
66 69
67 DictionaryValue* event = CreateAPIResourceEvent( 70 DictionaryValue* event = CreateAPIResourceEvent(
68 API_RESOURCE_EVENT_DATA_READ); 71 API_RESOURCE_EVENT_DATA_READ);
69 event->SetInteger(kResultCodeKey, result_code); 72 event->SetInteger(kResultCodeKey, result_code);
70 event->Set(kDataKey, data); 73 event->Set(kDataKey, data);
71 event->SetString(kAddressKey, address); 74 event->SetString(kAddressKey, address);
72 event->SetInteger(kPortKey, port); 75 event->SetInteger(kPortKey, port);
73 DispatchEvent(event); 76 DispatchEvent(events::kExperimentalSocketOnEvent, event);
74 } 77 }
75 78
76 void APIResourceEventNotifier::OnWriteComplete(int result_code) { 79 void APIResourceEventNotifier::OnWriteComplete(int result_code) {
77 SendEventWithResultCode(API_RESOURCE_EVENT_WRITE_COMPLETE, result_code); 80 SendEventWithResultCode(events::kExperimentalSocketOnEvent,
81 API_RESOURCE_EVENT_WRITE_COMPLETE, result_code);
82 }
83
84 void APIResourceEventNotifier::OnTransferComplete(int result_code,
85 base::ListValue* data) {
86 if (src_id_ < 0) {
87 delete data;
88 return;
89 }
90
91 DictionaryValue* event = CreateAPIResourceEvent(
92 API_RESOURCE_EVENT_TRANSFER_COMPLETE);
93 event->SetInteger(kResultCodeKey, result_code);
94 event->Set(kDataKey, data);
95 DispatchEvent(events::kExperimentalUsbOnEvent, event);
78 } 96 }
79 97
80 // static 98 // static
81 std::string APIResourceEventNotifier::APIResourceEventTypeToString( 99 std::string APIResourceEventNotifier::APIResourceEventTypeToString(
82 APIResourceEventType event_type) { 100 APIResourceEventType event_type) {
83 switch (event_type) { 101 switch (event_type) {
84 case API_RESOURCE_EVENT_CONNECT_COMPLETE: 102 case API_RESOURCE_EVENT_CONNECT_COMPLETE:
85 return kEventTypeConnectComplete; 103 return kEventTypeConnectComplete;
86 case API_RESOURCE_EVENT_DATA_READ: 104 case API_RESOURCE_EVENT_DATA_READ:
87 return kEventTypeDataRead; 105 return kEventTypeDataRead;
88 case API_RESOURCE_EVENT_WRITE_COMPLETE: 106 case API_RESOURCE_EVENT_WRITE_COMPLETE:
89 return kEventTypeWriteComplete; 107 return kEventTypeWriteComplete;
108 case API_RESOURCE_EVENT_TRANSFER_COMPLETE:
109 return kEventTypeTransferComplete;
90 } 110 }
91 111
92 NOTREACHED(); 112 NOTREACHED();
93 return std::string(); 113 return std::string();
94 } 114 }
95 115
96 APIResourceEventNotifier::~APIResourceEventNotifier() {} 116 APIResourceEventNotifier::~APIResourceEventNotifier() {}
97 117
98 void APIResourceEventNotifier::DispatchEvent(DictionaryValue* event) { 118 void APIResourceEventNotifier::DispatchEvent(const std::string &extension,
119 DictionaryValue* event) {
99 BrowserThread::PostTask( 120 BrowserThread::PostTask(
100 BrowserThread::UI, FROM_HERE, 121 BrowserThread::UI, FROM_HERE,
101 base::Bind( 122 base::Bind(
102 &APIResourceEventNotifier::DispatchEventOnUIThread, this, event)); 123 &APIResourceEventNotifier::DispatchEventOnUIThread, this, extension,
124 event));
103 } 125 }
104 126
105 void APIResourceEventNotifier::DispatchEventOnUIThread( 127 void APIResourceEventNotifier::DispatchEventOnUIThread(
106 DictionaryValue* event) { 128 const std::string &extension, DictionaryValue* event) {
107 ListValue args; 129 ListValue args;
108 130
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
110 132
111 args.Set(0, event); 133 args.Set(0, event);
112 std::string json_args; 134 std::string json_args;
113 base::JSONWriter::Write(&args, &json_args); 135 base::JSONWriter::Write(&args, &json_args);
114 router_->DispatchEventToExtension(src_extension_id_, 136 router_->DispatchEventToExtension(src_extension_id_, extension, json_args,
115 events::kOnAPIResourceEvent, 137 profile_, src_url_);
116 json_args, profile_, src_url_);
117 } 138 }
118 139
119 DictionaryValue* APIResourceEventNotifier::CreateAPIResourceEvent( 140 DictionaryValue* APIResourceEventNotifier::CreateAPIResourceEvent(
120 APIResourceEventType event_type) { 141 APIResourceEventType event_type) {
121 DictionaryValue* event = new DictionaryValue(); 142 DictionaryValue* event = new DictionaryValue();
122 event->SetString(kEventTypeKey, APIResourceEventTypeToString(event_type)); 143 event->SetString(kEventTypeKey, APIResourceEventTypeToString(event_type));
123 event->SetInteger(kSrcIdKey, src_id_); 144 event->SetInteger(kSrcIdKey, src_id_);
124 145
125 // TODO(miket): Signal that it's OK to clean up onEvent listeners. This is 146 // TODO(miket): Signal that it's OK to clean up onEvent listeners. This is
126 // the framework we'll use, but we need to start using it. 147 // the framework we'll use, but we need to start using it.
127 event->SetBoolean(kIsFinalEventKey, false); 148 event->SetBoolean(kIsFinalEventKey, false);
128 149
129 // The caller owns the created event, which typically is then given to a 150 // The caller owns the created event, which typically is then given to a
130 // ListValue to dispose of. 151 // ListValue to dispose of.
131 return event; 152 return event;
132 } 153 }
133 154
134 void APIResourceEventNotifier::SendEventWithResultCode( 155 void APIResourceEventNotifier::SendEventWithResultCode(
156 const std::string &extension,
135 APIResourceEventType event_type, 157 APIResourceEventType event_type,
136 int result_code) { 158 int result_code) {
137 if (src_id_ < 0) 159 if (src_id_ < 0)
138 return; 160 return;
139 161
140 DictionaryValue* event = CreateAPIResourceEvent(event_type); 162 DictionaryValue* event = CreateAPIResourceEvent(event_type);
141 event->SetInteger(kResultCodeKey, result_code); 163 event->SetInteger(kResultCodeKey, result_code);
142 DispatchEvent(event); 164 DispatchEvent(extension, event);
143 } 165 }
144 166
145 } // namespace extensions 167 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698