This is general question regarding to Chrome code design.
During exploring on net module, there are a lot of callback functions got my attention. There are two kinds of basically, first it's messageloop->PostTask() introduced which is used for different thread. This is fine and perfect to achieve async result.
However, another very common coding practice in this module is hard for me to understand. I found there is a lot of callback assignment is the same thread and *EXECUTED* in the same thread. IO thread run time has bounch of examples:
here is one of them in net module/http/httpNetworkTransaction.cc
int HttpNetworkTransaction::DoSendRequest() {
send_start_time_ = base::TimeTicks::Now();
next_state_ = STATE_SEND_REQUEST_COMPLETE;
return stream_->SendRequest(request_headers_, &response_, io_callback_);
}
This _io_callback is binding to OnIOComplete in contructor:
HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
HttpNetworkSession* session)
: pending_auth_target_(HttpAuth::AUTH_NONE),
io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
They are executed in the same thread. So I feel confuse why is there a need for a callback here since it doesn't benefit execution. Is is by design, and feel neat with this way?
To me it doesn't looks like it's necessary since we know (in Chrome) it will execute in the same thread, whatever it will be blocking call...It makes code hard to understand.
Side question: seems in each class it only contains one callback function variable like callback_ to handle different registration. This is also hard to image in DoCallback function how each caller know when should call it without know the details and get meaningful results.
For example, in one class definition, there are many methods will call DoCallback(), and internally it will use callback_ to handle it. But how do I know when (write program) this callback_ is my interested one. If use clear function like instance->method which is previously binded to callback_, then it's more clear to developer. There must be very concrete reason for this, I guess. BTW, what's the design pattern name here?
Thanks for any input!
All smooth...