DEV Community

djmitche
djmitche

Posted on • Updated on

Chromium Spelunking: Creating a Request

In the last post, I built an executable which created a URLRequestContext and then exited. Not very exciting, but it was a bit of work! In this post, I'll be using that instance to create an actual URLRequest.

URLRequestContext::CreateRequest

A skim of url_request_context.h shows a helpful-looking function, CreateRequest.

This takes a few straightforward arguments plus a NetworkTrafficAnnotationTag, and the comments focus on that type. Unfortunately, network_traffic_annotation.h doesn't contain a lot of comments that might help me understand what this is and what it does. Running codehistory on the file doesn't show much, either. From what I can guess, this is some kind of annotation that can be found in the source code and reviewed or audited. Anyway, in one of the commits that modified this file I find net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS). That didn't work immediately, but I see that the MutableNetworkTrafficAnnotationTag has an explicit cast operator to NetworkTrafficAnnotationTag, which does.

The types work out, and the compile succeeds, but it seems I've still not squared away the task runners:

[0509/212941.923727:FATAL:url_request.cc(597)] Check failed: base::SingleThreadTaskRunner::HasCurrentDefault(). 
Enter fullscreen mode Exit fullscreen mode

I thought I had handled this in the last post, so this was a bit disappointing. One of things about Chromium that I'm adjusting to is that a lot of the information is stored in people, and not as data. So, I asked a few people via chat, and their responses improved my understanding of things a little. One suggestion was to look at other one-off executables like net_watcher, or to use base::test::TaskEnvironment. But I wanted to learn what was going on, rather than just getting things done. With a little more digging, I determined that SingleThreadTaskRunner is a subclass of SequentialTaskRunner that is capable of running tasks on the same thread, if they have some reason to be tied together like that. This is contrary to some other suggestions that SingleThreadTaskRunner is only for testing!

Anyway, switching

-  auto thread_runner = base::ThreadPool::CreateSequentialTaskRunner(
+  auto thread_runner = base::ThreadPool::CreateSingleThreadTaskRunner(
       {base::TaskPriority::USER_VISIBLE});
Enter fullscreen mode Exit fullscreen mode

fixed this issue.

Next Up

This was a pretty short post, mostly because I was only able to devote a few minutes at a time to this work. Next up, I will call the Start method on the URLRequest, which will require a URLRequest::Delegate. Once the header is read, I'll call Read to read the response body. The end is in sight!

Top comments (0)