Commit 108c8fff authored by Randall Raymond's avatar Randall Raymond Committed by Commit Bot

[Payment Request] Add classes to fetch information on native apps from iTunes

These two new files are mostly a copy of json_response_fetcher.cc and
json_response_fetcher.h from chrome/browser/ui/app_list/search/common/ 
(https://cs.chromium.org/chromium/src/chrome/browser/ui/app_list/search/common/?q=chrome/browser/ui/app_list/search/common&sq=package:chromium&dr).
The main differences are that 1) The namespace is changed from app_list to
payment_request_util 2) Instead of using SafeJsonParser, a basic parser is used
instead since iOS does not currently support SafeJsonParser.

Bug: 602666
Change-Id: Ibf619f1c53a32eb53654e00959fbcba61720c8a5
Reviewed-on: https://chromium-review.googlesource.com/562581
Commit-Queue: Randall Raymond <rayraymond@google.com>
Reviewed-by: default avatarJean-François Geyelin <jif@chromium.org>
Reviewed-by: default avatarmahmadi <mahmadi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#485122}
parent ac319a7e
...@@ -9,6 +9,8 @@ source_set("payments") { ...@@ -9,6 +9,8 @@ source_set("payments") {
sources = [ sources = [
"ios_can_make_payment_query_factory.cc", "ios_can_make_payment_query_factory.cc",
"ios_can_make_payment_query_factory.h", "ios_can_make_payment_query_factory.h",
"itunes_json_request.cc",
"itunes_json_request.h",
"payment_request.h", "payment_request.h",
"payment_request.mm", "payment_request.mm",
"payment_request_util.h", "payment_request_util.h",
...@@ -27,7 +29,9 @@ source_set("payments") { ...@@ -27,7 +29,9 @@ source_set("payments") {
"//ios/chrome/browser/browser_state", "//ios/chrome/browser/browser_state",
"//ios/chrome/browser/signin", "//ios/chrome/browser/signin",
"//ios/web", "//ios/web",
"//net",
"//ui/base", "//ui/base",
"//url",
] ]
libs = [ "UIKit.framework" ] libs = [ "UIKit.framework" ]
} }
......
// Copyright 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 "ios/chrome/browser/payments/itunes_json_request.h"
#include <utility>
#include "base/bind.h"
#include "base/json/json_reader.h"
#include "base/memory/ptr_util.h"
#include "base/values.h"
#include "net/base/load_flags.h"
#include "net/http/http_status_code.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_status.h"
#include "url/gurl.h"
namespace payment_request_util {
const char kBadResponse[] = "Bad iTunes Store search response";
const char kITunesStoreLookupPrefix[] = "https://itunes.apple.com/lookup?";
const char kITunesStoreSearchPrefix[] = "https://itunes.apple.com/search?";
ITunesJsonRequest::ITunesJsonRequest(
const Callback& callback,
net::URLRequestContextGetter* context_getter)
: callback_(callback),
context_getter_(context_getter),
weak_factory_(this) {
DCHECK(!callback_.is_null());
}
ITunesJsonRequest::~ITunesJsonRequest() {}
void ITunesJsonRequest::Start(ITunesStoreRequestType request_type,
const std::string& request_query) {
Stop();
std::string complete_query;
switch (request_type) {
case LOOKUP:
complete_query = kITunesStoreLookupPrefix + request_query;
case SEARCH:
complete_query = kITunesStoreSearchPrefix + request_query;
}
fetcher_ =
net::URLFetcher::Create(GURL(complete_query), net::URLFetcher::GET, this);
fetcher_->SetRequestContext(context_getter_);
fetcher_->SetLoadFlags(net::LOAD_MAYBE_USER_GESTURE |
net::LOAD_DO_NOT_SAVE_COOKIES |
net::LOAD_VERIFY_EV_CERT);
fetcher_->Start();
}
void ITunesJsonRequest::Stop() {
fetcher_.reset();
weak_factory_.InvalidateWeakPtrs();
}
void ITunesJsonRequest::ParseJson(
const std::string& json,
const payment_request_util::ITunesJsonRequest::SuccessCallback&
success_callback,
const payment_request_util::ITunesJsonRequest::ErrorCallback&
error_callback) {
DCHECK(!success_callback.is_null());
DCHECK(!error_callback.is_null());
base::JSONReader json_reader;
std::unique_ptr<base::Value> value = json_reader.ReadToValue(json);
if (value) {
success_callback.Run(std::move(value));
} else {
error_callback.Run(json_reader.GetErrorMessage());
}
}
void ITunesJsonRequest::OnJsonParseSuccess(
std::unique_ptr<base::Value> parsed_json) {
if (!parsed_json->IsType(base::Value::Type::DICTIONARY)) {
OnJsonParseError(kBadResponse);
return;
}
callback_.Run(base::WrapUnique(
static_cast<base::DictionaryValue*>(parsed_json.release())));
}
void ITunesJsonRequest::OnJsonParseError(const std::string& error) {
callback_.Run(std::unique_ptr<base::DictionaryValue>());
}
void ITunesJsonRequest::OnURLFetchComplete(const net::URLFetcher* source) {
CHECK_EQ(fetcher_.get(), source);
std::unique_ptr<net::URLFetcher> fetcher(std::move(fetcher_));
if (!fetcher->GetStatus().is_success() ||
fetcher->GetResponseCode() != net::HTTP_OK) {
OnJsonParseError(kBadResponse);
return;
}
std::string json_data;
fetcher->GetResponseAsString(&json_data);
// The parser will call us back via one of the callbacks.
ParseJson(json_data,
base::Bind(&ITunesJsonRequest::OnJsonParseSuccess,
weak_factory_.GetWeakPtr()),
base::Bind(&ITunesJsonRequest::OnJsonParseError,
weak_factory_.GetWeakPtr()));
}
} // namespace payment_request_util
// Copyright 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.
#ifndef IOS_CHROME_BROWSER_PAYMENTS_ITUNES_JSON_REQUEST_H_
#define IOS_CHROME_BROWSER_PAYMENTS_ITUNES_JSON_REQUEST_H_
#include <memory>
#include <string>
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "net/url_request/url_fetcher_delegate.h"
namespace base {
class DictionaryValue;
class Value;
} // namespace base
namespace net {
class URLFetcher;
class URLRequestContextGetter;
} // namespace net
namespace payment_request_util {
// A class that fetches a JSON formatted response from the iTunes Store using
// the iTunes Search API and a basic JSON parser to parse the response as a
// DictionaryValue.
class ITunesJsonRequest : public net::URLFetcherDelegate {
public:
// Callback to pass back the parsed json dictionary returned from iTunes.
// Invoked with NULL if there is an error.
typedef base::Callback<void(std::unique_ptr<base::DictionaryValue>)> Callback;
ITunesJsonRequest(const Callback& callback,
net::URLRequestContextGetter* context_getter);
~ITunesJsonRequest() override;
// Used to express the type of request. Please view the following for info:
// https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/
enum ITunesStoreRequestType {
LOOKUP,
SEARCH,
};
// Starts to fetch results for the given |request_type| and |request_query|.
void Start(ITunesStoreRequestType request_type,
const std::string& request_query);
void Stop();
private:
// Callbacks for JSON parsing.
typedef base::Callback<void(std::unique_ptr<base::Value> result)>
SuccessCallback;
typedef base::Callback<void(const std::string& error)> ErrorCallback;
void ParseJson(const std::string& raw_json_string,
const SuccessCallback& success_callback,
const ErrorCallback& error_callback);
void OnJsonParseSuccess(std::unique_ptr<base::Value> parsed_json);
void OnJsonParseError(const std::string& error);
// net::URLFetcherDelegate overrides:
void OnURLFetchComplete(const net::URLFetcher* source) override;
Callback callback_;
net::URLRequestContextGetter* context_getter_;
std::unique_ptr<net::URLFetcher> fetcher_;
base::WeakPtrFactory<ITunesJsonRequest> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ITunesJsonRequest);
};
} // namespace payment_request_util
#endif // IOS_CHROME_BROWSER_PAYMENTS_ITUNES_JSON_REQUEST_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment