Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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 "chrome/browser/android/digital_asset_links/digital_asset_links_handler .h" | |
| 6 | |
| 7 #include "base/json/json_reader.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/strings/string_number_conversions.h" | |
| 10 #include "base/strings/stringprintf.h" | |
| 11 #include "base/values.h" | |
| 12 #include "net/base/load_flags.h" | |
| 13 #include "net/base/url_util.h" | |
| 14 #include "net/http/http_response_headers.h" | |
| 15 #include "net/http/http_status_code.h" | |
| 16 #include "net/http/http_util.h" | |
| 17 #include "net/url_request/url_request_status.h" | |
| 18 | |
| 19 namespace { | |
| 20 const char kDigitalAssetLinksBaseURL[] = | |
| 21 "https://digitalassetlinks.googleapis.com"; | |
| 22 const char kDigitalAssetLinksCheckAPI[] = "/v1/assetlinks:check?"; | |
| 23 const char kTargetOriginParam[] = "source.web.site"; | |
| 24 const char kSourcePackageNameParam[] = "target.androidApp.packageName"; | |
| 25 const char kSourceFingerprintParam[] = | |
| 26 "target.androidApp.certificate.sha256Fingerprint"; | |
| 27 const char kRelationshipParam[] = "relation"; | |
| 28 | |
| 29 GURL GetUrlForCheckingRelationship(const std::string& web_domain, | |
| 30 const std::string& package_name, | |
| 31 const std::string& fingerprint, | |
| 32 const std::string& relationship) { | |
| 33 GURL request_url = | |
| 34 GURL(kDigitalAssetLinksBaseURL).Resolve(kDigitalAssetLinksCheckAPI); | |
| 35 request_url = | |
| 36 net::AppendQueryParameter(request_url, kTargetOriginParam, web_domain); | |
| 37 request_url = net::AppendQueryParameter(request_url, kSourcePackageNameParam, | |
| 38 package_name); | |
| 39 request_url = net::AppendQueryParameter(request_url, kSourceFingerprintParam, | |
| 40 fingerprint); | |
| 41 request_url = | |
| 42 net::AppendQueryParameter(request_url, kRelationshipParam, relationship); | |
| 43 DCHECK(request_url.is_valid()); | |
| 44 return request_url; | |
| 45 } | |
| 46 } // namespace | |
| 47 | |
| 48 namespace digital_asset_links { | |
| 49 | |
| 50 const char kDigitalAssetLinksCheckResponseKeyLinked[] = "linked"; | |
| 51 | |
| 52 DigitalAssetLinksHandler::DigitalAssetLinksHandler( | |
| 53 const scoped_refptr<net::URLRequestContextGetter>& request_context) | |
| 54 : request_context_(request_context) {} | |
| 55 | |
| 56 DigitalAssetLinksHandler::~DigitalAssetLinksHandler() {} | |
| 57 | |
| 58 void DigitalAssetLinksHandler::OnURLFetchComplete( | |
| 59 const net::URLFetcher* source) { | |
| 60 if (!source->GetStatus().is_success() || | |
| 61 source->GetResponseCode() != net::HTTP_OK) { | |
| 62 LOG(WARNING) << base::StringPrintf( | |
| 63 "Digital Asset Links endpoint responded with code %d.", | |
| 64 source->GetResponseCode()); | |
| 65 callback_->Run(nullptr); | |
| 66 return; | |
| 67 } | |
| 68 | |
| 69 std::string response_body; | |
| 70 source->GetResponseAsString(&response_body); | |
| 71 | |
| 72 std::unique_ptr<base::Value> value = base::JSONReader::Read(response_body); | |
|
Benoit L
2017/04/04 18:44:12
DictionaryValue::From() gives a unique_ptr<> direc
Yusuf
2017/04/05 00:20:42
Done.
| |
| 73 base::DictionaryValue* dict; | |
| 74 if (!value || !value->GetAsDictionary(&dict)) { | |
| 75 callback_->Run(std::unique_ptr<base::DictionaryValue>(dict)); | |
| 76 return; | |
| 77 } | |
| 78 callback_->Run(nullptr); | |
|
Benoit L
2017/04/04 18:44:12
Should we delete the URLFetcher here?
Yusuf
2017/04/05 00:20:42
Resetted. Does that work? :)
| |
| 79 } | |
| 80 | |
| 81 bool DigitalAssetLinksHandler::CheckDigitalAssetLinkRelationship( | |
| 82 std::unique_ptr<RelationshipCheckResultCallback> listener, | |
| 83 const std::string& web_domain, | |
| 84 const std::string& package_name, | |
| 85 const std::string& fingerprint, | |
| 86 const std::string& relationship) { | |
| 87 GURL request_url = GetUrlForCheckingRelationship(web_domain, package_name, | |
| 88 fingerprint, relationship); | |
| 89 if (!request_url.is_valid()) | |
| 90 return false; | |
| 91 | |
| 92 callback_ = std::move(listener); | |
|
Benoit L
2017/04/04 18:44:12
nit: as suggested above, passing the callback by v
Yusuf
2017/04/05 00:20:42
Done.
| |
| 93 | |
| 94 url_fetcher_ = std::move( | |
|
Benoit L
2017/04/04 18:44:12
dumb question: is it OK to delete a URLFetcher whi
Yusuf
2017/04/05 00:20:42
I think so yes. At least I have seen examples of t
| |
| 95 net::URLFetcher::Create(0, request_url, net::URLFetcher::GET, this)); | |
| 96 url_fetcher_->SetAutomaticallyRetryOn5xx(false); | |
| 97 url_fetcher_->SetRequestContext(request_context_.get()); | |
| 98 url_fetcher_->Start(); | |
| 99 return true; | |
| 100 } | |
| 101 | |
| 102 } // namespace digital_asset_links | |
| OLD | NEW |