How to get callback with mojo?

48 Views Asked by At

I write a new function in web_cache_manager.cc in chromium, and use mojo to communicate with web_cache_impl.cc, and give a callback, but I cannot get anything back. Here is the detail:

  1. First, I get a render_process_id, get WebCacheManger's Instance and call the function which I create.
// src/cef/libcef/browser/browser_host_base.cc
void CefBrowserHostBase::FunctionA() {
   int render_process_id = GetWebContents()->GetMainFrame()->GetProcess()->GetID();
   auto manager = web_cache::WebCacheManager::GetInstance();
   manager->FuntionB(render_process_id);
}
  1. Then I use Add function in web_cache_manager.cc and render_process_id which I get from browser_host_base.cc to create pipe with mojo.
  2. Add function will save remote into a map which key is render_process_id, so after call Add, I get it from the map.
  3. After that I call the mojo interface with callback
// src/components/web_cache/browser/web_cache_manager.cc
void WebCacheManager::FunctionB(int render_process_id) {
    Add(render_process_id);
    auto it = web_cache_services_.find(render_process_id);
    if (it != web_cache_services_.end()) {
        const mojo::Remote<mojom::WebCache>& service = it->second;
        service->FunctionC(base::BindOnce(&WebCacheManager::GetCallback, base::Unretained(this)));
    }
}

void WebCacheManager::Callback(bool result) {
    // get the result;
}

// src/components/web_cache/public/mojom/web_cache.mojom
interface WebCache {
    FunctionC() => (bool result);
}

// src/components/web_cache/renderer/web_cache_impl.cc
void WebCacheImpl::FunctionC(FunctionCCallback callback) {
    bool ret = SomeFunction();
    std::move(callback).Run(ret);
}

I'm confused why it doesn't work. Am I write something wrong or forget what to add ? Please help me.

I expect the callback can work and give the result for browser.

0

There are 0 best solutions below