OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/dial/dial_api.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "chrome/browser/extensions/api/dial/dial_api_factory.h" |
| 10 #include "chrome/browser/extensions/event_names.h" |
| 11 #include "chrome/browser/extensions/event_router.h" |
| 12 #include "chrome/browser/extensions/extension_system.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/extensions/api/dial.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 const char kDialServiceError[] = "Dial service error."; |
| 20 |
| 21 } // namespace |
| 22 |
| 23 namespace extensions { |
| 24 |
| 25 DialAPI::DialAPI(Profile* profile) |
| 26 : RefcountedProfileKeyedService(content::BrowserThread::IO), |
| 27 profile_(profile) { |
| 28 ExtensionSystem::Get(profile)->event_router()->RegisterObserver( |
| 29 this, extensions::event_names::kOnDialDeviceList); |
| 30 } |
| 31 |
| 32 DialAPI::~DialAPI() {} |
| 33 |
| 34 DialRegistry* DialAPI::dial_registry() { |
| 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 36 if (!dial_registry_.get()) { |
| 37 dial_registry_.reset(new DialRegistry(this)); |
| 38 } |
| 39 return dial_registry_.get(); |
| 40 } |
| 41 |
| 42 void DialAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 44 BrowserThread::PostTask( |
| 45 BrowserThread::IO, FROM_HERE, |
| 46 base::Bind(&DialAPI::NotifyListenerAddedOnIOThread, this)); |
| 47 } |
| 48 |
| 49 void DialAPI::OnListenerRemoved(const EventListenerInfo& details) { |
| 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 51 BrowserThread::PostTask( |
| 52 BrowserThread::IO, FROM_HERE, |
| 53 base::Bind(&DialAPI::NotifyListenerRemovedOnIOThread, this)); |
| 54 } |
| 55 |
| 56 void DialAPI::NotifyListenerAddedOnIOThread() { |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 58 DVLOG(1) << "DIAL device event listener added."; |
| 59 dial_registry()->OnListenerAdded(); |
| 60 } |
| 61 |
| 62 void DialAPI::NotifyListenerRemovedOnIOThread() { |
| 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 64 DVLOG(1) << "DIAL device event listener removed"; |
| 65 dial_registry()->OnListenerRemoved(); |
| 66 } |
| 67 |
| 68 void DialAPI::OnDialDeviceEvent(const DialRegistry::DeviceList& devices) { |
| 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 70 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 71 base::Bind(&DialAPI::SendEventOnUIThread, this, devices)); |
| 72 } |
| 73 |
| 74 void DialAPI::SendEventOnUIThread(const DialRegistry::DeviceList& devices) { |
| 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 76 |
| 77 std::vector<linked_ptr<api::dial::DialDevice> > args; |
| 78 for (DialRegistry::DeviceList::const_iterator it = devices.begin(); |
| 79 it != devices.end(); ++it) { |
| 80 linked_ptr<api::dial::DialDevice> api_device = |
| 81 make_linked_ptr(new api::dial::DialDevice); |
| 82 it->FillDialDevice(api_device.get()); |
| 83 args.push_back(api_device); |
| 84 } |
| 85 scoped_ptr<base::ListValue> results = api::dial::OnDeviceList::Create(args); |
| 86 |
| 87 scoped_ptr<Event> event( |
| 88 new Event(event_names::kOnDialDeviceList, results.Pass())); |
| 89 |
| 90 extensions::ExtensionSystem::Get(profile_)->event_router()-> |
| 91 BroadcastEvent(event.Pass()); |
| 92 } |
| 93 |
| 94 void DialAPI::ShutdownOnUIThread() {} |
| 95 |
| 96 namespace api { |
| 97 |
| 98 DialDiscoverNowFunction::DialDiscoverNowFunction() |
| 99 : dial_(NULL), result_(false) { |
| 100 } |
| 101 |
| 102 bool DialDiscoverNowFunction::Prepare() { |
| 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 104 DCHECK(profile()); |
| 105 dial_ = DialAPIFactory::GetInstance()->GetForProfile(profile()); |
| 106 return true; |
| 107 } |
| 108 |
| 109 void DialDiscoverNowFunction::Work() { |
| 110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 111 result_ = dial_->dial_registry()->DiscoverNow(); |
| 112 } |
| 113 |
| 114 bool DialDiscoverNowFunction::Respond() { |
| 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 116 if (!result_) |
| 117 error_ = kDialServiceError; |
| 118 |
| 119 SetResult(base::Value::CreateBooleanValue(result_)); |
| 120 return true; |
| 121 } |
| 122 |
| 123 } // namespace api |
| 124 |
| 125 } // namespace extensions |
OLD | NEW |