Update mojo surfaces bindings and mojo/cc/ glue
[chromium-blink-merge.git] / chrome / browser / local_discovery / privetv3_session.cc
blob69583c2c3b094fc175737f36fa8221df1519dcdc
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 #include "chrome/browser/local_discovery/privetv3_session.h"
7 #include "base/json/json_writer.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/local_discovery/privet_http.h"
11 #include "chrome/common/cloud_print/cloud_print_constants.h"
13 namespace local_discovery {
15 namespace {
17 const char kUrlPlaceHolder[] = "http://host/";
19 GURL CreatePrivetURL(const std::string& path) {
20 GURL url(kUrlPlaceHolder);
21 GURL::Replacements replacements;
22 replacements.SetPathStr(path);
23 return url.ReplaceComponents(replacements);
26 } // namespace
28 class PrivetV3Session::FetcherDelegate : public PrivetURLFetcher::Delegate {
29 public:
30 FetcherDelegate(const base::WeakPtr<PrivetV3Session>& session,
31 Request* request);
32 virtual ~FetcherDelegate();
34 // PrivetURLFetcher::Delegate methods.
35 virtual void OnNeedPrivetToken(
36 PrivetURLFetcher* fetcher,
37 const PrivetURLFetcher::TokenCallback& callback) OVERRIDE;
38 virtual void OnError(PrivetURLFetcher* fetcher,
39 PrivetURLFetcher::ErrorType error) OVERRIDE;
40 virtual void OnParsedJson(PrivetURLFetcher* fetcher,
41 const base::DictionaryValue& value,
42 bool has_error) OVERRIDE;
44 private:
45 friend class PrivetV3Session;
46 scoped_ptr<PrivetURLFetcher> url_fetcher_;
47 base::WeakPtr<PrivetV3Session> session_;
48 Request* request_;
51 PrivetV3Session::FetcherDelegate::FetcherDelegate(
52 const base::WeakPtr<PrivetV3Session>& session,
53 Request* request)
54 : session_(session), request_(request) {
57 PrivetV3Session::FetcherDelegate::~FetcherDelegate() {
60 void PrivetV3Session::FetcherDelegate::OnNeedPrivetToken(
61 PrivetURLFetcher* fetcher,
62 const PrivetURLFetcher::TokenCallback& callback) {
63 if (session_)
64 session_->client_->RefreshPrivetToken(callback);
67 void PrivetV3Session::FetcherDelegate::OnError(
68 PrivetURLFetcher* fetcher,
69 PrivetURLFetcher::ErrorType error) {
70 request_->OnError(error);
73 void PrivetV3Session::FetcherDelegate::OnParsedJson(
74 PrivetURLFetcher* fetcher,
75 const base::DictionaryValue& value,
76 bool has_error) {
77 request_->OnParsedJson(value, has_error);
80 PrivetV3Session::Delegate::~Delegate() {
83 PrivetV3Session::Request::Request() {
86 PrivetV3Session::Request::~Request() {
89 PrivetV3Session::PrivetV3Session(scoped_ptr<PrivetHTTPClient> client,
90 Delegate* delegate)
91 : delegate_(delegate),
92 client_(client.Pass()),
93 code_confirmed_(false),
94 weak_ptr_factory_(this) {
97 PrivetV3Session::~PrivetV3Session() {
100 void PrivetV3Session::Start() {
101 base::MessageLoop::current()->PostDelayedTask(
102 FROM_HERE,
103 base::Bind(&PrivetV3Session::ConfirmFakeCode,
104 weak_ptr_factory_.GetWeakPtr()),
105 base::TimeDelta::FromSeconds(1));
108 void PrivetV3Session::ConfirmCode() {
109 code_confirmed_ = true;
110 delegate_->OnSessionEstablished();
113 void PrivetV3Session::StartRequest(Request* request) {
114 CHECK(code_confirmed_);
116 request->fetcher_delegate_.reset(
117 new FetcherDelegate(weak_ptr_factory_.GetWeakPtr(), request));
119 scoped_ptr<PrivetURLFetcher> url_fetcher =
120 client_->CreateURLFetcher(CreatePrivetURL(request->GetName()),
121 net::URLFetcher::POST,
122 request->fetcher_delegate_.get());
123 std::string json;
124 base::JSONWriter::WriteWithOptions(
125 &request->GetInput(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
126 url_fetcher->SetUploadData(cloud_print::kContentTypeJSON, json);
128 request->fetcher_delegate_->url_fetcher_ = url_fetcher.Pass();
129 request->fetcher_delegate_->url_fetcher_->Start();
132 void PrivetV3Session::ConfirmFakeCode() {
133 delegate_->OnSetupConfirmationNeeded("01234");
136 } // namespace local_discovery