• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_
7 
8 #include <memory>
9 #include <string>
10 #include <utility>
11 
12 #include "base/bind.h"
13 #include "base/callback.h"
14 #include "base/logging.h"
15 #include "base/macros.h"
16 #include "base/memory/ptr_util.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/single_thread_task_runner.h"
19 #include "mojo/public/cpp/bindings/bindings_export.h"
20 #include "mojo/public/cpp/bindings/connection_error_callback.h"
21 #include "mojo/public/cpp/bindings/filter_chain.h"
22 #include "mojo/public/cpp/bindings/interface_endpoint_client.h"
23 #include "mojo/public/cpp/bindings/interface_id.h"
24 #include "mojo/public/cpp/bindings/interface_ptr.h"
25 #include "mojo/public/cpp/bindings/interface_ptr_info.h"
26 #include "mojo/public/cpp/bindings/interface_request.h"
27 #include "mojo/public/cpp/bindings/lib/multiplex_router.h"
28 #include "mojo/public/cpp/bindings/message_header_validator.h"
29 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
30 #include "mojo/public/cpp/system/core.h"
31 
32 namespace mojo {
33 namespace internal {
34 
35 class MOJO_CPP_BINDINGS_EXPORT BindingStateBase {
36  public:
37   BindingStateBase();
38   ~BindingStateBase();
39 
40   void AddFilter(std::unique_ptr<MessageReceiver> filter);
41 
42   bool HasAssociatedInterfaces() const;
43 
44   void PauseIncomingMethodCallProcessing();
45   void ResumeIncomingMethodCallProcessing();
46 
47   bool WaitForIncomingMethodCall(
48       MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE);
49 
50   void Close();
51   void CloseWithReason(uint32_t custom_reason, const std::string& description);
52 
set_connection_error_handler(const base::Closure & error_handler)53   void set_connection_error_handler(const base::Closure& error_handler) {
54     DCHECK(is_bound());
55     endpoint_client_->set_connection_error_handler(error_handler);
56   }
57 
set_connection_error_with_reason_handler(const ConnectionErrorWithReasonCallback & error_handler)58   void set_connection_error_with_reason_handler(
59       const ConnectionErrorWithReasonCallback& error_handler) {
60     DCHECK(is_bound());
61     endpoint_client_->set_connection_error_with_reason_handler(error_handler);
62   }
63 
is_bound()64   bool is_bound() const { return !!router_; }
65 
handle()66   MessagePipeHandle handle() const {
67     DCHECK(is_bound());
68     return router_->handle();
69   }
70 
71   void FlushForTesting();
72 
73   void EnableTestingMode();
74 
75  protected:
76   void BindInternal(ScopedMessagePipeHandle handle,
77                     scoped_refptr<base::SingleThreadTaskRunner> runner,
78                     const char* interface_name,
79                     std::unique_ptr<MessageReceiver> request_validator,
80                     bool passes_associated_kinds,
81                     bool has_sync_methods,
82                     MessageReceiverWithResponderStatus* stub,
83                     uint32_t interface_version);
84 
85   scoped_refptr<internal::MultiplexRouter> router_;
86   std::unique_ptr<InterfaceEndpointClient> endpoint_client_;
87 };
88 
89 template <typename Interface, typename ImplRefTraits>
90 class BindingState : public BindingStateBase {
91  public:
92   using ImplPointerType = typename ImplRefTraits::PointerType;
93 
BindingState(ImplPointerType impl)94   explicit BindingState(ImplPointerType impl) {
95     stub_.set_sink(std::move(impl));
96   }
97 
~BindingState()98   ~BindingState() { Close(); }
99 
Bind(ScopedMessagePipeHandle handle,scoped_refptr<base::SingleThreadTaskRunner> runner)100   void Bind(ScopedMessagePipeHandle handle,
101             scoped_refptr<base::SingleThreadTaskRunner> runner) {
102     BindingStateBase::BindInternal(
103         std::move(handle), runner, Interface::Name_,
104         base::MakeUnique<typename Interface::RequestValidator_>(),
105         Interface::PassesAssociatedKinds_, Interface::HasSyncMethods_, &stub_,
106         Interface::Version_);
107   }
108 
Unbind()109   InterfaceRequest<Interface> Unbind() {
110     endpoint_client_.reset();
111     InterfaceRequest<Interface> request =
112         MakeRequest<Interface>(router_->PassMessagePipe());
113     router_ = nullptr;
114     return request;
115   }
116 
impl()117   Interface* impl() { return ImplRefTraits::GetRawPointer(&stub_.sink()); }
118 
119  private:
120   typename Interface::template Stub_<ImplRefTraits> stub_;
121 
122   DISALLOW_COPY_AND_ASSIGN(BindingState);
123 };
124 
125 }  // namesapce internal
126 }  // namespace mojo
127 
128 #endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_
129