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