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. 4 5 #include "content/browser/screen_orientation/screen_orientation_dispatcher_host.h" 6 7 #include "content/browser/screen_orientation/screen_orientation_provider.h" 8 #include "content/common/screen_orientation_messages.h" 9 #include "content/public/browser/render_frame_host.h" 10 #include "content/public/browser/web_contents.h" 11 12 namespace content { 13 14 ScreenOrientationDispatcherHost::ScreenOrientationDispatcherHost( 15 WebContents* web_contents) 16 : WebContentsObserver(web_contents) { 17 if (!provider_.get()) 18 provider_.reset(CreateProvider()); 19 } 20 21 ScreenOrientationDispatcherHost::~ScreenOrientationDispatcherHost() { 22 } 23 24 bool ScreenOrientationDispatcherHost::OnMessageReceived( 25 const IPC::Message& message, 26 RenderFrameHost* render_frame_host) { 27 bool handled = true; 28 29 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(ScreenOrientationDispatcherHost, message, 30 render_frame_host) 31 IPC_MESSAGE_HANDLER(ScreenOrientationHostMsg_LockRequest, OnLockRequest) 32 IPC_MESSAGE_HANDLER(ScreenOrientationHostMsg_Unlock, OnUnlockRequest) 33 IPC_MESSAGE_UNHANDLED(handled = false) 34 IPC_END_MESSAGE_MAP() 35 36 return handled; 37 } 38 39 void ScreenOrientationDispatcherHost::OnOrientationChange( 40 blink::WebScreenOrientationType orientation) { 41 Send(new ScreenOrientationMsg_OrientationChange(orientation)); 42 } 43 44 void ScreenOrientationDispatcherHost::SetProviderForTests( 45 ScreenOrientationProvider* provider) { 46 provider_.reset(provider); 47 } 48 49 void ScreenOrientationDispatcherHost::OnLockRequest( 50 RenderFrameHost* render_frame_host, 51 blink::WebScreenOrientationLockType orientation, 52 int request_id) { 53 if (!provider_) { 54 render_frame_host->Send(new ScreenOrientationMsg_LockError( 55 render_frame_host->GetRoutingID(), 56 request_id, 57 blink::WebLockOrientationCallback::ErrorTypeNotAvailable)); 58 return; 59 } 60 61 // TODO(mlamouri): pass real values. 62 render_frame_host->Send(new ScreenOrientationMsg_LockSuccess( 63 render_frame_host->GetRoutingID(), 64 request_id, 65 0, 66 blink::WebScreenOrientationPortraitPrimary)); 67 provider_->LockOrientation(orientation); 68 } 69 70 void ScreenOrientationDispatcherHost::OnUnlockRequest( 71 RenderFrameHost* render_frame_host) { 72 if (!provider_.get()) 73 return; 74 75 provider_->UnlockOrientation(); 76 } 77 78 #if !defined(OS_ANDROID) 79 // static 80 ScreenOrientationProvider* ScreenOrientationDispatcherHost::CreateProvider() { 81 return NULL; 82 } 83 #endif 84 85 } // namespace content 86