| 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 "components/dom_distiller/content/browser/distillability_driver.h" | 5 #include "components/dom_distiller/content/browser/distillability_driver.h" |
| 6 | 6 |
| 7 #include "content/public/browser/render_frame_host.h" | 7 #include "content/public/browser/render_frame_host.h" |
| 8 #include "content/public/browser/web_contents.h" | 8 #include "content/public/browser/web_contents.h" |
| 9 #include "content/public/browser/web_contents_observer.h" | 9 #include "content/public/browser/web_contents_observer.h" |
| 10 #include "content/public/browser/web_contents_user_data.h" | 10 #include "content/public/browser/web_contents_user_data.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 private: | 35 private: |
| 36 mojo::StrongBinding<mojom::DistillabilityService> binding_; | 36 mojo::StrongBinding<mojom::DistillabilityService> binding_; |
| 37 base::WeakPtr<DistillabilityDriver> distillability_driver_; | 37 base::WeakPtr<DistillabilityDriver> distillability_driver_; |
| 38 }; | 38 }; |
| 39 | 39 |
| 40 DistillabilityDriver::DistillabilityDriver( | 40 DistillabilityDriver::DistillabilityDriver( |
| 41 content::WebContents* web_contents) | 41 content::WebContents* web_contents) |
| 42 : content::WebContentsObserver(web_contents), | 42 : content::WebContentsObserver(web_contents), |
| 43 weak_factory_(this) { | 43 weak_factory_(this) { |
| 44 SetupMojoService(); | 44 if (!web_contents) return; |
| 45 SetupMojoService(web_contents->GetMainFrame()); |
| 45 } | 46 } |
| 46 | 47 |
| 47 DistillabilityDriver::~DistillabilityDriver() { | 48 DistillabilityDriver::~DistillabilityDriver() { |
| 48 content::WebContentsObserver::Observe(nullptr); | 49 content::WebContentsObserver::Observe(nullptr); |
| 49 } | 50 } |
| 50 | 51 |
| 51 void DistillabilityDriver::CreateDistillabilityService( | 52 void DistillabilityDriver::CreateDistillabilityService( |
| 52 mojo::InterfaceRequest<mojom::DistillabilityService> request) { | 53 mojo::InterfaceRequest<mojom::DistillabilityService> request) { |
| 53 new DistillabilityServiceImpl(std::move(request), weak_factory_.GetWeakPtr()); | 54 new DistillabilityServiceImpl(std::move(request), weak_factory_.GetWeakPtr()); |
| 54 } | 55 } |
| 55 | 56 |
| 56 void DistillabilityDriver::SetDelegate( | 57 void DistillabilityDriver::SetDelegate( |
| 57 const base::Callback<void(bool, bool)>& delegate) { | 58 const base::Callback<void(bool, bool)>& delegate) { |
| 58 m_delegate_ = delegate; | 59 m_delegate_ = delegate; |
| 59 } | 60 } |
| 60 | 61 |
| 61 void DistillabilityDriver::OnDistillability( | 62 void DistillabilityDriver::OnDistillability( |
| 62 bool distillable, bool is_last) { | 63 bool distillable, bool is_last) { |
| 63 if (m_delegate_.is_null()) return; | 64 if (m_delegate_.is_null()) return; |
| 64 | 65 |
| 65 m_delegate_.Run(distillable, is_last); | 66 m_delegate_.Run(distillable, is_last); |
| 66 } | 67 } |
| 67 | 68 |
| 69 void DistillabilityDriver::RenderFrameHostChanged( |
| 70 content::RenderFrameHost* old_host, |
| 71 content::RenderFrameHost* new_host) { |
| 72 // If the RenderFrameHost changes (this will happen if the user navigates to |
| 73 // or from a native page), the service needs to be attached to that host. |
| 74 SetupMojoService(new_host); |
| 75 // Clean up the service on the old host if possible. |
| 76 if (!old_host) return; |
| 77 old_host->GetInterfaceRegistry() |
| 78 ->RemoveInterface<mojom::DistillabilityService>(); |
| 79 } |
| 80 |
| 68 void DistillabilityDriver::DidStartProvisionalLoadForFrame( | 81 void DistillabilityDriver::DidStartProvisionalLoadForFrame( |
| 69 content::RenderFrameHost* render_frame_host, const GURL& validated_url, | 82 content::RenderFrameHost* render_frame_host, const GURL& validated_url, |
| 70 bool is_error_page, bool is_iframe_srcdoc) { | 83 bool is_error_page, bool is_iframe_srcdoc) { |
| 71 SetupMojoService(); | 84 SetupMojoService(render_frame_host); |
| 72 } | 85 } |
| 73 | 86 |
| 74 void DistillabilityDriver::SetupMojoService() { | 87 void DistillabilityDriver::SetupMojoService( |
| 75 if (!web_contents()) return; | 88 content::RenderFrameHost* frame_host) { |
| 76 | |
| 77 content::RenderFrameHost* frame_host = web_contents()->GetMainFrame(); | |
| 78 if (!frame_host || !frame_host->GetInterfaceRegistry()) return; | 89 if (!frame_host || !frame_host->GetInterfaceRegistry()) return; |
| 79 | 90 |
| 80 frame_host->GetInterfaceRegistry()->AddInterface( | 91 frame_host->GetInterfaceRegistry()->AddInterface( |
| 81 base::Bind(&DistillabilityDriver::CreateDistillabilityService, | 92 base::Bind(&DistillabilityDriver::CreateDistillabilityService, |
| 82 weak_factory_.GetWeakPtr())); | 93 weak_factory_.GetWeakPtr())); |
| 83 } | 94 } |
| 84 | 95 |
| 85 } // namespace dom_distiller | 96 } // namespace dom_distiller |
| OLD | NEW |