OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/ui/cocoa/handoff_active_url_observer.h" |
| 6 |
| 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" |
| 13 |
| 14 HandoffActiveURLObserver::HandoffActiveURLObserver( |
| 15 HandoffActiveURLObserverDelegate* delegate) |
| 16 : delegate_(delegate), |
| 17 active_tab_strip_model_(nullptr), |
| 18 active_browser_(nullptr) { |
| 19 DCHECK(delegate_); |
| 20 |
| 21 active_browser_ = chrome::FindLastActiveWithHostDesktopType( |
| 22 chrome::HOST_DESKTOP_TYPE_NATIVE); |
| 23 BrowserList::AddObserver(this); |
| 24 UpdateObservations(); |
| 25 } |
| 26 |
| 27 HandoffActiveURLObserver::~HandoffActiveURLObserver() { |
| 28 BrowserList::RemoveObserver(this); |
| 29 StopObservingTabStripModel(); |
| 30 StopObservingWebContents(); |
| 31 } |
| 32 |
| 33 void HandoffActiveURLObserver::OnBrowserSetLastActive(Browser* browser) { |
| 34 active_browser_ = browser; |
| 35 UpdateObservations(); |
| 36 delegate_->HandoffActiveURLChanged(GetActiveWebContents()); |
| 37 } |
| 38 |
| 39 void HandoffActiveURLObserver::OnBrowserRemoved(Browser* removed_browser) { |
| 40 if (active_browser_ != removed_browser) |
| 41 return; |
| 42 |
| 43 active_browser_ = chrome::FindLastActiveWithHostDesktopType( |
| 44 chrome::HOST_DESKTOP_TYPE_NATIVE); |
| 45 UpdateObservations(); |
| 46 delegate_->HandoffActiveURLChanged(GetActiveWebContents()); |
| 47 } |
| 48 |
| 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); |
| 56 } |
| 57 |
| 58 void HandoffActiveURLObserver::TabStripModelDeleted() { |
| 59 StopObservingTabStripModel(); |
| 60 StopObservingWebContents(); |
| 61 } |
| 62 |
| 63 void HandoffActiveURLObserver::DidNavigateMainFrame( |
| 64 const content::LoadCommittedDetails& details, |
| 65 const content::FrameNavigateParams& params) { |
| 66 delegate_->HandoffActiveURLChanged(web_contents()); |
| 67 } |
| 68 |
| 69 void HandoffActiveURLObserver::WebContentsDestroyed() { |
| 70 StopObservingWebContents(); |
| 71 } |
| 72 |
| 73 void HandoffActiveURLObserver::UpdateObservations() { |
| 74 if (!active_browser_) { |
| 75 StopObservingTabStripModel(); |
| 76 StopObservingWebContents(); |
| 77 return; |
| 78 } |
| 79 |
| 80 TabStripModel* model = active_browser_->tab_strip_model(); |
| 81 StartObservingTabStripModel(model); |
| 82 |
| 83 content::WebContents* web_contents = model->GetActiveWebContents(); |
| 84 if (web_contents) |
| 85 StartObservingWebContents(web_contents); |
| 86 else |
| 87 StopObservingWebContents(); |
| 88 } |
| 89 |
| 90 void HandoffActiveURLObserver::StartObservingTabStripModel( |
| 91 TabStripModel* tab_strip_model) { |
| 92 DCHECK(tab_strip_model); |
| 93 |
| 94 if (active_tab_strip_model_ == tab_strip_model) |
| 95 return; |
| 96 |
| 97 StopObservingTabStripModel(); |
| 98 tab_strip_model->AddObserver(this); |
| 99 active_tab_strip_model_ = tab_strip_model; |
| 100 } |
| 101 |
| 102 void HandoffActiveURLObserver::StopObservingTabStripModel() { |
| 103 if (active_tab_strip_model_) { |
| 104 active_tab_strip_model_->RemoveObserver(this); |
| 105 active_tab_strip_model_ = nullptr; |
| 106 } |
| 107 } |
| 108 |
| 109 void HandoffActiveURLObserver::StartObservingWebContents( |
| 110 content::WebContents* web_contents) { |
| 111 DCHECK(web_contents); |
| 112 Observe(web_contents); |
| 113 } |
| 114 |
| 115 void HandoffActiveURLObserver::StopObservingWebContents() { |
| 116 Observe(nullptr); |
| 117 } |
| 118 |
| 119 content::WebContents* HandoffActiveURLObserver::GetActiveWebContents() { |
| 120 if (!active_browser_) |
| 121 return nullptr; |
| 122 |
| 123 return active_browser_->tab_strip_model()->GetActiveWebContents(); |
| 124 } |
OLD | NEW |