| 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/dial/dial_api.h" | 5 #include "chrome/browser/extensions/api/dial/dial_api.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "chrome/browser/extensions/api/dial/dial_api_factory.h" | 10 #include "chrome/browser/extensions/api/dial/dial_api_factory.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 DialAPI::DialAPI(Profile* profile) | 39 DialAPI::DialAPI(Profile* profile) |
| 40 : RefcountedBrowserContextKeyedService(BrowserThread::IO), | 40 : RefcountedBrowserContextKeyedService(BrowserThread::IO), |
| 41 profile_(profile) { | 41 profile_(profile) { |
| 42 ExtensionSystem::Get(profile)->event_router()->RegisterObserver( | 42 ExtensionSystem::Get(profile)->event_router()->RegisterObserver( |
| 43 this, dial::OnDeviceList::kEventName); | 43 this, dial::OnDeviceList::kEventName); |
| 44 } | 44 } |
| 45 | 45 |
| 46 DialAPI::~DialAPI() {} | 46 DialAPI::~DialAPI() {} |
| 47 | 47 |
| 48 DialRegistry* DialAPI::dial_registry() { | 48 DialRegistry* DialAPI::dial_registry() { |
| 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 49 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 50 if (!dial_registry_.get()) { | 50 if (!dial_registry_.get()) { |
| 51 dial_registry_.reset(new DialRegistry(this, | 51 dial_registry_.reset(new DialRegistry(this, |
| 52 TimeDelta::FromSeconds(kDialRefreshIntervalSecs), | 52 TimeDelta::FromSeconds(kDialRefreshIntervalSecs), |
| 53 TimeDelta::FromSeconds(kDialExpirationSecs), | 53 TimeDelta::FromSeconds(kDialExpirationSecs), |
| 54 kDialMaxDevices)); | 54 kDialMaxDevices)); |
| 55 } | 55 } |
| 56 return dial_registry_.get(); | 56 return dial_registry_.get(); |
| 57 } | 57 } |
| 58 | 58 |
| 59 void DialAPI::OnListenerAdded(const EventListenerInfo& details) { | 59 void DialAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 60 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 61 BrowserThread::PostTask( | 61 BrowserThread::PostTask( |
| 62 BrowserThread::IO, FROM_HERE, | 62 BrowserThread::IO, FROM_HERE, |
| 63 base::Bind(&DialAPI::NotifyListenerAddedOnIOThread, this)); | 63 base::Bind(&DialAPI::NotifyListenerAddedOnIOThread, this)); |
| 64 } | 64 } |
| 65 | 65 |
| 66 void DialAPI::OnListenerRemoved(const EventListenerInfo& details) { | 66 void DialAPI::OnListenerRemoved(const EventListenerInfo& details) { |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 67 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 68 BrowserThread::PostTask( | 68 BrowserThread::PostTask( |
| 69 BrowserThread::IO, FROM_HERE, | 69 BrowserThread::IO, FROM_HERE, |
| 70 base::Bind(&DialAPI::NotifyListenerRemovedOnIOThread, this)); | 70 base::Bind(&DialAPI::NotifyListenerRemovedOnIOThread, this)); |
| 71 } | 71 } |
| 72 | 72 |
| 73 void DialAPI::NotifyListenerAddedOnIOThread() { | 73 void DialAPI::NotifyListenerAddedOnIOThread() { |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 74 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 75 VLOG(1) << "DIAL device event listener added."; | 75 VLOG(1) << "DIAL device event listener added."; |
| 76 dial_registry()->OnListenerAdded(); | 76 dial_registry()->OnListenerAdded(); |
| 77 } | 77 } |
| 78 | 78 |
| 79 void DialAPI::NotifyListenerRemovedOnIOThread() { | 79 void DialAPI::NotifyListenerRemovedOnIOThread() { |
| 80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 80 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 81 VLOG(1) << "DIAL device event listener removed"; | 81 VLOG(1) << "DIAL device event listener removed"; |
| 82 dial_registry()->OnListenerRemoved(); | 82 dial_registry()->OnListenerRemoved(); |
| 83 } | 83 } |
| 84 | 84 |
| 85 void DialAPI::OnDialDeviceEvent(const DialRegistry::DeviceList& devices) { | 85 void DialAPI::OnDialDeviceEvent(const DialRegistry::DeviceList& devices) { |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 86 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 87 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 87 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 88 base::Bind(&DialAPI::SendEventOnUIThread, this, devices)); | 88 base::Bind(&DialAPI::SendEventOnUIThread, this, devices)); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void DialAPI::OnDialError(const DialRegistry::DialErrorCode code) { | 91 void DialAPI::OnDialError(const DialRegistry::DialErrorCode code) { |
| 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 92 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 93 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 93 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 94 base::Bind(&DialAPI::SendErrorOnUIThread, this, code)); | 94 base::Bind(&DialAPI::SendErrorOnUIThread, this, code)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 void DialAPI::SendEventOnUIThread(const DialRegistry::DeviceList& devices) { | 97 void DialAPI::SendEventOnUIThread(const DialRegistry::DeviceList& devices) { |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 98 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 99 | 99 |
| 100 std::vector<linked_ptr<api::dial::DialDevice> > args; | 100 std::vector<linked_ptr<api::dial::DialDevice> > args; |
| 101 for (DialRegistry::DeviceList::const_iterator it = devices.begin(); | 101 for (DialRegistry::DeviceList::const_iterator it = devices.begin(); |
| 102 it != devices.end(); ++it) { | 102 it != devices.end(); ++it) { |
| 103 linked_ptr<api::dial::DialDevice> api_device = | 103 linked_ptr<api::dial::DialDevice> api_device = |
| 104 make_linked_ptr(new api::dial::DialDevice); | 104 make_linked_ptr(new api::dial::DialDevice); |
| 105 it->FillDialDevice(api_device.get()); | 105 it->FillDialDevice(api_device.get()); |
| 106 args.push_back(api_device); | 106 args.push_back(api_device); |
| 107 } | 107 } |
| 108 scoped_ptr<base::ListValue> results = api::dial::OnDeviceList::Create(args); | 108 scoped_ptr<base::ListValue> results = api::dial::OnDeviceList::Create(args); |
| 109 scoped_ptr<Event> event( | 109 scoped_ptr<Event> event( |
| 110 new Event(dial::OnDeviceList::kEventName, results.Pass())); | 110 new Event(dial::OnDeviceList::kEventName, results.Pass())); |
| 111 extensions::ExtensionSystem::Get(profile_)->event_router()-> | 111 extensions::ExtensionSystem::Get(profile_)->event_router()-> |
| 112 BroadcastEvent(event.Pass()); | 112 BroadcastEvent(event.Pass()); |
| 113 } | 113 } |
| 114 | 114 |
| 115 void DialAPI::SendErrorOnUIThread(const DialRegistry::DialErrorCode code) { | 115 void DialAPI::SendErrorOnUIThread(const DialRegistry::DialErrorCode code) { |
| 116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 116 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 117 | 117 |
| 118 api::dial::DialError dial_error; | 118 api::dial::DialError dial_error; |
| 119 switch (code) { | 119 switch (code) { |
| 120 case DialRegistry::DIAL_NO_LISTENERS: | 120 case DialRegistry::DIAL_NO_LISTENERS: |
| 121 dial_error.code = api::dial::DIAL_ERROR_CODE_NO_LISTENERS; | 121 dial_error.code = api::dial::DIAL_ERROR_CODE_NO_LISTENERS; |
| 122 break; | 122 break; |
| 123 case DialRegistry::DIAL_NO_INTERFACES: | 123 case DialRegistry::DIAL_NO_INTERFACES: |
| 124 dial_error.code = api::dial::DIAL_ERROR_CODE_NO_VALID_NETWORK_INTERFACES; | 124 dial_error.code = api::dial::DIAL_ERROR_CODE_NO_VALID_NETWORK_INTERFACES; |
| 125 break; | 125 break; |
| 126 case DialRegistry::DIAL_CELLULAR_NETWORK: | 126 case DialRegistry::DIAL_CELLULAR_NETWORK: |
| (...skipping 18 matching lines...) Expand all Loading... |
| 145 | 145 |
| 146 void DialAPI::ShutdownOnUIThread() {} | 146 void DialAPI::ShutdownOnUIThread() {} |
| 147 | 147 |
| 148 namespace api { | 148 namespace api { |
| 149 | 149 |
| 150 DialDiscoverNowFunction::DialDiscoverNowFunction() | 150 DialDiscoverNowFunction::DialDiscoverNowFunction() |
| 151 : dial_(NULL), result_(false) { | 151 : dial_(NULL), result_(false) { |
| 152 } | 152 } |
| 153 | 153 |
| 154 bool DialDiscoverNowFunction::Prepare() { | 154 bool DialDiscoverNowFunction::Prepare() { |
| 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 155 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 156 DCHECK(browser_context()); | 156 DCHECK(browser_context()); |
| 157 dial_ = DialAPIFactory::GetForBrowserContext(browser_context()).get(); | 157 dial_ = DialAPIFactory::GetForBrowserContext(browser_context()).get(); |
| 158 return true; | 158 return true; |
| 159 } | 159 } |
| 160 | 160 |
| 161 void DialDiscoverNowFunction::Work() { | 161 void DialDiscoverNowFunction::Work() { |
| 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 162 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 163 result_ = dial_->dial_registry()->DiscoverNow(); | 163 result_ = dial_->dial_registry()->DiscoverNow(); |
| 164 } | 164 } |
| 165 | 165 |
| 166 bool DialDiscoverNowFunction::Respond() { | 166 bool DialDiscoverNowFunction::Respond() { |
| 167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 167 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 168 if (!result_) | 168 if (!result_) |
| 169 error_ = kDialServiceError; | 169 error_ = kDialServiceError; |
| 170 | 170 |
| 171 SetResult(new base::FundamentalValue(result_)); | 171 SetResult(new base::FundamentalValue(result_)); |
| 172 return true; | 172 return true; |
| 173 } | 173 } |
| 174 | 174 |
| 175 } // namespace api | 175 } // namespace api |
| 176 | 176 |
| 177 } // namespace extensions | 177 } // namespace extensions |
| OLD | NEW |