Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
find_package(Threads REQUIRED)
# GLOG
find_package(glog CONFIG REQUIRED)
# curl
find_package(CURL CONFIG REQUIRED)


#
Expand Down
59 changes: 58 additions & 1 deletion examples/simple/main.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <thread>

#include "base/bind.h"
#include "base/bind_post_task.h"
#include "base/callback.h"
#include "base/init.h"
#include "base/logging.h"
#include "base/net/simple_url_loader.h"
#include "base/synchronization/auto_signaller.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
Expand Down Expand Up @@ -138,8 +140,61 @@ void ThreadPoolSingleThreadExample() {
pool.Stop();
}

void NetExample() {
base::Thread thread{};
thread.Start();

base::WaitableEvent finished_event{};
auto response_callback = base::BindOnce(
[](base::WaitableEvent* event, base::net::ResourceResponse response) {
LOG(INFO) << "Result: " << static_cast<int>(response.result);
LOG(INFO) << "HTTP code: " << response.code;
LOG(INFO) << "Final URL: " << response.final_url;
LOG(INFO) << "Downloaded " << response.data.size() << " bytes";
LOG(INFO) << "Latency: " << response.timing_connect.InMilliseconds()
<< "ms";
LOG(INFO) << "Headers";
for (const auto& [h, v] : response.headers) {
LOG(INFO) << " " << h << ": " << v;
}
LOG(INFO) << "Content:\n"
<< std::string{response.data.begin(), response.data.end()};
event->Signal();
},
&finished_event);

// Try to download and signal on finish
base::net::SimpleUrlLoader::DownloadUnbounded(
base::net::ResourceRequest{
"https://www.google.com/robots.txt",
base::net::kDefaultHeaders,
base::net::kNoTimeout,
base::net::kNoTimeout,
true,
true,
},
base::BindPostTask(thread.TaskRunner(), std::move(response_callback),
FROM_HERE));

// Timeout = 5s
thread.TaskRunner()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](base::WaitableEvent* event) {
LOG(WARNING) << "Failed to download in 5 seconds";
event->Signal();
},
&finished_event),
base::Seconds(5));

finished_event.Wait();
}

int main(int argc, char* argv[]) {
base::Initialize(argc, argv);
base::InitOptions init_options;
init_options.InitializeNetworking = true;

base::Initialize(argc, argv, std::move(init_options));

const auto timer = base::ElapsedTimer{};

Expand All @@ -149,6 +204,8 @@ int main(int argc, char* argv[]) {
ThreadPoolSequencedExample();
ThreadPoolSingleThreadExample();

NetExample();

LOG(INFO) << "Example finished in " << timer.Elapsed().InMillisecondsF()
<< "ms";

Expand Down
38 changes: 24 additions & 14 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,77 +32,87 @@ target_include_directories(libbase PUBLIC
target_link_libraries(libbase PUBLIC
${LIBBASE_LINK_FLAGS}
Threads::Threads
glog::glog)
glog::glog
CURL::libcurl)

target_sources(libbase
PRIVATE
base/auto_reset.h
base/barrier_callback.h
base/barrier_closure.cc
base/barrier_closure.h
base/bind.h
base/bind_internals.h
base/bind_post_task.h
base/callback.h
base/bind.h
base/callback_forward.h
base/callback_helpers.cc
base/callback_helpers.h
base/callback_iface.h
base/callback_internals.h
base/callback.h
base/init.cc
base/init.h
base/logging.cc
base/logging.h
base/memory/weak_ptr.h
base/message_loop/message_loop.h
base/message_loop/message_loop_impl.cc
base/message_loop/message_loop_impl.h
base/message_loop/message_pump.h
base/message_loop/message_loop.h
base/message_loop/message_pump_impl.cc
base/message_loop/message_pump_impl.h
base/message_loop/message_pump.h
base/net/impl/net_thread_impl.cc
base/net/impl/net_thread_impl.h
base/net/impl/net_thread.cc
base/net/impl/net_thread.h
base/net/resource_request.h
base/net/resource_response.h
base/net/result.h
base/net/simple_url_loader.cc
base/net/simple_url_loader.h
base/sequence_checker.cc
base/sequence_checker.h
base/sequence_id.cc
base/sequence_id.h
base/sequenced_task_runner.h
base/sequenced_task_runner_helpers.cc
base/sequenced_task_runner_helpers.h
base/sequenced_task_runner_internals.h
base/sequenced_task_runner.h
base/single_thread_task_runner.h
base/source_location.h
base/synchronization/auto_signaller.cc
base/synchronization/auto_signaller.h
base/synchronization/waitable_event.cc
base/synchronization/waitable_event.h
base/task_runner_internals.h
base/task_runner.cc
base/task_runner.h
base/task_runner_internals.h
base/threading/delayed_task_manager.cc
base/threading/delayed_task_manager.h
base/threading/delayed_task_manager_shared_instance.cc
base/threading/delayed_task_manager_shared_instance.h
base/threading/delayed_task_manager.cc
base/threading/delayed_task_manager.h
base/threading/sequenced_task_runner_handle.cc
base/threading/sequenced_task_runner_handle.h
base/threading/task_runner_impl.cc
base/threading/task_runner_impl.h
base/threading/thread.cc
base/threading/thread.h
base/threading/thread_pool.cc
base/threading/thread_pool.h
base/time/time.cc
base/time/time.h
base/threading/thread.cc
base/threading/thread.h
base/time/time_delta.cc
base/time/time_delta.h
base/time/time_ticks.cc
base/time/time_ticks.h
base/time/time.cc
base/time/time.h
base/timer/elapsed_timer.cc
base/timer/elapsed_timer.h
base/trace_event/trace_argument_packer.h
base/trace_event/trace_async.h
base/trace_event/trace_complete.h
base/trace_event/trace_counter.h
base/trace_event/trace_event.h
base/trace_event/trace_event_register.h
base/trace_event/trace_event.h
base/trace_event/trace_events.cc
base/trace_event/trace_events.h
base/trace_event/trace_flow.h
Expand Down
32 changes: 28 additions & 4 deletions src/base/init.cc
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
#include "base/init.h"

#include "base/logging.h"
#include "base/net/impl/net_thread.h"

namespace base {

namespace {
InitOptions g_init_options;
}

// NOLINTNEXTLINE(modernize-avoid-c-arrays)
void Initialize(int /*argc*/, char* argv[]) {
FLAGS_logtostderr = true;
void Initialize(int /*argc*/, char* argv[], InitOptions options) {
g_init_options = options;

Check warning on line 14 in src/base/init.cc

View check run for this annotation

Codecov / codecov/patch

src/base/init.cc#L13-L14

Added lines #L13 - L14 were not covered by tests

FLAGS_logtostderr = g_init_options.LogToStderr;

Check warning on line 16 in src/base/init.cc

View check run for this annotation

Codecov / codecov/patch

src/base/init.cc#L16

Added line #L16 was not covered by tests

google::InitGoogleLogging(argv[0]);
google::InstallPrefixFormatter(&detail::LogFormatter);
google::InstallFailureSignalHandler();

if (g_init_options.InitializeNetworking) {
// Initialize Networking thread
net::NetThread::GetInstance().Start();

Check warning on line 24 in src/base/init.cc

View check run for this annotation

Codecov / codecov/patch

src/base/init.cc#L24

Added line #L24 was not covered by tests
}
}

// NOLINTNEXTLINE(modernize-avoid-c-arrays)
void InitializeForTests(int /*argc*/, char* argv[]) {
FLAGS_logtostderr = true;
void InitializeForTests(int /*argc*/, char* argv[], InitOptions options) {
g_init_options = options;

FLAGS_logtostderr = g_init_options.LogToStderr;

google::InitGoogleLogging(argv[0]);
google::InstallPrefixFormatter(&detail::LogFormatter);

if (g_init_options.InitializeNetworking) {
// Initialize Networking thread
net::NetThread::GetInstance().Start();

Check warning on line 39 in src/base/init.cc

View check run for this annotation

Codecov / codecov/patch

src/base/init.cc#L39

Added line #L39 was not covered by tests
}
}

void Deinitialize() {
if (g_init_options.InitializeNetworking) {
// Deinitialize Networking thread
net::NetThread::GetInstance().Stop();

Check warning on line 46 in src/base/init.cc

View check run for this annotation

Codecov / codecov/patch

src/base/init.cc#L46

Added line #L46 was not covered by tests
}

google::ShutdownGoogleLogging();
}

Expand Down
12 changes: 10 additions & 2 deletions src/base/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

namespace base {

struct InitOptions {
// Logging
bool LogToStderr = true;

// Networking
bool InitializeNetworking = false;
};

// NOLINTNEXTLINE(modernize-avoid-c-arrays)
void Initialize(int argc, char* argv[]);
void Initialize(int argc, char* argv[], InitOptions options);
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
void InitializeForTests(int argc, char* argv[]);
void InitializeForTests(int argc, char* argv[], InitOptions options);
void Deinitialize();

} // namespace base
40 changes: 40 additions & 0 deletions src/base/net/impl/net_thread.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "base/net/impl/net_thread.h"

#include "base/logging.h"
#include "base/net/impl/net_thread_impl.h"

namespace base {
namespace net {

// static
NetThread& NetThread::GetInstance() {
static NetThread instance;
return instance;

Check warning on line 12 in src/base/net/impl/net_thread.cc

View check run for this annotation

Codecov / codecov/patch

src/base/net/impl/net_thread.cc#L10-L12

Added lines #L10 - L12 were not covered by tests
}

void NetThread::Start() {
DCHECK(!impl_);

Check warning on line 16 in src/base/net/impl/net_thread.cc

View check run for this annotation

Codecov / codecov/patch

src/base/net/impl/net_thread.cc#L15-L16

Added lines #L15 - L16 were not covered by tests

impl_ = std::make_unique<NetThreadImpl>();
impl_->Start();
}

Check warning on line 20 in src/base/net/impl/net_thread.cc

View check run for this annotation

Codecov / codecov/patch

src/base/net/impl/net_thread.cc#L18-L20

Added lines #L18 - L20 were not covered by tests

void NetThread::Stop() {
DCHECK(impl_);

Check warning on line 23 in src/base/net/impl/net_thread.cc

View check run for this annotation

Codecov / codecov/patch

src/base/net/impl/net_thread.cc#L22-L23

Added lines #L22 - L23 were not covered by tests

impl_->Stop();
impl_.reset();
}

Check warning on line 27 in src/base/net/impl/net_thread.cc

View check run for this annotation

Codecov / codecov/patch

src/base/net/impl/net_thread.cc#L25-L27

Added lines #L25 - L27 were not covered by tests

void NetThread::EnqueueDownload(

Check warning on line 29 in src/base/net/impl/net_thread.cc

View check run for this annotation

Codecov / codecov/patch

src/base/net/impl/net_thread.cc#L29

Added line #L29 was not covered by tests
ResourceRequest request,
std::optional<size_t> max_response_size_bytes,
OnceCallback<void(ResourceResponse)> on_done_callback) {
DCHECK(impl_);

Check warning on line 33 in src/base/net/impl/net_thread.cc

View check run for this annotation

Codecov / codecov/patch

src/base/net/impl/net_thread.cc#L33

Added line #L33 was not covered by tests

impl_->EnqueueDownload(std::move(request), std::move(max_response_size_bytes),
std::move(on_done_callback));
}

Check warning on line 37 in src/base/net/impl/net_thread.cc

View check run for this annotation

Codecov / codecov/patch

src/base/net/impl/net_thread.cc#L35-L37

Added lines #L35 - L37 were not covered by tests

} // namespace net
} // namespace base
31 changes: 31 additions & 0 deletions src/base/net/impl/net_thread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <memory>
#include <optional>

#include "base/callback.h"
#include "base/net/resource_request.h"
#include "base/net/resource_response.h"

namespace base {
namespace net {

class NetThread {
public:
static NetThread& GetInstance();

void Start();
void Stop();

void EnqueueDownload(ResourceRequest request,
std::optional<size_t> max_response_size_bytes,
OnceCallback<void(ResourceResponse)> on_done_callback);

private:
class NetThreadImpl;

std::unique_ptr<NetThreadImpl> impl_;
};

} // namespace net
} // namespace base
Loading