Revert of Remove OneClickSigninHelper since it is no longer used. (patchset #5 id...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / handoff_active_url_observer.cc
blob592a757408ab6233c7904badf30ee0b32485b2d2
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/cocoa/handoff_active_url_observer.h"
7 #include "base/logging.h"
8 #include "chrome/browser/ui/browser_finder.h"
9 #include "chrome/browser/ui/browser_list.h"
10 #include "chrome/browser/ui/cocoa/handoff_active_url_observer_delegate.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "content/public/browser/web_contents.h"
14 HandoffActiveURLObserver::HandoffActiveURLObserver(
15 HandoffActiveURLObserverDelegate* delegate)
16 : delegate_(delegate),
17 active_tab_strip_model_(nullptr),
18 active_browser_(nullptr) {
19 DCHECK(delegate_);
21 active_browser_ = chrome::FindLastActiveWithHostDesktopType(
22 chrome::HOST_DESKTOP_TYPE_NATIVE);
23 BrowserList::AddObserver(this);
24 UpdateObservations();
27 HandoffActiveURLObserver::~HandoffActiveURLObserver() {
28 BrowserList::RemoveObserver(this);
29 StopObservingTabStripModel();
30 StopObservingWebContents();
33 void HandoffActiveURLObserver::OnBrowserSetLastActive(Browser* browser) {
34 active_browser_ = browser;
35 UpdateObservations();
36 delegate_->HandoffActiveURLChanged(GetActiveWebContents());
39 void HandoffActiveURLObserver::OnBrowserRemoved(Browser* removed_browser) {
40 if (active_browser_ != removed_browser)
41 return;
43 active_browser_ = chrome::FindLastActiveWithHostDesktopType(
44 chrome::HOST_DESKTOP_TYPE_NATIVE);
45 UpdateObservations();
46 delegate_->HandoffActiveURLChanged(GetActiveWebContents());
49 void HandoffActiveURLObserver::ActiveTabChanged(
50 content::WebContents* old_contents,
51 content::WebContents* new_contents,
52 int index,
53 int reason) {
54 StartObservingWebContents(new_contents);
55 delegate_->HandoffActiveURLChanged(new_contents);
58 void HandoffActiveURLObserver::TabStripModelDeleted() {
59 StopObservingTabStripModel();
60 StopObservingWebContents();
63 void HandoffActiveURLObserver::DidNavigateMainFrame(
64 const content::LoadCommittedDetails& details,
65 const content::FrameNavigateParams& params) {
66 delegate_->HandoffActiveURLChanged(web_contents());
69 void HandoffActiveURLObserver::WebContentsDestroyed() {
70 StopObservingWebContents();
73 void HandoffActiveURLObserver::UpdateObservations() {
74 if (!active_browser_) {
75 StopObservingTabStripModel();
76 StopObservingWebContents();
77 return;
80 TabStripModel* model = active_browser_->tab_strip_model();
81 StartObservingTabStripModel(model);
83 content::WebContents* web_contents = model->GetActiveWebContents();
84 if (web_contents)
85 StartObservingWebContents(web_contents);
86 else
87 StopObservingWebContents();
90 void HandoffActiveURLObserver::StartObservingTabStripModel(
91 TabStripModel* tab_strip_model) {
92 DCHECK(tab_strip_model);
94 if (active_tab_strip_model_ == tab_strip_model)
95 return;
97 StopObservingTabStripModel();
98 tab_strip_model->AddObserver(this);
99 active_tab_strip_model_ = tab_strip_model;
102 void HandoffActiveURLObserver::StopObservingTabStripModel() {
103 if (active_tab_strip_model_) {
104 active_tab_strip_model_->RemoveObserver(this);
105 active_tab_strip_model_ = nullptr;
109 void HandoffActiveURLObserver::StartObservingWebContents(
110 content::WebContents* web_contents) {
111 DCHECK(web_contents);
112 Observe(web_contents);
115 void HandoffActiveURLObserver::StopObservingWebContents() {
116 Observe(nullptr);
119 content::WebContents* HandoffActiveURLObserver::GetActiveWebContents() {
120 if (!active_browser_)
121 return nullptr;
123 return active_browser_->tab_strip_model()->GetActiveWebContents();