Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / local_discovery / privetv3_session.h
blob8a26f780e316a80ec37a1ba639294d6bc556ead2
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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SESSION_H_
6 #define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SESSION_H_
8 #include <string>
10 #include "base/callback.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/memory/weak_ptr.h"
15 #include "chrome/common/extensions/api/gcd_private.h"
16 #include "net/url_request/url_fetcher.h"
18 namespace base {
19 class DictionaryValue;
22 namespace crypto {
23 class P224EncryptedKeyExchange;
26 namespace local_discovery {
28 class PrivetHTTPClient;
29 class PrivetJSONOperation;
30 class PrivetURLFetcher;
32 // Manages secure communication between browser and local Privet device.
33 class PrivetV3Session {
34 private:
35 class FetcherDelegate;
37 public:
38 typedef extensions::api::gcd_private::PairingType PairingType;
39 typedef extensions::api::gcd_private::Status Result;
41 typedef base::Callback<
42 void(Result result, const base::DictionaryValue& response)> InitCallback;
44 typedef base::Callback<void(Result result)> ResultCallback;
45 typedef base::Callback<void(Result result,
46 const base::DictionaryValue& response)>
47 MessageCallback;
49 explicit PrivetV3Session(scoped_ptr<PrivetHTTPClient> client);
50 ~PrivetV3Session();
52 // Initializes session. Queries /privet/info and returns supported pairing
53 // types in callback.
54 void Init(const InitCallback& callback);
56 // Starts pairing by calling /privet/v3/pairing/start.
57 void StartPairing(PairingType pairing_type, const ResultCallback& callback);
59 // Confirms pairing code by calling /privet/v3/pairing/confirm.
60 // Calls /privet/v3/pairing/auth after pairing.
61 void ConfirmCode(const std::string& code, const ResultCallback& callback);
63 // TODO(vitalybuka): Make HTTPS request to device with certificate validation.
64 void SendMessage(const std::string& api,
65 const base::DictionaryValue& input,
66 const MessageCallback& callback);
68 private:
69 friend class PrivetV3SessionTest;
70 FRIEND_TEST_ALL_PREFIXES(PrivetV3SessionTest, Pairing);
72 void OnInfoDone(const InitCallback& callback,
73 Result result,
74 const base::DictionaryValue& response);
75 void OnPairingStartDone(const ResultCallback& callback,
76 Result result,
77 const base::DictionaryValue& response);
78 void OnPairingConfirmDone(const ResultCallback& callback,
79 Result result,
80 const base::DictionaryValue& response);
81 void OnAuthenticateDone(const ResultCallback& callback,
82 Result result,
83 const base::DictionaryValue& response);
85 void RunCallback(const base::Closure& callback);
86 void StartGetRequest(const std::string& api, const MessageCallback& callback);
87 void StartPostRequest(const std::string& api,
88 const base::DictionaryValue& input,
89 const MessageCallback& callback);
90 PrivetURLFetcher* CreateFetcher(const std::string& api,
91 net::URLFetcher::RequestType request_type,
92 const MessageCallback& callback);
93 void DeleteFetcher(const FetcherDelegate* fetcher);
94 void Cancel();
96 // Creates instances of PrivetURLFetcher.
97 scoped_ptr<PrivetHTTPClient> client_;
99 // Current authentication token.
100 std::string privet_auth_token_;
102 // ID of the session received from pairing/start.
103 std::string session_id_;
105 // Device commitment received from pairing/start.
106 std::string commitment_;
108 // Key exchange algorithm for pairing.
109 scoped_ptr<crypto::P224EncryptedKeyExchange> spake_;
111 // HTTPS certificate fingerprint received during pairing.
112 std::string fingerprint_;
114 // List of fetches to cancel when session is destroyed.
115 ScopedVector<FetcherDelegate> fetchers_;
117 // Intercepts POST requests. Used by tests only.
118 base::Callback<void(const base::DictionaryValue&)> on_post_data_;
120 base::WeakPtrFactory<PrivetV3Session> weak_ptr_factory_;
121 DISALLOW_COPY_AND_ASSIGN(PrivetV3Session);
124 } // namespace local_discovery
126 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SESSION_H_