| 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/browser/presentation/presentation_service_impl.h" | 5 #include "content/browser/presentation/presentation_service_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "content/public/browser/content_browser_client.h" |
| 9 #include "content/public/browser/navigation_details.h" |
| 10 #include "content/public/browser/render_frame_host.h" |
| 11 #include "content/public/browser/render_process_host.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 #include "content/public/common/content_client.h" |
| 14 #include "content/public/common/frame_navigate_params.h" |
| 8 | 15 |
| 9 namespace content { | 16 namespace content { |
| 10 | 17 |
| 11 PresentationServiceImpl::PresentationServiceImpl() { | 18 PresentationServiceImpl::PresentationServiceImpl( |
| 19 RenderFrameHost* render_frame_host, |
| 20 WebContents* web_contents, |
| 21 PresentationServiceDelegate* delegate) |
| 22 : WebContentsObserver(web_contents), |
| 23 render_frame_host_(render_frame_host), |
| 24 delegate_(delegate) { |
| 25 DCHECK(render_frame_host_); |
| 26 DCHECK(web_contents); |
| 27 VLOG(2) << "PresentationServiceImpl: " |
| 28 << render_frame_host_->GetProcess()->GetID() << ", " |
| 29 << render_frame_host_->GetRoutingID(); |
| 30 if (delegate_) |
| 31 delegate_->AddObserver(this); |
| 12 } | 32 } |
| 13 | 33 |
| 14 PresentationServiceImpl::~PresentationServiceImpl() { | 34 PresentationServiceImpl::~PresentationServiceImpl() { |
| 35 if (delegate_) |
| 36 delegate_->RemoveObserver(this); |
| 15 } | 37 } |
| 16 | 38 |
| 17 // static | 39 // static |
| 18 void PresentationServiceImpl::CreateMojoService( | 40 void PresentationServiceImpl::CreateMojoService( |
| 41 RenderFrameHost* render_frame_host, |
| 19 mojo::InterfaceRequest<presentation::PresentationService> request) { | 42 mojo::InterfaceRequest<presentation::PresentationService> request) { |
| 20 mojo::BindToRequest(new PresentationServiceImpl(), &request); | 43 VLOG(2) << "PresentationServiceImpl::CreateService"; |
| 44 WebContents* web_contents = |
| 45 WebContents::FromRenderFrameHost(render_frame_host); |
| 46 DCHECK(web_contents); |
| 47 |
| 48 mojo::BindToRequest( |
| 49 new PresentationServiceImpl( |
| 50 render_frame_host, |
| 51 web_contents, |
| 52 GetContentClient()->browser()->GetPresentationServiceDelegate( |
| 53 web_contents)), |
| 54 &request); |
| 55 } |
| 56 |
| 57 void PresentationServiceImpl::OnConnectionError() { |
| 58 VLOG(1) << "PresentationServiceImpl::OnConnectionError: " |
| 59 << render_frame_host_->GetProcess()->GetID() << ", " |
| 60 << render_frame_host_->GetRoutingID(); |
| 21 } | 61 } |
| 22 | 62 |
| 23 void PresentationServiceImpl::GetScreenAvailability( | 63 void PresentationServiceImpl::GetScreenAvailability( |
| 24 const mojo::String& presentation_url, | 64 const mojo::String& presentation_url, |
| 25 const AvailabilityCallback& callback) { | 65 const ScreenAvailabilityMojoCallback& callback) { |
| 26 NOTIMPLEMENTED(); | 66 VLOG(2) << "PresentationServiceImpl::GetScreenAvailability"; |
| 67 if (!delegate_) |
| 68 return; |
| 69 |
| 70 const std::string& presentation_url_str = !presentation_url.is_null() ? |
| 71 presentation_url.get() : default_presentation_url_; |
| 72 |
| 73 // GetScreenAvailability() is called with no URL and there is no default |
| 74 // Presentation URL. |
| 75 if (presentation_url_str.empty()) |
| 76 return; |
| 77 |
| 78 auto it = availability_contexts_.find(presentation_url_str); |
| 79 if (it == availability_contexts_.end()) { |
| 80 linked_ptr<ScreenAvailabilityContext> context( |
| 81 new ScreenAvailabilityContext(presentation_url_str)); |
| 82 |
| 83 if (!delegate_->AddScreenAvailabilityListener( |
| 84 render_frame_host_->GetProcess()->GetID(), |
| 85 render_frame_host_->GetRoutingID(), |
| 86 context.get())) { |
| 87 VLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request."; |
| 88 return; |
| 89 } |
| 90 |
| 91 it = availability_contexts_.insert( |
| 92 std::make_pair(presentation_url_str, context)).first; |
| 93 } |
| 94 |
| 95 it->second->CallbackReceived(callback); |
| 27 } | 96 } |
| 28 | 97 |
| 29 void PresentationServiceImpl::OnScreenAvailabilityListenerRemoved() { | 98 void PresentationServiceImpl::OnScreenAvailabilityListenerRemoved() { |
| 30 NOTIMPLEMENTED(); | 99 NOTIMPLEMENTED(); |
| 31 } | 100 } |
| 32 | 101 |
| 102 void PresentationServiceImpl::DidNavigateAnyFrame( |
| 103 content::RenderFrameHost* render_frame_host, |
| 104 const content::LoadCommittedDetails& details, |
| 105 const content::FrameNavigateParams& params) { |
| 106 VLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame"; |
| 107 if (render_frame_host_ != render_frame_host) |
| 108 return; |
| 109 |
| 110 std::string prev_url_host = details.previous_url.host(); |
| 111 std::string curr_url_host = params.url.host(); |
| 112 |
| 113 // If a frame navigation is in-page (e.g. navigating to a fragment in |
| 114 // same page) then we do not unregister listeners. |
| 115 bool in_page_navigation = details.is_in_page || |
| 116 details.type == content::NAVIGATION_TYPE_IN_PAGE; |
| 117 |
| 118 VLOG(2) << "DidNavigateAnyFrame: " |
| 119 << "prev host: " << prev_url_host << ", curr host: " << curr_url_host |
| 120 << ", in_page_navigation: " << in_page_navigation; |
| 121 |
| 122 if (in_page_navigation) |
| 123 return; |
| 124 |
| 125 // Unregister all sources if the frame actually navigated. |
| 126 RemoveAllListeners(); |
| 127 } |
| 128 |
| 129 void PresentationServiceImpl::RenderFrameDeleted( |
| 130 content::RenderFrameHost* render_frame_host) { |
| 131 VLOG(2) << "PresentationServiceImpl::RenderFrameDeleted"; |
| 132 if (render_frame_host_ != render_frame_host) |
| 133 return; |
| 134 |
| 135 // RenderFrameDeleted means this object is getting deleted soon. |
| 136 RemoveAllListeners(); |
| 137 } |
| 138 |
| 139 void PresentationServiceImpl::RemoveAllListeners() { |
| 140 VLOG(2) << "PresentationServiceImpl::RemoveAllListeners"; |
| 141 if (!delegate_) |
| 142 return; |
| 143 |
| 144 delegate_->RemoveAllScreenAvailabilityListeners( |
| 145 render_frame_host_->GetProcess()->GetID(), |
| 146 render_frame_host_->GetRoutingID()); |
| 147 |
| 148 availability_contexts_.clear(); |
| 149 } |
| 150 |
| 151 void PresentationServiceImpl::OnDelegateDestroyed() { |
| 152 VLOG(2) << "PresentationServiceImpl::OnDelegateDestroyed"; |
| 153 delegate_ = nullptr; |
| 154 } |
| 155 |
| 156 PresentationServiceImpl::ScreenAvailabilityContext::ScreenAvailabilityContext( |
| 157 const std::string& presentation_url) |
| 158 : presentation_url_(presentation_url) { |
| 159 } |
| 160 |
| 161 PresentationServiceImpl::ScreenAvailabilityContext:: |
| 162 ~ScreenAvailabilityContext() { |
| 163 } |
| 164 |
| 165 void PresentationServiceImpl::ScreenAvailabilityContext::CallbackReceived( |
| 166 const ScreenAvailabilityMojoCallback& callback) { |
| 167 // NOTE: This will overwrite previously registered callback if any. |
| 168 if (!available_ptr_) { |
| 169 // No results yet, store callback for later invocation. |
| 170 callback_ptr_.reset(new ScreenAvailabilityMojoCallback(callback)); |
| 171 } else { |
| 172 // Run callback now, reset result. |
| 173 // There shouldn't be any callbacks stored in this scenario. |
| 174 DCHECK(!callback_ptr_); |
| 175 callback.Run(*available_ptr_); |
| 176 Reset(); |
| 177 } |
| 178 } |
| 179 |
| 180 void PresentationServiceImpl::ScreenAvailabilityContext::Reset() { |
| 181 callback_ptr_.reset(); |
| 182 available_ptr_.reset(); |
| 183 } |
| 184 |
| 185 std::string PresentationServiceImpl::ScreenAvailabilityContext |
| 186 ::GetPresentationUrl() const { |
| 187 return presentation_url_; |
| 188 } |
| 189 |
| 190 void PresentationServiceImpl::ScreenAvailabilityContext |
| 191 ::OnScreenAvailabilityChanged(bool available) { |
| 192 if (!callback_ptr_) { |
| 193 // No callback, stash the result for now. |
| 194 available_ptr_.reset(new bool(available)); |
| 195 } else { |
| 196 // Invoke callback and erase it. |
| 197 // There shouldn't be any result stored in this scenario. |
| 198 DCHECK(!available_ptr_); |
| 199 callback_ptr_->Run(available); |
| 200 Reset(); |
| 201 } |
| 202 } |
| 203 |
| 33 } // namespace content | 204 } // namespace content |
| OLD | NEW |