diff --git a/chrome/browser/rlz/chrome_rlz_tracker_delegate.cc b/chrome/browser/rlz/chrome_rlz_tracker_delegate.cc index 78d7336fe694c8..e0c0610d41460f 100644 --- a/chrome/browser/rlz/chrome_rlz_tracker_delegate.cc +++ b/chrome/browser/rlz/chrome_rlz_tracker_delegate.cc @@ -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 +ChromeRLZTrackerDelegate::GetURLLoaderFactory() { + return g_browser_process->shared_url_loader_factory(); } bool ChromeRLZTrackerDelegate::GetBrand(std::string* brand) { diff --git a/chrome/browser/rlz/chrome_rlz_tracker_delegate.h b/chrome/browser/rlz/chrome_rlz_tracker_delegate.h index 172d5d4d6303c1..f1774e77321949 100644 --- a/chrome/browser/rlz/chrome_rlz_tracker_delegate.h +++ b/chrome/browser/rlz/chrome_rlz_tracker_delegate.h @@ -36,7 +36,7 @@ class ChromeRLZTrackerDelegate : public rlz::RLZTrackerDelegate, // RLZTrackerDelegate implementation. void Cleanup() override; bool IsOnUIThread() override; - net::URLRequestContextGetter* GetRequestContext() override; + scoped_refptr GetURLLoaderFactory() override; bool GetBrand(std::string* brand) override; bool IsBrandOrganic(const std::string& brand) override; bool GetReactivationBrand(std::string* brand) override; diff --git a/components/rlz/BUILD.gn b/components/rlz/BUILD.gn index f8434c4241d2b7..9b86d7feeb7376 100644 --- a/components/rlz/BUILD.gn +++ b/components/rlz/BUILD.gn @@ -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", ] diff --git a/components/rlz/DEPS b/components/rlz/DEPS index 15cc8cc7f12b52..7a00b9394a7388 100644 --- a/components/rlz/DEPS +++ b/components/rlz/DEPS @@ -4,6 +4,7 @@ include_rules = [ "+net", "+rlz", "+ui/base", + "+services/network/public", # rlz is used on iOS. "-content", diff --git a/components/rlz/rlz_tracker.cc b/components/rlz/rlz_tracker.cc index 7c92c0ddd1696b..9e6cd6f3081f88 100644 --- a/components/rlz/rlz_tracker.cc +++ b/components/rlz/rlz_tracker.cc @@ -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 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 url_loader_factory_; + + // Runner for RLZ main thread tasks. + scoped_refptr main_thread_task_runner_; + + DISALLOW_COPY_AND_ASSIGN(WrapperURLLoaderFactory); +}; + // static RLZTracker* RLZTracker::GetInstance() { return tracker_ ? tracker_ : base::Singleton::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(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 diff --git a/components/rlz/rlz_tracker.h b/components/rlz/rlz_tracker.h index 6e6009aa6c1ff3..7e164b6a781210 100644 --- a/components/rlz/rlz_tracker.h +++ b/components/rlz/rlz_tracker.h @@ -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 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 background_task_runner_; SEQUENCE_CHECKER(sequence_checker_); diff --git a/components/rlz/rlz_tracker_delegate.h b/components/rlz/rlz_tracker_delegate.h index bbf43dfe1323cc..d2c2df9fbbdd72 100644 --- a/components/rlz/rlz_tracker_delegate.h +++ b/components/rlz/rlz_tracker_delegate.h @@ -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 + 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. diff --git a/components/rlz/rlz_tracker_unittest.cc b/components/rlz/rlz_tracker_unittest.cc index 879fd521d0b84b..e33f7dc947131e 100644 --- a/components/rlz/rlz_tracker_unittest.cc +++ b/components/rlz/rlz_tracker_unittest.cc @@ -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 GetURLLoaderFactory() + override { + NOTIMPLEMENTED() << "If this is called, it needs an implementation."; + return nullptr; } bool GetBrand(std::string* brand) override { diff --git a/rlz/BUILD.gn b/rlz/BUILD.gn index 59d4816fb2bd56..99c82632275e9b 100644 --- a/rlz/BUILD.gn +++ b/rlz/BUILD.gn @@ -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", diff --git a/rlz/DEPS b/rlz/DEPS index a42d5f34e4cb60..a7e2515f22cf9b 100644 --- a/rlz/DEPS +++ b/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 = [ diff --git a/rlz/lib/financial_ping.cc b/rlz/lib/financial_ping.cc index 13b67759289b65..3a0e2cb50dcf72 100644 --- a/rlz/lib/financial_ping.cc +++ b/rlz/lib/financial_ping.cc @@ -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(context)); +bool FinancialPing::SetURLLoaderFactory( + network::mojom::URLLoaderFactory* factory) { + base::subtle::Release_Store(&g_URLLoaderFactory, + reinterpret_cast(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 event) - : event_(std::move(event)) {} - - void SetFetcher(std::unique_ptr fetcher) { - fetcher_ = std::move(fetcher); +// The URL load complete callback signals an instance of +// RefCountedWaitableEvent when the load completes. +void OnURLLoadComplete(std::unique_ptr url_loader, + scoped_refptr event, + std::unique_ptr 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 event_; - std::unique_ptr fetcher_; -}; + event->SignalFetchComplete(response_code, std::move(response)); +} bool send_financial_ping_interrupted_for_test = false; @@ -260,7 +253,7 @@ void ShutdownCheck(scoped_refptr 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 event) { void PingRlzServer(std::string url, scoped_refptr 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( - base::subtle::Acquire_Load(&g_context)); + network::mojom::URLLoaderFactory* url_loader_factory = + reinterpret_cast( + 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 fetcher = net::URLFetcher::Create( - GURL(url), net::URLFetcher::GET, delegate, traffic_annotation); - - fetcher->SetLoadFlags( + auto resource_request = std::make_unique(); + 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); - - // Ensure rlz_lib::SetURLRequestContext() has been called before sending - // pings. - fetcher->SetRequestContext(context); - fetcher->Start(); - - // 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)); + net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES; + + auto url_loader = network::SimpleURLLoader::Create( + std::move(resource_request), traffic_annotation); + + // 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; diff --git a/rlz/lib/financial_ping.h b/rlz/lib/financial_ping.h index c3132ae8c7ba3c..7fae95469d2f0b 100644 --- a/rlz/lib/financial_ping.h +++ b/rlz/lib/financial_ping.h @@ -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: diff --git a/rlz/lib/rlz_lib.cc b/rlz/lib/rlz_lib.cc index 493ae904a75af7..703d14446d6036 100644 --- a/rlz/lib/rlz_lib.cc +++ b/rlz/lib/rlz_lib.cc @@ -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()); diff --git a/rlz/lib/rlz_lib.h b/rlz/lib/rlz_lib.h index 886bd0cd333552..f645825b305321 100644 --- a/rlz/lib/rlz_lib.h +++ b/rlz/lib/rlz_lib.h @@ -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. diff --git a/rlz/lib/rlz_lib_test.cc b/rlz/lib/rlz_lib_test.cc index 5de04ff901e82a..3febb0e5286bd5 100644 --- a/rlz/lib/rlz_lib_test.cc +++ b/rlz/lib/rlz_lib_test.cc @@ -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 + test_shared_url_loader_factory = + base::MakeRefCounted( + &test_url_loader_factory); - base::Thread io_thread("rlz_unittest_io_thread"); - ASSERT_TRUE(io_thread.StartWithOptions(options)); - - scoped_refptr 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 context = - new net::TestURLRequestContextGetter(io_thread.task_runner()); - URLRequestRAII set_context(context.get()); + network::TestURLLoaderFactory test_url_loader_factory; + scoped_refptr + test_shared_url_loader_factory = + base::MakeRefCounted( + &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(); diff --git a/rlz/test/rlz_unittest_main.cc b/rlz/test/rlz_unittest_main.cc index b7a41ca9f245f3..e109ead3eedfac 100644 --- a/rlz/test/rlz_unittest_main.cc +++ b/rlz/test/rlz_unittest_main.cc @@ -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