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 REMOTING_HOST_IT2ME_IT2ME_NATIVE_MESSAGING_HOST_H_
6#define REMOTING_HOST_IT2ME_IT2ME_NATIVE_MESSAGING_HOST_H_
7
8#include "base/memory/ref_counted.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/memory/weak_ptr.h"
11#include "extensions/browser/api/messaging/native_messaging_channel.h"
12#include "remoting/base/auto_thread_task_runner.h"
13#include "remoting/host/it2me/it2me_host.h"
14
15namespace base {
16class DictionaryValue;
17}  // namespace base
18
19namespace remoting {
20
21// Implementation of the native messaging host process.
22class It2MeNativeMessagingHost
23    : public It2MeHost::Observer,
24      public extensions::NativeMessagingChannel::EventHandler {
25 public:
26  It2MeNativeMessagingHost(
27      scoped_refptr<AutoThreadTaskRunner> task_runner,
28      scoped_ptr<extensions::NativeMessagingChannel> channel,
29      scoped_ptr<It2MeHostFactory> factory);
30  virtual ~It2MeNativeMessagingHost();
31
32  void Start(const base::Closure& quit_closure);
33
34  // extensions::NativeMessagingChannel::EventHandler implementation.
35  virtual void OnMessage(scoped_ptr<base::Value> message) OVERRIDE;
36  virtual void OnDisconnect() OVERRIDE;
37
38  // It2MeHost::Observer implementation.
39  virtual void OnClientAuthenticated(const std::string& client_username)
40      OVERRIDE;
41  virtual void OnStoreAccessCode(const std::string& access_code,
42                                 base::TimeDelta access_code_lifetime) OVERRIDE;
43  virtual void OnNatPolicyChanged(bool nat_traversal_enabled) OVERRIDE;
44  virtual void OnStateChanged(It2MeHostState state) OVERRIDE;
45
46  static std::string HostStateToString(It2MeHostState host_state);
47
48 private:
49  // These "Process.." methods handle specific request types. The |response|
50  // dictionary is pre-filled by ProcessMessage() with the parts of the
51  // response already known ("id" and "type" fields).
52  void ProcessHello(const base::DictionaryValue& message,
53                    scoped_ptr<base::DictionaryValue> response) const;
54  void ProcessConnect(const base::DictionaryValue& message,
55                      scoped_ptr<base::DictionaryValue> response);
56  void ProcessDisconnect(const base::DictionaryValue& message,
57                         scoped_ptr<base::DictionaryValue> response);
58  void SendErrorAndExit(scoped_ptr<base::DictionaryValue> response,
59                        const std::string& description) const;
60
61  base::Closure quit_closure_;
62
63  scoped_refptr<AutoThreadTaskRunner> task_runner() const;
64
65  scoped_ptr<extensions::NativeMessagingChannel> channel_;
66  scoped_ptr<It2MeHostFactory> factory_;
67  scoped_ptr<ChromotingHostContext> host_context_;
68  scoped_refptr<It2MeHost> it2me_host_;
69
70  // Cached, read-only copies of |it2me_host_| session state.
71  It2MeHostState state_;
72  std::string access_code_;
73  base::TimeDelta access_code_lifetime_;
74  std::string client_username_;
75
76  // IT2Me Talk server configuration used by |it2me_host_| to connect.
77  XmppSignalStrategy::XmppServerConfig xmpp_server_config_;
78
79  // Chromoting Bot JID used by |it2me_host_| to register the host.
80  std::string directory_bot_jid_;
81
82  base::WeakPtr<It2MeNativeMessagingHost> weak_ptr_;
83  base::WeakPtrFactory<It2MeNativeMessagingHost> weak_factory_;
84
85  DISALLOW_COPY_AND_ASSIGN(It2MeNativeMessagingHost);
86};
87
88}  // namespace remoting
89
90#endif  // REMOTING_HOST_IT2ME_IT2ME_NATIVE_MESSAGING_HOST_H_
91
92