summaryrefslogtreecommitdiffstats
path: root/chrome/browser/local_discovery/privetv3_session.cc
blob: 18c3ff6a9c4573459bc693ad25507a963151e59d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/local_discovery/privetv3_session.h"

#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "chrome/browser/local_discovery/privet_http.h"
#include "chrome/common/cloud_print/cloud_print_constants.h"

namespace local_discovery {

namespace {

const char kUrlPlaceHolder[] = "http://host/";

const char kStubPrivetCode[] = "1234";

GURL CreatePrivetURL(const std::string& path) {
  GURL url(kUrlPlaceHolder);
  GURL::Replacements replacements;
  replacements.SetPathStr(path);
  return url.ReplaceComponents(replacements);
}

}  // namespace

class PrivetV3Session::FetcherDelegate : public PrivetURLFetcher::Delegate {
 public:
  FetcherDelegate(const base::WeakPtr<PrivetV3Session>& session,
                  const PrivetV3Session::MessageCallback& callback);
  ~FetcherDelegate() override;

  // PrivetURLFetcher::Delegate methods.
  void OnNeedPrivetToken(
      PrivetURLFetcher* fetcher,
      const PrivetURLFetcher::TokenCallback& callback) override;
  void OnError(PrivetURLFetcher* fetcher,
               PrivetURLFetcher::ErrorType error) override;
  void OnParsedJson(PrivetURLFetcher* fetcher,
                    const base::DictionaryValue& value,
                    bool has_error) override;

 private:
  friend class PrivetV3Session;
  void DeleteThis();

  scoped_ptr<PrivetURLFetcher> url_fetcher_;
  base::WeakPtr<PrivetV3Session> session_;
  MessageCallback callback_;
};

PrivetV3Session::FetcherDelegate::FetcherDelegate(
    const base::WeakPtr<PrivetV3Session>& session,
    const PrivetV3Session::MessageCallback& callback)
    : session_(session), callback_(callback) {
}

PrivetV3Session::FetcherDelegate::~FetcherDelegate() {
}

void PrivetV3Session::FetcherDelegate::OnNeedPrivetToken(
    PrivetURLFetcher* fetcher,
    const PrivetURLFetcher::TokenCallback& callback) {
  NOTREACHED();
}

void PrivetV3Session::FetcherDelegate::OnError(
    PrivetURLFetcher* fetcher,
    PrivetURLFetcher::ErrorType error) {
  if (session_) {
    DeleteThis();
    callback_.Run(Result::STATUS_CONNECTIONERROR, base::DictionaryValue());
  }
}

void PrivetV3Session::FetcherDelegate::OnParsedJson(
    PrivetURLFetcher* fetcher,
    const base::DictionaryValue& value,
    bool has_error) {
  if (session_) {
    DeleteThis();
    callback_.Run(
        has_error ? Result::STATUS_DEVICEERROR : Result::STATUS_SUCCESS, value);
  }
}

void PrivetV3Session::FetcherDelegate::DeleteThis() {
  base::MessageLoop::current()->PostTask(
      FROM_HERE, base::Bind(&PrivetV3Session::DeleteFetcher, session_,
                            base::Unretained(this)));
}

PrivetV3Session::Request::Request() {
}

PrivetV3Session::Request::~Request() {
}

PrivetV3Session::PrivetV3Session(scoped_ptr<PrivetHTTPClient> client)
    : client_(client.Pass()), code_confirmed_(false), weak_ptr_factory_(this) {
}

PrivetV3Session::~PrivetV3Session() {
}

void PrivetV3Session::Init(const InitCallback& callback) {
  // TODO: call /info.
  base::MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&PrivetV3Session::RunCallback, weak_ptr_factory_.GetWeakPtr(),
                 base::Bind(callback, Result::STATUS_SUCCESS,
                            std::vector<PairingType>(
                                1, PairingType::PAIRING_TYPE_EMBEDDEDCODE))),
      base::TimeDelta::FromSeconds(1));
}

void PrivetV3Session::StartPairing(PairingType pairing_type,
                                   const ResultCallback& callback) {
  // TODO: call /privet/v3/pairing/start.
  base::MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&PrivetV3Session::RunCallback, weak_ptr_factory_.GetWeakPtr(),
                 base::Bind(callback, Result::STATUS_SUCCESS)),
      base::TimeDelta::FromSeconds(1));
}

void PrivetV3Session::ConfirmCode(const std::string& code,
                                  const ResultCallback& callback) {
  // TODO: call /privet/v3/pairing/confirm.
  if (code == kStubPrivetCode) {
    code_confirmed_ = true;
    callback.Run(Result::STATUS_SUCCESS);
  } else {
    callback.Run(Result::STATUS_BADPAIRINGCODEERROR);
  }
}

void PrivetV3Session::SendMessage(const std::string& api,
                                  const base::DictionaryValue& input,
                                  const MessageCallback& callback) {
  if (!code_confirmed_)
    return callback.Run(Result::STATUS_SESSIONERROR, base::DictionaryValue());

  FetcherDelegate* fetcher_delegate(
      new FetcherDelegate(weak_ptr_factory_.GetWeakPtr(), callback));
  fetchers_.push_back(fetcher_delegate);

  scoped_ptr<PrivetURLFetcher> url_fetcher(client_->CreateURLFetcher(
      CreatePrivetURL(api), net::URLFetcher::POST, fetcher_delegate));

  std::string json;
  base::JSONWriter::WriteWithOptions(
      &input, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
  url_fetcher->SetUploadData(cloud_print::kContentTypeJSON, json);

  fetcher_delegate->url_fetcher_ = url_fetcher.Pass();
  fetcher_delegate->url_fetcher_->V3Mode();
  fetcher_delegate->url_fetcher_->Start();
}

void PrivetV3Session::RunCallback(const base::Closure& callback) {
  callback.Run();
}

void PrivetV3Session::DeleteFetcher(const FetcherDelegate* fetcher) {
  fetchers_.erase(std::find(fetchers_.begin(), fetchers_.end(), fetcher));
}

}  // namespace local_discovery