Add method to get filters for a bookmark in Enhanced bookmark bridge
[chromium-blink-merge.git] / chrome / browser / local_discovery / privetv3_session.cc
blobceee47c0dc5c2b79b21d6270738efa14e57471b4
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 const char kStubPrivetCode[] = "1234";
21 GURL CreatePrivetURL(const std::string& path) {
22 GURL url(kUrlPlaceHolder);
23 GURL::Replacements replacements;
24 replacements.SetPathStr(path);
25 return url.ReplaceComponents(replacements);
28 } // namespace
30 class PrivetV3Session::FetcherDelegate : public PrivetURLFetcher::Delegate {
31 public:
32 FetcherDelegate(const base::WeakPtr<PrivetV3Session>& session,
33 Request* request);
34 ~FetcherDelegate() override;
36 // PrivetURLFetcher::Delegate methods.
37 void OnNeedPrivetToken(
38 PrivetURLFetcher* fetcher,
39 const PrivetURLFetcher::TokenCallback& callback) override;
40 void OnError(PrivetURLFetcher* fetcher,
41 PrivetURLFetcher::ErrorType error) override;
42 void OnParsedJson(PrivetURLFetcher* fetcher,
43 const base::DictionaryValue& value,
44 bool has_error) override;
46 private:
47 friend class PrivetV3Session;
48 scoped_ptr<PrivetURLFetcher> url_fetcher_;
49 base::WeakPtr<PrivetV3Session> session_;
50 Request* request_;
53 PrivetV3Session::FetcherDelegate::FetcherDelegate(
54 const base::WeakPtr<PrivetV3Session>& session,
55 Request* request)
56 : session_(session), request_(request) {
59 PrivetV3Session::FetcherDelegate::~FetcherDelegate() {
62 void PrivetV3Session::FetcherDelegate::OnNeedPrivetToken(
63 PrivetURLFetcher* fetcher,
64 const PrivetURLFetcher::TokenCallback& callback) {
65 if (session_)
66 session_->client_->RefreshPrivetToken(callback);
69 void PrivetV3Session::FetcherDelegate::OnError(
70 PrivetURLFetcher* fetcher,
71 PrivetURLFetcher::ErrorType error) {
72 request_->OnError();
75 void PrivetV3Session::FetcherDelegate::OnParsedJson(
76 PrivetURLFetcher* fetcher,
77 const base::DictionaryValue& value,
78 bool has_error) {
79 request_->OnParsedJson(value, has_error);
82 PrivetV3Session::Delegate::~Delegate() {
85 PrivetV3Session::Request::Request() {
88 PrivetV3Session::Request::~Request() {
91 PrivetV3Session::PrivetV3Session(scoped_ptr<PrivetHTTPClient> client,
92 Delegate* delegate)
93 : delegate_(delegate),
94 client_(client.Pass()),
95 code_confirmed_(false),
96 weak_ptr_factory_(this) {
99 PrivetV3Session::~PrivetV3Session() {
102 void PrivetV3Session::Start() {
103 base::MessageLoop::current()->PostDelayedTask(
104 FROM_HERE,
105 base::Bind(&PrivetV3Session::ConfirmFakeCode,
106 weak_ptr_factory_.GetWeakPtr()),
107 base::TimeDelta::FromSeconds(1));
110 void PrivetV3Session::ConfirmCode(const std::string& code) {
111 if (code == kStubPrivetCode) {
112 code_confirmed_ = true;
113 delegate_->OnSessionStatus(extensions::api::gcd_private::STATUS_SUCCESS);
114 } else {
115 delegate_->OnSessionStatus(
116 extensions::api::gcd_private::STATUS_BADCONFIRMATIONCODEERROR);
120 void PrivetV3Session::StartRequest(Request* request) {
121 if (!code_confirmed_) {
122 delegate_->OnSessionStatus(
123 extensions::api::gcd_private::STATUS_SESSIONERROR);
124 return;
127 request->fetcher_delegate_.reset(
128 new FetcherDelegate(weak_ptr_factory_.GetWeakPtr(), request));
130 scoped_ptr<PrivetURLFetcher> url_fetcher =
131 client_->CreateURLFetcher(CreatePrivetURL(request->GetName()),
132 net::URLFetcher::POST,
133 request->fetcher_delegate_.get());
134 std::string json;
135 base::JSONWriter::WriteWithOptions(
136 &request->GetInput(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
137 url_fetcher->SetUploadData(cloud_print::kContentTypeJSON, json);
139 request->fetcher_delegate_->url_fetcher_ = url_fetcher.Pass();
140 request->fetcher_delegate_->url_fetcher_->V3Mode();
141 request->fetcher_delegate_->url_fetcher_->Start();
144 void PrivetV3Session::ConfirmFakeCode() {
145 delegate_->OnSetupConfirmationNeeded(
146 kStubPrivetCode,
147 extensions::api::gcd_private::CONFIRMATION_TYPE_DISPLAYCODE);
150 } // namespace local_discovery