| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/presentation/presentation_dispatcher.h" | 5 #include "content/renderer/presentation/presentation_dispatcher.h" |
| 6 | 6 |
| 7 #include "content/common/presentation/presentation_service.mojom.h" | 7 #include "content/common/presentation/presentation_service.mojom.h" |
| 8 #include "content/public/common/service_registry.h" | 8 #include "content/public/common/service_registry.h" |
| 9 #include "content/public/renderer/render_frame.h" | 9 #include "content/public/renderer/render_frame.h" |
| 10 #include "third_party/WebKit/public/platform/WebString.h" |
| 10 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nController.h" | 11 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nController.h" |
| 12 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nError.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 blink::WebPresentationError::ErrorType GetWebPresentationErrorTypeFromMojo( |
| 17 presentation::PresentationErrorType mojoErrorType) { |
| 18 switch (mojoErrorType) { |
| 19 case presentation::PRESENTATION_ERROR_TYPE_NO_AVAILABLE_SCREENS: |
| 20 return blink::WebPresentationError::ErrorTypeNoAvailableScreens; |
| 21 case presentation::PRESENTATION_ERROR_TYPE_SESSION_REQUEST_CANCELLED: |
| 22 return blink::WebPresentationError::ErrorTypeSessionRequestCancelled; |
| 23 case presentation::PRESENTATION_ERROR_TYPE_NO_PRESENTATION_FOUND: |
| 24 return blink::WebPresentationError::ErrorTypeNoPresentationFound; |
| 25 case presentation::PRESENTATION_ERROR_TYPE_UNKNOWN: |
| 26 default: |
| 27 return blink::WebPresentationError::ErrorTypeUnknown; |
| 28 } |
| 29 } |
| 30 |
| 31 } // namespace |
| 11 | 32 |
| 12 namespace content { | 33 namespace content { |
| 13 | 34 |
| 14 PresentationDispatcher::PresentationDispatcher(RenderFrame* render_frame) | 35 PresentationDispatcher::PresentationDispatcher(RenderFrame* render_frame) |
| 15 : RenderFrameObserver(render_frame), | 36 : RenderFrameObserver(render_frame), |
| 16 controller_(nullptr) { | 37 controller_(nullptr) { |
| 17 } | 38 } |
| 18 | 39 |
| 19 PresentationDispatcher::~PresentationDispatcher() { | 40 PresentationDispatcher::~PresentationDispatcher() { |
| 20 // Controller should be destroyed before the dispatcher when frame is | 41 // Controller should be destroyed before the dispatcher when frame is |
| (...skipping 16 matching lines...) Expand all Loading... |
| 37 if (watched) { | 58 if (watched) { |
| 38 presentation_service_->GetScreenAvailability( | 59 presentation_service_->GetScreenAvailability( |
| 39 mojo::String(), | 60 mojo::String(), |
| 40 base::Bind(&PresentationDispatcher::OnScreenAvailabilityChanged, | 61 base::Bind(&PresentationDispatcher::OnScreenAvailabilityChanged, |
| 41 base::Unretained(this))); | 62 base::Unretained(this))); |
| 42 } else { | 63 } else { |
| 43 presentation_service_->OnScreenAvailabilityListenerRemoved(); | 64 presentation_service_->OnScreenAvailabilityListenerRemoved(); |
| 44 } | 65 } |
| 45 } | 66 } |
| 46 | 67 |
| 68 void PresentationDispatcher::startSession( |
| 69 const blink::WebString& presentationUrl, |
| 70 const blink::WebString& presentationId, |
| 71 blink::WebPresentationSessionClientCallbacks* callback) { |
| 72 ConnectToPresentationServiceIfNeeded(); |
| 73 presentation_service_->StartSession( |
| 74 presentationUrl.utf8(), |
| 75 presentationId.utf8(), |
| 76 base::Bind(&PresentationDispatcher::OnSessionCreated, |
| 77 base::Unretained(this), |
| 78 base::Owned(callback))); |
| 79 } |
| 80 |
| 81 void PresentationDispatcher::joinSession( |
| 82 const blink::WebString& presentationUrl, |
| 83 const blink::WebString& presentationId, |
| 84 blink::WebPresentationSessionClientCallbacks* callback) { |
| 85 ConnectToPresentationServiceIfNeeded(); |
| 86 presentation_service_->JoinSession( |
| 87 presentationUrl.utf8(), |
| 88 presentationId.utf8(), |
| 89 base::Bind(&PresentationDispatcher::OnSessionCreated, |
| 90 base::Unretained(this), |
| 91 base::Owned(callback))); |
| 92 } |
| 93 |
| 47 void PresentationDispatcher::OnScreenAvailabilityChanged(bool available) { | 94 void PresentationDispatcher::OnScreenAvailabilityChanged(bool available) { |
| 48 if (!controller_) | 95 if (!controller_) |
| 49 return; | 96 return; |
| 50 | 97 |
| 51 // Reset the callback to get the next event. | 98 // Reset the callback to get the next event. |
| 52 updateAvailableChangeWatched(controller_->isAvailableChangeWatched()); | 99 updateAvailableChangeWatched(controller_->isAvailableChangeWatched()); |
| 53 | 100 |
| 54 controller_->didChangeAvailability(available); | 101 controller_->didChangeAvailability(available); |
| 55 } | 102 } |
| 56 | 103 |
| 104 void PresentationDispatcher::OnSessionCreated( |
| 105 blink::WebPresentationSessionClientCallbacks* callback, |
| 106 presentation::PresentationSessionInfoPtr session_info, |
| 107 presentation::PresentationErrorPtr error) { |
| 108 if (!callback) |
| 109 return; |
| 110 if (!error.is_null()) { |
| 111 DCHECK(session_info.is_null()); |
| 112 callback->onError(new blink::WebPresentationError( |
| 113 GetWebPresentationErrorTypeFromMojo(error->errorType), |
| 114 blink::WebString::fromUTF8(error->message))); |
| 115 return; |
| 116 } |
| 117 |
| 118 DCHECK(!session_info.is_null()); |
| 119 PresentationSessionDispatcher* session_dispatcher = |
| 120 new PresentationSessionDispatcher(session_info.Pass()); |
| 121 presentation_sessions_.push_back(session_dispatcher); |
| 122 callback->onSuccess(new blink::WebString(session_dispatcher->getId())); |
| 123 } |
| 124 |
| 57 void PresentationDispatcher::ConnectToPresentationServiceIfNeeded() { | 125 void PresentationDispatcher::ConnectToPresentationServiceIfNeeded() { |
| 58 if (presentation_service_.get()) | 126 if (presentation_service_.get()) |
| 59 return; | 127 return; |
| 60 | 128 |
| 61 render_frame()->GetServiceRegistry()->ConnectToRemoteService( | 129 render_frame()->GetServiceRegistry()->ConnectToRemoteService( |
| 62 &presentation_service_); | 130 &presentation_service_); |
| 63 } | 131 } |
| 64 | 132 |
| 65 } // namespace content | 133 } // namespace content |
| OLD | NEW |