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