| 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 24 matching lines...) Expand all Loading... |
| 35 APIResourceEventNotifier::APIResourceEventNotifier( | 35 APIResourceEventNotifier::APIResourceEventNotifier( |
| 36 ExtensionEventRouter* router, | 36 ExtensionEventRouter* router, |
| 37 Profile* profile, | 37 Profile* profile, |
| 38 const std::string& src_extension_id, | 38 const std::string& src_extension_id, |
| 39 int src_id, | 39 int src_id, |
| 40 const GURL& src_url) | 40 const GURL& src_url) |
| 41 : router_(router), | 41 : router_(router), |
| 42 profile_(profile), | 42 profile_(profile), |
| 43 src_extension_id_(src_extension_id), | 43 src_extension_id_(src_extension_id), |
| 44 src_id_(src_id), | 44 src_id_(src_id), |
| 45 src_url_(src_url) {} | 45 src_url_(src_url) { |
| 46 | 46 } |
| 47 APIResourceEventNotifier::~APIResourceEventNotifier() {} | |
| 48 | 47 |
| 49 void APIResourceEventNotifier::OnConnectComplete(int result_code) { | 48 void APIResourceEventNotifier::OnConnectComplete(int result_code) { |
| 50 SendEventWithResultCode(API_RESOURCE_EVENT_CONNECT_COMPLETE, result_code); | 49 SendEventWithResultCode(API_RESOURCE_EVENT_CONNECT_COMPLETE, result_code); |
| 51 } | 50 } |
| 52 | 51 |
| 53 void APIResourceEventNotifier::OnDataRead(int result_code, | 52 void APIResourceEventNotifier::OnDataRead(int result_code, |
| 54 base::ListValue* data) { | 53 base::ListValue* data) { |
| 55 // Do we have a destination for this event? There will be one if a source id | 54 // Do we have a destination for this event? There will be one if a source id |
| 56 // was injected by the request handler for the resource's create method in | 55 // was injected by the request handler for the resource's create method in |
| 57 // schema_generated_bindings.js, which will in turn be the case if the caller | 56 // schema_generated_bindings.js, which will in turn be the case if the caller |
| 58 // of the create method provided an onEvent closure. | 57 // of the create method provided an onEvent closure. |
| 59 if (src_id_ < 0) { | 58 if (src_id_ < 0) { |
| 60 delete data; | 59 delete data; |
| 61 return; | 60 return; |
| 62 } | 61 } |
| 63 | 62 |
| 64 DictionaryValue* event = CreateAPIResourceEvent( | 63 DictionaryValue* event = CreateAPIResourceEvent( |
| 65 API_RESOURCE_EVENT_DATA_READ); | 64 API_RESOURCE_EVENT_DATA_READ); |
| 66 event->SetInteger(kResultCodeKey, result_code); | 65 event->SetInteger(kResultCodeKey, result_code); |
| 67 event->Set(kDataKey, data); | 66 event->Set(kDataKey, data); |
| 68 DispatchEvent(event); | 67 DispatchEvent(event); |
| 69 } | 68 } |
| 70 | 69 |
| 71 void APIResourceEventNotifier::OnWriteComplete(int result_code) { | 70 void APIResourceEventNotifier::OnWriteComplete(int result_code) { |
| 72 SendEventWithResultCode(API_RESOURCE_EVENT_WRITE_COMPLETE, result_code); | 71 SendEventWithResultCode(API_RESOURCE_EVENT_WRITE_COMPLETE, result_code); |
| 73 } | 72 } |
| 74 | 73 |
| 75 void APIResourceEventNotifier::SendEventWithResultCode( | 74 // static |
| 76 APIResourceEventType event_type, | 75 std::string APIResourceEventNotifier::APIResourceEventTypeToString( |
| 77 int result_code) { | 76 APIResourceEventType event_type) { |
| 78 if (src_id_ < 0) | 77 switch (event_type) { |
| 79 return; | 78 case API_RESOURCE_EVENT_CONNECT_COMPLETE: |
| 79 return kEventTypeConnectComplete; |
| 80 case API_RESOURCE_EVENT_DATA_READ: |
| 81 return kEventTypeDataRead; |
| 82 case API_RESOURCE_EVENT_WRITE_COMPLETE: |
| 83 return kEventTypeWriteComplete; |
| 84 } |
| 80 | 85 |
| 81 DictionaryValue* event = CreateAPIResourceEvent(event_type); | 86 NOTREACHED(); |
| 82 event->SetInteger(kResultCodeKey, result_code); | 87 return std::string(); |
| 83 DispatchEvent(event); | |
| 84 } | 88 } |
| 85 | 89 |
| 90 APIResourceEventNotifier::~APIResourceEventNotifier() {} |
| 91 |
| 86 void APIResourceEventNotifier::DispatchEvent(DictionaryValue* event) { | 92 void APIResourceEventNotifier::DispatchEvent(DictionaryValue* event) { |
| 87 BrowserThread::PostTask( | 93 BrowserThread::PostTask( |
| 88 BrowserThread::UI, FROM_HERE, | 94 BrowserThread::UI, FROM_HERE, |
| 89 base::Bind( | 95 base::Bind( |
| 90 &APIResourceEventNotifier::DispatchEventOnUIThread, this, event)); | 96 &APIResourceEventNotifier::DispatchEventOnUIThread, this, event)); |
| 91 } | 97 } |
| 92 | 98 |
| 93 void APIResourceEventNotifier::DispatchEventOnUIThread( | 99 void APIResourceEventNotifier::DispatchEventOnUIThread( |
| 94 DictionaryValue* event) { | 100 DictionaryValue* event) { |
| 95 ListValue args; | 101 ListValue args; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 112 | 118 |
| 113 // TODO(miket): Signal that it's OK to clean up onEvent listeners. This is | 119 // TODO(miket): Signal that it's OK to clean up onEvent listeners. This is |
| 114 // the framework we'll use, but we need to start using it. | 120 // the framework we'll use, but we need to start using it. |
| 115 event->SetBoolean(kIsFinalEventKey, false); | 121 event->SetBoolean(kIsFinalEventKey, false); |
| 116 | 122 |
| 117 // The caller owns the created event, which typically is then given to a | 123 // The caller owns the created event, which typically is then given to a |
| 118 // ListValue to dispose of. | 124 // ListValue to dispose of. |
| 119 return event; | 125 return event; |
| 120 } | 126 } |
| 121 | 127 |
| 122 // static | 128 void APIResourceEventNotifier::SendEventWithResultCode( |
| 123 std::string APIResourceEventNotifier::APIResourceEventTypeToString( | 129 APIResourceEventType event_type, |
| 124 APIResourceEventType event_type) { | 130 int result_code) { |
| 125 switch (event_type) { | 131 if (src_id_ < 0) |
| 126 case API_RESOURCE_EVENT_CONNECT_COMPLETE: | 132 return; |
| 127 return kEventTypeConnectComplete; | |
| 128 case API_RESOURCE_EVENT_DATA_READ: | |
| 129 return kEventTypeDataRead; | |
| 130 case API_RESOURCE_EVENT_WRITE_COMPLETE: | |
| 131 return kEventTypeWriteComplete; | |
| 132 } | |
| 133 | 133 |
| 134 NOTREACHED(); | 134 DictionaryValue* event = CreateAPIResourceEvent(event_type); |
| 135 return std::string(); | 135 event->SetInteger(kResultCodeKey, result_code); |
| 136 DispatchEvent(event); |
| 136 } | 137 } |
| 137 | 138 |
| 138 } // namespace extensions | 139 } // namespace extensions |
| OLD | NEW |