Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / remoting / host / register_support_host_request.h
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
3 // found in the LICENSE file.
4
5 #ifndef REMOTING_HOST_REGISTER_SUPPORT_HOST_REQUEST_H_
6 #define REMOTING_HOST_REGISTER_SUPPORT_HOST_REQUEST_H_
7
8 #include <string>
9
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "remoting/base/rsa_key_pair.h"
14 #include "remoting/signaling/signal_strategy.h"
15 #include "testing/gtest/include/gtest/gtest_prod.h"
16
17 namespace buzz {
18 class XmlElement;
19 }  // namespace buzz
20
21 namespace base {
22 class TimeDelta;
23 }  // namespace base
24
25 namespace remoting {
26
27 class IqRequest;
28 class IqSender;
29
30 // RegisterSupportHostRequest sends a request to register the host for
31 // a SupportID, as soon as the associated SignalStrategy becomes
32 // connected. When a response is received from the bot, it calls the
33 // callback specified in the Init() method.
34 class RegisterSupportHostRequest : public SignalStrategy::Listener {
35  public:
36   // First parameter is set to true on success. Second parameter is
37   // the new SessionID received from the bot. Third parameter is the
38   // amount of time until that id expires.
39   typedef base::Callback<void(bool, const std::string&,
40                               const base::TimeDelta&)> RegisterCallback;
41
42   // |signal_strategy| and |key_pair| must outlive this
43   // object. |callback| is called when registration response is
44   // received from the server. Callback is never called if the bot
45   // malfunctions and doesn't respond to the request.
46   //
47   // TODO(sergeyu): This class should have timeout for the bot
48   // response.
49   RegisterSupportHostRequest(SignalStrategy* signal_strategy,
50                              scoped_refptr<RsaKeyPair> key_pair,
51                              const std::string& directory_bot_jid,
52                              const RegisterCallback& callback);
53   ~RegisterSupportHostRequest() override;
54
55   // HostStatusObserver implementation.
56   void OnSignalStrategyStateChange(SignalStrategy::State state) override;
57   bool OnSignalStrategyIncomingStanza(const buzz::XmlElement* stanza) override;
58
59  private:
60   void DoSend();
61
62   scoped_ptr<buzz::XmlElement> CreateRegistrationRequest(
63       const std::string& jid);
64   scoped_ptr<buzz::XmlElement> CreateSignature(const std::string& jid);
65
66   void ProcessResponse(IqRequest* request, const buzz::XmlElement* response);
67   bool ParseResponse(const buzz::XmlElement* response,
68                      std::string* support_id, base::TimeDelta* lifetime);
69
70   void CallCallback(
71       bool success, const std::string& support_id, base::TimeDelta lifetime);
72
73   SignalStrategy* signal_strategy_;
74   scoped_refptr<RsaKeyPair> key_pair_;
75   std::string directory_bot_jid_;
76   RegisterCallback callback_;
77
78   scoped_ptr<IqSender> iq_sender_;
79   scoped_ptr<IqRequest> request_;
80
81   DISALLOW_COPY_AND_ASSIGN(RegisterSupportHostRequest);
82 };
83
84 }  // namespace remoting
85
86 #endif  // REMOTING_HOST_REGISTER_SUPPORT_HOST_REQUEST_H_