OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/device_orientation/orientation_message_filter.h" | 5 #include "content/browser/device_orientation/orientation_message_filter.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "content/browser/device_orientation/device_data.h" |
8 #include "content/browser/device_orientation/orientation.h" | |
9 #include "content/browser/device_orientation/provider.h" | |
10 #include "content/browser/renderer_host/render_view_host_impl.h" | |
11 #include "content/common/device_orientation_messages.h" | 8 #include "content/common/device_orientation_messages.h" |
12 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
13 | 10 |
14 using content::BrowserThread; | 11 using content::BrowserThread; |
15 | 12 |
16 namespace device_orientation { | 13 namespace device_orientation { |
17 | 14 |
18 OrientationMessageFilter::OrientationMessageFilter() : provider_(NULL) { | 15 OrientationMessageFilter::OrientationMessageFilter() |
| 16 : MessageFilter(DeviceData::kTypeOrientation) { |
19 } | 17 } |
20 | 18 |
21 OrientationMessageFilter::~OrientationMessageFilter() { | 19 OrientationMessageFilter::~OrientationMessageFilter() { |
22 } | 20 } |
23 | 21 |
24 class OrientationMessageFilter::ObserverDelegate | |
25 : public base::RefCounted<ObserverDelegate>, public Provider::Observer { | |
26 public: | |
27 // Create ObserverDelegate that observes provider and forwards updates to | |
28 // render_view_id in process_id. | |
29 // Will stop observing provider when destructed. | |
30 ObserverDelegate(Provider* provider, | |
31 int render_view_id, | |
32 IPC::Sender* sender); | |
33 | |
34 // From Provider::Observer. | |
35 virtual void OnOrientationUpdate(const Orientation& orientation); | |
36 | |
37 private: | |
38 friend class base::RefCounted<ObserverDelegate>; | |
39 virtual ~ObserverDelegate(); | |
40 | |
41 scoped_refptr<Provider> provider_; | |
42 int render_view_id_; | |
43 IPC::Sender* sender_; // Weak pointer. | |
44 | |
45 DISALLOW_COPY_AND_ASSIGN(ObserverDelegate); | |
46 }; | |
47 | |
48 OrientationMessageFilter::ObserverDelegate::ObserverDelegate(Provider* provider, | |
49 int render_view_id, | |
50 IPC::Sender* sender) | |
51 : provider_(provider), | |
52 render_view_id_(render_view_id), | |
53 sender_(sender) { | |
54 provider_->AddObserver(this); | |
55 } | |
56 | |
57 OrientationMessageFilter::ObserverDelegate::~ObserverDelegate() { | |
58 provider_->RemoveObserver(this); | |
59 } | |
60 | |
61 void OrientationMessageFilter::ObserverDelegate::OnOrientationUpdate( | |
62 const Orientation& orientation) { | |
63 DeviceOrientationMsg_Updated_Params params; | |
64 params.can_provide_alpha = orientation.can_provide_alpha(); | |
65 params.alpha = orientation.alpha(); | |
66 params.can_provide_beta = orientation.can_provide_beta(); | |
67 params.beta = orientation.beta(); | |
68 params.can_provide_gamma = orientation.can_provide_gamma(); | |
69 params.gamma = orientation.gamma(); | |
70 params.can_provide_absolute = orientation.can_provide_absolute(); | |
71 params.absolute = orientation.absolute(); | |
72 | |
73 sender_->Send(new DeviceOrientationMsg_Updated(render_view_id_, params)); | |
74 } | |
75 | |
76 bool OrientationMessageFilter::OnMessageReceived(const IPC::Message& message, | 22 bool OrientationMessageFilter::OnMessageReceived(const IPC::Message& message, |
77 bool* message_was_ok) { | 23 bool* message_was_ok) { |
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
79 bool handled = true; | 25 bool handled = true; |
80 IPC_BEGIN_MESSAGE_MAP_EX(OrientationMessageFilter, message, *message_was_ok) | 26 IPC_BEGIN_MESSAGE_MAP_EX(OrientationMessageFilter, message, *message_was_ok) |
81 IPC_MESSAGE_HANDLER(DeviceOrientationHostMsg_StartUpdating, OnStartUpdating) | 27 IPC_MESSAGE_HANDLER(DeviceOrientationHostMsg_StartUpdating, OnStartUpdating) |
82 IPC_MESSAGE_HANDLER(DeviceOrientationHostMsg_StopUpdating, OnStopUpdating) | 28 IPC_MESSAGE_HANDLER(DeviceOrientationHostMsg_StopUpdating, OnStopUpdating) |
83 IPC_MESSAGE_UNHANDLED(handled = false) | 29 IPC_MESSAGE_UNHANDLED(handled = false) |
84 IPC_END_MESSAGE_MAP() | 30 IPC_END_MESSAGE_MAP() |
85 return handled; | 31 return handled; |
86 } | 32 } |
87 | 33 |
88 void OrientationMessageFilter::OnStartUpdating(int render_view_id) { | |
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
90 | |
91 if (!provider_) | |
92 provider_ = Provider::GetInstance(); | |
93 | |
94 observers_map_[render_view_id] = new ObserverDelegate(provider_, | |
95 render_view_id, | |
96 this); | |
97 } | |
98 | |
99 void OrientationMessageFilter::OnStopUpdating(int render_view_id) { | |
100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
101 | |
102 observers_map_.erase(render_view_id); | |
103 } | |
104 | |
105 } // namespace device_orientation | 34 } // namespace device_orientation |
OLD | NEW |