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

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: 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 scoped pointer to the event payload.
imcheng 2017/07/11 01:31:15 Update the comments here, but also see mfoltz@'s o
zhaobin 2017/07/12 21:58:08 Done.
76 using EventDispatchCallback =
77 base::Callback<void(const std::string&, std::unique_ptr<Event>)>;
78
79 // Receives incoming messages and errors and provides additional API context.
80 class CastMessageHandler : public cast_channel::CastSocket::Observer {
81 public:
82 CastMessageHandler(const EventDispatchCallback& ui_dispatch_cb,
83 scoped_refptr<cast_channel::Logger> logger);
84 ~CastMessageHandler() override;
85
86 // CastSocket::Observer implementation.
87 void OnError(const cast_channel::CastSocket& socket,
88 cast_channel::ChannelError error_state) override;
89 void OnMessage(const cast_channel::CastSocket& socket,
90 const cast_channel::CastMessage& message) override;
91
92 void RegisterExtensionId(const std::string& extension_id);
93
94 private:
95 // Callback for sending events to the extension.
96 // Should be bound to a weak pointer, to prevent any use-after-free
97 // conditions.
98 EventDispatchCallback const ui_dispatch_cb_;
99 // Logger object for reporting error details.
100 scoped_refptr<cast_channel::Logger> logger_;
101 // Extension IDs call open channel function with this observer.
102 std::set<std::string> extension_ids_;
103
104 THREAD_CHECKER(thread_checker_);
105
106 DISALLOW_COPY_AND_ASSIGN(CastMessageHandler);
107 };
108
68 ~CastChannelAPI() override; 109 ~CastChannelAPI() override;
69 110
70 // BrowserContextKeyedAPI implementation. 111 // BrowserContextKeyedAPI implementation.
71 static const char* service_name() { return "CastChannelAPI"; } 112 static const char* service_name() { return "CastChannelAPI"; }
72 113
73 content::BrowserContext* const browser_context_; 114 content::BrowserContext* const browser_context_;
74 std::unique_ptr<cast_channel::CastSocket> socket_for_test_; 115 std::unique_ptr<cast_channel::CastSocket> socket_for_test_;
116 // Created and destroyed on the IO thread.
117 std::unique_ptr<CastMessageHandler, content::BrowserThread::DeleteOnIOThread>
118 observer_;
75 119
76 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI); 120 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI);
77 }; 121 };
78 122
79 class CastChannelAsyncApiFunction : public AsyncApiFunction { 123 class CastChannelAsyncApiFunction : public AsyncApiFunction {
80 public: 124 public:
81 CastChannelAsyncApiFunction(); 125 CastChannelAsyncApiFunction();
82 126
83 protected: 127 protected:
84 ~CastChannelAsyncApiFunction() override; 128 ~CastChannelAsyncApiFunction() override;
85 129
86 // AsyncApiFunction: 130 // AsyncApiFunction:
87 bool PrePrepare() override; 131 bool PrePrepare() override;
88 bool Respond() override; 132 bool Respond() override;
89 133
90 // Sets the function result to a ChannelInfo obtained from the state of 134 // Sets the function result to a ChannelInfo obtained from the state of
91 // |socket|. 135 // |socket|.
92 void SetResultFromSocket(const cast_channel::CastSocket& socket); 136 void SetResultFromSocket(const cast_channel::CastSocket& socket);
93 137
94 // Sets the function result to a ChannelInfo populated with |channel_id| and 138 // Sets the function result to a ChannelInfo populated with |channel_id| and
95 // |error|. 139 // |error|.
96 void SetResultFromError(int channel_id, 140 void SetResultFromError(int channel_id,
97 api::cast_channel::ChannelError error); 141 api::cast_channel::ChannelError error);
98 142
99 // Manages creating and removing Cast sockets. 143 // Raw pointer of leaky singleton CastSocketService, which manages creating
100 scoped_refptr<cast_channel::CastSocketService> cast_socket_service_; 144 // and removing Cast sockets.
145 cast_channel::CastSocketService* cast_socket_service_;
101 146
102 private: 147 private:
103 // Sets the function result from |channel_info|. 148 // Sets the function result from |channel_info|.
104 void SetResultFromChannelInfo( 149 void SetResultFromChannelInfo(
105 const api::cast_channel::ChannelInfo& channel_info); 150 const api::cast_channel::ChannelInfo& channel_info);
106 }; 151 };
107 152
108 class CastChannelOpenFunction : public CastChannelAsyncApiFunction { 153 class CastChannelOpenFunction : public CastChannelAsyncApiFunction {
109 public: 154 public:
110 CastChannelOpenFunction(); 155 CastChannelOpenFunction();
111 156
112 protected: 157 protected:
113 ~CastChannelOpenFunction() override; 158 ~CastChannelOpenFunction() override;
114 159
115 // AsyncApiFunction: 160 // AsyncApiFunction:
116 bool PrePrepare() override; 161 bool PrePrepare() override;
117 bool Prepare() override; 162 bool Prepare() override;
118 void AsyncWorkStart() override; 163 void AsyncWorkStart() override;
119 164
120 private: 165 private:
121 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN) 166 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN)
122 167
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 168 // Validates that |connect_info| represents a valid IP end point and returns a
153 // new IPEndPoint if so. Otherwise returns nullptr. 169 // new IPEndPoint if so. Otherwise returns nullptr.
154 static net::IPEndPoint* ParseConnectInfo( 170 static net::IPEndPoint* ParseConnectInfo(
155 const api::cast_channel::ConnectInfo& connect_info); 171 const api::cast_channel::ConnectInfo& connect_info);
156 172
157 void OnOpen(int channel_id, cast_channel::ChannelError result); 173 void OnOpen(int channel_id, cast_channel::ChannelError result);
158 174
159 std::unique_ptr<api::cast_channel::Open::Params> params_; 175 std::unique_ptr<api::cast_channel::Open::Params> params_;
160 CastChannelAPI* api_; 176 CastChannelAPI* api_;
161 std::unique_ptr<net::IPEndPoint> ip_endpoint_; 177 std::unique_ptr<net::IPEndPoint> ip_endpoint_;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 222
207 std::unique_ptr<api::cast_channel::Close::Params> params_; 223 std::unique_ptr<api::cast_channel::Close::Params> params_;
208 CastChannelAPI* api_; 224 CastChannelAPI* api_;
209 225
210 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction); 226 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction);
211 }; 227 };
212 228
213 } // namespace extensions 229 } // namespace extensions
214 230
215 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ 231 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698