Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: components/dom_distiller/content/browser/distillability_driver.cc

Issue 2326913003: Privatize StrongBinding lifetime management (Closed)
Patch Set: rebase Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "mojo/public/cpp/bindings/strong_binding.h"
11 #include "services/shell/public/cpp/interface_registry.h" 12 #include "services/shell/public/cpp/interface_registry.h"
12 13
13 DEFINE_WEB_CONTENTS_USER_DATA_KEY( 14 DEFINE_WEB_CONTENTS_USER_DATA_KEY(
14 dom_distiller::DistillabilityDriver); 15 dom_distiller::DistillabilityDriver);
15 16
16 namespace dom_distiller { 17 namespace dom_distiller {
17 18
18 // Implementation of the Mojo DistillabilityService. This is called by the 19 // Implementation of the Mojo DistillabilityService. This is called by the
19 // renderer to notify the browser that a page is distillable. 20 // renderer to notify the browser that a page is distillable.
20 class DistillabilityServiceImpl : public mojom::DistillabilityService { 21 class DistillabilityServiceImpl : public mojom::DistillabilityService {
21 public: 22 public:
22 DistillabilityServiceImpl( 23 explicit DistillabilityServiceImpl(
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 : distillability_driver_(distillability_driver) {}
26 distillability_driver_(distillability_driver) {}
27 26
28 ~DistillabilityServiceImpl() override { 27 ~DistillabilityServiceImpl() override {
29 if (!distillability_driver_) return; 28 if (!distillability_driver_) return;
30 distillability_driver_->SetNeedsMojoSetup(); 29 distillability_driver_->SetNeedsMojoSetup();
31 } 30 }
32 31
33 void NotifyIsDistillable(bool is_distillable, bool is_last_update) override { 32 void NotifyIsDistillable(bool is_distillable, bool is_last_update) override {
34 if (!distillability_driver_) return; 33 if (!distillability_driver_) return;
35 distillability_driver_->OnDistillability(is_distillable, is_last_update); 34 distillability_driver_->OnDistillability(is_distillable, is_last_update);
36 } 35 }
37 36
38 private: 37 private:
39 mojo::StrongBinding<mojom::DistillabilityService> binding_;
40 base::WeakPtr<DistillabilityDriver> distillability_driver_; 38 base::WeakPtr<DistillabilityDriver> distillability_driver_;
41 }; 39 };
42 40
43 DistillabilityDriver::DistillabilityDriver( 41 DistillabilityDriver::DistillabilityDriver(
44 content::WebContents* web_contents) 42 content::WebContents* web_contents)
45 : content::WebContentsObserver(web_contents), 43 : content::WebContentsObserver(web_contents),
46 mojo_needs_setup_(true), 44 mojo_needs_setup_(true),
47 weak_factory_(this) { 45 weak_factory_(this) {
48 if (!web_contents) return; 46 if (!web_contents) return;
49 SetupMojoService(web_contents->GetMainFrame()); 47 SetupMojoService(web_contents->GetMainFrame());
50 } 48 }
51 49
52 DistillabilityDriver::~DistillabilityDriver() { 50 DistillabilityDriver::~DistillabilityDriver() {
53 content::WebContentsObserver::Observe(nullptr); 51 content::WebContentsObserver::Observe(nullptr);
54 } 52 }
55 53
56 void DistillabilityDriver::CreateDistillabilityService( 54 void DistillabilityDriver::CreateDistillabilityService(
57 mojo::InterfaceRequest<mojom::DistillabilityService> request) { 55 mojo::InterfaceRequest<mojom::DistillabilityService> request) {
58 new DistillabilityServiceImpl(std::move(request), weak_factory_.GetWeakPtr()); 56 mojo::MakeStrongBinding(
57 base::MakeUnique<DistillabilityServiceImpl>(weak_factory_.GetWeakPtr()),
58 std::move(request));
59 } 59 }
60 60
61 void DistillabilityDriver::SetDelegate( 61 void DistillabilityDriver::SetDelegate(
62 const base::Callback<void(bool, bool)>& delegate) { 62 const base::Callback<void(bool, bool)>& delegate) {
63 m_delegate_ = delegate; 63 m_delegate_ = delegate;
64 } 64 }
65 65
66 void DistillabilityDriver::OnDistillability( 66 void DistillabilityDriver::OnDistillability(
67 bool distillable, bool is_last) { 67 bool distillable, bool is_last) {
68 if (m_delegate_.is_null()) return; 68 if (m_delegate_.is_null()) return;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 return; 100 return;
101 } 101 }
102 102
103 frame_host->GetInterfaceRegistry()->AddInterface( 103 frame_host->GetInterfaceRegistry()->AddInterface(
104 base::Bind(&DistillabilityDriver::CreateDistillabilityService, 104 base::Bind(&DistillabilityDriver::CreateDistillabilityService,
105 weak_factory_.GetWeakPtr())); 105 weak_factory_.GetWeakPtr()));
106 mojo_needs_setup_ = false; 106 mojo_needs_setup_ = false;
107 } 107 }
108 108
109 } // namespace dom_distiller 109 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698