1 // Copyright 2014 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.
5 #include "content/browser/screen_orientation/screen_orientation_dispatcher_host_impl.h"
7 #include "content/common/screen_orientation_messages.h"
8 #include "content/public/browser/navigation_details.h"
9 #include "content/public/browser/render_frame_host.h"
10 #include "content/public/browser/render_process_host.h"
11 #include "content/public/browser/render_view_host.h"
12 #include "content/public/browser/render_widget_host.h"
13 #include "content/public/browser/screen_orientation_provider.h"
14 #include "content/public/browser/web_contents.h"
15 #include "third_party/WebKit/public/platform/WebScreenInfo.h"
19 ScreenOrientationDispatcherHostImpl::LockInformation::LockInformation(
20 int request_id
, int process_id
, int routing_id
)
21 : request_id(request_id
),
22 process_id(process_id
),
23 routing_id(routing_id
) {
26 ScreenOrientationDispatcherHostImpl::ScreenOrientationDispatcherHostImpl(
27 WebContents
* web_contents
)
28 : WebContentsObserver(web_contents
),
30 provider_
.reset(new ScreenOrientationProvider(this, web_contents
));
33 ScreenOrientationDispatcherHostImpl::~ScreenOrientationDispatcherHostImpl() {
37 void ScreenOrientationDispatcherHostImpl::ResetCurrentLock() {
44 bool ScreenOrientationDispatcherHostImpl::OnMessageReceived(
45 const IPC::Message
& message
,
46 RenderFrameHost
* render_frame_host
) {
49 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(ScreenOrientationDispatcherHostImpl
, message
,
51 IPC_MESSAGE_HANDLER(ScreenOrientationHostMsg_LockRequest
, OnLockRequest
)
52 IPC_MESSAGE_HANDLER(ScreenOrientationHostMsg_Unlock
, OnUnlockRequest
)
53 IPC_MESSAGE_UNHANDLED(handled
= false)
59 void ScreenOrientationDispatcherHostImpl::DidNavigateMainFrame(
60 const LoadCommittedDetails
& details
, const FrameNavigateParams
& params
) {
61 if (!provider_
|| details
.is_in_page
)
63 provider_
->UnlockOrientation();
67 ScreenOrientationDispatcherHostImpl::GetRenderFrameHostForRequestID(
69 if (!current_lock_
|| current_lock_
->request_id
!= request_id
)
72 return RenderFrameHost::FromID(current_lock_
->process_id
,
73 current_lock_
->routing_id
);
76 void ScreenOrientationDispatcherHostImpl::NotifyLockSuccess(int request_id
) {
77 RenderFrameHost
* render_frame_host
=
78 GetRenderFrameHostForRequestID(request_id
);
79 if (!render_frame_host
)
82 render_frame_host
->Send(new ScreenOrientationMsg_LockSuccess(
83 render_frame_host
->GetRoutingID(), request_id
));
87 void ScreenOrientationDispatcherHostImpl::NotifyLockError(
88 int request_id
, blink::WebLockOrientationError error
) {
89 RenderFrameHost
* render_frame_host
=
90 GetRenderFrameHostForRequestID(request_id
);
91 if (!render_frame_host
)
94 NotifyLockError(request_id
, render_frame_host
, error
);
97 void ScreenOrientationDispatcherHostImpl::NotifyLockError(
99 RenderFrameHost
* render_frame_host
,
100 blink::WebLockOrientationError error
) {
101 render_frame_host
->Send(new ScreenOrientationMsg_LockError(
102 render_frame_host
->GetRoutingID(), request_id
, error
));
106 void ScreenOrientationDispatcherHostImpl::OnOrientationChange() {
108 provider_
->OnOrientationChange();
111 void ScreenOrientationDispatcherHostImpl::OnLockRequest(
112 RenderFrameHost
* render_frame_host
,
113 blink::WebScreenOrientationLockType orientation
,
116 NotifyLockError(current_lock_
->request_id
, render_frame_host
,
117 blink::WebLockOrientationErrorCanceled
);
121 NotifyLockError(request_id
, render_frame_host
,
122 blink::WebLockOrientationErrorNotAvailable
);
126 current_lock_
= new LockInformation(request_id
,
127 render_frame_host
->GetProcess()->GetID(),
128 render_frame_host
->GetRoutingID());
130 provider_
->LockOrientation(request_id
, orientation
);
133 void ScreenOrientationDispatcherHostImpl::OnUnlockRequest(
134 RenderFrameHost
* render_frame_host
) {
136 NotifyLockError(current_lock_
->request_id
, render_frame_host
,
137 blink::WebLockOrientationErrorCanceled
);
143 provider_
->UnlockOrientation();
146 } // namespace content