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 callback_.Run( |
| 73 base::DictionaryValue::From(base::JSONReader::Read(response_body))); |
| 74 |
| 75 url_fetcher_.reset(nullptr); |
| 76 } |
| 77 |
| 78 bool DigitalAssetLinksHandler::CheckDigitalAssetLinkRelationship( |
| 79 RelationshipCheckResultCallback listener, |
| 80 const std::string& web_domain, |
| 81 const std::string& package_name, |
| 82 const std::string& fingerprint, |
| 83 const std::string& relationship) { |
| 84 GURL request_url = GetUrlForCheckingRelationship(web_domain, package_name, |
| 85 fingerprint, relationship); |
| 86 if (!request_url.is_valid()) |
| 87 return false; |
| 88 |
| 89 callback_ = listener; |
| 90 |
| 91 url_fetcher_ = |
| 92 net::URLFetcher::Create(0, request_url, net::URLFetcher::GET, this); |
| 93 url_fetcher_->SetAutomaticallyRetryOn5xx(false); |
| 94 url_fetcher_->SetRequestContext(request_context_.get()); |
| 95 url_fetcher_->Start(); |
| 96 return true; |
| 97 } |
| 98 |
| 99 } // namespace digital_asset_links |
OLD | NEW |