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

Side by Side Diff: extensions/browser/api/cast_channel/cast_channel_api.h

Issue 2974523002: [cast_channel] Make CastSocketService a global leaky singleton (Closed)
Patch Set: merge with master Created 3 years, 5 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ 5 #ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ 6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 10
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 // Returns a test CastSocket instance, if it is defined. 53 // Returns a test CastSocket instance, if it is defined.
54 // Otherwise returns a scoped_ptr with a nullptr value. 54 // Otherwise returns a scoped_ptr with a nullptr value.
55 std::unique_ptr<cast_channel::CastSocket> GetSocketForTest(); 55 std::unique_ptr<cast_channel::CastSocket> GetSocketForTest();
56 56
57 // Returns the API browser context. 57 // Returns the API browser context.
58 content::BrowserContext* GetBrowserContext() const; 58 content::BrowserContext* GetBrowserContext() const;
59 59
60 // Sends an event to the extension's EventRouter, if it exists. 60 // Sends an event to the extension's EventRouter, if it exists.
61 void SendEvent(const std::string& extension_id, std::unique_ptr<Event> event); 61 void SendEvent(const std::string& extension_id, std::unique_ptr<Event> event);
62 62
63 // Registers |extension_id| with |observer_| and returns |observer_|.
64 cast_channel::CastSocket::Observer* GetObserver(
65 const std::string& extension_id,
66 scoped_refptr<cast_channel::Logger> logger);
67
63 private: 68 private:
64 friend class BrowserContextKeyedAPIFactory<CastChannelAPI>; 69 friend class BrowserContextKeyedAPIFactory<CastChannelAPI>;
65 friend class ::CastChannelAPITest; 70 friend class ::CastChannelAPITest;
66 friend class CastTransportDelegate; 71 friend class CastTransportDelegate;
67 72
73 // Defines a callback used to send events to the extension's
74 // EventRouter.
75 // Parameter #0 is a unique pointer to the event payload.
76 using EventDispatchCallback = base::Callback<void(std::unique_ptr<Event>)>;
77
78 // Receives incoming messages and errors and provides additional API context.
79 class CastMessageHandler : public cast_channel::CastSocket::Observer {
80 public:
81 CastMessageHandler(const EventDispatchCallback& ui_dispatch_cb,
82 scoped_refptr<cast_channel::Logger> logger);
83 ~CastMessageHandler() override;
84
85 // CastSocket::Observer implementation.
86 void OnError(const cast_channel::CastSocket& socket,
87 cast_channel::ChannelError error_state) override;
88 void OnMessage(const cast_channel::CastSocket& socket,
89 const cast_channel::CastMessage& message) override;
90
91 void RegisterExtensionId(const std::string& extension_id);
92
93 private:
94 // Callback for sending events to the extension.
95 // Should be bound to a weak pointer, to prevent any use-after-free
96 // conditions.
97 EventDispatchCallback const ui_dispatch_cb_;
98 // Logger object for reporting error details.
99 scoped_refptr<cast_channel::Logger> logger_;
100
101 THREAD_CHECKER(thread_checker_);
102
103 DISALLOW_COPY_AND_ASSIGN(CastMessageHandler);
104 };
105
68 ~CastChannelAPI() override; 106 ~CastChannelAPI() override;
69 107
70 // BrowserContextKeyedAPI implementation. 108 // BrowserContextKeyedAPI implementation.
71 static const char* service_name() { return "CastChannelAPI"; } 109 static const char* service_name() { return "CastChannelAPI"; }
72 110
73 content::BrowserContext* const browser_context_; 111 content::BrowserContext* const browser_context_;
74 std::unique_ptr<cast_channel::CastSocket> socket_for_test_; 112 std::unique_ptr<cast_channel::CastSocket> socket_for_test_;
113 // Created and destroyed on the IO thread.
114 std::unique_ptr<CastMessageHandler, content::BrowserThread::DeleteOnIOThread>
115 observer_;
75 116
76 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI); 117 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI);
77 }; 118 };
78 119
79 class CastChannelAsyncApiFunction : public AsyncApiFunction { 120 class CastChannelAsyncApiFunction : public AsyncApiFunction {
80 public: 121 public:
81 CastChannelAsyncApiFunction(); 122 CastChannelAsyncApiFunction();
82 123
83 protected: 124 protected:
84 ~CastChannelAsyncApiFunction() override; 125 ~CastChannelAsyncApiFunction() override;
85 126
86 // AsyncApiFunction: 127 // AsyncApiFunction:
87 bool PrePrepare() override; 128 bool PrePrepare() override;
88 bool Respond() override; 129 bool Respond() override;
89 130
90 // Sets the function result to a ChannelInfo obtained from the state of 131 // Sets the function result to a ChannelInfo obtained from the state of
91 // |socket|. 132 // |socket|.
92 void SetResultFromSocket(const cast_channel::CastSocket& socket); 133 void SetResultFromSocket(const cast_channel::CastSocket& socket);
93 134
94 // Sets the function result to a ChannelInfo populated with |channel_id| and 135 // Sets the function result to a ChannelInfo populated with |channel_id| and
95 // |error|. 136 // |error|.
96 void SetResultFromError(int channel_id, 137 void SetResultFromError(int channel_id,
97 api::cast_channel::ChannelError error); 138 api::cast_channel::ChannelError error);
98 139
99 // Manages creating and removing Cast sockets. 140 // Raw pointer of leaky singleton CastSocketService, which manages creating
100 scoped_refptr<cast_channel::CastSocketService> cast_socket_service_; 141 // and removing Cast sockets.
142 cast_channel::CastSocketService* cast_socket_service_;
101 143
102 private: 144 private:
103 // Sets the function result from |channel_info|. 145 // Sets the function result from |channel_info|.
104 void SetResultFromChannelInfo( 146 void SetResultFromChannelInfo(
105 const api::cast_channel::ChannelInfo& channel_info); 147 const api::cast_channel::ChannelInfo& channel_info);
106 }; 148 };
107 149
108 class CastChannelOpenFunction : public CastChannelAsyncApiFunction { 150 class CastChannelOpenFunction : public CastChannelAsyncApiFunction {
109 public: 151 public:
110 CastChannelOpenFunction(); 152 CastChannelOpenFunction();
111 153
112 protected: 154 protected:
113 ~CastChannelOpenFunction() override; 155 ~CastChannelOpenFunction() override;
114 156
115 // AsyncApiFunction: 157 // AsyncApiFunction:
116 bool PrePrepare() override; 158 bool PrePrepare() override;
117 bool Prepare() override; 159 bool Prepare() override;
118 void AsyncWorkStart() override; 160 void AsyncWorkStart() override;
119 161
120 private: 162 private:
121 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN) 163 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN)
122 164
123 // Defines a callback used to send events to the extension's
124 // EventRouter.
125 // Parameter #0 is a scoped pointer to the event payload.
126 using EventDispatchCallback = base::Callback<void(std::unique_ptr<Event>)>;
127
128 // Receives incoming messages and errors and provides additional API context.
129 class CastMessageHandler : public cast_channel::CastSocket::Observer {
130 public:
131 CastMessageHandler(const EventDispatchCallback& ui_dispatch_cb,
132 scoped_refptr<cast_channel::Logger> logger);
133 ~CastMessageHandler() override;
134
135 // CastSocket::Observer implementation.
136 void OnError(const cast_channel::CastSocket& socket,
137 cast_channel::ChannelError error_state) override;
138 void OnMessage(const cast_channel::CastSocket& socket,
139 const cast_channel::CastMessage& message) override;
140
141 private:
142 // Callback for sending events to the extension.
143 // Should be bound to a weak pointer, to prevent any use-after-free
144 // conditions.
145 EventDispatchCallback const ui_dispatch_cb_;
146 // Logger object for reporting error details.
147 scoped_refptr<cast_channel::Logger> logger_;
148
149 DISALLOW_COPY_AND_ASSIGN(CastMessageHandler);
150 };
151
152 // Validates that |connect_info| represents a valid IP end point and returns a 165 // Validates that |connect_info| represents a valid IP end point and returns a
153 // new IPEndPoint if so. Otherwise returns nullptr. 166 // new IPEndPoint if so. Otherwise returns nullptr.
154 static net::IPEndPoint* ParseConnectInfo( 167 static net::IPEndPoint* ParseConnectInfo(
155 const api::cast_channel::ConnectInfo& connect_info); 168 const api::cast_channel::ConnectInfo& connect_info);
156 169
157 void OnOpen(int channel_id, cast_channel::ChannelError result); 170 void OnOpen(int channel_id, cast_channel::ChannelError result);
158 171
159 std::unique_ptr<api::cast_channel::Open::Params> params_; 172 std::unique_ptr<api::cast_channel::Open::Params> params_;
160 CastChannelAPI* api_; 173 CastChannelAPI* api_;
161 std::unique_ptr<net::IPEndPoint> ip_endpoint_; 174 std::unique_ptr<net::IPEndPoint> ip_endpoint_;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 219
207 std::unique_ptr<api::cast_channel::Close::Params> params_; 220 std::unique_ptr<api::cast_channel::Close::Params> params_;
208 CastChannelAPI* api_; 221 CastChannelAPI* api_;
209 222
210 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction); 223 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction);
211 }; 224 };
212 225
213 } // namespace extensions 226 } // namespace extensions
214 227
215 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ 228 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_
OLDNEW
« no previous file with comments | « components/cast_channel/cast_test_util.cc ('k') | extensions/browser/api/cast_channel/cast_channel_api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698