| 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 26 matching lines...) Expand all Loading... |
| 37 APIResourceEventNotifier::APIResourceEventNotifier( | 37 APIResourceEventNotifier::APIResourceEventNotifier( |
| 38 ExtensionEventRouter* router, | 38 ExtensionEventRouter* router, |
| 39 Profile* profile, | 39 Profile* profile, |
| 40 const std::string& src_extension_id, | 40 const std::string& src_extension_id, |
| 41 int src_id, | 41 int src_id, |
| 42 const GURL& src_url) | 42 const GURL& src_url) |
| 43 : router_(router), | 43 : router_(router), |
| 44 profile_(profile), | 44 profile_(profile), |
| 45 src_extension_id_(src_extension_id), | 45 src_extension_id_(src_extension_id), |
| 46 src_id_(src_id), | 46 src_id_(src_id), |
| 47 src_url_(src_url) {} | 47 src_url_(src_url) { |
| 48 | 48 } |
| 49 APIResourceEventNotifier::~APIResourceEventNotifier() {} | |
| 50 | 49 |
| 51 void APIResourceEventNotifier::OnConnectComplete(int result_code) { | 50 void APIResourceEventNotifier::OnConnectComplete(int result_code) { |
| 52 SendEventWithResultCode(API_RESOURCE_EVENT_CONNECT_COMPLETE, result_code); | 51 SendEventWithResultCode(API_RESOURCE_EVENT_CONNECT_COMPLETE, result_code); |
| 53 } | 52 } |
| 54 | 53 |
| 55 void APIResourceEventNotifier::OnDataRead(int result_code, | 54 void APIResourceEventNotifier::OnDataRead(int result_code, |
| 56 base::ListValue* data, | 55 base::ListValue* data, |
| 57 const std::string& address, | 56 const std::string& address, |
| 58 int port) { | 57 int port) { |
| 59 // Do we have a destination for this event? There will be one if a source id | 58 // Do we have a destination for this event? There will be one if a source id |
| (...skipping 11 matching lines...) Expand all Loading... |
| 71 event->Set(kDataKey, data); | 70 event->Set(kDataKey, data); |
| 72 event->SetString(kAddressKey, address); | 71 event->SetString(kAddressKey, address); |
| 73 event->SetInteger(kPortKey, port); | 72 event->SetInteger(kPortKey, port); |
| 74 DispatchEvent(event); | 73 DispatchEvent(event); |
| 75 } | 74 } |
| 76 | 75 |
| 77 void APIResourceEventNotifier::OnWriteComplete(int result_code) { | 76 void APIResourceEventNotifier::OnWriteComplete(int result_code) { |
| 78 SendEventWithResultCode(API_RESOURCE_EVENT_WRITE_COMPLETE, result_code); | 77 SendEventWithResultCode(API_RESOURCE_EVENT_WRITE_COMPLETE, result_code); |
| 79 } | 78 } |
| 80 | 79 |
| 81 void APIResourceEventNotifier::SendEventWithResultCode( | 80 // static |
| 82 APIResourceEventType event_type, | 81 std::string APIResourceEventNotifier::APIResourceEventTypeToString( |
| 83 int result_code) { | 82 APIResourceEventType event_type) { |
| 84 if (src_id_ < 0) | 83 switch (event_type) { |
| 85 return; | 84 case API_RESOURCE_EVENT_CONNECT_COMPLETE: |
| 85 return kEventTypeConnectComplete; |
| 86 case API_RESOURCE_EVENT_DATA_READ: |
| 87 return kEventTypeDataRead; |
| 88 case API_RESOURCE_EVENT_WRITE_COMPLETE: |
| 89 return kEventTypeWriteComplete; |
| 90 } |
| 86 | 91 |
| 87 DictionaryValue* event = CreateAPIResourceEvent(event_type); | 92 NOTREACHED(); |
| 88 event->SetInteger(kResultCodeKey, result_code); | 93 return std::string(); |
| 89 DispatchEvent(event); | |
| 90 } | 94 } |
| 91 | 95 |
| 96 APIResourceEventNotifier::~APIResourceEventNotifier() {} |
| 97 |
| 92 void APIResourceEventNotifier::DispatchEvent(DictionaryValue* event) { | 98 void APIResourceEventNotifier::DispatchEvent(DictionaryValue* event) { |
| 93 BrowserThread::PostTask( | 99 BrowserThread::PostTask( |
| 94 BrowserThread::UI, FROM_HERE, | 100 BrowserThread::UI, FROM_HERE, |
| 95 base::Bind( | 101 base::Bind( |
| 96 &APIResourceEventNotifier::DispatchEventOnUIThread, this, event)); | 102 &APIResourceEventNotifier::DispatchEventOnUIThread, this, event)); |
| 97 } | 103 } |
| 98 | 104 |
| 99 void APIResourceEventNotifier::DispatchEventOnUIThread( | 105 void APIResourceEventNotifier::DispatchEventOnUIThread( |
| 100 DictionaryValue* event) { | 106 DictionaryValue* event) { |
| 101 ListValue args; | 107 ListValue args; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 118 | 124 |
| 119 // TODO(miket): Signal that it's OK to clean up onEvent listeners. This is | 125 // 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. | 126 // the framework we'll use, but we need to start using it. |
| 121 event->SetBoolean(kIsFinalEventKey, false); | 127 event->SetBoolean(kIsFinalEventKey, false); |
| 122 | 128 |
| 123 // The caller owns the created event, which typically is then given to a | 129 // The caller owns the created event, which typically is then given to a |
| 124 // ListValue to dispose of. | 130 // ListValue to dispose of. |
| 125 return event; | 131 return event; |
| 126 } | 132 } |
| 127 | 133 |
| 128 // static | 134 void APIResourceEventNotifier::SendEventWithResultCode( |
| 129 std::string APIResourceEventNotifier::APIResourceEventTypeToString( | 135 APIResourceEventType event_type, |
| 130 APIResourceEventType event_type) { | 136 int result_code) { |
| 131 switch (event_type) { | 137 if (src_id_ < 0) |
| 132 case API_RESOURCE_EVENT_CONNECT_COMPLETE: | 138 return; |
| 133 return kEventTypeConnectComplete; | |
| 134 case API_RESOURCE_EVENT_DATA_READ: | |
| 135 return kEventTypeDataRead; | |
| 136 case API_RESOURCE_EVENT_WRITE_COMPLETE: | |
| 137 return kEventTypeWriteComplete; | |
| 138 } | |
| 139 | 139 |
| 140 NOTREACHED(); | 140 DictionaryValue* event = CreateAPIResourceEvent(event_type); |
| 141 return std::string(); | 141 event->SetInteger(kResultCodeKey, result_code); |
| 142 DispatchEvent(event); |
| 142 } | 143 } |
| 143 | 144 |
| 144 } // namespace extensions | 145 } // namespace extensions |
| OLD | NEW |