Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / local_discovery / privetv3_session.h
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.
4
5 #ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SESSION_H_
6 #define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SESSION_H_
7
8 #include <string>
9
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "chrome/browser/local_discovery/privet_url_fetcher.h"
14 #include "chrome/common/extensions/api/gcd_private.h"
15
16 namespace base {
17 class DictionaryValue;
18 }
19
20 namespace local_discovery {
21
22 class PrivetHTTPClient;
23
24 // Manages secure communication between browser and local Privet device.
25 class PrivetV3Session {
26  private:
27   class FetcherDelegate;
28
29  public:
30   typedef extensions::api::gcd_private::PairingType PairingType;
31   typedef extensions::api::gcd_private::Status Result;
32
33   typedef base::Callback<
34       void(Result result, const std::vector<PairingType>& types)> InitCallback;
35
36   typedef base::Callback<void(Result result)> ResultCallback;
37   typedef base::Callback<void(Result result,
38                               const base::DictionaryValue& response)>
39       MessageCallback;
40
41   explicit PrivetV3Session(scoped_ptr<PrivetHTTPClient> client);
42   ~PrivetV3Session();
43
44   // Initialized session.
45   void Init(const InitCallback& callback);
46
47   void StartPairing(PairingType pairing_type, const ResultCallback& callback);
48
49   void ConfirmCode(const std::string& code, const ResultCallback& callback);
50
51   // Create a single /privet/v3/session/call request.
52   void SendMessage(const std::string& api,
53                    const base::DictionaryValue& input,
54                    const MessageCallback& callback);
55
56  private:
57   // Represents request in progress using secure session.
58   class Request {
59    public:
60     Request();
61     virtual ~Request();
62
63     virtual void OnError() = 0;
64     virtual void OnParsedJson(const base::DictionaryValue& value,
65                               bool has_error) = 0;
66
67    private:
68     friend class PrivetV3Session;
69     scoped_ptr<FetcherDelegate> fetcher_delegate_;
70   };
71
72   void RunCallback(const base::Closure& callback);
73   void DeleteFetcher(const FetcherDelegate* fetcher);
74
75   scoped_ptr<PrivetHTTPClient> client_;
76   bool code_confirmed_;
77   ScopedVector<FetcherDelegate> fetchers_;
78   base::WeakPtrFactory<PrivetV3Session> weak_ptr_factory_;
79   DISALLOW_COPY_AND_ASSIGN(PrivetV3Session);
80 };
81
82 }  // namespace local_discovery
83
84 #endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SESSION_H_