Do not create the NativeViewport PlatformEventSource when headless.
[chromium-blink-merge.git] / remoting / protocol / negotiating_authenticator_base.h
blob9357d204e6d0fa6f6a0435068087b5a0f3669347
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.
5 #ifndef REMOTING_PROTOCOL_NEGOTIATING_AUTHENTICATOR_BASE_H_
6 #define REMOTING_PROTOCOL_NEGOTIATING_AUTHENTICATOR_BASE_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "remoting/protocol/authentication_method.h"
15 #include "remoting/protocol/authenticator.h"
16 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
18 namespace remoting {
19 namespace protocol {
21 // This class provides the common base for a meta-authenticator that allows
22 // clients and hosts that support multiple authentication methods to negotiate a
23 // method to use.
25 // The typical flow is:
26 // * Client sends a message to host with its supported methods.
27 // (clients may additionally pick a method and send its first message).
28 // * Host picks a method and sends its first message (if any).
29 // (if a message for that method was sent by the client, it is processed).
30 // * Client creates the authenticator selected by the host. If the method
31 // starts with a message from the host, it is processed.
32 // * Client and host exchange messages until the authentication is ACCEPTED or
33 // REJECTED.
35 // The details:
36 // * CreateAuthenticator() may be asynchronous (i.e. require user interaction
37 // to determine initial parameters, like PIN). This happens inside
38 // ProcessMessage, so to the outside this behaves like any asynchronous
39 // message processing. Internally, CreateAuthenticator() receives a
40 // callback, that will resume the authentication once the authenticator is
41 // created. If there is already a message to be processed by the new
42 // authenticator, this callback includes a call to the underlying
43 // ProcessMessage().
44 // * Some authentication methods may have a specific starting direction (e.g.
45 // host always sends the first message), while others are versatile (e.g.
46 // SPAKE, where either side can send the first message). When an
47 // authenticator is created, it is given a preferred initial state, which
48 // the authenticator may ignore.
49 // * If the new authenticator state doesn't match the preferred one,
50 // the NegotiatingAuthenticator deals with that, by sending an empty
51 // <authenticator> stanza if the method has no message to send, and
52 // ignoring such empty messages on the receiving end.
53 // * The client may optimistically pick a method on its first message (assuming
54 // it doesn't require user interaction to start). If the host doesn't
55 // support that method, it will just discard that message, and choose
56 // another method from the client's supported methods list.
57 // * The host never sends its own supported methods back to the client, so once
58 // the host picks a method from the client's list, it's final.
59 // * Any change in this class must maintain compatibility between any version
60 // mix of webapp, client plugin and host, for both Me2Me and IT2Me.
61 class NegotiatingAuthenticatorBase : public Authenticator {
62 public:
63 ~NegotiatingAuthenticatorBase() override;
65 // Authenticator interface.
66 State state() const override;
67 bool started() const override;
68 RejectionReason rejection_reason() const override;
69 scoped_ptr<ChannelAuthenticator> CreateChannelAuthenticator() const override;
71 // Calls |current_authenticator_| to process |message|, passing the supplied
72 // |resume_callback|.
73 void ProcessMessageInternal(const buzz::XmlElement* message,
74 const base::Closure& resume_callback);
76 const AuthenticationMethod& current_method_for_testing() const {
77 return current_method_;
80 protected:
81 static const buzz::StaticQName kMethodAttributeQName;
82 static const buzz::StaticQName kSupportedMethodsAttributeQName;
83 static const char kSupportedMethodsSeparator;
85 explicit NegotiatingAuthenticatorBase(Authenticator::State initial_state);
87 void AddMethod(const AuthenticationMethod& method);
89 // Updates |state_| to reflect the current underlying authenticator state.
90 // |resume_callback| is called after the state is updated.
91 void UpdateState(const base::Closure& resume_callback);
93 // Gets the next message from |current_authenticator_|, if any, and fills in
94 // the 'method' tag with |current_method_|.
95 virtual scoped_ptr<buzz::XmlElement> GetNextMessageInternal();
97 std::vector<AuthenticationMethod> methods_;
98 AuthenticationMethod current_method_;
99 scoped_ptr<Authenticator> current_authenticator_;
100 State state_;
101 RejectionReason rejection_reason_;
103 private:
104 DISALLOW_COPY_AND_ASSIGN(NegotiatingAuthenticatorBase);
107 } // namespace protocol
108 } // namespace remoting
110 #endif // REMOTING_PROTOCOL_NEGOTIATING_AUTHENTICATOR_BASE_H_