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
17namespace buzz {
18class XmlElement;
19}  // namespace buzz
20
21namespace base {
22class TimeDelta;
23}  // namespace base
24
25namespace remoting {
26
27class IqRequest;
28class 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.
34class 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  virtual ~RegisterSupportHostRequest();
54
55  // HostStatusObserver implementation.
56  virtual void OnSignalStrategyStateChange(
57      SignalStrategy::State state) OVERRIDE;
58  virtual bool OnSignalStrategyIncomingStanza(
59      const buzz::XmlElement* stanza) OVERRIDE;
60
61 private:
62  void DoSend();
63
64  scoped_ptr<buzz::XmlElement> CreateRegistrationRequest(
65      const std::string& jid);
66  scoped_ptr<buzz::XmlElement> CreateSignature(const std::string& jid);
67
68  void ProcessResponse(IqRequest* request, const buzz::XmlElement* response);
69  bool ParseResponse(const buzz::XmlElement* response,
70                     std::string* support_id, base::TimeDelta* lifetime);
71
72  void CallCallback(
73      bool success, const std::string& support_id, base::TimeDelta lifetime);
74
75  SignalStrategy* signal_strategy_;
76  scoped_refptr<RsaKeyPair> key_pair_;
77  std::string directory_bot_jid_;
78  RegisterCallback callback_;
79
80  scoped_ptr<IqSender> iq_sender_;
81  scoped_ptr<IqRequest> request_;
82
83  DISALLOW_COPY_AND_ASSIGN(RegisterSupportHostRequest);
84};
85
86}  // namespace remoting
87
88#endif  // REMOTING_HOST_REGISTER_SUPPORT_HOST_REQUEST_H_
89