0%

[M132][Part_1] chromium codelab

Prerequisite: Getting the Code

Part 1: Using command-line arguments

main函数开头需要加上下面的代码设置下日志的输出方式

1
2
3
4
logging::LoggingSettings settings;
settings.logging_dest = logging::LOG_TO_ALL; // 默认就是控制台
settings.log_file_path = FILE_PATH_LITERAL("hello_world.log");
logging::InitLogging(settings);

Part 2: Callbacks and Bind

是个move-only对象

1
2
3
4
5
6
// The type of a callback that:
// - Can run only once.
// - Is move-only and non-copyable.
// - Takes no arguments and does not return anything.
// base::OnceClosure is an alias of this type.
base::OnceCallback<void()>

执行的时候要加std::move

1
2
3
4
5
6
7
8
void MyFunction1(base::OnceCallback<int(std::string, double)> my_callback) {
// OnceCallback
int result1 = std::move(my_callback).Run("my string 1", 1.0);

// After running a OnceCallback, it's consumed and nulled out.
DCHECK(!my_callback);
...
}

Part 3: Threads and task runners

Threading and Tasks in Chrome

There are a number of ways to post tasks to a thread pool or task runner.

  • PostTask()
  • PostDelayedTask() if you want to add a delay.
  • PostTaskAndReply() lets you post a task which will post a task back to your current thread when its done.
  • PostTaskAndReplyWithResult() to automatically pass the return value of the first call as argument to the second call.

参考链接

  1. C++ in Chromium 101 - Codelab
  2. GN Language and Operation