- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / cast_channel / cast_channel_api.h
1 // Copyright 2013 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.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_CHANNEL_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_CHANNEL_API_H_
7
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/threading/thread_checker.h"
11 #include "chrome/browser/extensions/api/api_function.h"
12 #include "chrome/browser/extensions/api/api_resource_manager.h"
13 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h"
14 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
15 #include "chrome/browser/extensions/extension_function.h"
16 #include "chrome/common/extensions/api/cast_channel.h"
17
18 class GURL;
19 class Profile;
20 class CastChannelAPITest;
21
22 namespace extensions {
23
24 namespace cast_channel = api::cast_channel;
25
26 class CastChannelAPI : public ProfileKeyedAPI,
27                        public cast_channel::CastSocket::Delegate {
28
29  public:
30   explicit CastChannelAPI(Profile* profile);
31
32   static CastChannelAPI* Get(Profile* profile);
33
34   // ProfileKeyedAPI implementation.
35   static ProfileKeyedAPIFactory<CastChannelAPI>* GetFactoryInstance();
36
37   // Returns a new CastSocket that connects to |url| and is to be owned by
38   // |extension_id|.
39   scoped_ptr<cast_channel::CastSocket> CreateCastSocket(
40       const std::string& extension_id, const GURL& gurl);
41
42   // Sets the CastSocket instance to be returned by CreateCastSocket for
43   // testing.
44   void SetSocketForTest(scoped_ptr<cast_channel::CastSocket> socket_for_test);
45
46  private:
47   friend class ProfileKeyedAPIFactory<CastChannelAPI>;
48   friend class ::CastChannelAPITest;
49
50   virtual ~CastChannelAPI();
51
52   // CastSocket::Delegate.  Called on IO thread.
53   virtual void OnError(const cast_channel::CastSocket* socket,
54                        cast_channel::ChannelError error) OVERRIDE;
55   virtual void OnMessage(const cast_channel::CastSocket* socket,
56                          const cast_channel::MessageInfo& message) OVERRIDE;
57
58   // ProfileKeyedAPI implementation.
59   static const char* service_name() {
60     return "CastChannelAPI";
61   }
62
63   Profile* const profile_;
64   scoped_ptr<cast_channel::CastSocket> socket_for_test_;
65
66   DISALLOW_COPY_AND_ASSIGN(CastChannelAPI);
67 };
68
69 class CastChannelAsyncApiFunction : public AsyncApiFunction {
70  public:
71   CastChannelAsyncApiFunction();
72
73  protected:
74   virtual ~CastChannelAsyncApiFunction();
75
76   // AsyncApiFunction:
77   virtual bool PrePrepare() OVERRIDE;
78   virtual bool Respond() OVERRIDE;
79
80   // Returns the socket corresponding to |channel_id| if one exists.  Otherwise,
81   // sets the function result with CHANNEL_ERROR_INVALID_CHANNEL_ID, completes
82   // the function, and returns null.
83   cast_channel::CastSocket* GetSocketOrCompleteWithError(int channel_id);
84
85   // Adds |socket| to |manager_| and returns the new channel_id.  |manager_|
86   // assumes ownership of |socket|.
87   int AddSocket(cast_channel::CastSocket* socket);
88
89   // Removes the CastSocket corresponding to |channel_id| from the resource
90   // manager.
91   void RemoveSocket(int channel_id);
92
93   // Sets the function result to a ChannelInfo obtained from the state of the
94   // CastSocket corresponding to |channel_id|.
95   void SetResultFromSocket(int channel_id);
96
97   // Sets the function result to a ChannelInfo with |error|.
98   void SetResultFromError(cast_channel::ChannelError error);
99
100   // Returns the socket corresponding to |channel_id| if one exists, or null
101   // otherwise.
102   cast_channel::CastSocket* GetSocket(int channel_id);
103
104  private:
105   // Sets the function result from |channel_info|.
106   void SetResultFromChannelInfo(
107       const cast_channel::ChannelInfo& channel_info);
108
109   // The API resource manager for CastSockets.
110   ApiResourceManager<cast_channel::CastSocket>* manager_;
111
112   // The result of the function.
113   cast_channel::ChannelError error_;
114 };
115
116 class CastChannelOpenFunction : public CastChannelAsyncApiFunction {
117  public:
118   CastChannelOpenFunction();
119
120  protected:
121   virtual ~CastChannelOpenFunction();
122
123   // AsyncApiFunction:
124   virtual bool PrePrepare() OVERRIDE;
125   virtual bool Prepare() OVERRIDE;
126   virtual void AsyncWorkStart() OVERRIDE;
127
128  private:
129   DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN)
130
131   void OnOpen(int result);
132
133   scoped_ptr<cast_channel::Open::Params> params_;
134   // The id of the newly opened socket.
135   int new_channel_id_;
136   CastChannelAPI* api_;
137
138   DISALLOW_COPY_AND_ASSIGN(CastChannelOpenFunction);
139 };
140
141 class CastChannelSendFunction : public CastChannelAsyncApiFunction {
142  public:
143   CastChannelSendFunction();
144
145  protected:
146   virtual ~CastChannelSendFunction();
147
148   // AsyncApiFunction:
149   virtual bool Prepare() OVERRIDE;
150   virtual void AsyncWorkStart() OVERRIDE;
151
152  private:
153   DECLARE_EXTENSION_FUNCTION("cast.channel.send", CAST_CHANNEL_SEND)
154
155   void OnSend(int result);
156
157   scoped_ptr<cast_channel::Send::Params> params_;
158
159   DISALLOW_COPY_AND_ASSIGN(CastChannelSendFunction);
160 };
161
162 class CastChannelCloseFunction : public CastChannelAsyncApiFunction {
163  public:
164   CastChannelCloseFunction();
165
166  protected:
167   virtual ~CastChannelCloseFunction();
168
169   // AsyncApiFunction:
170   virtual bool Prepare() OVERRIDE;
171   virtual void AsyncWorkStart() OVERRIDE;
172
173  private:
174   DECLARE_EXTENSION_FUNCTION("cast.channel.close", CAST_CHANNEL_CLOSE)
175
176   void OnClose(int result);
177
178   scoped_ptr<cast_channel::Close::Params> params_;
179
180   DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction);
181 };
182
183 }  // namespace extensions
184
185 #endif  // CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_CHANNEL_API_H_