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/renderer/device_orientation_dispatcher.h" | |
6 | |
7 #include "chrome/common/render_messages.h" | |
8 #include "chrome/common/render_messages_params.h" | |
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDeviceOrientation.
h" | |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDeviceOrientationC
ontroller.h" | |
11 | |
12 DeviceOrientationDispatcher::DeviceOrientationDispatcher( | |
13 RenderView* render_view) | |
14 : RenderViewObserver(render_view), | |
15 controller_(NULL), | |
16 started_(false) { | |
17 } | |
18 | |
19 DeviceOrientationDispatcher::~DeviceOrientationDispatcher() { | |
20 if (started_) | |
21 stopUpdating(); | |
22 } | |
23 | |
24 bool DeviceOrientationDispatcher::OnMessageReceived(const IPC::Message& msg) { | |
25 bool handled = true; | |
26 IPC_BEGIN_MESSAGE_MAP(DeviceOrientationDispatcher, msg) | |
27 IPC_MESSAGE_HANDLER(ViewMsg_DeviceOrientationUpdated, | |
28 OnDeviceOrientationUpdated) | |
29 IPC_MESSAGE_UNHANDLED(handled = false) | |
30 IPC_END_MESSAGE_MAP() | |
31 return handled; | |
32 } | |
33 | |
34 void DeviceOrientationDispatcher::setController( | |
35 WebKit::WebDeviceOrientationController* controller) { | |
36 controller_.reset(controller); | |
37 } | |
38 | |
39 void DeviceOrientationDispatcher::startUpdating() { | |
40 Send(new ViewHostMsg_DeviceOrientation_StartUpdating(routing_id())); | |
41 started_ = true; | |
42 } | |
43 | |
44 void DeviceOrientationDispatcher::stopUpdating() { | |
45 Send(new ViewHostMsg_DeviceOrientation_StopUpdating(routing_id())); | |
46 started_ = false; | |
47 } | |
48 | |
49 WebKit::WebDeviceOrientation DeviceOrientationDispatcher::lastOrientation() | |
50 const { | |
51 if (!last_orientation_.get()) | |
52 return WebKit::WebDeviceOrientation::nullOrientation(); | |
53 | |
54 return *last_orientation_; | |
55 } | |
56 | |
57 namespace { | |
58 bool OrientationsEqual(const ViewMsg_DeviceOrientationUpdated_Params& a, | |
59 WebKit::WebDeviceOrientation* b) { | |
60 if (a.can_provide_alpha != b->canProvideAlpha()) | |
61 return false; | |
62 if (a.can_provide_alpha && a.alpha != b->alpha()) | |
63 return false; | |
64 if (a.can_provide_beta != b->canProvideBeta()) | |
65 return false; | |
66 if (a.can_provide_beta && a.beta != b->beta()) | |
67 return false; | |
68 if (a.can_provide_gamma != b->canProvideGamma()) | |
69 return false; | |
70 if (a.can_provide_gamma && a.gamma != b->gamma()) | |
71 return false; | |
72 | |
73 return true; | |
74 } | |
75 } // namespace | |
76 | |
77 void DeviceOrientationDispatcher::OnDeviceOrientationUpdated( | |
78 const ViewMsg_DeviceOrientationUpdated_Params& p) { | |
79 | |
80 if (last_orientation_.get() && OrientationsEqual(p, last_orientation_.get())) | |
81 return; | |
82 | |
83 last_orientation_.reset(new WebKit::WebDeviceOrientation(p.can_provide_alpha, | |
84 p.alpha, | |
85 p.can_provide_beta, | |
86 p.beta, | |
87 p.can_provide_gamma, | |
88 p.gamma)); | |
89 | |
90 controller_->didChangeDeviceOrientation(*last_orientation_); | |
91 } | |
OLD | NEW |