| 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" |
| 11 #include "services/shell/public/cpp/interface_registry.h" | 11 #include "services/shell/public/cpp/interface_registry.h" |
| 12 | 12 |
| 13 DEFINE_WEB_CONTENTS_USER_DATA_KEY( | 13 DEFINE_WEB_CONTENTS_USER_DATA_KEY( |
| 14 dom_distiller::DistillabilityDriver); | 14 dom_distiller::DistillabilityDriver); |
| 15 | 15 |
| 16 namespace dom_distiller { | 16 namespace dom_distiller { |
| 17 | 17 |
| 18 // Implementation of the Mojo DistillabilityService. This is called by the | 18 // Implementation of the Mojo DistillabilityService. This is called by the |
| 19 // renderer to notify the browser that a page is distillable. | 19 // renderer to notify the browser that a page is distillable. |
| 20 class DistillabilityServiceImpl : public mojom::DistillabilityService { | 20 class DistillabilityServiceImpl : public mojom::DistillabilityService { |
| 21 public: | 21 public: |
| 22 DistillabilityServiceImpl( | 22 DistillabilityServiceImpl( |
| 23 mojo::InterfaceRequest<mojom::DistillabilityService> request, | 23 mojo::InterfaceRequest<mojom::DistillabilityService> request, |
| 24 base::WeakPtr<DistillabilityDriver> distillability_driver) | 24 base::WeakPtr<DistillabilityDriver> distillability_driver) |
| 25 : binding_(this, std::move(request)), | 25 : binding_(this, std::move(request)), |
| 26 distillability_driver_(distillability_driver) {} | 26 distillability_driver_(distillability_driver) {} |
| 27 | 27 |
| 28 ~DistillabilityServiceImpl() override {} | 28 ~DistillabilityServiceImpl() override { |
| 29 if (!distillability_driver_) return; |
| 30 distillability_driver_->SetNeedsMojoSetup(); |
| 31 } |
| 29 | 32 |
| 30 void NotifyIsDistillable(bool is_distillable, bool is_last_update) override { | 33 void NotifyIsDistillable(bool is_distillable, bool is_last_update) override { |
| 31 if (!distillability_driver_) return; | 34 if (!distillability_driver_) return; |
| 32 distillability_driver_->OnDistillability(is_distillable, is_last_update); | 35 distillability_driver_->OnDistillability(is_distillable, is_last_update); |
| 33 } | 36 } |
| 34 | 37 |
| 35 private: | 38 private: |
| 36 mojo::StrongBinding<mojom::DistillabilityService> binding_; | 39 mojo::StrongBinding<mojom::DistillabilityService> binding_; |
| 37 base::WeakPtr<DistillabilityDriver> distillability_driver_; | 40 base::WeakPtr<DistillabilityDriver> distillability_driver_; |
| 38 }; | 41 }; |
| 39 | 42 |
| 40 DistillabilityDriver::DistillabilityDriver( | 43 DistillabilityDriver::DistillabilityDriver( |
| 41 content::WebContents* web_contents) | 44 content::WebContents* web_contents) |
| 42 : content::WebContentsObserver(web_contents), | 45 : content::WebContentsObserver(web_contents), |
| 46 mojo_needs_setup_(true), |
| 43 weak_factory_(this) { | 47 weak_factory_(this) { |
| 44 if (!web_contents) return; | 48 if (!web_contents) return; |
| 45 SetupMojoService(web_contents->GetMainFrame()); | 49 SetupMojoService(web_contents->GetMainFrame()); |
| 46 } | 50 } |
| 47 | 51 |
| 48 DistillabilityDriver::~DistillabilityDriver() { | 52 DistillabilityDriver::~DistillabilityDriver() { |
| 49 content::WebContentsObserver::Observe(nullptr); | 53 content::WebContentsObserver::Observe(nullptr); |
| 50 } | 54 } |
| 51 | 55 |
| 52 void DistillabilityDriver::CreateDistillabilityService( | 56 void DistillabilityDriver::CreateDistillabilityService( |
| 53 mojo::InterfaceRequest<mojom::DistillabilityService> request) { | 57 mojo::InterfaceRequest<mojom::DistillabilityService> request) { |
| 54 new DistillabilityServiceImpl(std::move(request), weak_factory_.GetWeakPtr()); | 58 new DistillabilityServiceImpl(std::move(request), weak_factory_.GetWeakPtr()); |
| 55 } | 59 } |
| 56 | 60 |
| 57 void DistillabilityDriver::SetDelegate( | 61 void DistillabilityDriver::SetDelegate( |
| 58 const base::Callback<void(bool, bool)>& delegate) { | 62 const base::Callback<void(bool, bool)>& delegate) { |
| 59 m_delegate_ = delegate; | 63 m_delegate_ = delegate; |
| 60 } | 64 } |
| 61 | 65 |
| 62 void DistillabilityDriver::OnDistillability( | 66 void DistillabilityDriver::OnDistillability( |
| 63 bool distillable, bool is_last) { | 67 bool distillable, bool is_last) { |
| 64 if (m_delegate_.is_null()) return; | 68 if (m_delegate_.is_null()) return; |
| 65 | 69 |
| 66 m_delegate_.Run(distillable, is_last); | 70 m_delegate_.Run(distillable, is_last); |
| 67 } | 71 } |
| 68 | 72 |
| 73 void DistillabilityDriver::SetNeedsMojoSetup() { |
| 74 mojo_needs_setup_ = true; |
| 75 } |
| 76 |
| 69 void DistillabilityDriver::RenderFrameHostChanged( | 77 void DistillabilityDriver::RenderFrameHostChanged( |
| 70 content::RenderFrameHost* old_host, | 78 content::RenderFrameHost* old_host, |
| 71 content::RenderFrameHost* new_host) { | 79 content::RenderFrameHost* new_host) { |
| 80 // This method is invoked if any of the active RenderFrameHosts are swapped. |
| 81 // Only add the mojo service to the main frame host. |
| 82 if (!web_contents() || web_contents()->GetMainFrame() != new_host) return; |
| 83 |
| 72 // If the RenderFrameHost changes (this will happen if the user navigates to | 84 // 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. | 85 // or from a native page), the service needs to be attached to that host. |
| 86 mojo_needs_setup_ = true; |
| 74 SetupMojoService(new_host); | 87 SetupMojoService(new_host); |
| 75 // Clean up the service on the old host if possible. | 88 // Clean up the service on the old host if possible. |
| 76 if (!old_host) return; | 89 if (!old_host) return; |
| 77 old_host->GetInterfaceRegistry() | 90 old_host->GetInterfaceRegistry() |
| 78 ->RemoveInterface<mojom::DistillabilityService>(); | 91 ->RemoveInterface<mojom::DistillabilityService>(); |
| 79 } | 92 } |
| 80 | 93 |
| 81 void DistillabilityDriver::DidStartProvisionalLoadForFrame( | 94 void DistillabilityDriver::DidStartProvisionalLoadForFrame( |
| 82 content::RenderFrameHost* render_frame_host, const GURL& validated_url, | 95 content::RenderFrameHost* render_frame_host, const GURL& validated_url, |
| 83 bool is_error_page, bool is_iframe_srcdoc) { | 96 bool is_error_page, bool is_iframe_srcdoc) { |
| 84 SetupMojoService(render_frame_host); | 97 SetupMojoService(render_frame_host); |
| 85 } | 98 } |
| 86 | 99 |
| 87 void DistillabilityDriver::SetupMojoService( | 100 void DistillabilityDriver::SetupMojoService( |
| 88 content::RenderFrameHost* frame_host) { | 101 content::RenderFrameHost* frame_host) { |
| 89 if (!frame_host || !frame_host->GetInterfaceRegistry()) return; | 102 if (!frame_host || !frame_host->GetInterfaceRegistry() |
| 103 || !mojo_needs_setup_) { |
| 104 return; |
| 105 } |
| 90 | 106 |
| 91 frame_host->GetInterfaceRegistry()->AddInterface( | 107 frame_host->GetInterfaceRegistry()->AddInterface( |
| 92 base::Bind(&DistillabilityDriver::CreateDistillabilityService, | 108 base::Bind(&DistillabilityDriver::CreateDistillabilityService, |
| 93 weak_factory_.GetWeakPtr())); | 109 weak_factory_.GetWeakPtr())); |
| 110 mojo_needs_setup_ = false; |
| 94 } | 111 } |
| 95 | 112 |
| 96 } // namespace dom_distiller | 113 } // namespace dom_distiller |
| OLD | NEW |