| 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 #include "components/dom_distiller/content/common/distiller_messages.h" | |
| 7 | 6 |
| 8 #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" |
| 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 "content/public/common/service_registry.h" |
| 11 | 12 |
| 12 DEFINE_WEB_CONTENTS_USER_DATA_KEY( | 13 DEFINE_WEB_CONTENTS_USER_DATA_KEY( |
| 13 dom_distiller::DistillabilityDriver); | 14 dom_distiller::DistillabilityDriver); |
| 14 | 15 |
| 15 namespace dom_distiller { | 16 namespace dom_distiller { |
| 16 | 17 |
| 18 // Implementation of the Mojo DistillabilityService. This is called by the |
| 19 // renderer to notify the browser that a page is distillable. |
| 20 class DistillabilityServiceImpl : public DistillabilityService { |
| 21 public: |
| 22 DistillabilityServiceImpl( |
| 23 mojo::InterfaceRequest<DistillabilityService> request, |
| 24 DistillabilityDriver* distillability_driver) |
| 25 : binding_(this, std::move(request)), |
| 26 distillability_driver_(distillability_driver) {} |
| 27 |
| 28 ~DistillabilityServiceImpl() override {} |
| 29 |
| 30 void NotifyIsDistillable(bool is_distillable, bool is_last_update) override { |
| 31 distillability_driver_->OnDistillability(is_distillable, is_last_update); |
| 32 } |
| 33 |
| 34 private: |
| 35 mojo::StrongBinding<DistillabilityService> binding_; |
| 36 DistillabilityDriver* distillability_driver_; |
| 37 }; |
| 38 |
| 17 DistillabilityDriver::DistillabilityDriver( | 39 DistillabilityDriver::DistillabilityDriver( |
| 18 content::WebContents* web_contents) | 40 content::WebContents* web_contents) |
| 19 : content::WebContentsObserver(web_contents) { | 41 : content::WebContentsObserver(web_contents), |
| 42 weak_factory_(this) { |
| 43 if (!web_contents) return; |
| 44 web_contents->GetMainFrame()->GetServiceRegistry()->AddService( |
| 45 base::Bind(&DistillabilityDriver::CreateDistillabilityService, |
| 46 weak_factory_.GetWeakPtr())); |
| 20 } | 47 } |
| 21 | 48 |
| 22 DistillabilityDriver::~DistillabilityDriver() { | 49 DistillabilityDriver::~DistillabilityDriver() { |
| 23 CleanUp(); | 50 CleanUp(); |
| 24 } | 51 } |
| 25 | 52 |
| 53 void DistillabilityDriver::CreateDistillabilityService( |
| 54 mojo::InterfaceRequest<DistillabilityService> request) { |
| 55 new DistillabilityServiceImpl(std::move(request), this); |
| 56 } |
| 57 |
| 26 void DistillabilityDriver::SetDelegate( | 58 void DistillabilityDriver::SetDelegate( |
| 27 const base::Callback<void(bool, bool)>& delegate) { | 59 const base::Callback<void(bool, bool)>& delegate) { |
| 28 m_delegate_ = delegate; | 60 m_delegate_ = delegate; |
| 29 } | 61 } |
| 30 | 62 |
| 31 bool DistillabilityDriver::OnMessageReceived(const IPC::Message &msg, | |
| 32 content::RenderFrameHost* render_frame_host) { | |
| 33 bool handled = true; | |
| 34 IPC_BEGIN_MESSAGE_MAP(DistillabilityDriver, msg) | |
| 35 IPC_MESSAGE_HANDLER(FrameHostMsg_Distillability, | |
| 36 OnDistillability) | |
| 37 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 38 IPC_END_MESSAGE_MAP() | |
| 39 return handled; | |
| 40 } | |
| 41 | |
| 42 void DistillabilityDriver::OnDistillability( | 63 void DistillabilityDriver::OnDistillability( |
| 43 bool distillable, bool is_last) { | 64 bool distillable, bool is_last) { |
| 44 if (m_delegate_.is_null()) return; | 65 if (m_delegate_.is_null()) return; |
| 45 | 66 |
| 46 m_delegate_.Run(distillable, is_last); | 67 m_delegate_.Run(distillable, is_last); |
| 47 } | 68 } |
| 48 | 69 |
| 49 void DistillabilityDriver::RenderProcessGone( | 70 void DistillabilityDriver::RenderProcessGone( |
| 50 base::TerminationStatus status) { | 71 base::TerminationStatus status) { |
| 51 CleanUp(); | 72 CleanUp(); |
| 52 } | 73 } |
| 53 | 74 |
| 54 void DistillabilityDriver::CleanUp() { | 75 void DistillabilityDriver::CleanUp() { |
| 76 if (!web_contents()) return; |
| 77 web_contents()->GetMainFrame()->GetServiceRegistry() |
| 78 ->RemoveService<DistillabilityService>(); |
| 55 content::WebContentsObserver::Observe(NULL); | 79 content::WebContentsObserver::Observe(NULL); |
| 56 } | 80 } |
| 57 | 81 |
| 58 } // namespace dom_distiller | 82 } // namespace dom_distiller |
| OLD | NEW |