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

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

Issue 383023002: Partial setup flow with unittest. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months 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 | Annotate | Revision Log
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/logging.h" 8 #include "base/logging.h"
8 #include "chrome/browser/local_discovery/privet_http.h" 9 #include "chrome/browser/local_discovery/privet_http.h"
10 #include "chrome/common/cloud_print/cloud_print_constants.h"
9 11
10 namespace local_discovery { 12 namespace local_discovery {
11 13
14 namespace {
15
16 const char kUrlPlaceHolder[] = "http://host/";
17
18 GURL CreatePrivetURL(const std::string& path) {
19 GURL url(kUrlPlaceHolder);
20 GURL::Replacements replacements;
21 replacements.SetPathStr(path);
22 return url.ReplaceComponents(replacements);
23 }
24
25 } // namespace
26
27 class PrivetV3Session::FetcherDelegate : public PrivetURLFetcher::Delegate {
28 public:
29 FetcherDelegate(const base::WeakPtr<PrivetV3Session>& session,
30 Request* request);
31 virtual ~FetcherDelegate();
32
33 // PrivetURLFetcher::Delegate methods.
34 virtual void OnNeedPrivetToken(
35 PrivetURLFetcher* fetcher,
36 const PrivetURLFetcher::TokenCallback& callback) OVERRIDE;
37 virtual void OnError(PrivetURLFetcher* fetcher,
38 PrivetURLFetcher::ErrorType error) OVERRIDE;
39 virtual void OnParsedJson(PrivetURLFetcher* fetcher,
40 const base::DictionaryValue& value,
41 bool has_error) OVERRIDE;
42
43 private:
44 friend class PrivetV3Session;
45 scoped_ptr<PrivetURLFetcher> url_fetcher_;
46 base::WeakPtr<PrivetV3Session> session_;
47 Request* request_;
48 };
49
50 PrivetV3Session::FetcherDelegate::FetcherDelegate(
51 const base::WeakPtr<PrivetV3Session>& session,
52 Request* request)
53 : session_(session), request_(request) {
54 }
55
56 PrivetV3Session::FetcherDelegate::~FetcherDelegate() {
57 }
58
59 void PrivetV3Session::FetcherDelegate::OnNeedPrivetToken(
60 PrivetURLFetcher* fetcher,
61 const PrivetURLFetcher::TokenCallback& callback) {
62 if (session_)
63 session_->client_->RefreshPrivetToken(callback);
64 }
65
66 void PrivetV3Session::FetcherDelegate::OnError(
67 PrivetURLFetcher* fetcher,
68 PrivetURLFetcher::ErrorType error) {
69 request_->OnError(error);
70 }
71
72 void PrivetV3Session::FetcherDelegate::OnParsedJson(
73 PrivetURLFetcher* fetcher,
74 const base::DictionaryValue& value,
75 bool has_error) {
76 request_->OnParsedJson(value, has_error);
77 }
78
12 PrivetV3Session::Delegate::~Delegate() { 79 PrivetV3Session::Delegate::~Delegate() {
13 } 80 }
14 81
82 PrivetV3Session::Request::Request() {
83 }
84
15 PrivetV3Session::Request::~Request() { 85 PrivetV3Session::Request::~Request() {
16 } 86 }
17 87
18 PrivetV3Session::PrivetV3Session(scoped_ptr<PrivetHTTPClient> client, 88 PrivetV3Session::PrivetV3Session(scoped_ptr<PrivetHTTPClient> client,
19 Delegate* delegate) { 89 Delegate* delegate)
90 : delegate_(delegate),
91 client_(client.Pass()),
92 code_confirmed_(false),
93 weak_ptr_factory_(this) {
20 } 94 }
21 95
22 PrivetV3Session::~PrivetV3Session() { 96 PrivetV3Session::~PrivetV3Session() {
23 } 97 }
24 98
25 void PrivetV3Session::Start() { 99 void PrivetV3Session::Start() {
100 delegate_->OnSetupConfirmationNeeded("01234");
101 }
102
103 void PrivetV3Session::ConfirmCode() {
104 code_confirmed_ = true;
105 delegate_->OnSessionEstablished();
106 }
107
108 void PrivetV3Session::StartRequest(Request* request) {
109 CHECK(code_confirmed_);
110
111 request->fetcher_delegate_.reset(
112 new FetcherDelegate(weak_ptr_factory_.GetWeakPtr(), request));
113
114 scoped_ptr<PrivetURLFetcher> url_fetcher =
115 client_->CreateURLFetcher(CreatePrivetURL(request->GetName()),
116 net::URLFetcher::POST,
117 request->fetcher_delegate_.get());
118 std::string json;
119 base::JSONWriter::WriteWithOptions(
120 &request->GetInput(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
121 url_fetcher->SetUploadData(cloud_print::kContentTypeJSON, json);
122
123 request->fetcher_delegate_->url_fetcher_ = url_fetcher.Pass();
124 request->fetcher_delegate_->url_fetcher_->Start();
26 } 125 }
27 126
28 } // namespace local_discovery 127 } // namespace local_discovery
OLDNEW
« no previous file with comments | « chrome/browser/local_discovery/privetv3_session.h ('k') | chrome/browser/local_discovery/privetv3_session_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698