| 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 "chrome/browser/local_discovery/privet_url_fetcher.h" |
| 12 | 13 |
| 13 namespace base { | 14 namespace base { |
| 14 class DictionaryValue; | 15 class DictionaryValue; |
| 15 } | 16 } |
| 16 | 17 |
| 17 namespace local_discovery { | 18 namespace local_discovery { |
| 18 | 19 |
| 19 class PrivetHTTPClient; | 20 class PrivetHTTPClient; |
| 20 | 21 |
| 21 // Manages secure communication between browser and local Privet device. | 22 // Manages secure communication between browser and local Privet device. |
| 22 class PrivetV3Session { | 23 class PrivetV3Session { |
| 23 public: | 24 public: |
| 24 typedef base::Callback< | |
| 25 void(bool success, const base::DictionaryValue& response)> | |
| 26 RequestCallback; | |
| 27 | |
| 28 // Delegate to be implemented by client code. | 25 // Delegate to be implemented by client code. |
| 29 class Delegate { | 26 class Delegate { |
| 30 public: | 27 public: |
| 31 typedef base::Callback<void(bool confirm)> ConfirmationCallback; | |
| 32 | |
| 33 virtual ~Delegate(); | 28 virtual ~Delegate(); |
| 34 | 29 |
| 35 // Called when client code should prompt user to check |confirmation_code|. | 30 // Called when client code should prompt user to check |confirmation_code|. |
| 36 virtual void OnSetupConfirmationNeeded( | 31 virtual void OnSetupConfirmationNeeded( |
| 37 const std::string& confirmation_code, | 32 const std::string& confirmation_code) = 0; |
| 38 const ConfirmationCallback& callback) = 0; | |
| 39 | 33 |
| 40 // Called when session successfully establish and client code my call | 34 // Called when session successfully establish and client code my call |
| 41 // |CreateRequest| method. | 35 // |CreateRequest| method. |
| 42 virtual void OnSessionEstablished() = 0; | 36 virtual void OnSessionEstablished() = 0; |
| 43 | 37 |
| 44 // Called when session setup fails. | 38 // Called when session setup fails. |
| 45 virtual void OnCannotEstablishSession() = 0; | 39 virtual void OnCannotEstablishSession() = 0; |
| 46 }; | 40 }; |
| 47 | 41 |
| 48 // Represents request in progress using secure session. | 42 // Represents request in progress using secure session. |
| 49 class Request { | 43 class Request : public PrivetURLFetcher::Delegate { |
| 50 public: | 44 public: |
| 45 Request(); |
| 51 virtual ~Request(); | 46 virtual ~Request(); |
| 52 virtual void Start() = 0; | 47 |
| 48 virtual std::string GetCallName() = 0; |
| 49 virtual const base::DictionaryValue& GetInput() = 0; |
| 50 virtual std::vector<std::string> GetExtraRequestHeaders(); |
| 51 |
| 52 private: |
| 53 friend PrivetV3Session; |
| 54 |
| 55 scoped_ptr<PrivetURLFetcher> url_fetcher_; |
| 53 }; | 56 }; |
| 54 | 57 |
| 55 PrivetV3Session(scoped_ptr<PrivetHTTPClient> client, Delegate* delegate); | 58 PrivetV3Session(scoped_ptr<PrivetHTTPClient> client, Delegate* delegate); |
| 56 ~PrivetV3Session(); | 59 ~PrivetV3Session(); |
| 57 | 60 |
| 58 // Establishes a session, will call |OnSetupConfirmationNeeded| and then | 61 // Establishes a session, will call |OnSetupConfirmationNeeded| and then |
| 59 // |OnSessionEstablished|. | 62 // |OnSessionEstablished|. |
| 60 void Start(); | 63 void Start(); |
| 61 | 64 |
| 65 void ConfirmCode(); |
| 66 |
| 62 // Create a single /privet/v3/session/call request. | 67 // Create a single /privet/v3/session/call request. |
| 63 // Must be called only after receiving |OnSessionEstablished|. | 68 void StartRequest(Request* request); |
| 64 scoped_ptr<Request> CreateRequest(const std::string& api_name, | |
| 65 const base::DictionaryValue& request, | |
| 66 const RequestCallback& callback); | |
| 67 | 69 |
| 68 private: | 70 private: |
| 71 Delegate* delegate_; |
| 72 scoped_ptr<PrivetHTTPClient> client_; |
| 73 bool code_confirmed_; |
| 69 DISALLOW_COPY_AND_ASSIGN(PrivetV3Session); | 74 DISALLOW_COPY_AND_ASSIGN(PrivetV3Session); |
| 70 }; | 75 }; |
| 71 | 76 |
| 72 } // namespace local_discovery | 77 } // namespace local_discovery |
| 73 | 78 |
| 74 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SESSION_H_ | 79 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SESSION_H_ |
| OLD | NEW |