| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/metrics/desktop_session_duration/desktop_session_durati
on_observer.h" | 5 #include "chrome/browser/metrics/desktop_session_duration/desktop_session_durati
on_observer.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" |
| 7 #include "chrome/browser/metrics/desktop_session_duration/desktop_session_durati
on_tracker.h" | 8 #include "chrome/browser/metrics/desktop_session_duration/desktop_session_durati
on_tracker.h" |
| 8 #include "content/public/browser/render_view_host.h" | 9 #include "content/public/browser/render_view_host.h" |
| 9 | 10 |
| 10 DEFINE_WEB_CONTENTS_USER_DATA_KEY(metrics::DesktopSessionDurationObserver); | 11 DEFINE_WEB_CONTENTS_USER_DATA_KEY(metrics::DesktopSessionDurationObserver); |
| 11 | 12 |
| 12 namespace metrics { | 13 namespace metrics { |
| 13 | 14 |
| 14 DesktopSessionDurationObserver::DesktopSessionDurationObserver( | 15 DesktopSessionDurationObserver::DesktopSessionDurationObserver( |
| 15 content::WebContents* web_contents, | 16 content::WebContents* web_contents, |
| 16 DesktopSessionDurationTracker* service) | 17 DesktopSessionDurationTracker* service) |
| 17 : content::WebContentsObserver(web_contents), service_(service) { | 18 : content::WebContentsObserver(web_contents), service_(service) { |
| 18 RegisterInputEventObserver(web_contents->GetRenderViewHost()); | 19 RegisterInputEventObserver(web_contents->GetRenderViewHost()); |
| 19 } | 20 } |
| 20 | 21 |
| 21 DesktopSessionDurationObserver::~DesktopSessionDurationObserver() {} | 22 DesktopSessionDurationObserver::~DesktopSessionDurationObserver() {} |
| 22 | 23 |
| 23 // static | 24 // static |
| 24 DesktopSessionDurationObserver* | 25 DesktopSessionDurationObserver* |
| 25 DesktopSessionDurationObserver::CreateForWebContents( | 26 DesktopSessionDurationObserver::CreateForWebContents( |
| 26 content::WebContents* web_contents) { | 27 content::WebContents* web_contents) { |
| 27 DCHECK(web_contents); | 28 DCHECK(web_contents); |
| 28 | 29 |
| 29 if (!DesktopSessionDurationTracker::IsInitialized()) | 30 if (!DesktopSessionDurationTracker::IsInitialized()) |
| 30 return nullptr; | 31 return nullptr; |
| 31 | 32 |
| 32 DesktopSessionDurationObserver* observer = FromWebContents(web_contents); | 33 DesktopSessionDurationObserver* observer = FromWebContents(web_contents); |
| 33 if (!observer) { | 34 if (!observer) { |
| 34 observer = new DesktopSessionDurationObserver( | 35 observer = new DesktopSessionDurationObserver( |
| 35 web_contents, DesktopSessionDurationTracker::Get()); | 36 web_contents, DesktopSessionDurationTracker::Get()); |
| 36 web_contents->SetUserData(UserDataKey(), observer); | 37 web_contents->SetUserData(UserDataKey(), base::WrapUnique(observer)); |
| 37 } | 38 } |
| 38 return observer; | 39 return observer; |
| 39 } | 40 } |
| 40 | 41 |
| 41 void DesktopSessionDurationObserver::RegisterInputEventObserver( | 42 void DesktopSessionDurationObserver::RegisterInputEventObserver( |
| 42 content::RenderViewHost* host) { | 43 content::RenderViewHost* host) { |
| 43 if (host != nullptr) | 44 if (host != nullptr) |
| 44 host->GetWidget()->AddInputEventObserver(this); | 45 host->GetWidget()->AddInputEventObserver(this); |
| 45 } | 46 } |
| 46 | 47 |
| 47 void DesktopSessionDurationObserver::UnregisterInputEventObserver( | 48 void DesktopSessionDurationObserver::UnregisterInputEventObserver( |
| 48 content::RenderViewHost* host) { | 49 content::RenderViewHost* host) { |
| 49 if (host != nullptr) | 50 if (host != nullptr) |
| 50 host->GetWidget()->RemoveInputEventObserver(this); | 51 host->GetWidget()->RemoveInputEventObserver(this); |
| 51 } | 52 } |
| 52 | 53 |
| 53 void DesktopSessionDurationObserver::OnInputEvent( | 54 void DesktopSessionDurationObserver::OnInputEvent( |
| 54 const blink::WebInputEvent& event) { | 55 const blink::WebInputEvent& event) { |
| 55 service_->OnUserEvent(); | 56 service_->OnUserEvent(); |
| 56 } | 57 } |
| 57 | 58 |
| 58 void DesktopSessionDurationObserver::RenderViewHostChanged( | 59 void DesktopSessionDurationObserver::RenderViewHostChanged( |
| 59 content::RenderViewHost* old_host, | 60 content::RenderViewHost* old_host, |
| 60 content::RenderViewHost* new_host) { | 61 content::RenderViewHost* new_host) { |
| 61 UnregisterInputEventObserver(old_host); | 62 UnregisterInputEventObserver(old_host); |
| 62 RegisterInputEventObserver(new_host); | 63 RegisterInputEventObserver(new_host); |
| 63 } | 64 } |
| 64 | 65 |
| 65 } // namespace metrics | 66 } // namespace metrics |
| OLD | NEW |