Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(431)

Side by Side Diff: mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc

Issue 2326913003: Privatize StrongBinding lifetime management (Closed)
Patch Set: . Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdint.h> 5 #include <stdint.h>
6 #include <utility> 6 #include <utility>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 ptr.set_connection_error_handler(run_loop3.QuitClosure()); 474 ptr.set_connection_error_handler(run_loop3.QuitClosure());
475 ptr->SetInteger(789, sample::Enum::VALUE); 475 ptr->SetInteger(789, sample::Enum::VALUE);
476 run_loop3.Run(); 476 run_loop3.Run();
477 EXPECT_TRUE(ptr.encountered_error()); 477 EXPECT_TRUE(ptr.encountered_error());
478 // The call to SetInteger() after RequireVersion(4u) is ignored. 478 // The call to SetInteger() after RequireVersion(4u) is ignored.
479 EXPECT_EQ(456, impl.integer()); 479 EXPECT_EQ(456, impl.integer());
480 } 480 }
481 481
482 class StrongMathCalculatorImpl : public math::Calculator { 482 class StrongMathCalculatorImpl : public math::Calculator {
483 public: 483 public:
484 StrongMathCalculatorImpl(ScopedMessagePipeHandle handle, 484 StrongMathCalculatorImpl(bool* destroyed) : destroyed_(destroyed) {}
485 bool* error_received,
486 bool* destroyed,
487 const base::Closure& closure)
488 : error_received_(error_received),
489 destroyed_(destroyed),
490 closure_(closure),
491 binding_(this, std::move(handle)) {
492 binding_.set_connection_error_handler(
493 base::Bind(&SetFlagAndRunClosure, error_received_, closure_));
494 }
495 ~StrongMathCalculatorImpl() override { *destroyed_ = true; } 485 ~StrongMathCalculatorImpl() override { *destroyed_ = true; }
496 486
497 // math::Calculator implementation. 487 // math::Calculator implementation.
498 void Clear(const CalcCallback& callback) override { callback.Run(total_); } 488 void Clear(const CalcCallback& callback) override { callback.Run(total_); }
499 489
500 void Add(double value, const CalcCallback& callback) override { 490 void Add(double value, const CalcCallback& callback) override {
501 total_ += value; 491 total_ += value;
502 callback.Run(total_); 492 callback.Run(total_);
503 } 493 }
504 494
505 void Multiply(double value, const CalcCallback& callback) override { 495 void Multiply(double value, const CalcCallback& callback) override {
506 total_ *= value; 496 total_ *= value;
507 callback.Run(total_); 497 callback.Run(total_);
508 } 498 }
509 499
510 private: 500 private:
511 double total_ = 0.0; 501 double total_ = 0.0;
512 bool* error_received_;
513 bool* destroyed_; 502 bool* destroyed_;
514 base::Closure closure_;
515
516 StrongBinding<math::Calculator> binding_;
517 }; 503 };
518 504
519 TEST(StrongConnectorTest, Math) { 505 TEST(StrongConnectorTest, Math) {
520 base::MessageLoop loop; 506 base::MessageLoop loop;
521 507
522 bool error_received = false; 508 bool error_received = false;
523 bool destroyed = false; 509 bool destroyed = false;
524 MessagePipe pipe; 510 math::CalculatorPtr calc;
525 base::RunLoop run_loop; 511 base::RunLoop run_loop;
526 new StrongMathCalculatorImpl(std::move(pipe.handle0), &error_received,
527 &destroyed, run_loop.QuitClosure());
528 512
529 math::CalculatorPtr calc; 513 auto binding = MakeStrongBinding(
530 calc.Bind(InterfacePtrInfo<math::Calculator>(std::move(pipe.handle1), 0u)); 514 base::MakeUnique<StrongMathCalculatorImpl>(&destroyed), GetProxy(&calc));
515 binding->set_connection_error_handler(
516 base::Bind(&SetFlagAndRunClosure, &error_received,
517 run_loop.QuitClosure()));
531 518
532 { 519 {
533 // Suppose this is instantiated in a process that has the other end of the 520 // Suppose this is instantiated in a process that has the other end of the
534 // message pipe. 521 // message pipe.
535 MathCalculatorUI calculator_ui(std::move(calc)); 522 MathCalculatorUI calculator_ui(std::move(calc));
536 523
537 base::RunLoop run_loop, run_loop2; 524 base::RunLoop run_loop, run_loop2;
538 calculator_ui.Add(2.0, run_loop.QuitClosure()); 525 calculator_ui.Add(2.0, run_loop.QuitClosure());
539 calculator_ui.Multiply(5.0, run_loop2.QuitClosure()); 526 calculator_ui.Multiply(5.0, run_loop2.QuitClosure());
540 run_loop.Run(); 527 run_loop.Run();
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 // end which will destroy the instance since it is strongly bound. 608 // end which will destroy the instance since it is strongly bound.
622 } 609 }
623 610
624 run_loop.Run(); 611 run_loop.Run();
625 EXPECT_TRUE(error_received); 612 EXPECT_TRUE(error_received);
626 EXPECT_FALSE(destroyed); 613 EXPECT_FALSE(destroyed);
627 } 614 }
628 615
629 class CImpl : public C { 616 class CImpl : public C {
630 public: 617 public:
631 CImpl(bool* d_called, InterfaceRequest<C> request, 618 CImpl(bool* d_called, const base::Closure& closure)
632 const base::Closure& closure) 619 : d_called_(d_called), closure_(closure) {}
633 : d_called_(d_called), binding_(this, std::move(request)),
634 closure_(closure) {}
635 ~CImpl() override {} 620 ~CImpl() override {}
636 621
637 private: 622 private:
638 void D() override { 623 void D() override {
639 *d_called_ = true; 624 *d_called_ = true;
640 closure_.Run(); 625 closure_.Run();
641 } 626 }
642 627
643 bool* d_called_; 628 bool* d_called_;
644 StrongBinding<C> binding_;
645 base::Closure closure_; 629 base::Closure closure_;
646 }; 630 };
647 631
648 class BImpl : public B { 632 class BImpl : public B {
649 public: 633 public:
650 BImpl(bool* d_called, InterfaceRequest<B> request, 634 BImpl(bool* d_called, const base::Closure& closure)
651 const base::Closure& closure) 635 : d_called_(d_called), closure_(closure) {}
652 : d_called_(d_called), binding_(this, std::move(request)),
653 closure_(closure) {}
654 ~BImpl() override {} 636 ~BImpl() override {}
655 637
656 private: 638 private:
657 void GetC(InterfaceRequest<C> c) override { 639 void GetC(InterfaceRequest<C> c) override {
658 new CImpl(d_called_, std::move(c), closure_); 640 MakeStrongBinding(base::MakeUnique<CImpl>(d_called_, closure_),
641 std::move(c));
659 } 642 }
660 643
661 bool* d_called_; 644 bool* d_called_;
662 StrongBinding<B> binding_;
663 base::Closure closure_; 645 base::Closure closure_;
664 }; 646 };
665 647
666 class AImpl : public A { 648 class AImpl : public A {
667 public: 649 public:
668 AImpl(InterfaceRequest<A> request, const base::Closure& closure) 650 AImpl(InterfaceRequest<A> request, const base::Closure& closure)
669 : d_called_(false), binding_(this, std::move(request)), 651 : d_called_(false), binding_(this, std::move(request)),
670 closure_(closure) {} 652 closure_(closure) {}
671 ~AImpl() override {} 653 ~AImpl() override {}
672 654
673 bool d_called() const { return d_called_; } 655 bool d_called() const { return d_called_; }
674 656
675 private: 657 private:
676 void GetB(InterfaceRequest<B> b) override { 658 void GetB(InterfaceRequest<B> b) override {
677 new BImpl(&d_called_, std::move(b), closure_); 659 MakeStrongBinding(base::MakeUnique<BImpl>(&d_called_, closure_),
660 std::move(b));
678 } 661 }
679 662
680 bool d_called_; 663 bool d_called_;
681 Binding<A> binding_; 664 Binding<A> binding_;
682 base::Closure closure_; 665 base::Closure closure_;
683 }; 666 };
684 667
685 TEST_F(InterfacePtrTest, Scoping) { 668 TEST_F(InterfacePtrTest, Scoping) {
686 APtr a; 669 APtr a;
687 base::RunLoop run_loop; 670 base::RunLoop run_loop;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
768 bool called = false; 751 bool called = false;
769 calc.set_connection_error_handler(base::Bind(&SetBool, &called)); 752 calc.set_connection_error_handler(base::Bind(&SetBool, &called));
770 calc.FlushForTesting(); 753 calc.FlushForTesting();
771 EXPECT_TRUE(called); 754 EXPECT_TRUE(called);
772 calc.FlushForTesting(); 755 calc.FlushForTesting();
773 } 756 }
774 757
775 } // namespace 758 } // namespace
776 } // namespace test 759 } // namespace test
777 } // namespace mojo 760 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698