OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/device_orientation/dispatcher_host.h" | 5 #include "chrome/browser/device_orientation/dispatcher_host.h" |
6 | 6 |
7 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
8 #include "chrome/browser/chrome_thread.h" | 8 #include "chrome/browser/chrome_thread.h" |
9 #include "chrome/browser/device_orientation/orientation.h" | 9 #include "chrome/browser/device_orientation/orientation.h" |
10 #include "chrome/browser/device_orientation/provider.h" | 10 #include "chrome/browser/device_orientation/provider.h" |
11 #include "chrome/browser/renderer_host/render_view_host.h" | 11 #include "chrome/browser/renderer_host/render_view_host.h" |
12 #include "chrome/browser/renderer_host/render_view_host_notification_task.h" | 12 #include "chrome/browser/renderer_host/render_view_host_notification_task.h" |
13 #include "chrome/common/chrome_switches.h" | |
14 #include "chrome/common/render_messages.h" | 13 #include "chrome/common/render_messages.h" |
15 #include "ipc/ipc_message.h" | 14 #include "ipc/ipc_message.h" |
16 | 15 |
17 namespace device_orientation { | 16 namespace device_orientation { |
18 | 17 |
19 DispatcherHost::DispatcherHost(int process_id) | 18 DispatcherHost::DispatcherHost(int process_id) |
20 : process_id_(process_id) { | 19 : process_id_(process_id), |
| 20 observers_map_(), |
| 21 provider_(NULL) { |
21 } | 22 } |
22 | 23 |
23 DispatcherHost::~DispatcherHost() { | 24 DispatcherHost::~DispatcherHost() { |
24 if (provider_) | 25 } |
25 provider_->RemoveObserver(this); | 26 |
| 27 class DispatcherHost::ObserverDelegate |
| 28 : public base::RefCounted<ObserverDelegate>, public Provider::Observer { |
| 29 public: |
| 30 // Create ObserverDelegate that observes provider and forwards updates to |
| 31 // render_view_id in process_id. |
| 32 // Will stop observing provider when destructed. |
| 33 ObserverDelegate(Provider* provider, |
| 34 int process_id, |
| 35 int render_view_id); |
| 36 |
| 37 // From Provider::Observer. |
| 38 virtual void OnOrientationUpdate(const Orientation& orientation); |
| 39 |
| 40 private: |
| 41 friend class base::RefCounted<ObserverDelegate>; |
| 42 virtual ~ObserverDelegate(); |
| 43 |
| 44 scoped_refptr<Provider> provider_; |
| 45 int process_id_; |
| 46 int render_view_id_; |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(ObserverDelegate); |
| 49 }; |
| 50 |
| 51 DispatcherHost::ObserverDelegate::ObserverDelegate(Provider* provider, |
| 52 int process_id, |
| 53 int render_view_id) |
| 54 : provider_(provider), |
| 55 process_id_(process_id), |
| 56 render_view_id_(render_view_id) { |
| 57 provider_->AddObserver(this); |
| 58 } |
| 59 |
| 60 DispatcherHost::ObserverDelegate::~ObserverDelegate() { |
| 61 provider_->RemoveObserver(this); |
| 62 } |
| 63 |
| 64 void DispatcherHost::ObserverDelegate::OnOrientationUpdate( |
| 65 const Orientation& orientation) { |
| 66 ViewMsg_DeviceOrientationUpdated_Params params; |
| 67 params.can_provide_alpha = orientation.can_provide_alpha_; |
| 68 params.alpha = orientation.alpha_; |
| 69 params.can_provide_beta = orientation.can_provide_beta_; |
| 70 params.beta = orientation.beta_; |
| 71 params.can_provide_gamma = orientation.can_provide_gamma_; |
| 72 params.gamma = orientation.gamma_; |
| 73 |
| 74 IPC::Message* message = new ViewMsg_DeviceOrientationUpdated(render_view_id_, |
| 75 params); |
| 76 CallRenderViewHost(process_id_, render_view_id_, &RenderViewHost::Send, |
| 77 message); |
26 } | 78 } |
27 | 79 |
28 bool DispatcherHost::OnMessageReceived(const IPC::Message& msg, | 80 bool DispatcherHost::OnMessageReceived(const IPC::Message& msg, |
29 bool* msg_was_ok) { | 81 bool* msg_was_ok) { |
30 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | 82 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
31 bool handled = true; | 83 bool handled = true; |
32 IPC_BEGIN_MESSAGE_MAP_EX(DispatcherHost, msg, *msg_was_ok) | 84 IPC_BEGIN_MESSAGE_MAP_EX(DispatcherHost, msg, *msg_was_ok) |
33 IPC_MESSAGE_HANDLER(ViewHostMsg_DeviceOrientation_StartUpdating, | 85 IPC_MESSAGE_HANDLER(ViewHostMsg_DeviceOrientation_StartUpdating, |
34 OnStartUpdating) | 86 OnStartUpdating) |
35 IPC_MESSAGE_HANDLER(ViewHostMsg_DeviceOrientation_StopUpdating, | 87 IPC_MESSAGE_HANDLER(ViewHostMsg_DeviceOrientation_StopUpdating, |
36 OnStopUpdating) | 88 OnStopUpdating) |
37 IPC_MESSAGE_UNHANDLED(handled = false) | 89 IPC_MESSAGE_UNHANDLED(handled = false) |
38 IPC_END_MESSAGE_MAP() | 90 IPC_END_MESSAGE_MAP() |
39 return handled; | 91 return handled; |
40 } | 92 } |
41 | 93 |
42 void DispatcherHost::OnOrientationUpdate(const Orientation& orientation) { | |
43 ViewMsg_DeviceOrientationUpdated_Params params; | |
44 params.can_provide_alpha = orientation.can_provide_alpha_; | |
45 params.alpha = orientation.alpha_; | |
46 params.can_provide_beta = orientation.can_provide_beta_; | |
47 params.beta = orientation.beta_; | |
48 params.can_provide_gamma = orientation.can_provide_gamma_; | |
49 params.gamma = orientation.gamma_; | |
50 | |
51 typedef std::set<int>::const_iterator Iterator; | |
52 for (Iterator i = render_view_ids_.begin(), e = render_view_ids_.end(); | |
53 i != e; ++i) { | |
54 IPC::Message* message = new ViewMsg_DeviceOrientationUpdated(*i, params); | |
55 CallRenderViewHost(process_id_, *i, &RenderViewHost::Send, message); | |
56 } | |
57 } | |
58 | |
59 void DispatcherHost::OnStartUpdating(int render_view_id) { | 94 void DispatcherHost::OnStartUpdating(int render_view_id) { |
60 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | 95 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
61 | 96 |
62 render_view_ids_.insert(render_view_id); | 97 if (!provider_) |
63 if (render_view_ids_.size() == 1) { | |
64 DCHECK(!provider_); | |
65 provider_ = Provider::GetInstance(); | 98 provider_ = Provider::GetInstance(); |
66 provider_->AddObserver(this); | 99 |
67 } | 100 observers_map_[render_view_id] = new ObserverDelegate(provider_, |
| 101 process_id_, |
| 102 render_view_id); |
68 } | 103 } |
69 | 104 |
70 void DispatcherHost::OnStopUpdating(int render_view_id) { | 105 void DispatcherHost::OnStopUpdating(int render_view_id) { |
71 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | 106 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
72 | 107 |
73 render_view_ids_.erase(render_view_id); | 108 observers_map_.erase(render_view_id); |
74 if (render_view_ids_.empty()) { | |
75 provider_->RemoveObserver(this); | |
76 provider_ = NULL; | |
77 } | |
78 } | 109 } |
79 | 110 |
80 } // namespace device_orientation | 111 } // namespace device_orientation |
OLD | NEW |