Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: chrome/browser/local_discovery/privetv3_session.cc

Issue 695253002: chrome.gcdPrivate allows app to choose pairing method. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Mon Nov 3 15:09:22 PST 2014 Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "chrome/browser/local_discovery/privetv3_session.h" 5 #include "chrome/browser/local_discovery/privetv3_session.h"
6 6
7 #include "base/json/json_writer.h" 7 #include "base/json/json_writer.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/local_discovery/privet_http.h" 10 #include "chrome/browser/local_discovery/privet_http.h"
(...skipping 12 matching lines...) Expand all
23 GURL::Replacements replacements; 23 GURL::Replacements replacements;
24 replacements.SetPathStr(path); 24 replacements.SetPathStr(path);
25 return url.ReplaceComponents(replacements); 25 return url.ReplaceComponents(replacements);
26 } 26 }
27 27
28 } // namespace 28 } // namespace
29 29
30 class PrivetV3Session::FetcherDelegate : public PrivetURLFetcher::Delegate { 30 class PrivetV3Session::FetcherDelegate : public PrivetURLFetcher::Delegate {
31 public: 31 public:
32 FetcherDelegate(const base::WeakPtr<PrivetV3Session>& session, 32 FetcherDelegate(const base::WeakPtr<PrivetV3Session>& session,
33 Request* request); 33 const PrivetV3Session::MessageCallback& callback);
34 ~FetcherDelegate() override; 34 ~FetcherDelegate() override;
35 35
36 // PrivetURLFetcher::Delegate methods. 36 // PrivetURLFetcher::Delegate methods.
37 void OnNeedPrivetToken( 37 void OnNeedPrivetToken(
38 PrivetURLFetcher* fetcher, 38 PrivetURLFetcher* fetcher,
39 const PrivetURLFetcher::TokenCallback& callback) override; 39 const PrivetURLFetcher::TokenCallback& callback) override;
40 void OnError(PrivetURLFetcher* fetcher, 40 void OnError(PrivetURLFetcher* fetcher,
41 PrivetURLFetcher::ErrorType error) override; 41 PrivetURLFetcher::ErrorType error) override;
42 void OnParsedJson(PrivetURLFetcher* fetcher, 42 void OnParsedJson(PrivetURLFetcher* fetcher,
43 const base::DictionaryValue& value, 43 const base::DictionaryValue& value,
44 bool has_error) override; 44 bool has_error) override;
45 45
46 private: 46 private:
47 friend class PrivetV3Session; 47 friend class PrivetV3Session;
48 void DeleteThis();
49
48 scoped_ptr<PrivetURLFetcher> url_fetcher_; 50 scoped_ptr<PrivetURLFetcher> url_fetcher_;
49 base::WeakPtr<PrivetV3Session> session_; 51 base::WeakPtr<PrivetV3Session> session_;
50 Request* request_; 52 MessageCallback callback_;
51 }; 53 };
52 54
53 PrivetV3Session::FetcherDelegate::FetcherDelegate( 55 PrivetV3Session::FetcherDelegate::FetcherDelegate(
54 const base::WeakPtr<PrivetV3Session>& session, 56 const base::WeakPtr<PrivetV3Session>& session,
55 Request* request) 57 const PrivetV3Session::MessageCallback& callback)
56 : session_(session), request_(request) { 58 : session_(session), callback_(callback) {
57 } 59 }
58 60
59 PrivetV3Session::FetcherDelegate::~FetcherDelegate() { 61 PrivetV3Session::FetcherDelegate::~FetcherDelegate() {
60 } 62 }
61 63
62 void PrivetV3Session::FetcherDelegate::OnNeedPrivetToken( 64 void PrivetV3Session::FetcherDelegate::OnNeedPrivetToken(
63 PrivetURLFetcher* fetcher, 65 PrivetURLFetcher* fetcher,
64 const PrivetURLFetcher::TokenCallback& callback) { 66 const PrivetURLFetcher::TokenCallback& callback) {
65 if (session_) 67 if (session_)
66 session_->client_->RefreshPrivetToken(callback); 68 session_->client_->RefreshPrivetToken(callback);
67 } 69 }
68 70
69 void PrivetV3Session::FetcherDelegate::OnError( 71 void PrivetV3Session::FetcherDelegate::OnError(
70 PrivetURLFetcher* fetcher, 72 PrivetURLFetcher* fetcher,
71 PrivetURLFetcher::ErrorType error) { 73 PrivetURLFetcher::ErrorType error) {
72 request_->OnError(); 74 if (session_) {
75 callback_.Run(Result::STATUS_CONNECTIONERROR, base::DictionaryValue());
76 DeleteThis();
77 }
73 } 78 }
74 79
75 void PrivetV3Session::FetcherDelegate::OnParsedJson( 80 void PrivetV3Session::FetcherDelegate::OnParsedJson(
76 PrivetURLFetcher* fetcher, 81 PrivetURLFetcher* fetcher,
77 const base::DictionaryValue& value, 82 const base::DictionaryValue& value,
78 bool has_error) { 83 bool has_error) {
79 request_->OnParsedJson(value, has_error); 84 if (session_) {
85 callback_.Run(
86 has_error ? Result::STATUS_SETUPERROR : Result::STATUS_SUCCESS, value);
87 DeleteThis();
88 }
80 } 89 }
81 90
82 PrivetV3Session::Delegate::~Delegate() { 91 void PrivetV3Session::FetcherDelegate::DeleteThis() {
92 session_->fetchers_.erase(
93 std::find(session_->fetchers_.begin(), session_->fetchers_.end(), this));
83 } 94 }
84 95
85 PrivetV3Session::Request::Request() { 96 PrivetV3Session::Request::Request() {
86 } 97 }
87 98
88 PrivetV3Session::Request::~Request() { 99 PrivetV3Session::Request::~Request() {
89 } 100 }
90 101
91 PrivetV3Session::PrivetV3Session(scoped_ptr<PrivetHTTPClient> client, 102 PrivetV3Session::PrivetV3Session(scoped_ptr<PrivetHTTPClient> client)
92 Delegate* delegate) 103 : client_(client.Pass()), code_confirmed_(false), weak_ptr_factory_(this) {
93 : delegate_(delegate),
94 client_(client.Pass()),
95 code_confirmed_(false),
96 weak_ptr_factory_(this) {
97 } 104 }
98 105
99 PrivetV3Session::~PrivetV3Session() { 106 PrivetV3Session::~PrivetV3Session() {
100 } 107 }
101 108
102 void PrivetV3Session::Start() { 109 void PrivetV3Session::Init(const InitCallback& callback) {
110 // TODO: call /info.
103 base::MessageLoop::current()->PostDelayedTask( 111 base::MessageLoop::current()->PostDelayedTask(
104 FROM_HERE, 112 FROM_HERE,
105 base::Bind(&PrivetV3Session::ConfirmFakeCode, 113 base::Bind(&PrivetV3Session::RunCallback,
106 weak_ptr_factory_.GetWeakPtr()), 114 weak_ptr_factory_.GetWeakPtr(),
115 base::Bind(callback,
116 Result::STATUS_SUCCESS,
117 std::vector<PairingType>(
118 1, PairingType::PAIRING_TYPE_EMBEDDEDCODE))),
107 base::TimeDelta::FromSeconds(1)); 119 base::TimeDelta::FromSeconds(1));
108 } 120 }
109 121
110 void PrivetV3Session::ConfirmCode(const std::string& code) { 122 void PrivetV3Session::StartPairing(PairingType pairing_type,
123 const ResultCallback& callback) {
124 // TODO: call /privet/v3/pairing/start.
125 base::MessageLoop::current()->PostDelayedTask(
126 FROM_HERE,
127 base::Bind(&PrivetV3Session::RunCallback,
128 weak_ptr_factory_.GetWeakPtr(),
129 base::Bind(callback, Result::STATUS_SUCCESS)),
130 base::TimeDelta::FromSeconds(1));
131 }
132
133 void PrivetV3Session::ConfirmCode(const std::string& code,
134 const ResultCallback& callback) {
135 // TODO: call /privet/v3/pairing/confirm.
111 if (code == kStubPrivetCode) { 136 if (code == kStubPrivetCode) {
112 code_confirmed_ = true; 137 code_confirmed_ = true;
113 delegate_->OnSessionStatus(extensions::api::gcd_private::STATUS_SUCCESS); 138 callback.Run(Result::STATUS_SUCCESS);
114 } else { 139 } else {
115 delegate_->OnSessionStatus( 140 callback.Run(Result::STATUS_BADPAIRINGCODEERROR);
116 extensions::api::gcd_private::STATUS_BADCONFIRMATIONCODEERROR);
117 } 141 }
118 } 142 }
119 143
120 void PrivetV3Session::StartRequest(Request* request) { 144 void PrivetV3Session::SendMessage(const std::string& api,
121 if (!code_confirmed_) { 145 const base::DictionaryValue& input,
122 delegate_->OnSessionStatus( 146 const MessageCallback& callback) {
123 extensions::api::gcd_private::STATUS_SESSIONERROR); 147 if (!code_confirmed_)
124 return; 148 return callback.Run(Result::STATUS_SESSIONERROR, base::DictionaryValue());
125 }
126 149
127 request->fetcher_delegate_.reset( 150 FetcherDelegate* fetcher_delegate(
128 new FetcherDelegate(weak_ptr_factory_.GetWeakPtr(), request)); 151 new FetcherDelegate(weak_ptr_factory_.GetWeakPtr(), callback));
152 fetchers_.push_back(fetcher_delegate);
129 153
130 scoped_ptr<PrivetURLFetcher> url_fetcher = 154 scoped_ptr<PrivetURLFetcher> url_fetcher(client_->CreateURLFetcher(
131 client_->CreateURLFetcher(CreatePrivetURL(request->GetName()), 155 CreatePrivetURL(api), net::URLFetcher::POST, fetcher_delegate));
132 net::URLFetcher::POST, 156
133 request->fetcher_delegate_.get());
134 std::string json; 157 std::string json;
135 base::JSONWriter::WriteWithOptions( 158 base::JSONWriter::WriteWithOptions(
136 &request->GetInput(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); 159 &input, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
137 url_fetcher->SetUploadData(cloud_print::kContentTypeJSON, json); 160 url_fetcher->SetUploadData(cloud_print::kContentTypeJSON, json);
138 161
139 request->fetcher_delegate_->url_fetcher_ = url_fetcher.Pass(); 162 fetcher_delegate->url_fetcher_ = url_fetcher.Pass();
140 request->fetcher_delegate_->url_fetcher_->V3Mode(); 163 fetcher_delegate->url_fetcher_->V3Mode();
141 request->fetcher_delegate_->url_fetcher_->Start(); 164 fetcher_delegate->url_fetcher_->Start();
142 } 165 }
143 166
144 void PrivetV3Session::ConfirmFakeCode() { 167 void PrivetV3Session::RunCallback(const base::Closure& callback) {
145 delegate_->OnSetupConfirmationNeeded( 168 callback.Run();
146 kStubPrivetCode,
147 extensions::api::gcd_private::CONFIRMATION_TYPE_DISPLAYCODE);
148 } 169 }
149 170
150 } // namespace local_discovery 171 } // namespace local_discovery
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698