OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SESSION_H_ | 5 #ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SESSION_H_ |
6 #define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SESSION_H_ | 6 #define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SESSION_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "chrome/common/extensions/api/gcd_private.h" | 14 #include "chrome/common/extensions/api/gcd_private.h" |
| 15 #include "net/url_request/url_fetcher.h" |
15 | 16 |
16 namespace base { | 17 namespace base { |
17 class DictionaryValue; | 18 class DictionaryValue; |
18 } | 19 } |
19 | 20 |
| 21 namespace crypto { |
| 22 class P224EncryptedKeyExchange; |
| 23 } |
| 24 |
20 namespace local_discovery { | 25 namespace local_discovery { |
21 | 26 |
22 class PrivetHTTPClient; | 27 class PrivetHTTPClient; |
| 28 class PrivetJSONOperation; |
| 29 class PrivetURLFetcher; |
23 | 30 |
24 // Manages secure communication between browser and local Privet device. | 31 // Manages secure communication between browser and local Privet device. |
25 class PrivetV3Session { | 32 class PrivetV3Session { |
26 private: | 33 private: |
27 class FetcherDelegate; | 34 class FetcherDelegate; |
28 | 35 |
29 public: | 36 public: |
30 typedef extensions::api::gcd_private::PairingType PairingType; | 37 typedef extensions::api::gcd_private::PairingType PairingType; |
31 typedef extensions::api::gcd_private::Status Result; | 38 typedef extensions::api::gcd_private::Status Result; |
32 | 39 |
33 typedef base::Callback< | 40 typedef base::Callback< |
34 void(Result result, const std::vector<PairingType>& types)> InitCallback; | 41 void(Result result, const std::vector<PairingType>& types)> InitCallback; |
35 | 42 |
36 typedef base::Callback<void(Result result)> ResultCallback; | 43 typedef base::Callback<void(Result result)> ResultCallback; |
37 typedef base::Callback<void(Result result, | 44 typedef base::Callback<void(Result result, |
38 const base::DictionaryValue& response)> | 45 const base::DictionaryValue& response)> |
39 MessageCallback; | 46 MessageCallback; |
40 | 47 |
41 explicit PrivetV3Session(scoped_ptr<PrivetHTTPClient> client); | 48 explicit PrivetV3Session(scoped_ptr<PrivetHTTPClient> client); |
42 ~PrivetV3Session(); | 49 ~PrivetV3Session(); |
43 | 50 |
44 // Initialized session. | 51 // Initializes session. Queries /privet/info and returns supported pairing |
| 52 // types in callback. |
45 void Init(const InitCallback& callback); | 53 void Init(const InitCallback& callback); |
46 | 54 |
| 55 // Starts pairing by calling /privet/v3/pairing/start. |
47 void StartPairing(PairingType pairing_type, const ResultCallback& callback); | 56 void StartPairing(PairingType pairing_type, const ResultCallback& callback); |
48 | 57 |
| 58 // Confirms pairing code by calling /privet/v3/pairing/confirm. |
| 59 // TODO(vitalybuka): Call /privet/v3/pairing/auth after pairing. |
49 void ConfirmCode(const std::string& code, const ResultCallback& callback); | 60 void ConfirmCode(const std::string& code, const ResultCallback& callback); |
50 | 61 |
51 // Create a single /privet/v3/session/call request. | 62 // TODO(vitalybuka): Make HTTPS request to device with certificate validation. |
52 void SendMessage(const std::string& api, | 63 void SendMessage(const std::string& api, |
53 const base::DictionaryValue& input, | 64 const base::DictionaryValue& input, |
54 const MessageCallback& callback); | 65 const MessageCallback& callback); |
55 | 66 |
56 private: | 67 private: |
| 68 friend class PrivetV3SessionTest; |
| 69 |
| 70 void OnInfoDone(const InitCallback& callback, |
| 71 Result result, |
| 72 const base::DictionaryValue& response); |
| 73 void OnPairingStartDone(const ResultCallback& callback, |
| 74 Result result, |
| 75 const base::DictionaryValue& response); |
| 76 void OnPairingConfirmDone(const ResultCallback& callback, |
| 77 Result result, |
| 78 const base::DictionaryValue& response); |
57 void RunCallback(const base::Closure& callback); | 79 void RunCallback(const base::Closure& callback); |
| 80 void StartGetRequest(const std::string& api, const MessageCallback& callback); |
| 81 void StartPostRequest(const std::string& api, |
| 82 const base::DictionaryValue& input, |
| 83 const MessageCallback& callback); |
| 84 PrivetURLFetcher* CreateFetcher(const std::string& api, |
| 85 net::URLFetcher::RequestType request_type, |
| 86 const MessageCallback& callback); |
58 void DeleteFetcher(const FetcherDelegate* fetcher); | 87 void DeleteFetcher(const FetcherDelegate* fetcher); |
59 | 88 |
| 89 // Creates instances of PrivetURLFetcher. |
60 scoped_ptr<PrivetHTTPClient> client_; | 90 scoped_ptr<PrivetHTTPClient> client_; |
61 bool code_confirmed_; | 91 |
| 92 // Current authentication token. |
| 93 std::string privet_auth_token_; |
| 94 |
| 95 // ID of the session received from pairing/start. |
| 96 std::string session_id_; |
| 97 |
| 98 // Device commitment received from pairing/start. |
| 99 std::string commitment_; |
| 100 |
| 101 // Key exchange algorithm for pairing. |
| 102 scoped_ptr<crypto::P224EncryptedKeyExchange> spake_; |
| 103 |
| 104 // HTTPS certificate fingerprint received during pairing. |
| 105 std::string fingerprint_; |
| 106 |
| 107 // List of fetches to cancel when session is destroyed. |
62 ScopedVector<FetcherDelegate> fetchers_; | 108 ScopedVector<FetcherDelegate> fetchers_; |
63 std::string privet_auth_token_; | 109 |
| 110 // Intercepts POST requests. Used by tests only. |
| 111 base::Callback<void(const base::DictionaryValue&)> on_post_data_; |
64 | 112 |
65 base::WeakPtrFactory<PrivetV3Session> weak_ptr_factory_; | 113 base::WeakPtrFactory<PrivetV3Session> weak_ptr_factory_; |
66 DISALLOW_COPY_AND_ASSIGN(PrivetV3Session); | 114 DISALLOW_COPY_AND_ASSIGN(PrivetV3Session); |
67 }; | 115 }; |
68 | 116 |
69 } // namespace local_discovery | 117 } // namespace local_discovery |
70 | 118 |
71 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SESSION_H_ | 119 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SESSION_H_ |
OLD | NEW |