Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: chrome/browser/extensions/api/dial/dial_api.h

Issue 2583853004: [chrome.dial] Adds chrome.dial.fetchDeviceDecription API. (Closed)
Patch Set: Respond to lazyboy@ comments. Rebase w/namespace changes Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #ifndef CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_ 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_ 6 #define CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_
7 7
8 #include <memory> 8 #include <memory>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "chrome/browser/extensions/api/dial/dial_device_data.h" 11 #include "chrome/browser/extensions/api/dial/dial_device_data.h"
12 #include "chrome/browser/extensions/api/dial/dial_registry.h" 12 #include "chrome/browser/extensions/api/dial/dial_registry.h"
13 #include "chrome/common/extensions/api/dial.h"
13 #include "components/keyed_service/core/refcounted_keyed_service.h" 14 #include "components/keyed_service/core/refcounted_keyed_service.h"
14 #include "extensions/browser/api/async_api_function.h" 15 #include "extensions/browser/api/async_api_function.h"
15 #include "extensions/browser/event_router.h" 16 #include "extensions/browser/event_router.h"
16 17
17 namespace extensions { 18 namespace extensions {
18 19
19 namespace api { 20 namespace api {
20 namespace dial { 21 namespace dial {
21 class DialRegistry; 22 class DeviceDescriptionFetcher;
22 } // namespace dial 23 } // namespace dial
23 } // namespace api 24 } // namespace api
24 25
26 class DialFetchDeviceDescriptionFunction;
27
25 // Dial API which is a ref-counted KeyedService that manages 28 // Dial API which is a ref-counted KeyedService that manages
26 // the DIAL registry. It takes care of creating the registry on the IO thread 29 // the DIAL registry. It takes care of creating the registry on the IO thread
27 // and is an observer of the registry. It makes sure devices events are sent out 30 // and is an observer of the registry. It makes sure devices events are sent out
28 // to extension listeners on the right thread. 31 // to extension listeners on the right thread.
29 // 32 //
30 // TODO(mfoltz): This should probably inherit from BrowserContextKeyedAPI 33 // TODO(mfoltz): This should probably inherit from BrowserContextKeyedAPI
31 // instead; ShutdownOnUIThread below is a no-op, which is the whole point of 34 // instead; ShutdownOnUIThread below is a no-op, which is the whole point of
32 // RefcountedKeyedService. 35 // RefcountedKeyedService.
36 //
37 // TODO(mfoltz): The threading model for this API needs to be rethought. At a
38 // minimum, DialRegistry should move to the UI thread to avoid extra thread hops
39 // here, allow a straightforward GetDeviceList implementation
40 // (crbug.com/576817), cleanup some long-tail crashes (crbug.com/640011) that
41 // are likely due to lifetime issues, and simplify unit tests
42 // (crbug.com/661457).
33 class DialAPI : public RefcountedKeyedService, 43 class DialAPI : public RefcountedKeyedService,
34 public EventRouter::Observer, 44 public EventRouter::Observer,
35 public api::dial::DialRegistry::Observer { 45 public api::dial::DialRegistry::Observer {
36 public: 46 public:
37 explicit DialAPI(Profile* profile); 47 explicit DialAPI(Profile* profile);
38 48
39 // The DialRegistry for the API. This must always be used only from the IO 49 // The DialRegistry for the API. This must always be used only from the IO
40 // thread. 50 // thread.
41 api::dial::DialRegistry* dial_registry(); 51 api::dial::DialRegistry* dial_registry();
42 52
43 // Called by the DialRegistry on the IO thread so that the DialAPI dispatches 53 // Called by the DialRegistry on the IO thread so that the DialAPI dispatches
44 // the event to listeners on the UI thread. 54 // the event to listeners on the UI thread.
45 void SendEventOnUIThread(const api::dial::DialRegistry::DeviceList& devices); 55 void SendEventOnUIThread(const api::dial::DialRegistry::DeviceList& devices);
46 void SendErrorOnUIThread(const api::dial::DialRegistry::DialErrorCode type); 56 void SendErrorOnUIThread(const api::dial::DialRegistry::DialErrorCode type);
47 57
58 // Sets test device data. For tests only.
Wez 2017/01/05 22:29:24 nit: Suggest removing the "For tests only", since
mark a. foltz 2017/01/09 21:38:07 Done.
59 void SetDeviceForTest(
60 const api::dial::DialDeviceData& device_data,
61 const api::dial::DialDeviceDescriptionData& device_description);
62
48 private: 63 private:
49 ~DialAPI() override; 64 ~DialAPI() override;
50 65
66 friend class DialFetchDeviceDescriptionFunction;
67
51 // RefcountedKeyedService: 68 // RefcountedKeyedService:
52 void ShutdownOnUIThread() override; 69 void ShutdownOnUIThread() override;
53 70
54 // EventRouter::Observer: 71 // EventRouter::Observer:
55 void OnListenerAdded(const EventListenerInfo& details) override; 72 void OnListenerAdded(const EventListenerInfo& details) override;
56 void OnListenerRemoved(const EventListenerInfo& details) override; 73 void OnListenerRemoved(const EventListenerInfo& details) override;
57 74
58 // DialRegistry::Observer: 75 // DialRegistry::Observer:
59 void OnDialDeviceEvent( 76 void OnDialDeviceEvent(
60 const api::dial::DialRegistry::DeviceList& devices) override; 77 const api::dial::DialRegistry::DeviceList& devices) override;
61 void OnDialError(api::dial::DialRegistry::DialErrorCode type) override; 78 void OnDialError(api::dial::DialRegistry::DialErrorCode type) override;
62 79
63 // Methods to notify the DialRegistry on the correct thread of new/removed 80 // Methods to notify the DialRegistry on the correct thread of new/removed
64 // listeners. 81 // listeners.
65 void NotifyListenerAddedOnIOThread(); 82 void NotifyListenerAddedOnIOThread();
66 void NotifyListenerRemovedOnIOThread(); 83 void NotifyListenerRemovedOnIOThread();
67 84
68 Profile* profile_; 85 Profile* profile_;
69 86
70 // Created lazily on first access on the IO thread. 87 // Created lazily on first access on the IO thread.
71 std::unique_ptr<api::dial::DialRegistry> dial_registry_; 88 std::unique_ptr<api::dial::DialRegistry> dial_registry_;
72 89
90 // Device data for testing.
91 std::unique_ptr<api::dial::DialDeviceData> test_device_data_;
92 std::unique_ptr<api::dial::DialDeviceDescriptionData>
93 test_device_description_;
Wez 2017/01/05 22:29:24 Rather than have these fields and having to friend
mark a. foltz 2017/01/09 21:38:07 I like that idea. I'd rather take it on as part o
Wez 2017/01/09 22:12:58 Acknowledged.
94
73 DISALLOW_COPY_AND_ASSIGN(DialAPI); 95 DISALLOW_COPY_AND_ASSIGN(DialAPI);
74 }; 96 };
75 97
76 // DiscoverNow function. This function needs a round-trip from the IO thread 98 // DiscoverNow function. This function needs a round-trip from the IO thread
77 // because it needs to grab a pointer to the DIAL API in order to get a 99 // because it needs to grab a pointer to the DIAL API in order to get a
78 // reference to the DialRegistry while on the IO thread. Then, the result 100 // reference to the DialRegistry while on the IO thread. Then, the result
79 // must be returned on the UI thread. 101 // must be returned on the UI thread.
80 class DialDiscoverNowFunction : public AsyncApiFunction { 102 class DialDiscoverNowFunction : public AsyncApiFunction {
81 public: 103 public:
82 DialDiscoverNowFunction(); 104 DialDiscoverNowFunction();
(...skipping 13 matching lines...) Expand all
96 DialAPI* dial_; 118 DialAPI* dial_;
97 119
98 // Result of the discoverNow call to the DIAL registry. This result is 120 // Result of the discoverNow call to the DIAL registry. This result is
99 // retrieved on the IO thread but the function result is returned on the UI 121 // retrieved on the IO thread but the function result is returned on the UI
100 // thread. 122 // thread.
101 bool result_; 123 bool result_;
102 124
103 DISALLOW_COPY_AND_ASSIGN(DialDiscoverNowFunction); 125 DISALLOW_COPY_AND_ASSIGN(DialDiscoverNowFunction);
104 }; 126 };
105 127
128 class DialFetchDeviceDescriptionFunction : public AsyncExtensionFunction {
129 public:
130 DialFetchDeviceDescriptionFunction();
131
132 protected:
133 ~DialFetchDeviceDescriptionFunction() override;
134
135 // AsyncExtensionFunction:
136 bool RunAsync() override;
137
138 private:
139 DECLARE_EXTENSION_FUNCTION("dial.fetchDeviceDescription",
140 DIAL_FETCHDEVICEDESCRIPTION)
141
142 void GetDeviceDescriptionUrlOnIOThread(const std::string& label);
143 void MaybeStartFetch(const GURL& url);
144 void OnFetchComplete(const api::dial::DialDeviceDescriptionData& result);
145 void OnFetchError(const std::string& result);
146
147 std::unique_ptr<api::dial::FetchDeviceDescription::Params> params_;
148 std::unique_ptr<api::dial::DeviceDescriptionFetcher>
149 device_description_fetcher_;
150
151 DialAPI* dial_;
152
153 DISALLOW_COPY_AND_ASSIGN(DialFetchDeviceDescriptionFunction);
154 };
155
106 } // namespace extensions 156 } // namespace extensions
107 157
108 #endif // CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_ 158 #endif // CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698