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/chrome_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/chrome_switches.h" |
| 14 #include "chrome/common/render_messages.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 } |
| 22 |
| 23 DispatcherHost::~DispatcherHost() { |
| 24 if (provider_) |
| 25 provider_->RemoveObserver(this); |
| 26 } |
| 27 |
| 28 bool DispatcherHost::OnMessageReceived(const IPC::Message& msg, |
| 29 bool* msg_was_ok) { |
| 30 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 31 bool handled = true; |
| 32 IPC_BEGIN_MESSAGE_MAP_EX(DispatcherHost, msg, *msg_was_ok) |
| 33 IPC_MESSAGE_HANDLER(ViewHostMsg_DeviceOrientation_StartUpdating, |
| 34 OnStartUpdating) |
| 35 IPC_MESSAGE_HANDLER(ViewHostMsg_DeviceOrientation_StopUpdating, |
| 36 OnStopUpdating) |
| 37 IPC_MESSAGE_UNHANDLED(handled = false) |
| 38 IPC_END_MESSAGE_MAP() |
| 39 return handled; |
| 40 } |
| 41 |
| 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) { |
| 60 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 61 |
| 62 render_view_ids_.insert(render_view_id); |
| 63 if (render_view_ids_.size() == 1) { |
| 64 DCHECK(!provider_); |
| 65 provider_ = Provider::GetInstance(); |
| 66 provider_->AddObserver(this); |
| 67 } |
| 68 } |
| 69 |
| 70 void DispatcherHost::OnStopUpdating(int render_view_id) { |
| 71 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 72 |
| 73 render_view_ids_.erase(render_view_id); |
| 74 if (render_view_ids_.empty()) { |
| 75 provider_->RemoveObserver(this); |
| 76 provider_ = NULL; |
| 77 } |
| 78 } |
| 79 |
| 80 } // namespace device_orientation |
OLD | NEW |