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

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

Issue 2583853004: [chrome.dial] Adds chrome.dial.fetchDeviceDecription API. (Closed)
Patch Set: Rebase and fix typo. 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. This would also 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).
43 //
44 // Also, DialRegistry should be an interface that can be mocked and injected for
45 // tests; this would allow us to remove code that injects test data into the
46 // real DialRegsitry.
33 class DialAPI : public RefcountedKeyedService, 47 class DialAPI : public RefcountedKeyedService,
34 public EventRouter::Observer, 48 public EventRouter::Observer,
35 public api::dial::DialRegistry::Observer { 49 public api::dial::DialRegistry::Observer {
36 public: 50 public:
37 explicit DialAPI(Profile* profile); 51 explicit DialAPI(Profile* profile);
38 52
39 // The DialRegistry for the API. This must always be used only from the IO 53 // The DialRegistry for the API. This must always be used only from the IO
40 // thread. 54 // thread.
41 api::dial::DialRegistry* dial_registry(); 55 api::dial::DialRegistry* dial_registry();
42 56
43 // Called by the DialRegistry on the IO thread so that the DialAPI dispatches 57 // Called by the DialRegistry on the IO thread so that the DialAPI dispatches
44 // the event to listeners on the UI thread. 58 // the event to listeners on the UI thread.
45 void SendEventOnUIThread(const api::dial::DialRegistry::DeviceList& devices); 59 void SendEventOnUIThread(const api::dial::DialRegistry::DeviceList& devices);
46 void SendErrorOnUIThread(const api::dial::DialRegistry::DialErrorCode type); 60 void SendErrorOnUIThread(const api::dial::DialRegistry::DialErrorCode type);
47 61
62 // Sets test device data.
63 void SetDeviceForTest(
64 const api::dial::DialDeviceData& device_data,
65 const api::dial::DialDeviceDescriptionData& device_description);
66
48 private: 67 private:
49 ~DialAPI() override; 68 ~DialAPI() override;
50 69
70 friend class DialFetchDeviceDescriptionFunction;
71
51 // RefcountedKeyedService: 72 // RefcountedKeyedService:
52 void ShutdownOnUIThread() override; 73 void ShutdownOnUIThread() override;
53 74
54 // EventRouter::Observer: 75 // EventRouter::Observer:
55 void OnListenerAdded(const EventListenerInfo& details) override; 76 void OnListenerAdded(const EventListenerInfo& details) override;
56 void OnListenerRemoved(const EventListenerInfo& details) override; 77 void OnListenerRemoved(const EventListenerInfo& details) override;
57 78
58 // DialRegistry::Observer: 79 // DialRegistry::Observer:
59 void OnDialDeviceEvent( 80 void OnDialDeviceEvent(
60 const api::dial::DialRegistry::DeviceList& devices) override; 81 const api::dial::DialRegistry::DeviceList& devices) override;
61 void OnDialError(api::dial::DialRegistry::DialErrorCode type) override; 82 void OnDialError(api::dial::DialRegistry::DialErrorCode type) override;
62 83
63 // Methods to notify the DialRegistry on the correct thread of new/removed 84 // Methods to notify the DialRegistry on the correct thread of new/removed
64 // listeners. 85 // listeners.
65 void NotifyListenerAddedOnIOThread(); 86 void NotifyListenerAddedOnIOThread();
66 void NotifyListenerRemovedOnIOThread(); 87 void NotifyListenerRemovedOnIOThread();
67 88
68 Profile* profile_; 89 Profile* profile_;
69 90
70 // Created lazily on first access on the IO thread. 91 // Created lazily on first access on the IO thread.
71 std::unique_ptr<api::dial::DialRegistry> dial_registry_; 92 std::unique_ptr<api::dial::DialRegistry> dial_registry_;
72 93
94 // Device data for testing.
95 std::unique_ptr<api::dial::DialDeviceData> test_device_data_;
96 std::unique_ptr<api::dial::DialDeviceDescriptionData>
97 test_device_description_;
98
73 DISALLOW_COPY_AND_ASSIGN(DialAPI); 99 DISALLOW_COPY_AND_ASSIGN(DialAPI);
74 }; 100 };
75 101
76 // DiscoverNow function. This function needs a round-trip from the IO thread 102 // 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 103 // 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 104 // reference to the DialRegistry while on the IO thread. Then, the result
79 // must be returned on the UI thread. 105 // must be returned on the UI thread.
80 class DialDiscoverNowFunction : public AsyncApiFunction { 106 class DialDiscoverNowFunction : public AsyncApiFunction {
81 public: 107 public:
82 DialDiscoverNowFunction(); 108 DialDiscoverNowFunction();
(...skipping 13 matching lines...) Expand all
96 DialAPI* dial_; 122 DialAPI* dial_;
97 123
98 // Result of the discoverNow call to the DIAL registry. This result is 124 // 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 125 // retrieved on the IO thread but the function result is returned on the UI
100 // thread. 126 // thread.
101 bool result_; 127 bool result_;
102 128
103 DISALLOW_COPY_AND_ASSIGN(DialDiscoverNowFunction); 129 DISALLOW_COPY_AND_ASSIGN(DialDiscoverNowFunction);
104 }; 130 };
105 131
132 class DialFetchDeviceDescriptionFunction : public AsyncExtensionFunction {
133 public:
134 DialFetchDeviceDescriptionFunction();
135
136 protected:
137 ~DialFetchDeviceDescriptionFunction() override;
138
139 // AsyncExtensionFunction:
140 bool RunAsync() override;
141
142 private:
143 DECLARE_EXTENSION_FUNCTION("dial.fetchDeviceDescription",
144 DIAL_FETCHDEVICEDESCRIPTION)
145
146 void GetDeviceDescriptionUrlOnIOThread(const std::string& label);
147 void MaybeStartFetch(const GURL& url);
148 void OnFetchComplete(const api::dial::DialDeviceDescriptionData& result);
149 void OnFetchError(const std::string& result);
150
151 std::unique_ptr<api::dial::FetchDeviceDescription::Params> params_;
152 std::unique_ptr<api::dial::DeviceDescriptionFetcher>
153 device_description_fetcher_;
154
155 DialAPI* dial_;
156
157 DISALLOW_COPY_AND_ASSIGN(DialFetchDeviceDescriptionFunction);
158 };
159
106 } // namespace extensions 160 } // namespace extensions
107 161
108 #endif // CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_ 162 #endif // CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_API_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698