summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/handoff_active_url_observer.cc
blob: 592a757408ab6233c7904badf30ee0b32485b2d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/cocoa/handoff_active_url_observer.h"

#include "base/logging.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/cocoa/handoff_active_url_observer_delegate.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "content/public/browser/web_contents.h"

HandoffActiveURLObserver::HandoffActiveURLObserver(
    HandoffActiveURLObserverDelegate* delegate)
    : delegate_(delegate),
      active_tab_strip_model_(nullptr),
      active_browser_(nullptr) {
  DCHECK(delegate_);

  active_browser_ = chrome::FindLastActiveWithHostDesktopType(
      chrome::HOST_DESKTOP_TYPE_NATIVE);
  BrowserList::AddObserver(this);
  UpdateObservations();
}

HandoffActiveURLObserver::~HandoffActiveURLObserver() {
  BrowserList::RemoveObserver(this);
  StopObservingTabStripModel();
  StopObservingWebContents();
}

void HandoffActiveURLObserver::OnBrowserSetLastActive(Browser* browser) {
  active_browser_ = browser;
  UpdateObservations();
  delegate_->HandoffActiveURLChanged(GetActiveWebContents());
}

void HandoffActiveURLObserver::OnBrowserRemoved(Browser* removed_browser) {
  if (active_browser_ != removed_browser)
    return;

  active_browser_ = chrome::FindLastActiveWithHostDesktopType(
      chrome::HOST_DESKTOP_TYPE_NATIVE);
  UpdateObservations();
  delegate_->HandoffActiveURLChanged(GetActiveWebContents());
}

void HandoffActiveURLObserver::ActiveTabChanged(
    content::WebContents* old_contents,
    content::WebContents* new_contents,
    int index,
    int reason) {
  StartObservingWebContents(new_contents);
  delegate_->HandoffActiveURLChanged(new_contents);
}

void HandoffActiveURLObserver::TabStripModelDeleted() {
  StopObservingTabStripModel();
  StopObservingWebContents();
}

void HandoffActiveURLObserver::DidNavigateMainFrame(
    const content::LoadCommittedDetails& details,
    const content::FrameNavigateParams& params) {
  delegate_->HandoffActiveURLChanged(web_contents());
}

void HandoffActiveURLObserver::WebContentsDestroyed() {
  StopObservingWebContents();
}

void HandoffActiveURLObserver::UpdateObservations() {
  if (!active_browser_) {
    StopObservingTabStripModel();
    StopObservingWebContents();
    return;
  }

  TabStripModel* model = active_browser_->tab_strip_model();
  StartObservingTabStripModel(model);

  content::WebContents* web_contents = model->GetActiveWebContents();
  if (web_contents)
    StartObservingWebContents(web_contents);
  else
    StopObservingWebContents();
}

void HandoffActiveURLObserver::StartObservingTabStripModel(
    TabStripModel* tab_strip_model) {
  DCHECK(tab_strip_model);

  if (active_tab_strip_model_ == tab_strip_model)
    return;

  StopObservingTabStripModel();
  tab_strip_model->AddObserver(this);
  active_tab_strip_model_ = tab_strip_model;
}

void HandoffActiveURLObserver::StopObservingTabStripModel() {
  if (active_tab_strip_model_) {
    active_tab_strip_model_->RemoveObserver(this);
    active_tab_strip_model_ = nullptr;
  }
}

void HandoffActiveURLObserver::StartObservingWebContents(
    content::WebContents* web_contents) {
  DCHECK(web_contents);
  Observe(web_contents);
}

void HandoffActiveURLObserver::StopObservingWebContents() {
  Observe(nullptr);
}

content::WebContents* HandoffActiveURLObserver::GetActiveWebContents() {
  if (!active_browser_)
    return nullptr;

  return active_browser_->tab_strip_model()->GetActiveWebContents();
}