OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 CHROME_BROWSER_MULTI_PROCESS_NOTIFICATION_H_ |
| 6 #define CHROME_BROWSER_MULTI_PROCESS_NOTIFICATION_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/compiler_specific.h" |
| 13 #include "base/scoped_ptr.h" |
| 14 #include "base/task.h" |
| 15 |
| 16 class Task; |
| 17 class MessageLoop; |
| 18 |
| 19 // Platform abstraction for a notification that can be sent between processes. |
| 20 // Notifications are strings. The string will be prefixed accordingly per |
| 21 // platform (so on Mac OS X a "Happy" notification will become |
| 22 // "org.chromium.Happy"). |
| 23 namespace multi_process_notification { |
| 24 |
| 25 class ListenerImpl; |
| 26 |
| 27 enum Domain { |
| 28 // Notifications intended to be received by processes running with the |
| 29 // same uid and same profile. |
| 30 ProfileDomain, |
| 31 // Notifications intended to be received by processes running with the |
| 32 // same uid. |
| 33 UserDomain, |
| 34 // Notifications intended to be received by processes running on the |
| 35 // same system. |
| 36 SystemDomain |
| 37 }; |
| 38 |
| 39 // Posts a notification |name| to |domain|. |
| 40 // Returns true if the notification was posted. |
| 41 bool Post(const std::string& name, Domain domain); |
| 42 |
| 43 // A notification listener. Will listen for a given notification and |
| 44 // call the delegate. Note that the delegate is not owned by the listener. |
| 45 class Listener { |
| 46 public: |
| 47 class Delegate { |
| 48 public: |
| 49 virtual ~Delegate(); |
| 50 |
| 51 // OnNotificationReceived is called on the thread that called Start() on the |
| 52 // Listener associated with this delegate. |
| 53 virtual void OnNotificationReceived(const std::string& name, |
| 54 Domain domain) = 0; |
| 55 |
| 56 // OnListenerStarted is called on the thread that called Start() on the |
| 57 // Listener associated with this delegate. If success is false, there |
| 58 // was an error starting the listener, and it is invalid. |
| 59 virtual void OnListenerStarted( |
| 60 const std::string& name, Domain domain, bool success); |
| 61 }; |
| 62 |
| 63 class ListenerStartedTask : public Task { |
| 64 public: |
| 65 ListenerStartedTask(const std::string& name, Domain domain, |
| 66 Delegate* delegate, bool success); |
| 67 virtual ~ListenerStartedTask(); |
| 68 virtual void Run(); |
| 69 |
| 70 private: |
| 71 std::string name_; |
| 72 Domain domain_; |
| 73 Delegate* delegate_; |
| 74 bool success_; |
| 75 DISALLOW_COPY_AND_ASSIGN(ListenerStartedTask); |
| 76 }; |
| 77 |
| 78 class NotificationReceivedTask : public Task { |
| 79 public: |
| 80 NotificationReceivedTask( |
| 81 const std::string& name, Domain domain, Delegate* delegate); |
| 82 virtual ~NotificationReceivedTask(); |
| 83 virtual void Run(); |
| 84 |
| 85 private: |
| 86 std::string name_; |
| 87 Domain domain_; |
| 88 Delegate* delegate_; |
| 89 DISALLOW_COPY_AND_ASSIGN(NotificationReceivedTask); |
| 90 }; |
| 91 |
| 92 Listener(const std::string& name, Domain domain, Delegate* delegate); |
| 93 |
| 94 // A destructor is required for scoped_ptr to compile. |
| 95 ~Listener(); |
| 96 |
| 97 // A listener is not considered valid until Start() returns true and its |
| 98 // delegate's OnListenerStarted method is called with |success| == true |
| 99 bool Start(); |
| 100 |
| 101 private: |
| 102 scoped_ptr<ListenerImpl> impl_; |
| 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(Listener); |
| 105 }; |
| 106 |
| 107 } // namespace multi_process_notification |
| 108 |
| 109 #endif // CHROME_BROWSER_MULTI_PROCESS_NOTIFICATION_H_ |
OLD | NEW |