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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "chrome/browser/extensions/api/dial/dial_device_data.h"
11 #include "chrome/browser/extensions/api/dial/dial_registry.h"
12 #include "components/keyed_service/core/refcounted_keyed_service.h"
13 #include "extensions/browser/api/async_api_function.h"
14 #include "extensions/browser/event_router.h"
16 namespace extensions
{
20 // Dial API which is a ref-counted KeyedService that manages
21 // the DIAL registry. It takes care of creating the registry on the IO thread
22 // and is an observer of the registry. It makes sure devices events are sent out
23 // to extension listeners on the right thread.
25 // TODO(mfoltz): This should probably inherit from BrowserContextKeyedAPI
26 // instead; ShutdownOnUIThread below is a no-op, which is the whole point of
27 // RefcountedKeyedService.
28 class DialAPI
: public RefcountedKeyedService
,
29 public EventRouter::Observer
,
30 public DialRegistry::Observer
{
32 explicit DialAPI(Profile
* profile
);
34 // The DialRegistry for the API. This must always be used only from the IO
36 DialRegistry
* dial_registry();
38 // Called by the DialRegistry on the IO thread so that the DialAPI dispatches
39 // the event to listeners on the UI thread.
40 void SendEventOnUIThread(const DialRegistry::DeviceList
& devices
);
41 void SendErrorOnUIThread(const DialRegistry::DialErrorCode type
);
46 // RefcountedKeyedService:
47 void ShutdownOnUIThread() override
;
49 // EventRouter::Observer:
50 void OnListenerAdded(const EventListenerInfo
& details
) override
;
51 void OnListenerRemoved(const EventListenerInfo
& details
) override
;
53 // DialRegistry::Observer:
54 void OnDialDeviceEvent(const DialRegistry::DeviceList
& devices
) override
;
55 void OnDialError(DialRegistry::DialErrorCode type
) override
;
57 // Methods to notify the DialRegistry on the correct thread of new/removed
59 void NotifyListenerAddedOnIOThread();
60 void NotifyListenerRemovedOnIOThread();
64 // Created lazily on first access on the IO thread.
65 scoped_ptr
<DialRegistry
> dial_registry_
;
67 DISALLOW_COPY_AND_ASSIGN(DialAPI
);
72 // DiscoverNow function. This function needs a round-trip from the IO thread
73 // because it needs to grab a pointer to the DIAL API in order to get a
74 // reference to the DialRegistry while on the IO thread. Then, the result
75 // must be returned on the UI thread.
76 class DialDiscoverNowFunction
: public AsyncApiFunction
{
78 DialDiscoverNowFunction();
81 ~DialDiscoverNowFunction() override
{}
84 bool Prepare() override
;
86 bool Respond() override
;
89 DECLARE_EXTENSION_FUNCTION("dial.discoverNow", DIAL_DISCOVERNOW
)
91 // Pointer to the DIAL API for this profile. We get this on the UI thread.
94 // Result of the discoverNow call to the DIAL registry. This result is
95 // retrieved on the IO thread but the function result is returned on the UI
99 DISALLOW_COPY_AND_ASSIGN(DialDiscoverNowFunction
);
104 } // namespace extensions
106 #endif // CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_