Migrate FinancialPing to SimpleURLLoader
URLFetcher will stop working with advent of Network Service, and SimpleURLLoader is the replacement API for most clients. This CL migrates RLZTracker and FinancialPing away from URLFetcher. Bug=773295,844989 Change-Id: I07fc8d09b359727ad8649e38355dd6af2bc60b02 Reviewed-on: https://chromium-review.googlesource.com/1142749 Commit-Queue: Antonio Gomes <tonikitoo@igalia.com> Reviewed-by: John Abd-El-Malek <jam@chromium.org> Reviewed-by: Carlos Pizano <cpu@chromium.org> Reviewed-by: Roger Tawa <rogerta@chromium.org> Reviewed-by: Matt Menke <mmenke@chromium.org> Cr-Commit-Position: refs/heads/master@{#582698}
This commit is contained in:

committed by
Commit Bot

parent
cfb4c38332
commit
a739bf463e
@ -31,6 +31,7 @@
|
||||
#include "content/public/browser/notification_source.h"
|
||||
#include "content/public/common/content_switches.h"
|
||||
#include "rlz/buildflags/buildflags.h"
|
||||
#include "services/network/public/cpp/shared_url_loader_factory.h"
|
||||
|
||||
#if defined(OS_WIN)
|
||||
#include "chrome/installer/util/google_update_settings.h"
|
||||
@ -115,8 +116,9 @@ bool ChromeRLZTrackerDelegate::IsOnUIThread() {
|
||||
return content::BrowserThread::CurrentlyOn(content::BrowserThread::UI);
|
||||
}
|
||||
|
||||
net::URLRequestContextGetter* ChromeRLZTrackerDelegate::GetRequestContext() {
|
||||
return g_browser_process->system_request_context();
|
||||
scoped_refptr<network::SharedURLLoaderFactory>
|
||||
ChromeRLZTrackerDelegate::GetURLLoaderFactory() {
|
||||
return g_browser_process->shared_url_loader_factory();
|
||||
}
|
||||
|
||||
bool ChromeRLZTrackerDelegate::GetBrand(std::string* brand) {
|
||||
|
@ -36,7 +36,7 @@ class ChromeRLZTrackerDelegate : public rlz::RLZTrackerDelegate,
|
||||
// RLZTrackerDelegate implementation.
|
||||
void Cleanup() override;
|
||||
bool IsOnUIThread() override;
|
||||
net::URLRequestContextGetter* GetRequestContext() override;
|
||||
scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory() override;
|
||||
bool GetBrand(std::string* brand) override;
|
||||
bool IsBrandOrganic(const std::string& brand) override;
|
||||
bool GetReactivationBrand(std::string* brand) override;
|
||||
|
@ -18,6 +18,7 @@ static_library("rlz") {
|
||||
"//components/google/core/browser",
|
||||
"//net",
|
||||
"//rlz:rlz_lib",
|
||||
"//services/network/public/cpp:cpp",
|
||||
]
|
||||
|
||||
if (is_ios) {
|
||||
@ -35,6 +36,7 @@ source_set("unit_tests") {
|
||||
":rlz",
|
||||
"//net:test_support",
|
||||
"//rlz:test_support",
|
||||
"//services/network/public/cpp:cpp",
|
||||
"//ui/base",
|
||||
]
|
||||
|
||||
|
@ -4,6 +4,7 @@ include_rules = [
|
||||
"+net",
|
||||
"+rlz",
|
||||
"+ui/base",
|
||||
"+services/network/public",
|
||||
|
||||
# rlz is used on iOS.
|
||||
"-content",
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include "base/trace_event/trace_event.h"
|
||||
#include "build/build_config.h"
|
||||
#include "components/rlz/rlz_tracker_delegate.h"
|
||||
#include "services/network/public/cpp/shared_url_loader_factory.h"
|
||||
|
||||
#if defined(OS_CHROMEOS)
|
||||
#include "base/syslog_logging.h"
|
||||
@ -151,16 +152,60 @@ bool SendFinancialPing(const std::string& brand,
|
||||
#else
|
||||
product_signature = "chrome";
|
||||
#endif
|
||||
return rlz_lib::SendFinancialPing(rlz_lib::CHROME, points,
|
||||
product_signature.c_str(),
|
||||
brand.c_str(), referral_ascii.c_str(),
|
||||
lang_ascii.c_str(), false, true);
|
||||
return rlz_lib::SendFinancialPing(
|
||||
rlz_lib::CHROME, points, product_signature.c_str(), brand.c_str(),
|
||||
referral_ascii.c_str(), lang_ascii.c_str(), false, true);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
RLZTracker* RLZTracker::tracker_ = nullptr;
|
||||
|
||||
// WrapperURLLoaderFactory subclasses mojom::URLLoaderFactory as non-mojo, cross
|
||||
// thread class. It basically posts ::CreateLoaderAndStart calls over to the UI
|
||||
// thread, to call them on the real mojo object.
|
||||
class RLZTracker::WrapperURLLoaderFactory
|
||||
: public network::mojom::URLLoaderFactory {
|
||||
public:
|
||||
explicit WrapperURLLoaderFactory(
|
||||
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
|
||||
: url_loader_factory_(std::move(url_loader_factory)),
|
||||
main_thread_task_runner_(base::SequencedTaskRunnerHandle::Get()) {}
|
||||
|
||||
void CreateLoaderAndStart(network::mojom::URLLoaderRequest loader,
|
||||
int32_t routing_id,
|
||||
int32_t request_id,
|
||||
uint32_t options,
|
||||
const network::ResourceRequest& request,
|
||||
network::mojom::URLLoaderClientPtr client,
|
||||
const net::MutableNetworkTrafficAnnotationTag&
|
||||
traffic_annotation) override {
|
||||
if (main_thread_task_runner_->RunsTasksInCurrentSequence()) {
|
||||
url_loader_factory_->CreateLoaderAndStart(
|
||||
std::move(loader), routing_id, request_id, options, request,
|
||||
std::move(client), traffic_annotation);
|
||||
} else {
|
||||
main_thread_task_runner_->PostTask(
|
||||
FROM_HERE,
|
||||
base::BindOnce(&WrapperURLLoaderFactory::CreateLoaderAndStart,
|
||||
base::Unretained(this), std::move(loader), routing_id,
|
||||
request_id, options, request, std::move(client),
|
||||
traffic_annotation));
|
||||
}
|
||||
}
|
||||
void Clone(network::mojom::URLLoaderFactoryRequest factory) override {
|
||||
NOTIMPLEMENTED();
|
||||
}
|
||||
|
||||
private:
|
||||
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
|
||||
|
||||
// Runner for RLZ main thread tasks.
|
||||
scoped_refptr<base::SequencedTaskRunner> main_thread_task_runner_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(WrapperURLLoaderFactory);
|
||||
};
|
||||
|
||||
// static
|
||||
RLZTracker* RLZTracker::GetInstance() {
|
||||
return tracker_ ? tracker_ : base::Singleton<RLZTracker>::get();
|
||||
@ -261,9 +306,11 @@ bool RLZTracker::Init(bool first_run,
|
||||
#endif
|
||||
|
||||
// Could be null; don't run if so. RLZ will try again next restart.
|
||||
net::URLRequestContextGetter* context_getter = delegate_->GetRequestContext();
|
||||
if (context_getter) {
|
||||
rlz_lib::SetURLRequestContext(context_getter);
|
||||
auto shared_url_loader_factory = delegate_->GetURLLoaderFactory();
|
||||
if (shared_url_loader_factory) {
|
||||
custom_url_loader_factory_ =
|
||||
std::make_unique<WrapperURLLoaderFactory>(shared_url_loader_factory);
|
||||
rlz_lib::SetURLLoaderFactory(custom_url_loader_factory_.get());
|
||||
ScheduleDelayedInit(delay);
|
||||
}
|
||||
|
||||
@ -571,7 +618,7 @@ bool RLZTracker::ScheduleClearRlzState() {
|
||||
// static
|
||||
void RLZTracker::CleanupRlz() {
|
||||
GetInstance()->Cleanup();
|
||||
rlz_lib::SetURLRequestContext(nullptr);
|
||||
rlz_lib::SetURLLoaderFactory(nullptr);
|
||||
}
|
||||
|
||||
// static
|
||||
|
@ -232,7 +232,10 @@ class RLZTracker {
|
||||
// Minimum delay before sending financial ping after initialization.
|
||||
base::TimeDelta min_init_delay_;
|
||||
|
||||
// Runner for RLZ background tasks. The checked is used to verify operations
|
||||
class WrapperURLLoaderFactory;
|
||||
std::unique_ptr<WrapperURLLoaderFactory> custom_url_loader_factory_;
|
||||
|
||||
// Runner for RLZ background tasks. The checker is used to verify operations
|
||||
// occur in the correct sequence, especially in tests.
|
||||
scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
|
||||
SEQUENCE_CHECKER(sequence_checker_);
|
||||
|
@ -11,9 +11,9 @@
|
||||
#include "base/macros.h"
|
||||
#include "base/strings/string16.h"
|
||||
|
||||
namespace net {
|
||||
class URLRequestContextGetter;
|
||||
}
|
||||
namespace network {
|
||||
class SharedURLLoaderFactory;
|
||||
} // namespace network
|
||||
|
||||
namespace rlz {
|
||||
|
||||
@ -30,8 +30,9 @@ class RLZTrackerDelegate {
|
||||
// Returns whether the current thread is the UI thread.
|
||||
virtual bool IsOnUIThread() = 0;
|
||||
|
||||
// Returns the URLRequestContextGetter to use for network connections.
|
||||
virtual net::URLRequestContextGetter* GetRequestContext() = 0;
|
||||
// Returns the SharedURLLoaderFactory to use for network connections.
|
||||
virtual scoped_refptr<network::SharedURLLoaderFactory>
|
||||
GetURLLoaderFactory() = 0;
|
||||
|
||||
// Returns the brand code for the installation of Chrome in |brand| and a
|
||||
// boolean indicating whether the operation was a success or not.
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include "components/rlz/rlz_tracker_delegate.h"
|
||||
#include "net/url_request/url_request_test_util.h"
|
||||
#include "rlz/test/rlz_test_helpers.h"
|
||||
#include "services/network/public/cpp/shared_url_loader_factory.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
#if defined(OS_IOS)
|
||||
@ -71,8 +72,10 @@ class TestRLZTrackerDelegate : public RLZTrackerDelegate {
|
||||
|
||||
bool IsOnUIThread() override { return true; }
|
||||
|
||||
net::URLRequestContextGetter* GetRequestContext() override {
|
||||
return request_context_getter_.get();
|
||||
scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory()
|
||||
override {
|
||||
NOTIMPLEMENTED() << "If this is called, it needs an implementation.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool GetBrand(std::string* brand) override {
|
||||
|
@ -80,6 +80,8 @@ if (!is_android) {
|
||||
"//base",
|
||||
"//base/third_party/dynamic_annotations",
|
||||
"//net",
|
||||
"//services/network/public/cpp:cpp",
|
||||
"//services/network/public/mojom",
|
||||
"//url",
|
||||
]
|
||||
|
||||
@ -121,6 +123,7 @@ if (!is_android) {
|
||||
":rlz_lib",
|
||||
"//base",
|
||||
"//base/test:test_support",
|
||||
"//services/network/public/cpp:cpp",
|
||||
"//testing/gtest",
|
||||
]
|
||||
if (is_chromeos) {
|
||||
@ -149,7 +152,9 @@ if (!is_android) {
|
||||
":rlz_utils",
|
||||
":test_support",
|
||||
"//base",
|
||||
"//mojo/core/embedder",
|
||||
"//net:test_support",
|
||||
"//services/network:test_support",
|
||||
"//testing/gmock",
|
||||
"//testing/gtest",
|
||||
"//third_party/zlib",
|
||||
|
3
rlz/DEPS
3
rlz/DEPS
@ -58,8 +58,11 @@ include_rules = [
|
||||
"+build",
|
||||
"+chromeos/dbus",
|
||||
"+chromeos/system",
|
||||
"+mojo/core/embedder",
|
||||
"+net", # This is only used when force_rlz_use_chrome_net=1 is passed to gyp.
|
||||
"+third_party/zlib",
|
||||
"+services/network/public",
|
||||
"+services/network/test",
|
||||
]
|
||||
|
||||
hooks = [
|
||||
|
@ -29,6 +29,8 @@
|
||||
#include "rlz/lib/rlz_lib.h"
|
||||
#include "rlz/lib/rlz_value_store.h"
|
||||
#include "rlz/lib/string_utils.h"
|
||||
#include "services/network/public/cpp/shared_url_loader_factory.h"
|
||||
#include "services/network/public/cpp/simple_url_loader.h"
|
||||
|
||||
#if !defined(OS_WIN)
|
||||
#include "base/time/time.h"
|
||||
@ -61,10 +63,6 @@ class InternetHandle {
|
||||
#include "base/time/time.h"
|
||||
#include "net/base/load_flags.h"
|
||||
#include "net/traffic_annotation/network_traffic_annotation.h"
|
||||
#include "net/url_request/url_fetcher.h"
|
||||
#include "net/url_request/url_fetcher_delegate.h"
|
||||
#include "net/url_request/url_request_context.h"
|
||||
#include "net/url_request/url_request_context_getter.h"
|
||||
#include "url/gurl.h"
|
||||
|
||||
#endif
|
||||
@ -169,12 +167,12 @@ bool FinancialPing::FormRequest(Product product,
|
||||
// The pointer to URLRequestContextGetter used by FinancialPing::PingServer().
|
||||
// It is atomic pointer because it can be accessed and modified by multiple
|
||||
// threads.
|
||||
AtomicWord g_context;
|
||||
AtomicWord g_URLLoaderFactory;
|
||||
|
||||
bool FinancialPing::SetURLRequestContext(
|
||||
net::URLRequestContextGetter* context) {
|
||||
base::subtle::Release_Store(
|
||||
&g_context, reinterpret_cast<AtomicWord>(context));
|
||||
bool FinancialPing::SetURLLoaderFactory(
|
||||
network::mojom::URLLoaderFactory* factory) {
|
||||
base::subtle::Release_Store(&g_URLLoaderFactory,
|
||||
reinterpret_cast<AtomicWord>(factory));
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -225,31 +223,26 @@ class RefCountedWaitableEvent
|
||||
base::WaitableEvent event_;
|
||||
base::Lock lock_;
|
||||
std::string response_;
|
||||
int response_code_ = net::URLFetcher::RESPONSE_CODE_INVALID;
|
||||
int response_code_ = -1;
|
||||
};
|
||||
|
||||
// A fetcher delegate that signals an instance of RefCountedWaitableEvent when
|
||||
// the fetch completes.
|
||||
class FinancialPingUrlFetcherDelegate : public net::URLFetcherDelegate {
|
||||
public:
|
||||
FinancialPingUrlFetcherDelegate(scoped_refptr<RefCountedWaitableEvent> event)
|
||||
: event_(std::move(event)) {}
|
||||
|
||||
void SetFetcher(std::unique_ptr<net::URLFetcher> fetcher) {
|
||||
fetcher_ = std::move(fetcher);
|
||||
// The URL load complete callback signals an instance of
|
||||
// RefCountedWaitableEvent when the load completes.
|
||||
void OnURLLoadComplete(std::unique_ptr<network::SimpleURLLoader> url_loader,
|
||||
scoped_refptr<RefCountedWaitableEvent> event,
|
||||
std::unique_ptr<std::string> response_body) {
|
||||
int response_code = -1;
|
||||
if (url_loader->ResponseInfo() && url_loader->ResponseInfo()->headers) {
|
||||
response_code = url_loader->ResponseInfo()->headers->response_code();
|
||||
}
|
||||
|
||||
private:
|
||||
void OnURLFetchComplete(const net::URLFetcher* source) override {
|
||||
std::string response;
|
||||
source->GetResponseAsString(&response);
|
||||
event_->SignalFetchComplete(source->GetResponseCode(), std::move(response));
|
||||
base::SequencedTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
|
||||
std::string response;
|
||||
if (response_body) {
|
||||
response = std::move(*response_body);
|
||||
}
|
||||
|
||||
scoped_refptr<RefCountedWaitableEvent> event_;
|
||||
std::unique_ptr<net::URLFetcher> fetcher_;
|
||||
};
|
||||
event->SignalFetchComplete(response_code, std::move(response));
|
||||
}
|
||||
|
||||
bool send_financial_ping_interrupted_for_test = false;
|
||||
|
||||
@ -260,7 +253,7 @@ void ShutdownCheck(scoped_refptr<RefCountedWaitableEvent> event) {
|
||||
if (base::subtle::Acquire_Load(&g_cancelShutdownCheck))
|
||||
return;
|
||||
|
||||
if (!base::subtle::Acquire_Load(&g_context)) {
|
||||
if (!base::subtle::Acquire_Load(&g_URLLoaderFactory)) {
|
||||
send_financial_ping_interrupted_for_test = true;
|
||||
event->SignalShutdown();
|
||||
return;
|
||||
@ -276,22 +269,18 @@ void ShutdownCheck(scoped_refptr<RefCountedWaitableEvent> event) {
|
||||
|
||||
void PingRlzServer(std::string url,
|
||||
scoped_refptr<RefCountedWaitableEvent> event) {
|
||||
// Copy the pointer to stack because g_context may be set to NULL
|
||||
// Copy the pointer to stack because g_URLLoaderFactory may be set to NULL
|
||||
// in different thread. The instance is guaranteed to exist while
|
||||
// the method is running.
|
||||
net::URLRequestContextGetter* context =
|
||||
reinterpret_cast<net::URLRequestContextGetter*>(
|
||||
base::subtle::Acquire_Load(&g_context));
|
||||
network::mojom::URLLoaderFactory* url_loader_factory =
|
||||
reinterpret_cast<network::mojom::URLLoaderFactory*>(
|
||||
base::subtle::Acquire_Load(&g_URLLoaderFactory));
|
||||
|
||||
// Browser shutdown will cause the context to be reset to NULL.
|
||||
// Browser shutdown will cause the factory to be reset to NULL.
|
||||
// ShutdownCheck will catch this.
|
||||
if (!context)
|
||||
if (!url_loader_factory)
|
||||
return;
|
||||
|
||||
// Delegate will delete itself when the fetch completes.
|
||||
FinancialPingUrlFetcherDelegate* delegate =
|
||||
new FinancialPingUrlFetcherDelegate(event);
|
||||
|
||||
net::NetworkTrafficAnnotationTag traffic_annotation =
|
||||
net::DefineNetworkTrafficAnnotation("rlz_ping", R"(
|
||||
semantics {
|
||||
@ -315,21 +304,22 @@ void PingRlzServer(std::string url,
|
||||
setting: "This feature cannot be disabled in settings."
|
||||
policy_exception_justification: "Not implemented."
|
||||
})");
|
||||
std::unique_ptr<net::URLFetcher> fetcher = net::URLFetcher::Create(
|
||||
GURL(url), net::URLFetcher::GET, delegate, traffic_annotation);
|
||||
|
||||
fetcher->SetLoadFlags(
|
||||
auto resource_request = std::make_unique<network::ResourceRequest>();
|
||||
resource_request->url = GURL(url);
|
||||
resource_request->load_flags =
|
||||
net::LOAD_DISABLE_CACHE | net::LOAD_DO_NOT_SEND_AUTH_DATA |
|
||||
net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES);
|
||||
net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES;
|
||||
|
||||
// Ensure rlz_lib::SetURLRequestContext() has been called before sending
|
||||
// pings.
|
||||
fetcher->SetRequestContext(context);
|
||||
fetcher->Start();
|
||||
auto url_loader = network::SimpleURLLoader::Create(
|
||||
std::move(resource_request), traffic_annotation);
|
||||
|
||||
// Pass ownership of the fetcher to the delegate. Otherwise the fetch will
|
||||
// be canceled when the URLFetcher object is destroyed.
|
||||
delegate->SetFetcher(std::move(fetcher));
|
||||
// Pass ownership of the loader to the bound function. Otherwise the load will
|
||||
// be canceled when the SimpleURLLoader object is destroyed.
|
||||
auto* url_loader_ptr = url_loader.get();
|
||||
url_loader_ptr->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
|
||||
url_loader_factory,
|
||||
base::BindOnce(&OnURLLoadComplete, std::move(url_loader),
|
||||
std::move(event)));
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -426,7 +416,7 @@ FinancialPing::PingResponse FinancialPing::PingServer(const char* request,
|
||||
if (!is_signaled)
|
||||
return PING_FAILURE;
|
||||
|
||||
if (event->GetResponseCode() == net::URLFetcher::RESPONSE_CODE_INVALID) {
|
||||
if (event->GetResponseCode() == -1) {
|
||||
return PING_SHUTDOWN;
|
||||
} else if (event->GetResponseCode() != 200) {
|
||||
return PING_FAILURE;
|
||||
|
@ -11,9 +11,11 @@
|
||||
#include "rlz/lib/rlz_enums.h"
|
||||
|
||||
#if defined(RLZ_NETWORK_IMPLEMENTATION_CHROME_NET)
|
||||
namespace net {
|
||||
class URLRequestContextGetter;
|
||||
} // namespace net
|
||||
namespace network {
|
||||
namespace mojom {
|
||||
class URLLoaderFactory;
|
||||
}
|
||||
} // namespace network
|
||||
#endif
|
||||
|
||||
namespace rlz_lib {
|
||||
@ -62,7 +64,7 @@ class FinancialPing {
|
||||
static int64_t GetSystemTimeAsInt64();
|
||||
|
||||
#if defined(RLZ_NETWORK_IMPLEMENTATION_CHROME_NET)
|
||||
static bool SetURLRequestContext(net::URLRequestContextGetter* context);
|
||||
static bool SetURLLoaderFactory(network::mojom::URLLoaderFactory* factory);
|
||||
#endif
|
||||
|
||||
private:
|
||||
|
@ -22,6 +22,7 @@
|
||||
#include "rlz/lib/net_response_check.h"
|
||||
#include "rlz/lib/rlz_value_store.h"
|
||||
#include "rlz/lib/string_utils.h"
|
||||
#include "services/network/public/mojom/url_loader_factory.mojom.h"
|
||||
|
||||
namespace {
|
||||
|
||||
@ -216,8 +217,8 @@ bool GetProductEventsAsCgiHelper(rlz_lib::Product product, char* cgi,
|
||||
namespace rlz_lib {
|
||||
|
||||
#if defined(RLZ_NETWORK_IMPLEMENTATION_CHROME_NET)
|
||||
bool SetURLRequestContext(net::URLRequestContextGetter* context) {
|
||||
return FinancialPing::SetURLRequestContext(context);
|
||||
bool SetURLLoaderFactory(network::mojom::URLLoaderFactory* factory) {
|
||||
return FinancialPing::SetURLLoaderFactory(factory);
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -429,7 +430,6 @@ bool SendFinancialPing(Product product,
|
||||
std::string response;
|
||||
|
||||
#if defined(OS_CHROMEOS)
|
||||
|
||||
const net::BackoffEntry::Policy policy = {
|
||||
0, // Number of initial errors to ignore.
|
||||
base::TimeDelta::FromSeconds(5).InMilliseconds(), // Initial delay.
|
||||
@ -473,14 +473,11 @@ bool SendFinancialPing(Product product,
|
||||
}
|
||||
|
||||
SYSLOG(INFO) << "Succeeded in sending RLZ ping";
|
||||
|
||||
#else
|
||||
|
||||
FinancialPing::PingResponse res =
|
||||
FinancialPing::PingServer(request.c_str(), &response);
|
||||
if (res != FinancialPing::PING_SUCCESSFUL)
|
||||
return false;
|
||||
|
||||
#endif
|
||||
|
||||
return ParsePingResponse(product, response.c_str());
|
||||
|
@ -24,7 +24,7 @@
|
||||
// Define one of
|
||||
// + RLZ_NETWORK_IMPLEMENTATION_WIN_INET: Uses win inet to send financial pings.
|
||||
// + RLZ_NETWORK_IMPLEMENTATION_CHROME_NET: Uses chrome's network stack to send
|
||||
// financial pings. rlz_lib::SetURLRequestContext() must be called before
|
||||
// financial pings. rlz_lib::SetURLLoaderFactory() must be called before
|
||||
// any calls to SendFinancialPing().
|
||||
#if defined(RLZ_NETWORK_IMPLEMENTATION_WIN_INET) && \
|
||||
defined(RLZ_NETWORK_IMPLEMENTATION_CHROME_NET)
|
||||
@ -41,9 +41,11 @@
|
||||
#endif
|
||||
|
||||
#if defined(RLZ_NETWORK_IMPLEMENTATION_CHROME_NET)
|
||||
namespace net {
|
||||
class URLRequestContextGetter;
|
||||
} // namespace net
|
||||
namespace network {
|
||||
namespace mojom {
|
||||
class URLLoaderFactory;
|
||||
} // namespace mojom
|
||||
} // namespace network
|
||||
#endif
|
||||
|
||||
namespace rlz_lib {
|
||||
@ -74,10 +76,8 @@ const size_t kMaxDccLength = 128;
|
||||
const size_t kMaxCgiLength = 2048;
|
||||
|
||||
#if defined(RLZ_NETWORK_IMPLEMENTATION_CHROME_NET)
|
||||
// Set the URLRequestContextGetter used by SendFinancialPing(). The IO message
|
||||
// loop returned by this context will be used for the IO done by
|
||||
// SendFinancialPing().
|
||||
bool RLZ_LIB_API SetURLRequestContext(net::URLRequestContextGetter* context);
|
||||
// Set the URLLoaderFactory used by SendFinancialPing().
|
||||
bool RLZ_LIB_API SetURLLoaderFactory(network::mojom::URLLoaderFactory* factory);
|
||||
#endif
|
||||
|
||||
// RLZ storage functions.
|
||||
@ -182,7 +182,7 @@ bool RLZ_LIB_API ParseFinancialPingResponse(Product product,
|
||||
// This ping method should be called daily. (More frequent calls will fail).
|
||||
// Also, if there are no events, the call will succeed only once a week.
|
||||
//
|
||||
// If RLZ_NETWORK_IMPLEMENTATION_CHROME_NET is set, SetURLRequestContext() needs
|
||||
// If RLZ_NETWORK_IMPLEMENTATION_CHROME_NET is set, SetURLLoaderFactory() needs
|
||||
// to be called before calling this function.
|
||||
//
|
||||
// product : The product to ping for.
|
||||
|
@ -43,6 +43,9 @@
|
||||
#include "base/mac/scoped_nsautorelease_pool.h"
|
||||
#include "base/threading/thread.h"
|
||||
#include "net/url_request/url_request_test_util.h"
|
||||
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
|
||||
#include "services/network/public/mojom/url_loader_factory.mojom.h"
|
||||
#include "services/network/test/test_url_loader_factory.h"
|
||||
#endif
|
||||
|
||||
#if defined(OS_CHROMEOS)
|
||||
@ -79,7 +82,7 @@ class RlzLibTest : public RlzLibTestBase {
|
||||
const char* product_id,
|
||||
const char* product_lang,
|
||||
bool exclude_machine_id,
|
||||
net::FakeURLFetcherFactory* factory) {
|
||||
network::TestURLLoaderFactory* url_loader_factory) {
|
||||
const char kGoodPingResponses[] =
|
||||
"version: 3.0.914.7250\r\n"
|
||||
"url: "
|
||||
@ -97,10 +100,9 @@ class RlzLibTest : public RlzLibTestBase {
|
||||
EXPECT_TRUE(rlz_lib::FinancialPing::FormRequest(
|
||||
product, access_points, product_signature, product_brand, product_id,
|
||||
product_lang, exclude_machine_id, &request));
|
||||
GURL url = GURL(base::StringPrintf(
|
||||
"https://%s%s", rlz_lib::kFinancialServer, request.c_str()));
|
||||
factory->SetFakeResponse(url, kGoodPingResponses, net::HTTP_OK,
|
||||
net::URLRequestStatus::SUCCESS);
|
||||
std::string url = base::StringPrintf(
|
||||
"https://%s%s", rlz_lib::kFinancialServer, request.c_str());
|
||||
url_loader_factory->AddResponse(url, kGoodPingResponses);
|
||||
}
|
||||
|
||||
base::test::ScopedTaskEnvironment scoped_task_environment_;
|
||||
@ -483,14 +485,12 @@ TEST_F(RlzLibTest, ParsePingResponseWithStatefulEvents) {
|
||||
EXPECT_STREQ("events=W1I", value);
|
||||
}
|
||||
|
||||
class URLRequestRAII {
|
||||
class URLLoaderFactoryRAII {
|
||||
public:
|
||||
URLRequestRAII(net::URLRequestContextGetter* context) {
|
||||
rlz_lib::SetURLRequestContext(context);
|
||||
}
|
||||
~URLRequestRAII() {
|
||||
rlz_lib::SetURLRequestContext(NULL);
|
||||
URLLoaderFactoryRAII(network::mojom::URLLoaderFactory* factory) {
|
||||
rlz_lib::SetURLLoaderFactory(factory);
|
||||
}
|
||||
~URLLoaderFactoryRAII() { rlz_lib::SetURLLoaderFactory(nullptr); }
|
||||
};
|
||||
|
||||
TEST_F(RlzLibTest, SendFinancialPing) {
|
||||
@ -503,15 +503,13 @@ TEST_F(RlzLibTest, SendFinancialPing) {
|
||||
base::mac::ScopedNSAutoreleasePool pool;
|
||||
#endif
|
||||
|
||||
base::Thread::Options options;
|
||||
options.message_loop_type = base::MessageLoop::TYPE_IO;
|
||||
network::TestURLLoaderFactory test_url_loader_factory;
|
||||
scoped_refptr<network::SharedURLLoaderFactory>
|
||||
test_shared_url_loader_factory =
|
||||
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
|
||||
&test_url_loader_factory);
|
||||
|
||||
base::Thread io_thread("rlz_unittest_io_thread");
|
||||
ASSERT_TRUE(io_thread.StartWithOptions(options));
|
||||
|
||||
scoped_refptr<net::TestURLRequestContextGetter> context =
|
||||
new net::TestURLRequestContextGetter(io_thread.task_runner());
|
||||
URLRequestRAII set_context(context.get());
|
||||
URLLoaderFactoryRAII set_factory(test_shared_url_loader_factory.get());
|
||||
#endif
|
||||
|
||||
MachineDealCodeHelper::Clear();
|
||||
@ -533,23 +531,21 @@ TEST_F(RlzLibTest, SendFinancialPing) {
|
||||
rlz_lib::NO_ACCESS_POINT};
|
||||
|
||||
// Excluding machine id from requests so that a stable URL is used and
|
||||
// this test can use FakeURLFetcherFactory.
|
||||
net::FakeURLFetcherFactory factory(nullptr);
|
||||
// this test can use TestURLLoaderFactory.
|
||||
FakeGoodPingResponse(rlz_lib::TOOLBAR_NOTIFIER, points, "swg", "GGLA",
|
||||
"SwgProductId1234", "en-UK",
|
||||
/* exclude_machine_id */ true, &factory);
|
||||
/* exclude_machine_id */ true, &test_url_loader_factory);
|
||||
|
||||
EXPECT_TRUE(rlz_lib::SendFinancialPing(rlz_lib::TOOLBAR_NOTIFIER, points,
|
||||
"swg", "GGLA", "SwgProductId1234",
|
||||
"en-UK",
|
||||
/* exclude_machine_id */ true,
|
||||
/* skip_time_check */true));
|
||||
rlz_lib::SendFinancialPing(rlz_lib::TOOLBAR_NOTIFIER, points, "swg", "GGLA",
|
||||
"SwgProductId1234", "en-UK",
|
||||
/* exclude_machine_id */ true,
|
||||
/* skip_time_check */ true);
|
||||
}
|
||||
|
||||
#if defined(RLZ_NETWORK_IMPLEMENTATION_CHROME_NET)
|
||||
|
||||
void ResetContext() {
|
||||
rlz_lib::SetURLRequestContext(NULL);
|
||||
void ResetURLLoaderFactory() {
|
||||
rlz_lib::SetURLLoaderFactory(nullptr);
|
||||
}
|
||||
|
||||
TEST_F(RlzLibTest, SendFinancialPingDuringShutdown) {
|
||||
@ -567,9 +563,12 @@ TEST_F(RlzLibTest, SendFinancialPingDuringShutdown) {
|
||||
base::Thread io_thread("rlz_unittest_io_thread");
|
||||
ASSERT_TRUE(io_thread.StartWithOptions(options));
|
||||
|
||||
scoped_refptr<net::TestURLRequestContextGetter> context =
|
||||
new net::TestURLRequestContextGetter(io_thread.task_runner());
|
||||
URLRequestRAII set_context(context.get());
|
||||
network::TestURLLoaderFactory test_url_loader_factory;
|
||||
scoped_refptr<network::SharedURLLoaderFactory>
|
||||
test_shared_url_loader_factory =
|
||||
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
|
||||
&test_url_loader_factory);
|
||||
URLLoaderFactoryRAII set_factory(test_shared_url_loader_factory.get());
|
||||
|
||||
rlz_lib::AccessPoint points[] =
|
||||
{rlz_lib::IETB_SEARCH_BOX, rlz_lib::NO_ACCESS_POINT,
|
||||
@ -577,11 +576,13 @@ TEST_F(RlzLibTest, SendFinancialPingDuringShutdown) {
|
||||
rlz_lib::test::ResetSendFinancialPingInterrupted();
|
||||
EXPECT_FALSE(rlz_lib::test::WasSendFinancialPingInterrupted());
|
||||
|
||||
io_thread.task_runner()->PostTask(FROM_HERE, base::BindOnce(&ResetContext));
|
||||
std::string request;
|
||||
EXPECT_FALSE(rlz_lib::SendFinancialPing(rlz_lib::TOOLBAR_NOTIFIER, points,
|
||||
"swg", "GGLA", "SwgProductId1234", "en-UK", false,
|
||||
/*skip_time_check=*/true));
|
||||
io_thread.task_runner()->PostTask(FROM_HERE,
|
||||
base::BindOnce(&ResetURLLoaderFactory));
|
||||
|
||||
rlz_lib::SendFinancialPing(rlz_lib::TOOLBAR_NOTIFIER, points, "swg", "GGLA",
|
||||
"SwgProductId1234", "en-UK",
|
||||
/* exclude_machine_id */ false,
|
||||
/* skip_time_check */ true);
|
||||
|
||||
EXPECT_TRUE(rlz_lib::test::WasSendFinancialPingInterrupted());
|
||||
rlz_lib::test::ResetSendFinancialPingInterrupted();
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include "base/at_exit.h"
|
||||
#include "base/command_line.h"
|
||||
#include "build/build_config.h"
|
||||
#include "mojo/core/embedder/embedder.h"
|
||||
#include "rlz/lib/rlz_lib.h"
|
||||
#include "testing/gmock/include/gmock/gmock.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
@ -24,6 +25,8 @@ int main(int argc, char **argv) {
|
||||
testing::InitGoogleMock(&argc, argv);
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
|
||||
mojo::core::Init();
|
||||
|
||||
int ret = RUN_ALL_TESTS();
|
||||
if (ret == 0) {
|
||||
// Now re-run all the tests using a supplementary brand code. This brand
|
||||
|
Reference in New Issue
Block a user