Index: components/digital_asset_links/digital_asset_links_handler.cc |
diff --git a/components/digital_asset_links/digital_asset_links_handler.cc b/components/digital_asset_links/digital_asset_links_handler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..93e3956273be7217a18d6c5bf07b6134a287989e |
--- /dev/null |
+++ b/components/digital_asset_links/digital_asset_links_handler.cc |
@@ -0,0 +1,101 @@ |
+// Copyright (c) 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/digital_asset_links/digital_asset_links_handler.h" |
+ |
+#include "base/json/json_reader.h" |
+#include "base/logging.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/stringprintf.h" |
+#include "base/values.h" |
+#include "net/base/load_flags.h" |
+#include "net/base/url_util.h" |
+#include "net/http/http_response_headers.h" |
+#include "net/http/http_status_code.h" |
+#include "net/http/http_util.h" |
+#include "net/url_request/url_request_status.h" |
+ |
+namespace { |
+static const char kDigitalAssetLinksBaseURL[] = |
Benoit L
2017/03/31 15:38:43
nit: "static" is unnecessary here and below (due t
Yusuf
2017/03/31 22:41:49
Done.
|
+ "https://digitalassetlinks.googleapis.com"; |
+static const char kDigitalAssetLinksCheckAPI[] = "/v1/assetlinks:check?"; |
Benoit L
2017/03/31 15:38:42
nit: Should the path include the "?" at the end?
W
Yusuf
2017/03/31 22:41:50
I might be missing something but it looks like no.
|
+static const char kTargetOriginParam[] = "source.web.site"; |
+static const char kSourcePackageNameParam[] = "target.androidApp.packageName"; |
+static const char kSourceFingerprintParam[] = |
+ "target.androidApp.certificate.sha256Fingerprint"; |
+static const char kRelationshipParam[] = "relation"; |
+ |
+GURL GetUrlForCheckingRelationship(std::string web_domain, |
Benoit L
2017/03/31 15:38:42
nit: make all parameters
const std::string& param
Yusuf
2017/03/31 22:41:50
Done.
|
+ std::string package_name, |
+ std::string fingerprint, |
+ std::string relationship) { |
+ GURL request_url = |
+ GURL(kDigitalAssetLinksBaseURL).Resolve(kDigitalAssetLinksCheckAPI); |
+ request_url = |
+ net::AppendQueryParameter(request_url, kTargetOriginParam, web_domain); |
+ request_url = net::AppendQueryParameter(request_url, kSourcePackageNameParam, |
+ package_name); |
+ request_url = net::AppendQueryParameter(request_url, kSourceFingerprintParam, |
+ fingerprint); |
+ request_url = |
+ net::AppendQueryParameter(request_url, kRelationshipParam, relationship); |
+ return request_url; |
Benoit L
2017/03/31 15:38:43
nit: Add a DCHECK() above to check that the URL is
Yusuf
2017/03/31 22:41:50
Done.
|
+} |
+} |
Benoit L
2017/03/31 15:38:42
nit: Add
// namespace
Yusuf
2017/03/31 22:41:50
Done.
|
+ |
+namespace digital_asset_links { |
+ |
+DigitalAssetLinksHandler::DigitalAssetLinksHandler( |
+ const scoped_refptr<net::URLRequestContextGetter>& request_context) { |
Benoit L
2017/03/31 15:38:42
nit: Initializer list instead of inline in the con
Yusuf
2017/03/31 22:41:49
Done.
|
+ request_context_ = request_context; |
+} |
+ |
+DigitalAssetLinksHandler::~DigitalAssetLinksHandler() {} |
+ |
+void DigitalAssetLinksHandler::OnURLFetchComplete( |
+ const net::URLFetcher* source) { |
+ base::DictionaryValue* dict = NULL; |
+ if (!source->GetStatus().is_success() || |
+ source->GetResponseCode() != net::HTTP_OK) { |
+ LOG(WARNING) << base::StringPrintf( |
+ "Digital Asset Links endpoint responded with code %d.", |
+ source->GetResponseCode()); |
+ listener_.get()->OnRelationshipCheckComplete(dict); |
Benoit L
2017/03/31 15:38:42
nit: listener_->OnRelationshipCheckComplete(nullpt
Yusuf
2017/03/31 22:41:50
Done.
|
+ return; |
+ } |
+ std::string response_body; |
+ source->GetResponseAsString(&response_body); |
+ |
+ std::unique_ptr<base::Value> value = base::JSONReader::Read(response_body); |
+ if (!value.get() || !value->GetAsDictionary(&dict)) { |
Benoit L
2017/03/31 15:38:42
nit:
if (!value || !value->GetAsDictionary(...))
Yusuf
2017/03/31 22:41:50
Done.
|
+ listener_.get()->OnRelationshipCheckComplete(dict); |
+ return; |
+ } |
+ listener_.get()->OnRelationshipCheckComplete(dict); |
+} |
+ |
+bool DigitalAssetLinksHandler::CheckDigitalAssetLinkRelationship( |
+ AssetLinkListener* listener, |
+ std::string web_domain, |
Benoit L
2017/03/31 15:38:42
nit: make the std::string arguments
const std::str
Yusuf
2017/03/31 22:41:50
Done.
|
+ std::string package_name, |
+ std::string fingerprint, |
+ std::string relationship) { |
+ GURL request_url = GetUrlForCheckingRelationship(web_domain, package_name, |
+ fingerprint, relationship); |
+ if (!request_url.is_valid()) |
+ return false; |
+ |
+ listener_.reset(listener); |
Benoit L
2017/03/31 15:38:42
If this object takes ownership of the listener, th
Yusuf
2017/03/31 22:41:50
After the conversion to Callback, it made sense to
|
+ |
+ url_fetcher_.reset(); |
Benoit L
2017/03/31 15:38:42
nit:
url_fetcher_ = std::move(net::URLFetcher::Cr
Yusuf
2017/03/31 22:41:50
Done.
|
+ |
+ url_fetcher_ = |
Benoit L
2017/03/31 15:38:42
Question: What is the behavior of URLFetcher with
Yusuf
2017/03/31 22:41:50
From url_fetcher.h:
Normally, URLFetcher will abor
|
+ net::URLFetcher::Create(0, request_url, net::URLFetcher::GET, this); |
+ url_fetcher_->SetAutomaticallyRetryOn5xx(false); |
+ url_fetcher_->SetRequestContext(request_context_.get()); |
+ url_fetcher_->Start(); |
+ return true; |
+} |
+ |
+} // namespace digital_asset_links |