| OLD | NEW |
| 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" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 const char kSrcIdKey[] = "srcId"; | 31 const char kSrcIdKey[] = "srcId"; |
| 32 const char kIsFinalEventKey[] = "isFinalEvent"; | 32 const char kIsFinalEventKey[] = "isFinalEvent"; |
| 33 | 33 |
| 34 const char kResultCodeKey[] = "resultCode"; | 34 const char kResultCodeKey[] = "resultCode"; |
| 35 const char kDataKey[] = "data"; | 35 const char kDataKey[] = "data"; |
| 36 const char kAddressKey[] = "address"; | 36 const char kAddressKey[] = "address"; |
| 37 const char kPortKey[] = "port"; | 37 const char kPortKey[] = "port"; |
| 38 const char kErrorKey[] = "error"; | 38 const char kErrorKey[] = "error"; |
| 39 | 39 |
| 40 APIResourceEventNotifier::APIResourceEventNotifier( | 40 ApiResourceEventNotifier::ApiResourceEventNotifier( |
| 41 ExtensionEventRouter* router, | 41 ExtensionEventRouter* router, |
| 42 Profile* profile, | 42 Profile* profile, |
| 43 const std::string& src_extension_id, | 43 const std::string& src_extension_id, |
| 44 int src_id, | 44 int src_id, |
| 45 const GURL& src_url) | 45 const GURL& src_url) |
| 46 : router_(router), | 46 : router_(router), |
| 47 profile_(profile), | 47 profile_(profile), |
| 48 src_extension_id_(src_extension_id), | 48 src_extension_id_(src_extension_id), |
| 49 src_id_(src_id), | 49 src_id_(src_id), |
| 50 src_url_(src_url) { | 50 src_url_(src_url) { |
| 51 } | 51 } |
| 52 | 52 |
| 53 void APIResourceEventNotifier::OnConnectComplete(int result_code) { | 53 void ApiResourceEventNotifier::OnConnectComplete(int result_code) { |
| 54 SendEventWithResultCode(events::kExperimentalSocketOnEvent, | 54 SendEventWithResultCode(events::kExperimentalSocketOnEvent, |
| 55 API_RESOURCE_EVENT_CONNECT_COMPLETE, result_code); | 55 API_RESOURCE_EVENT_CONNECT_COMPLETE, result_code); |
| 56 } | 56 } |
| 57 | 57 |
| 58 void APIResourceEventNotifier::OnDataRead(int result_code, | 58 void ApiResourceEventNotifier::OnDataRead(int result_code, |
| 59 base::ListValue* data, | 59 base::ListValue* data, |
| 60 const std::string& address, | 60 const std::string& address, |
| 61 int port) { | 61 int port) { |
| 62 // 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 |
| 63 // 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 |
| 64 // 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 |
| 65 // of the create method provided an onEvent closure. | 65 // of the create method provided an onEvent closure. |
| 66 if (src_id_ < 0) { | 66 if (src_id_ < 0) { |
| 67 delete data; | 67 delete data; |
| 68 return; | 68 return; |
| 69 } | 69 } |
| 70 | 70 |
| 71 DictionaryValue* event = CreateAPIResourceEvent( | 71 DictionaryValue* event = CreateApiResourceEvent( |
| 72 API_RESOURCE_EVENT_DATA_READ); | 72 API_RESOURCE_EVENT_DATA_READ); |
| 73 event->SetInteger(kResultCodeKey, result_code); | 73 event->SetInteger(kResultCodeKey, result_code); |
| 74 event->Set(kDataKey, data); | 74 event->Set(kDataKey, data); |
| 75 event->SetString(kAddressKey, address); | 75 event->SetString(kAddressKey, address); |
| 76 event->SetInteger(kPortKey, port); | 76 event->SetInteger(kPortKey, port); |
| 77 DispatchEvent(events::kExperimentalSocketOnEvent, event); | 77 DispatchEvent(events::kExperimentalSocketOnEvent, event); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void APIResourceEventNotifier::OnWriteComplete(int result_code) { | 80 void ApiResourceEventNotifier::OnWriteComplete(int result_code) { |
| 81 SendEventWithResultCode(events::kExperimentalSocketOnEvent, | 81 SendEventWithResultCode(events::kExperimentalSocketOnEvent, |
| 82 API_RESOURCE_EVENT_WRITE_COMPLETE, result_code); | 82 API_RESOURCE_EVENT_WRITE_COMPLETE, result_code); |
| 83 } | 83 } |
| 84 | 84 |
| 85 void APIResourceEventNotifier::OnTransferComplete(UsbTransferStatus status, | 85 void ApiResourceEventNotifier::OnTransferComplete(UsbTransferStatus status, |
| 86 const std::string& error, | 86 const std::string& error, |
| 87 base::BinaryValue* data) { | 87 base::BinaryValue* data) { |
| 88 if (src_id_ < 0) { | 88 if (src_id_ < 0) { |
| 89 delete data; | 89 delete data; |
| 90 return; | 90 return; |
| 91 } | 91 } |
| 92 | 92 |
| 93 DictionaryValue* event = CreateAPIResourceEvent( | 93 DictionaryValue* event = CreateApiResourceEvent( |
| 94 API_RESOURCE_EVENT_TRANSFER_COMPLETE); | 94 API_RESOURCE_EVENT_TRANSFER_COMPLETE); |
| 95 event->SetInteger(kResultCodeKey, status); | 95 event->SetInteger(kResultCodeKey, status); |
| 96 event->Set(kDataKey, data); | 96 event->Set(kDataKey, data); |
| 97 if (!error.empty()) { | 97 if (!error.empty()) { |
| 98 event->SetString(kErrorKey, error); | 98 event->SetString(kErrorKey, error); |
| 99 } | 99 } |
| 100 | 100 |
| 101 DispatchEvent(events::kExperimentalUsbOnEvent, event); | 101 DispatchEvent(events::kExperimentalUsbOnEvent, event); |
| 102 } | 102 } |
| 103 | 103 |
| 104 // static | 104 // static |
| 105 std::string APIResourceEventNotifier::APIResourceEventTypeToString( | 105 std::string ApiResourceEventNotifier::ApiResourceEventTypeToString( |
| 106 APIResourceEventType event_type) { | 106 ApiResourceEventType event_type) { |
| 107 switch (event_type) { | 107 switch (event_type) { |
| 108 case API_RESOURCE_EVENT_CONNECT_COMPLETE: | 108 case API_RESOURCE_EVENT_CONNECT_COMPLETE: |
| 109 return kEventTypeConnectComplete; | 109 return kEventTypeConnectComplete; |
| 110 case API_RESOURCE_EVENT_DATA_READ: | 110 case API_RESOURCE_EVENT_DATA_READ: |
| 111 return kEventTypeDataRead; | 111 return kEventTypeDataRead; |
| 112 case API_RESOURCE_EVENT_WRITE_COMPLETE: | 112 case API_RESOURCE_EVENT_WRITE_COMPLETE: |
| 113 return kEventTypeWriteComplete; | 113 return kEventTypeWriteComplete; |
| 114 case API_RESOURCE_EVENT_TRANSFER_COMPLETE: | 114 case API_RESOURCE_EVENT_TRANSFER_COMPLETE: |
| 115 return kEventTypeTransferComplete; | 115 return kEventTypeTransferComplete; |
| 116 } | 116 } |
| 117 | 117 |
| 118 NOTREACHED(); | 118 NOTREACHED(); |
| 119 return std::string(); | 119 return std::string(); |
| 120 } | 120 } |
| 121 | 121 |
| 122 APIResourceEventNotifier::~APIResourceEventNotifier() {} | 122 ApiResourceEventNotifier::~ApiResourceEventNotifier() {} |
| 123 | 123 |
| 124 void APIResourceEventNotifier::DispatchEvent(const std::string &extension, | 124 void ApiResourceEventNotifier::DispatchEvent(const std::string &extension, |
| 125 DictionaryValue* event) { | 125 DictionaryValue* event) { |
| 126 BrowserThread::PostTask( | 126 BrowserThread::PostTask( |
| 127 BrowserThread::UI, FROM_HERE, | 127 BrowserThread::UI, FROM_HERE, |
| 128 base::Bind( | 128 base::Bind( |
| 129 &APIResourceEventNotifier::DispatchEventOnUIThread, this, extension, | 129 &ApiResourceEventNotifier::DispatchEventOnUIThread, this, extension, |
| 130 event)); | 130 event)); |
| 131 } | 131 } |
| 132 | 132 |
| 133 void APIResourceEventNotifier::DispatchEventOnUIThread( | 133 void ApiResourceEventNotifier::DispatchEventOnUIThread( |
| 134 const std::string &extension, DictionaryValue* event) { | 134 const std::string &extension, DictionaryValue* event) { |
| 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 136 | 136 |
| 137 ListValue args; | 137 ListValue args; |
| 138 args.Set(0, event); | 138 args.Set(0, event); |
| 139 router_->DispatchEventToExtension(src_extension_id_, extension, args, | 139 router_->DispatchEventToExtension(src_extension_id_, extension, args, |
| 140 profile_, src_url_); | 140 profile_, src_url_); |
| 141 } | 141 } |
| 142 | 142 |
| 143 DictionaryValue* APIResourceEventNotifier::CreateAPIResourceEvent( | 143 DictionaryValue* ApiResourceEventNotifier::CreateApiResourceEvent( |
| 144 APIResourceEventType event_type) { | 144 ApiResourceEventType event_type) { |
| 145 DictionaryValue* event = new DictionaryValue(); | 145 DictionaryValue* event = new DictionaryValue(); |
| 146 event->SetString(kEventTypeKey, APIResourceEventTypeToString(event_type)); | 146 event->SetString(kEventTypeKey, ApiResourceEventTypeToString(event_type)); |
| 147 event->SetInteger(kSrcIdKey, src_id_); | 147 event->SetInteger(kSrcIdKey, src_id_); |
| 148 | 148 |
| 149 // TODO(miket): Signal that it's OK to clean up onEvent listeners. This is | 149 // TODO(miket): Signal that it's OK to clean up onEvent listeners. This is |
| 150 // the framework we'll use, but we need to start using it. | 150 // the framework we'll use, but we need to start using it. |
| 151 event->SetBoolean(kIsFinalEventKey, false); | 151 event->SetBoolean(kIsFinalEventKey, false); |
| 152 | 152 |
| 153 // The caller owns the created event, which typically is then given to a | 153 // The caller owns the created event, which typically is then given to a |
| 154 // ListValue to dispose of. | 154 // ListValue to dispose of. |
| 155 return event; | 155 return event; |
| 156 } | 156 } |
| 157 | 157 |
| 158 void APIResourceEventNotifier::SendEventWithResultCode( | 158 void ApiResourceEventNotifier::SendEventWithResultCode( |
| 159 const std::string &extension, | 159 const std::string &extension, |
| 160 APIResourceEventType event_type, | 160 ApiResourceEventType event_type, |
| 161 int result_code) { | 161 int result_code) { |
| 162 if (src_id_ < 0) | 162 if (src_id_ < 0) |
| 163 return; | 163 return; |
| 164 | 164 |
| 165 DictionaryValue* event = CreateAPIResourceEvent(event_type); | 165 DictionaryValue* event = CreateApiResourceEvent(event_type); |
| 166 event->SetInteger(kResultCodeKey, result_code); | 166 event->SetInteger(kResultCodeKey, result_code); |
| 167 DispatchEvent(extension, event); | 167 DispatchEvent(extension, event); |
| 168 } | 168 } |
| 169 | 169 |
| 170 } // namespace extensions | 170 } // namespace extensions |
| OLD | NEW |