OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/browser/google/google_search_counter_android.h" | 5 #include "chrome/browser/ui/exclusive_access/exclusive_access_controller_base.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
8 #include "chrome/browser/google/google_search_counter.h" | 8 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/prerender/prerender_manager.h" | 9 #include "chrome/browser/ui/browser.h" |
10 #include "chrome/browser/prerender/prerender_manager_factory.h" | 10 #include "chrome/browser/ui/browser_window.h" |
11 #include "components/google/core/browser/google_search_metrics.h" | |
12 #include "content/public/browser/navigation_details.h" | 11 #include "content/public/browser/navigation_details.h" |
13 #include "content/public/browser/navigation_entry.h" | 12 #include "content/public/browser/navigation_entry.h" |
14 #include "content/public/browser/notification_service.h" | 13 #include "content/public/browser/notification_service.h" |
15 #include "content/public/browser/notification_types.h" | 14 #include "content/public/browser/web_contents.h" |
16 | 15 |
17 GoogleSearchCounterAndroid::GoogleSearchCounterAndroid(Profile* profile) | 16 using content::WebContents; |
18 : profile_(profile) { | 17 |
19 // We always listen for all COMMITTED navigations from all sources, as any | 18 ExclusiveAccessControllerBase::ExclusiveAccessControllerBase( |
20 // one of them could be a navigation of interest. | 19 ExclusiveAccessManager* manager, |
21 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 20 Browser* browser) |
22 content::NotificationService::AllSources()); | 21 : manager_(manager), |
| 22 browser_(browser), |
| 23 profile_(browser->profile()), |
| 24 tab_with_exclusive_access_(nullptr) { |
| 25 DCHECK(profile_); |
23 } | 26 } |
24 | 27 |
25 GoogleSearchCounterAndroid::~GoogleSearchCounterAndroid() { | 28 ExclusiveAccessControllerBase::~ExclusiveAccessControllerBase() { |
26 } | 29 } |
27 | 30 |
28 void GoogleSearchCounterAndroid::ProcessCommittedEntry( | 31 GURL ExclusiveAccessControllerBase::GetExclusiveAccessBubbleURL() const { |
29 const content::NotificationSource& source, | 32 return manager_->GetExclusiveAccessBubbleURL(); |
30 const content::NotificationDetails& details) { | |
31 GoogleSearchCounter* counter = GoogleSearchCounter::GetInstance(); | |
32 DCHECK(counter); | |
33 if (!counter->ShouldRecordCommittedDetails(details)) | |
34 return; | |
35 | |
36 const content::NavigationEntry& entry = | |
37 *content::Details<content::LoadCommittedDetails>(details)->entry; | |
38 prerender::PrerenderManager* prerender_manager = | |
39 prerender::PrerenderManagerFactory::GetForProfile(profile_); | |
40 // |prerender_manager| is NULL when prerendering is disabled. | |
41 bool prerender_enabled = | |
42 prerender_manager ? prerender_manager->IsEnabled() : false; | |
43 counter->search_metrics()->RecordAndroidGoogleSearch( | |
44 counter->GetGoogleSearchAccessPointForSearchNavEntry(entry), | |
45 prerender_enabled); | |
46 } | 33 } |
47 | 34 |
48 void GoogleSearchCounterAndroid::Observe( | 35 GURL ExclusiveAccessControllerBase::GetURLForExclusiveAccessBubble() const { |
| 36 if (tab_with_exclusive_access_) |
| 37 return tab_with_exclusive_access_->GetURL(); |
| 38 return GURL(); |
| 39 } |
| 40 |
| 41 void ExclusiveAccessControllerBase::OnTabDeactivated( |
| 42 WebContents* web_contents) { |
| 43 if (web_contents == tab_with_exclusive_access_) |
| 44 ExitExclusiveAccessIfNecessary(); |
| 45 } |
| 46 |
| 47 void ExclusiveAccessControllerBase::OnTabDetachedFromView( |
| 48 WebContents* old_contents) { |
| 49 // Derived class will have to implement if necessary. |
| 50 } |
| 51 |
| 52 void ExclusiveAccessControllerBase::OnTabClosing(WebContents* web_contents) { |
| 53 if (web_contents == tab_with_exclusive_access_) { |
| 54 ExitExclusiveAccessIfNecessary(); |
| 55 |
| 56 // The call to exit exclusive access may result in asynchronous notification |
| 57 // of state change (e.g. fullscreen change on Linux). We don't want to rely |
| 58 // on it to call NotifyTabExclusiveAccessLost(), because at that point |
| 59 // |tab_with_exclusive_access_| may not be valid. Instead, we call it here |
| 60 // to clean up exclusive access tab related state. |
| 61 NotifyTabExclusiveAccessLost(); |
| 62 } |
| 63 } |
| 64 |
| 65 void ExclusiveAccessControllerBase::Observe( |
49 int type, | 66 int type, |
50 const content::NotificationSource& source, | 67 const content::NotificationSource& source, |
51 const content::NotificationDetails& details) { | 68 const content::NotificationDetails& details) { |
52 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); | 69 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); |
53 ProcessCommittedEntry(source, details); | 70 if (content::Details<content::LoadCommittedDetails>(details) |
| 71 ->is_navigation_to_different_page()) |
| 72 ExitExclusiveAccessIfNecessary(); |
54 } | 73 } |
| 74 |
| 75 void ExclusiveAccessControllerBase::SetTabWithExclusiveAccess( |
| 76 WebContents* tab) { |
| 77 // Tab should never be replaced with another tab, or |
| 78 // UpdateNotificationRegistrations would need updating. |
| 79 DCHECK(tab_with_exclusive_access_ == tab || |
| 80 tab_with_exclusive_access_ == nullptr || tab == nullptr); |
| 81 tab_with_exclusive_access_ = tab; |
| 82 UpdateNotificationRegistrations(); |
| 83 } |
| 84 |
| 85 void ExclusiveAccessControllerBase::UpdateNotificationRegistrations() { |
| 86 if (tab_with_exclusive_access_ && registrar_.IsEmpty()) { |
| 87 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 88 content::Source<content::NavigationController>( |
| 89 &tab_with_exclusive_access_->GetController())); |
| 90 } else if (!tab_with_exclusive_access_ && !registrar_.IsEmpty()) { |
| 91 registrar_.RemoveAll(); |
| 92 } |
| 93 } |
OLD | NEW |