Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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. | |
| 4 | |
| 5 #include "components/ntp_snippets/breaking_news/subscription_json_request.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <utility> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/command_line.h" | |
| 12 #include "base/json/json_writer.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "base/metrics/histogram_macros.h" | |
| 15 #include "base/metrics/sparse_histogram.h" | |
| 16 #include "base/strings/stringprintf.h" | |
| 17 #include "base/time/clock.h" | |
| 18 #include "base/values.h" | |
| 19 #include "components/data_use_measurement/core/data_use_user_data.h" | |
| 20 #include "components/ntp_snippets/breaking_news/subscription_request_params.h" | |
| 21 #include "components/ntp_snippets/category_info.h" | |
| 22 #include "components/ntp_snippets/features.h" | |
| 23 #include "components/ntp_snippets/user_classifier.h" | |
| 24 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
| 25 #include "components/signin/core/browser/signin_manager.h" | |
| 26 #include "components/signin/core/browser/signin_manager_base.h" | |
| 27 #include "components/strings/grit/components_strings.h" | |
| 28 #include "components/variations/net/variations_http_headers.h" | |
| 29 #include "components/variations/variations_associated_data.h" | |
| 30 #include "net/base/load_flags.h" | |
| 31 #include "net/http/http_response_headers.h" | |
| 32 #include "net/http/http_status_code.h" | |
| 33 #include "net/traffic_annotation/network_traffic_annotation.h" | |
| 34 #include "net/url_request/url_fetcher.h" | |
| 35 #include "net/url_request/url_request_context_getter.h" | |
| 36 #include "third_party/icu/source/common/unicode/uloc.h" | |
| 37 #include "third_party/icu/source/common/unicode/utypes.h" | |
| 38 #include "ui/base/l10n/l10n_util.h" | |
| 39 | |
| 40 using net::URLFetcher; | |
| 41 using net::URLRequestContextGetter; | |
| 42 using net::HttpRequestHeaders; | |
| 43 using net::URLRequestStatus; | |
| 44 | |
| 45 namespace ntp_snippets { | |
| 46 | |
| 47 namespace internal { | |
| 48 | |
| 49 SubscriptionJsonRequest::SubscriptionJsonRequest( | |
| 50 const ParseJSONCallback& callback) | |
| 51 : parse_json_callback_(callback), weak_ptr_factory_(this) {} | |
| 52 | |
| 53 SubscriptionJsonRequest::~SubscriptionJsonRequest() { | |
| 54 LOG_IF(DFATAL, !request_completed_callback_.is_null()) | |
|
Bernhard Bauer
2017/05/31 13:12:45
It's illegal to destroy a request before it has co
mamir
2017/05/31 14:25:03
Done.
| |
| 55 << "The CompletionCallback was never called!"; | |
| 56 } | |
| 57 | |
| 58 void SubscriptionJsonRequest::Start(CompletedCallback callback) { | |
| 59 request_completed_callback_ = std::move(callback); | |
| 60 url_fetcher_->Start(); | |
| 61 } | |
| 62 | |
| 63 std::string SubscriptionJsonRequest::GetResponseString() const { | |
| 64 std::string response; | |
| 65 url_fetcher_->GetResponseAsString(&response); | |
| 66 return response; | |
| 67 } | |
| 68 | |
| 69 //////////////////////////////////////////////////////////////////////////////// | |
| 70 // URLFetcherDelegate overrides | |
| 71 void SubscriptionJsonRequest::OnURLFetchComplete( | |
| 72 const net::URLFetcher* source) { | |
| 73 DCHECK_EQ(url_fetcher_.get(), source); | |
| 74 const URLRequestStatus& status = url_fetcher_->GetStatus(); | |
| 75 int response = url_fetcher_->GetResponseCode(); | |
| 76 | |
| 77 if (!status.is_success()) { | |
| 78 std::move(request_completed_callback_) | |
| 79 .Run(/*result=*/nullptr, | |
| 80 /*error_details=*/base::StringPrintf(" %d", status.error())); | |
| 81 } else if (response != net::HTTP_OK) { | |
| 82 std::move(request_completed_callback_) | |
| 83 .Run(/*result=*/nullptr, | |
| 84 /*error_details=*/base::StringPrintf(" %d", response)); | |
| 85 } else { | |
| 86 // request succeeded. | |
|
Bernhard Bauer
2017/05/31 13:12:44
So the callback is never called if the request suc
mamir
2017/05/31 14:25:03
After an offline discussion with fhorschig@ this i
| |
| 87 } | |
| 88 } | |
| 89 | |
| 90 SubscriptionJsonRequest::Builder::Builder() {} | |
| 91 SubscriptionJsonRequest::Builder::Builder(SubscriptionJsonRequest::Builder&&) = | |
| 92 default; | |
| 93 SubscriptionJsonRequest::Builder::~Builder() = default; | |
| 94 | |
| 95 std::unique_ptr<SubscriptionJsonRequest> | |
| 96 SubscriptionJsonRequest::Builder::Build() const { | |
| 97 DCHECK(!url_.is_empty()); | |
| 98 DCHECK(url_request_context_getter_); | |
| 99 auto request = | |
| 100 base::MakeUnique<SubscriptionJsonRequest>(parse_json_callback_); | |
| 101 std::string body = BuildBody(); | |
| 102 std::string headers = BuildHeaders(); | |
| 103 request->url_fetcher_ = BuildURLFetcher(request.get(), headers, body); | |
| 104 | |
| 105 // Log the request for debugging network issues. | |
| 106 VLOG(1) << "Sending a subscription request to " << url_ << ":\n" | |
| 107 << headers << "\n" | |
| 108 << body; | |
| 109 | |
| 110 return request; | |
| 111 } | |
| 112 | |
| 113 SubscriptionJsonRequest::Builder& SubscriptionJsonRequest::Builder::SetParams( | |
| 114 const SubscriptionRequestParams& params) { | |
| 115 params_ = params; | |
| 116 return *this; | |
| 117 } | |
| 118 | |
| 119 SubscriptionJsonRequest::Builder& SubscriptionJsonRequest::Builder::SetUrl( | |
| 120 const GURL& url) { | |
| 121 url_ = url; | |
| 122 return *this; | |
| 123 } | |
| 124 | |
| 125 SubscriptionJsonRequest::Builder& | |
| 126 SubscriptionJsonRequest::Builder::SetUrlRequestContextGetter( | |
| 127 const scoped_refptr<net::URLRequestContextGetter>& context_getter) { | |
| 128 url_request_context_getter_ = context_getter; | |
| 129 return *this; | |
| 130 } | |
| 131 | |
| 132 SubscriptionJsonRequest::Builder& | |
| 133 SubscriptionJsonRequest::Builder::SetParseJsonCallback( | |
| 134 ParseJSONCallback callback) { | |
| 135 parse_json_callback_ = callback; | |
| 136 return *this; | |
| 137 } | |
| 138 | |
| 139 std::string SubscriptionJsonRequest::Builder::BuildHeaders() const { | |
| 140 net::HttpRequestHeaders headers; | |
| 141 headers.SetHeader("Content-Type", "application/json; charset=UTF-8"); | |
| 142 if (!auth_header_.empty()) { | |
| 143 headers.SetHeader("Authorization", auth_header_); | |
| 144 } | |
| 145 // Add X-Client-Data header with experiment IDs from field trials. | |
| 146 // Note: It's OK to pass |is_signed_in| false if it's unknown, as it does | |
| 147 // not affect transmission of experiments coming from the variations server. | |
| 148 bool is_signed_in = false; | |
| 149 variations::AppendVariationHeaders(url_, | |
| 150 false, // incognito | |
| 151 false, // uma_enabled | |
| 152 is_signed_in, &headers); | |
| 153 return headers.ToString(); | |
| 154 } | |
| 155 | |
| 156 std::string SubscriptionJsonRequest::Builder::BuildBody() const { | |
| 157 auto request = base::MakeUnique<base::DictionaryValue>(); | |
| 158 | |
| 159 std::string request_json; | |
| 160 bool success = base::JSONWriter::WriteWithOptions( | |
| 161 *request, base::JSONWriter::OPTIONS_PRETTY_PRINT, &request_json); | |
| 162 DCHECK(success); | |
| 163 return request_json; | |
| 164 } | |
| 165 | |
| 166 std::unique_ptr<net::URLFetcher> | |
| 167 SubscriptionJsonRequest::Builder::BuildURLFetcher( | |
| 168 net::URLFetcherDelegate* delegate, | |
| 169 const std::string& headers, | |
| 170 const std::string& body) const { | |
| 171 net::NetworkTrafficAnnotationTag traffic_annotation = | |
| 172 net::DefineNetworkTrafficAnnotation("gcm_subscription", R"( | |
| 173 semantics { | |
| 174 sender: "Subscribe for breaking news delivered via GCM push messages" | |
| 175 description: | |
| 176 "Chromium can received breaking news via GCM push messages. " | |
|
Bernhard Bauer
2017/05/31 13:12:44
"receive"
mamir
2017/05/31 14:25:03
Done.
| |
| 177 "This request suscribes the client to receiving them." | |
| 178 trigger: | |
| 179 "Suscrpion takes place only once per profile lifetime. " | |
|
Bernhard Bauer
2017/05/31 13:12:44
"Subscription"
mamir
2017/05/31 14:25:03
Done.
| |
| 180 data: | |
| 181 "The sunscrption token that identifies this Chromium profile." | |
|
Bernhard Bauer
2017/05/31 13:12:44
"subscription"
mamir
2017/05/31 14:25:03
Done.
| |
| 182 destination: GOOGLE_OWNED_SERVICE | |
| 183 } | |
| 184 policy { | |
| 185 cookies_allowed: false | |
| 186 setting: | |
| 187 "This feature cannot be disabled by settings now" | |
| 188 chrome_policy { | |
| 189 NTPContentSuggestionsEnabled { | |
| 190 policy_options {mode: MANDATORY} | |
| 191 NTPContentSuggestionsEnabled: false | |
| 192 } | |
| 193 } | |
| 194 })"); | |
| 195 std::unique_ptr<net::URLFetcher> url_fetcher = net::URLFetcher::Create( | |
| 196 url_, net::URLFetcher::POST, delegate, traffic_annotation); | |
| 197 url_fetcher->SetRequestContext(url_request_context_getter_.get()); | |
| 198 url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
| 199 net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 200 data_use_measurement::DataUseUserData::AttachToFetcher( | |
| 201 url_fetcher.get(), | |
| 202 data_use_measurement::DataUseUserData::NTP_SNIPPETS_SUGGESTIONS); | |
| 203 | |
| 204 url_fetcher->SetExtraRequestHeaders(headers); | |
| 205 url_fetcher->SetUploadData("application/json", body); | |
| 206 | |
| 207 // Fetchers are sometimes cancelled because a network change was detected. | |
| 208 url_fetcher->SetAutomaticallyRetryOnNetworkChanges(3); | |
| 209 return url_fetcher; | |
| 210 } | |
| 211 | |
| 212 } // namespace internal | |
| 213 | |
| 214 } // namespace ntp_snippets | |
| OLD | NEW |