| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef CONTENT_BROWSER_BROWSER_ASSOCIATED_INTERFACE_H_ | 5 #ifndef CONTENT_BROWSER_BROWSER_ASSOCIATED_INTERFACE_H_ |
| 6 #define CONTENT_BROWSER_BROWSER_ASSOCIATED_INTERFACE_H_ | 6 #define CONTENT_BROWSER_BROWSER_ASSOCIATED_INTERFACE_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
| 14 #include "content/public/browser/browser_message_filter.h" | 14 #include "content/public/browser/browser_message_filter.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "ipc/ipc_channel_proxy.h" | 16 #include "ipc/ipc_channel_proxy.h" |
| 17 #include "mojo/public/cpp/bindings/associated_binding.h" | 17 #include "mojo/public/cpp/bindings/associated_binding_set.h" |
| 18 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 18 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
| 19 | 19 |
| 20 namespace content { | 20 namespace content { |
| 21 | 21 |
| 22 // A helper interface which owns an associated interface binding on the IO | 22 // A helper interface which owns an associated interface binding on the IO |
| 23 // thread. Subclassess of BrowserMessageFilter may use this to simplify | 23 // thread. Subclassess of BrowserMessageFilter may use this to simplify |
| 24 // the transition to Mojo interfaces. | 24 // the transition to Mojo interfaces. |
| 25 // | 25 // |
| 26 // In general the correct pattern for using this is as follows: | 26 // In general the correct pattern for using this is as follows: |
| 27 // | 27 // |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 class InternalState : public base::RefCountedThreadSafe<InternalState> { | 69 class InternalState : public base::RefCountedThreadSafe<InternalState> { |
| 70 public: | 70 public: |
| 71 explicit InternalState(Interface* impl) : impl_(impl) {} | 71 explicit InternalState(Interface* impl) : impl_(impl) {} |
| 72 | 72 |
| 73 void Initialize() { | 73 void Initialize() { |
| 74 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 74 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 75 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 75 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 76 base::Bind(&InternalState::Initialize, this)); | 76 base::Bind(&InternalState::Initialize, this)); |
| 77 return; | 77 return; |
| 78 } | 78 } |
| 79 binding_.reset(new mojo::AssociatedBinding<Interface>(impl_)); | 79 bindings_.reset(new mojo::AssociatedBindingSet<Interface>); |
| 80 } | 80 } |
| 81 | 81 |
| 82 void ShutDown() { | 82 void ShutDown() { |
| 83 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 83 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 84 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 84 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 85 base::Bind(&InternalState::ShutDown, this)); | 85 base::Bind(&InternalState::ShutDown, this)); |
| 86 return; | 86 return; |
| 87 } | 87 } |
| 88 binding_.reset(); | 88 bindings_.reset(); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void BindRequest(mojo::ScopedInterfaceEndpointHandle handle) { | 91 void BindRequest(mojo::ScopedInterfaceEndpointHandle handle) { |
| 92 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 92 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 93 // If this interface has already been shut down or is already bound, we | 93 // If this interface has already been shut down we drop the request. |
| 94 // drop the request. | 94 if (!bindings_) |
| 95 if (!binding_ || binding_->is_bound()) | |
| 96 return; | 95 return; |
| 97 | 96 bindings_->AddBinding( |
| 98 binding_->Bind(mojo::MakeAssociatedRequest<Interface>(std::move(handle))); | 97 impl_, mojo::MakeAssociatedRequest<Interface>(std::move(handle))); |
| 99 binding_->set_connection_error_handler( | |
| 100 base::Bind(&InternalState::ShutDown, base::Unretained(this))); | |
| 101 } | 98 } |
| 102 | 99 |
| 103 private: | 100 private: |
| 104 friend class base::RefCountedThreadSafe<InternalState>; | 101 friend class base::RefCountedThreadSafe<InternalState>; |
| 105 | 102 |
| 106 ~InternalState() {} | 103 ~InternalState() {} |
| 107 | 104 |
| 108 Interface* impl_; | 105 Interface* impl_; |
| 109 std::unique_ptr<mojo::AssociatedBinding<Interface>> binding_; | 106 std::unique_ptr<mojo::AssociatedBindingSet<Interface>> bindings_; |
| 110 | 107 |
| 111 DISALLOW_COPY_AND_ASSIGN(InternalState); | 108 DISALLOW_COPY_AND_ASSIGN(InternalState); |
| 112 }; | 109 }; |
| 113 | 110 |
| 114 scoped_refptr<InternalState> internal_state_; | 111 scoped_refptr<InternalState> internal_state_; |
| 115 | 112 |
| 116 DISALLOW_COPY_AND_ASSIGN(BrowserAssociatedInterface); | 113 DISALLOW_COPY_AND_ASSIGN(BrowserAssociatedInterface); |
| 117 }; | 114 }; |
| 118 | 115 |
| 119 }; | 116 }; |
| 120 | 117 |
| 121 #endif // CONTENT_BROWSER_BROWSER_ASSOCIATED_INTERFACE_H_ | 118 #endif // CONTENT_BROWSER_BROWSER_ASSOCIATED_INTERFACE_H_ |
| OLD | NEW |