Index: chrome/browser/local_discovery/privetv3_session.cc |
diff --git a/chrome/browser/local_discovery/privetv3_session.cc b/chrome/browser/local_discovery/privetv3_session.cc |
index ceee47c0dc5c2b79b21d6270738efa14e57471b4..865352396a3dba537abbe0f4825d2ac31e6d3368 100644 |
--- a/chrome/browser/local_discovery/privetv3_session.cc |
+++ b/chrome/browser/local_discovery/privetv3_session.cc |
@@ -30,7 +30,7 @@ GURL CreatePrivetURL(const std::string& path) { |
class PrivetV3Session::FetcherDelegate : public PrivetURLFetcher::Delegate { |
public: |
FetcherDelegate(const base::WeakPtr<PrivetV3Session>& session, |
- Request* request); |
+ const PrivetV3Session::MessageCallback& callback); |
~FetcherDelegate() override; |
// PrivetURLFetcher::Delegate methods. |
@@ -45,15 +45,17 @@ class PrivetV3Session::FetcherDelegate : public PrivetURLFetcher::Delegate { |
private: |
friend class PrivetV3Session; |
+ void DeleteThis(); |
+ |
scoped_ptr<PrivetURLFetcher> url_fetcher_; |
base::WeakPtr<PrivetV3Session> session_; |
- Request* request_; |
+ MessageCallback callback_; |
}; |
PrivetV3Session::FetcherDelegate::FetcherDelegate( |
const base::WeakPtr<PrivetV3Session>& session, |
- Request* request) |
- : session_(session), request_(request) { |
+ const PrivetV3Session::MessageCallback& callback) |
+ : session_(session), callback_(callback) { |
} |
PrivetV3Session::FetcherDelegate::~FetcherDelegate() { |
@@ -62,24 +64,33 @@ PrivetV3Session::FetcherDelegate::~FetcherDelegate() { |
void PrivetV3Session::FetcherDelegate::OnNeedPrivetToken( |
PrivetURLFetcher* fetcher, |
const PrivetURLFetcher::TokenCallback& callback) { |
- if (session_) |
- session_->client_->RefreshPrivetToken(callback); |
+ NOTREACHED(); |
} |
void PrivetV3Session::FetcherDelegate::OnError( |
PrivetURLFetcher* fetcher, |
PrivetURLFetcher::ErrorType error) { |
- request_->OnError(); |
+ if (session_) { |
+ DeleteThis(); |
+ callback_.Run(Result::STATUS_CONNECTIONERROR, base::DictionaryValue()); |
+ } |
} |
void PrivetV3Session::FetcherDelegate::OnParsedJson( |
PrivetURLFetcher* fetcher, |
const base::DictionaryValue& value, |
bool has_error) { |
- request_->OnParsedJson(value, has_error); |
+ if (session_) { |
+ DeleteThis(); |
+ callback_.Run( |
+ has_error ? Result::STATUS_SETUPERROR : Result::STATUS_SUCCESS, value); |
+ } |
} |
-PrivetV3Session::Delegate::~Delegate() { |
+void PrivetV3Session::FetcherDelegate::DeleteThis() { |
+ base::MessageLoop::current()->PostTask( |
+ FROM_HERE, base::Bind(&PrivetV3Session::DeleteFetcher, session_, |
+ base::Unretained(this))); |
} |
PrivetV3Session::Request::Request() { |
@@ -88,63 +99,74 @@ PrivetV3Session::Request::Request() { |
PrivetV3Session::Request::~Request() { |
} |
-PrivetV3Session::PrivetV3Session(scoped_ptr<PrivetHTTPClient> client, |
- Delegate* delegate) |
- : delegate_(delegate), |
- client_(client.Pass()), |
- code_confirmed_(false), |
- weak_ptr_factory_(this) { |
+PrivetV3Session::PrivetV3Session(scoped_ptr<PrivetHTTPClient> client) |
+ : client_(client.Pass()), code_confirmed_(false), weak_ptr_factory_(this) { |
} |
PrivetV3Session::~PrivetV3Session() { |
} |
-void PrivetV3Session::Start() { |
+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::ConfirmFakeCode, |
- weak_ptr_factory_.GetWeakPtr()), |
+ 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) { |
+void PrivetV3Session::ConfirmCode(const std::string& code, |
+ const ResultCallback& callback) { |
+ // TODO: call /privet/v3/pairing/confirm. |
if (code == kStubPrivetCode) { |
code_confirmed_ = true; |
- delegate_->OnSessionStatus(extensions::api::gcd_private::STATUS_SUCCESS); |
+ callback.Run(Result::STATUS_SUCCESS); |
} else { |
- delegate_->OnSessionStatus( |
- extensions::api::gcd_private::STATUS_BADCONFIRMATIONCODEERROR); |
+ callback.Run(Result::STATUS_BADPAIRINGCODEERROR); |
} |
} |
-void PrivetV3Session::StartRequest(Request* request) { |
- if (!code_confirmed_) { |
- delegate_->OnSessionStatus( |
- extensions::api::gcd_private::STATUS_SESSIONERROR); |
- return; |
- } |
+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()); |
- request->fetcher_delegate_.reset( |
- new FetcherDelegate(weak_ptr_factory_.GetWeakPtr(), request)); |
+ 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)); |
- scoped_ptr<PrivetURLFetcher> url_fetcher = |
- client_->CreateURLFetcher(CreatePrivetURL(request->GetName()), |
- net::URLFetcher::POST, |
- request->fetcher_delegate_.get()); |
std::string json; |
base::JSONWriter::WriteWithOptions( |
- &request->GetInput(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
+ &input, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
url_fetcher->SetUploadData(cloud_print::kContentTypeJSON, json); |
- request->fetcher_delegate_->url_fetcher_ = url_fetcher.Pass(); |
- request->fetcher_delegate_->url_fetcher_->V3Mode(); |
- request->fetcher_delegate_->url_fetcher_->Start(); |
+ 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::ConfirmFakeCode() { |
- delegate_->OnSetupConfirmationNeeded( |
- kStubPrivetCode, |
- extensions::api::gcd_private::CONFIRMATION_TYPE_DISPLAYCODE); |
+void PrivetV3Session::DeleteFetcher(const FetcherDelegate* fetcher) { |
+ fetchers_.erase(std::find(fetchers_.begin(), fetchers_.end(), fetcher)); |
} |
} // namespace local_discovery |