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

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 17:47:42 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 NOTREACHED();
66 session_->client_->RefreshPrivetToken(callback);
67 } 68 }
68 69
69 void PrivetV3Session::FetcherDelegate::OnError( 70 void PrivetV3Session::FetcherDelegate::OnError(
70 PrivetURLFetcher* fetcher, 71 PrivetURLFetcher* fetcher,
71 PrivetURLFetcher::ErrorType error) { 72 PrivetURLFetcher::ErrorType error) {
72 request_->OnError(); 73 if (session_) {
74 DeleteThis();
75 callback_.Run(Result::STATUS_CONNECTIONERROR, base::DictionaryValue());
76 }
73 } 77 }
74 78
75 void PrivetV3Session::FetcherDelegate::OnParsedJson( 79 void PrivetV3Session::FetcherDelegate::OnParsedJson(
76 PrivetURLFetcher* fetcher, 80 PrivetURLFetcher* fetcher,
77 const base::DictionaryValue& value, 81 const base::DictionaryValue& value,
78 bool has_error) { 82 bool has_error) {
79 request_->OnParsedJson(value, has_error); 83 if (session_) {
84 DeleteThis();
85 callback_.Run(
86 has_error ? Result::STATUS_SETUPERROR : Result::STATUS_SUCCESS, value);
87 }
80 } 88 }
81 89
82 PrivetV3Session::Delegate::~Delegate() { 90 void PrivetV3Session::FetcherDelegate::DeleteThis() {
91 base::MessageLoop::current()->PostTask(
92 FROM_HERE, base::Bind(&PrivetV3Session::DeleteFetcher, session_,
93 base::Unretained(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, weak_ptr_factory_.GetWeakPtr(),
106 weak_ptr_factory_.GetWeakPtr()), 114 base::Bind(callback, Result::STATUS_SUCCESS,
115 std::vector<PairingType>(
116 1, PairingType::PAIRING_TYPE_EMBEDDEDCODE))),
107 base::TimeDelta::FromSeconds(1)); 117 base::TimeDelta::FromSeconds(1));
108 } 118 }
109 119
110 void PrivetV3Session::ConfirmCode(const std::string& code) { 120 void PrivetV3Session::StartPairing(PairingType pairing_type,
121 const ResultCallback& callback) {
122 // TODO: call /privet/v3/pairing/start.
123 base::MessageLoop::current()->PostDelayedTask(
124 FROM_HERE,
125 base::Bind(&PrivetV3Session::RunCallback, weak_ptr_factory_.GetWeakPtr(),
126 base::Bind(callback, Result::STATUS_SUCCESS)),
127 base::TimeDelta::FromSeconds(1));
128 }
129
130 void PrivetV3Session::ConfirmCode(const std::string& code,
131 const ResultCallback& callback) {
132 // TODO: call /privet/v3/pairing/confirm.
111 if (code == kStubPrivetCode) { 133 if (code == kStubPrivetCode) {
112 code_confirmed_ = true; 134 code_confirmed_ = true;
113 delegate_->OnSessionStatus(extensions::api::gcd_private::STATUS_SUCCESS); 135 callback.Run(Result::STATUS_SUCCESS);
114 } else { 136 } else {
115 delegate_->OnSessionStatus( 137 callback.Run(Result::STATUS_BADPAIRINGCODEERROR);
116 extensions::api::gcd_private::STATUS_BADCONFIRMATIONCODEERROR);
117 } 138 }
118 } 139 }
119 140
120 void PrivetV3Session::StartRequest(Request* request) { 141 void PrivetV3Session::SendMessage(const std::string& api,
121 if (!code_confirmed_) { 142 const base::DictionaryValue& input,
122 delegate_->OnSessionStatus( 143 const MessageCallback& callback) {
123 extensions::api::gcd_private::STATUS_SESSIONERROR); 144 if (!code_confirmed_)
124 return; 145 return callback.Run(Result::STATUS_SESSIONERROR, base::DictionaryValue());
125 }
126 146
127 request->fetcher_delegate_.reset( 147 FetcherDelegate* fetcher_delegate(
128 new FetcherDelegate(weak_ptr_factory_.GetWeakPtr(), request)); 148 new FetcherDelegate(weak_ptr_factory_.GetWeakPtr(), callback));
149 fetchers_.push_back(fetcher_delegate);
129 150
130 scoped_ptr<PrivetURLFetcher> url_fetcher = 151 scoped_ptr<PrivetURLFetcher> url_fetcher(client_->CreateURLFetcher(
131 client_->CreateURLFetcher(CreatePrivetURL(request->GetName()), 152 CreatePrivetURL(api), net::URLFetcher::POST, fetcher_delegate));
132 net::URLFetcher::POST, 153
133 request->fetcher_delegate_.get());
134 std::string json; 154 std::string json;
135 base::JSONWriter::WriteWithOptions( 155 base::JSONWriter::WriteWithOptions(
136 &request->GetInput(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); 156 &input, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
137 url_fetcher->SetUploadData(cloud_print::kContentTypeJSON, json); 157 url_fetcher->SetUploadData(cloud_print::kContentTypeJSON, json);
138 158
139 request->fetcher_delegate_->url_fetcher_ = url_fetcher.Pass(); 159 fetcher_delegate->url_fetcher_ = url_fetcher.Pass();
140 request->fetcher_delegate_->url_fetcher_->V3Mode(); 160 fetcher_delegate->url_fetcher_->V3Mode();
141 request->fetcher_delegate_->url_fetcher_->Start(); 161 fetcher_delegate->url_fetcher_->Start();
142 } 162 }
143 163
144 void PrivetV3Session::ConfirmFakeCode() { 164 void PrivetV3Session::RunCallback(const base::Closure& callback) {
145 delegate_->OnSetupConfirmationNeeded( 165 callback.Run();
146 kStubPrivetCode, 166 }
147 extensions::api::gcd_private::CONFIRMATION_TYPE_DISPLAYCODE); 167
168 void PrivetV3Session::DeleteFetcher(const FetcherDelegate* fetcher) {
169 fetchers_.erase(std::find(fetchers_.begin(), fetchers_.end(), fetcher));
148 } 170 }
149 171
150 } // namespace local_discovery 172 } // namespace local_discovery
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698