Skip to content

Commit

Permalink
Fix callback definition/usage in C++ bindings doc.
Browse files Browse the repository at this point in the history
Mojo generated code now uses move-only base::OnceCallback
values, instead of const references to base::Callback, so
update the example code to reflect this.

Also add a small note explaining that a dependency needs to
be added from one of the top-level BUILD.gn groups to the new
interface GN target to ensure the compilation instructions
work correctly (otherwise, ninja complains that it doesn't
know about the target).

BUG=None
R=yzshen@chromium.org,sky@chromium.org

Change-Id: I2289af199da7da2bf8ed3ac0bd2f715d4cf8fbf0
Reviewed-on: https://chromium-review.googlesource.com/612387
Commit-Queue: David Turner <digit@chromium.org>
Reviewed-by: Yuzhu Shen <yzshen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#512161}
  • Loading branch information
digit-android authored and Commit Bot committed Oct 27, 2017
1 parent b21b82d commit 743836f
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions mojo/public/cpp/bindings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ mojom("interfaces") {
}
```

Ensure that any target that needs this interface depends on it, e.g. with a line like:

```
deps += [ '//services/db/public/interfaces' ]
```

If we then build this target:

```
Expand Down Expand Up @@ -305,9 +311,9 @@ class Logger {

virtual void Log(const std::string& message) = 0;

using GetTailCallback = base::Callback<void(const std::string& message)>;
using GetTailCallback = base::OnceCallback<void(const std::string& message)>;

virtual void GetTail(const GetTailCallback& callback) = 0;
virtual void GetTail(GetTailCallback callback) = 0;
}

} // namespace mojom
Expand Down Expand Up @@ -335,8 +341,8 @@ class LoggerImpl : public sample::mojom::Logger {
lines_.push_back(message);
}
void GetTail(const GetTailCallback& callback) override {
callback.Run(lines_.back());
void GetTail(GetTailCallback callback) override {
std::move(callback).Run(lines_.back());
}
private:
Expand All @@ -354,7 +360,7 @@ void OnGetTail(const std::string& message) {
LOG(ERROR) << "Tail was: " << message;
}

logger->GetTail(base::Bind(&OnGetTail));
logger->GetTail(base::BindOnce(&OnGetTail));
```
Behind the scenes, the implementation-side callback is actually serializing the
Expand Down

0 comments on commit 743836f

Please sign in to comment.