privetv3_session.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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 "chrome/browser/local_discovery/privet_url_fetcher.h"
13#include "chrome/common/extensions/api/gcd_private.h"
14
15namespace base {
16class DictionaryValue;
17}
18
19namespace local_discovery {
20
21class PrivetHTTPClient;
22
23// Manages secure communication between browser and local Privet device.
24class PrivetV3Session {
25 private:
26  class FetcherDelegate;
27
28 public:
29  // Delegate to be implemented by client code.
30  class Delegate {
31   public:
32    virtual ~Delegate();
33
34    // Called when client code should prompt user to check |confirmation_code|.
35    virtual void OnSetupConfirmationNeeded(
36        const std::string& confirmation_code,
37        extensions::api::gcd_private::ConfirmationType confirmation_type) = 0;
38
39    virtual void OnSessionStatus(
40        extensions::api::gcd_private::Status status) = 0;
41  };
42
43  // Represents request in progress using secure session.
44  class Request {
45   public:
46    Request();
47    virtual ~Request();
48
49    virtual std::string GetName() = 0;
50    virtual const base::DictionaryValue& GetInput() = 0;
51    virtual void OnError(PrivetURLFetcher::ErrorType error) = 0;
52    virtual void OnParsedJson(const base::DictionaryValue& value,
53                              bool has_error) = 0;
54
55   private:
56    friend class PrivetV3Session;
57    scoped_ptr<FetcherDelegate> fetcher_delegate_;
58  };
59
60  PrivetV3Session(scoped_ptr<PrivetHTTPClient> client, Delegate* delegate);
61  ~PrivetV3Session();
62
63  // Establishes a session, will call |OnSetupConfirmationNeeded| and then
64  // |OnSessionEstablished|.
65  void Start();
66
67  void ConfirmCode(const std::string& code);
68
69  // Create a single /privet/v3/session/call request.
70  void StartRequest(Request* request);
71
72 private:
73  void ConfirmFakeCode();
74
75  Delegate* delegate_;
76  scoped_ptr<PrivetHTTPClient> client_;
77  bool code_confirmed_;
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_
85