Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: remoting/client/jni/chromoting_jni_instance.h

Issue 19253003: Separate singleton out of ChromotingJNIInstance (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add missing checks and fix class names' CamelCase Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef REMOTING_CLIENT_CHROMOTING_JNI_INSTANCE_H_ 5 #ifndef REMOTING_CLIENT_CHROMOTING_JNI_INSTANCE_H_
6 #define REMOTING_CLIENT_CHROMOTING_JNI_INSTANCE_H_ 6 #define REMOTING_CLIENT_CHROMOTING_JNI_INSTANCE_H_
7 7
8 #include <jni.h>
9 #include <string> 8 #include <string>
10 9
11 #include "base/at_exit.h"
12 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
15 #include "net/url_request/url_request_context_getter.h"
16 #include "remoting/base/auto_thread.h"
17 #include "remoting/client/chromoting_client.h" 13 #include "remoting/client/chromoting_client.h"
18 #include "remoting/client/client_config.h" 14 #include "remoting/client/client_config.h"
19 #include "remoting/client/client_context.h" 15 #include "remoting/client/client_context.h"
20 #include "remoting/client/client_user_interface.h" 16 #include "remoting/client/client_user_interface.h"
21 #include "remoting/client/frame_consumer_proxy.h" 17 #include "remoting/client/frame_consumer_proxy.h"
22 #include "remoting/jingle_glue/network_settings.h" 18 #include "remoting/jingle_glue/network_settings.h"
23 #include "remoting/jingle_glue/xmpp_signal_strategy.h" 19 #include "remoting/jingle_glue/xmpp_signal_strategy.h"
24 #include "remoting/protocol/connection_to_host.h" 20 #include "remoting/protocol/connection_to_host.h"
25 21
26 template<typename T> struct DefaultSingletonTraits;
27
28 namespace remoting { 22 namespace remoting {
29 23 class ChromotingJni;
30 // Class and package name of the Java class supporting the methods we call.
31 const char* const JAVA_CLASS = "org/chromium/chromoting/jni/JNIInterface";
32 24
33 // TODO(solb) Move into location shared with client plugin. 25 // TODO(solb) Move into location shared with client plugin.
34 const char* const CHAT_SERVER = "talk.google.com"; 26 const char* const CHAT_SERVER = "talk.google.com";
35 const int CHAT_PORT = 5222; 27 const int CHAT_PORT = 5222;
36 const bool CHAT_USE_TLS = true; 28 const bool CHAT_USE_TLS = true;
37 const char* const CHAT_AUTH_METHOD = "oauth2"; 29 const char* const CHAT_AUTH_METHOD = "oauth2";
38 30
39 // ClientUserInterface that makes and (indirectly) receives JNI calls. It also 31 // ClientUserInterface that indirectly makes and receives JNI calls.
40 // contains global resources on which the Chromoting components run 32 class ChromotingJniInstance
41 // (e.g. message loops and task runners). 33 : public ClientUserInterface,
42 class ChromotingJNIInstance : public ClientUserInterface { 34 public base::RefCountedThreadSafe<ChromotingJniInstance> {
Wez 2013/07/16 13:46:56 Why does this need to be RefCountedThreadSafe?
solb 2013/07/16 19:23:02 In order to maintain thread safety in ChromotingJn
43 public: 35 public:
44 // This class is instantiated at process initialization and persists until 36 // Initiates a connection with the specified host. Call from the UI thread.
45 // we close. It reuses many of its components between connections (i.e. when 37 ChromotingJniInstance(
46 // a DisconnectFromHost() call is followed by a ConnectToHost() one.
47 static ChromotingJNIInstance* GetInstance();
48
49 // Initiates a connection with the specified host. This may only be called
50 // when |connected_| is false, and must be invoked on the UI thread.
51 void ConnectToHost(
52 const char* username, 38 const char* username,
53 const char* auth_token, 39 const char* auth_token,
54 const char* host_jid, 40 const char* host_jid,
55 const char* host_id, 41 const char* host_id,
56 const char* host_pubkey); 42 const char* host_pubkey);
57 43
58 // Terminates the current connection (if it hasn't already failed) and clean 44 // Terminates the current connection (if it hasn't already failed) and cleans
59 // up. This may only be called when |connected_|, and only from the UI thread. 45 // up. Must be called before destruction.
60 void DisconnectFromHost(); 46 void Cleanup();
61 47
62 // Provides the user's PIN and resumes the host authentication attempt. Call 48 // Provides the user's PIN and resumes the host authentication attempt. Call
63 // on the UI thread once the user has finished entering this PIN into the UI, 49 // on the UI thread once the user has finished entering this PIN into the UI,
64 // but only after the UI has been asked to provide a PIN (via FetchSecret()). 50 // but only after the UI has been asked to provide a PIN (via FetchSecret()).
65 void ProvideSecret(const char* pin); 51 void ProvideSecret(const char* pin);
66 52
67 // ClientUserInterface implementation. 53 // ClientUserInterface implementation.
68 virtual void OnConnectionState( 54 virtual void OnConnectionState(
69 protocol::ConnectionToHost::State state, 55 protocol::ConnectionToHost::State state,
70 protocol::ErrorCode error) OVERRIDE; 56 protocol::ErrorCode error) OVERRIDE;
71 virtual void OnConnectionReady(bool ready) OVERRIDE; 57 virtual void OnConnectionReady(bool ready) OVERRIDE;
72 virtual void SetCapabilities(const std::string& capabilities) OVERRIDE; 58 virtual void SetCapabilities(const std::string& capabilities) OVERRIDE;
73 virtual void SetPairingResponse( 59 virtual void SetPairingResponse(
74 const protocol::PairingResponse& response) OVERRIDE; 60 const protocol::PairingResponse& response) OVERRIDE;
75 virtual protocol::ClipboardStub* GetClipboardStub() OVERRIDE; 61 virtual protocol::ClipboardStub* GetClipboardStub() OVERRIDE;
76 virtual protocol::CursorShapeStub* GetCursorShapeStub() OVERRIDE; 62 virtual protocol::CursorShapeStub* GetCursorShapeStub() OVERRIDE;
77 virtual scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher> 63 virtual scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher>
78 GetTokenFetcher(const std::string& host_public_key) OVERRIDE; 64 GetTokenFetcher(const std::string& host_public_key) OVERRIDE;
79 65
80 private: 66 private:
81 ChromotingJNIInstance(); 67 // This object is ref-counted, so it cleans itself up.
82 68 virtual ~ChromotingJniInstance();
83 // Any existing or attempted connection must have been terminated using
84 // DisconnectFromHost() before this singleton is destroyed. Because
85 // destruction only occurs at application exit after all connections have
86 // terminated, it is safe to make unretained cross-thread calls on the class.
87 // As a singleton, this object must be destroyed on the main (UI) thread.
88 virtual ~ChromotingJNIInstance();
89 69
90 void ConnectToHostOnDisplayThread(); 70 void ConnectToHostOnDisplayThread();
91 void ConnectToHostOnNetworkThread(); 71 void ConnectToHostOnNetworkThread();
92 72
93 void DisconnectFromHostOnNetworkThread();
94
95 // Notifies the user interface that the user needs to enter a PIN. The 73 // Notifies the user interface that the user needs to enter a PIN. The
96 // current authentication attempt is put on hold until |callback| is invoked. 74 // current authentication attempt is put on hold until |callback| is invoked.
97 void FetchSecret(bool pairable, 75 void FetchSecret(bool pairable,
98 const protocol::SecretFetchedCallback& callback); 76 const protocol::SecretFetchedCallback& callback);
99 77
100 // The below variables are reused across consecutive sessions.
101
102 // Reference to the Java class into which we make JNI calls.
103 jclass class_;
104
105 // Used by the Chromium libraries to clean up the base and net libraries' JNI
106 // bindings. It must persist for the lifetime of the singleton.
107 scoped_ptr<base::AtExitManager> collector_;
108
109 // Chromium code's connection to the Java message loop.
110 scoped_ptr<base::MessageLoopForUI> ui_loop_;
111
112 // Runners that allow posting tasks to the various native threads.
113 scoped_refptr<AutoThreadTaskRunner> ui_task_runner_;
114 scoped_refptr<AutoThreadTaskRunner> network_task_runner_;
115 scoped_refptr<AutoThreadTaskRunner> display_task_runner_;
116
117 scoped_refptr<net::URLRequestContextGetter> url_requester_;
118 scoped_refptr<FrameConsumerProxy> frame_consumer_; 78 scoped_refptr<FrameConsumerProxy> frame_consumer_;
119 79
120 // All below variables are specific to each connection.
121
122 // True iff ConnectToHost() has been called without a subsequent
123 // call to DisconnectFromHost() (i.e. while connecting, once connected, and
124 // between the time a connection fails and DisconnectFromHost() is called).
125 // To be used on the UI thread.
126 bool connected_;
127
128 // This group of variables is to be used on the network thread. 80 // This group of variables is to be used on the network thread.
129 scoped_ptr<ClientConfig> client_config_; 81 scoped_ptr<ClientConfig> client_config_;
130 scoped_ptr<ClientContext> client_context_; 82 scoped_ptr<ClientContext> client_context_;
131 scoped_ptr<protocol::ConnectionToHost> connection_; 83 scoped_ptr<protocol::ConnectionToHost> connection_;
132 scoped_ptr<ChromotingClient> client_; 84 scoped_ptr<ChromotingClient> client_;
133 scoped_ptr<XmppSignalStrategy::XmppServerConfig> signaling_config_; 85 scoped_ptr<XmppSignalStrategy::XmppServerConfig> signaling_config_;
134 scoped_ptr<XmppSignalStrategy> signaling_; // Must outlive client_ 86 scoped_ptr<XmppSignalStrategy> signaling_; // Must outlive client_
135 scoped_ptr<NetworkSettings> network_settings_; 87 scoped_ptr<NetworkSettings> network_settings_;
136 88
137 // Pass this the user's PIN once we have it. To be assigned and accessed on 89 // Pass this the user's PIN once we have it. To be assigned and accessed on
138 // the UI thread, but must be posted to the network thread to call it. 90 // the UI thread, but must be posted to the network thread to call it.
139 protocol::SecretFetchedCallback pin_callback_; 91 protocol::SecretFetchedCallback pin_callback_;
140 92
141 // These strings describe the current connection, and are not reused. They 93 // These strings describe the current connection, and are not reused. They
142 // are initialized in ConnectionToHost(), but thereafter are only to be used 94 // are initialized in ConnectionToHost(), but thereafter are only to be used
143 // on the network thread. (This is safe because ConnectionToHost()'s finishes 95 // on the network thread. (This is safe because ConnectionToHost()'s finishes
144 // using them on the UI thread before they are ever touched from network.) 96 // using them on the UI thread before they are ever touched from network.)
145 std::string username_; 97 std::string username_;
146 std::string auth_token_; 98 std::string auth_token_;
147 std::string host_jid_; 99 std::string host_jid_;
148 std::string host_id_; 100 std::string host_id_;
149 std::string host_pubkey_; 101 std::string host_pubkey_;
150 102
151 friend struct DefaultSingletonTraits<ChromotingJNIInstance>; 103 friend class base::RefCountedThreadSafe<ChromotingJniInstance>;
152 104
153 DISALLOW_COPY_AND_ASSIGN(ChromotingJNIInstance); 105 DISALLOW_COPY_AND_ASSIGN(ChromotingJniInstance);
154 }; 106 };
155 107
156 } // namespace remoting 108 } // namespace remoting
157 109
158 #endif 110 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698