This source file includes following definitions.
- OnBrowserAdded
- OnBrowserRemoved
- OnBrowserSetLastActive
- ShouldCheckNetworkProfile
- CheckNetworkProfile
- SetNotificationShown
- RegisterProfilePrefs
- RecordUmaEvent
- NotifyNetworkProfileDetected
#include "chrome/browser/ui/network_profile_bubble.h"
#include <windows.h>
#include <wtsapi32.h>
#pragma comment(lib, "wtsapi32.lib")
#include "base/bind.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_service.h"
#include "base/time/time.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/user_prefs/pref_registry_syncable.h"
#include "content/public/browser/browser_thread.h"
namespace {
const int kSilenceDurationDays = 100;
const int kMaxWarnings = 2;
class BrowserListObserver : public chrome::BrowserListObserver {
private:
virtual ~BrowserListObserver();
virtual void OnBrowserAdded(Browser* browser) OVERRIDE;
virtual void OnBrowserRemoved(Browser* browser) OVERRIDE;
virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE;
};
BrowserListObserver::~BrowserListObserver() {
}
void BrowserListObserver::OnBrowserAdded(Browser* browser) {
}
void BrowserListObserver::OnBrowserRemoved(Browser* browser) {
}
void BrowserListObserver::OnBrowserSetLastActive(Browser* browser) {
NetworkProfileBubble::ShowNotification(browser);
BrowserList::RemoveObserver(this);
delete this;
}
const char kMetricNetworkedProfileCheck[] = "NetworkedProfile.Check";
}
bool NetworkProfileBubble::notification_shown_ = false;
bool NetworkProfileBubble::ShouldCheckNetworkProfile(Profile* profile) {
PrefService* prefs = profile->GetPrefs();
if (prefs->GetInteger(prefs::kNetworkProfileWarningsLeft))
return !notification_shown_;
int64 last_check = prefs->GetInt64(prefs::kNetworkProfileLastWarningTime);
base::TimeDelta time_since_last_check =
base::Time::Now() - base::Time::FromTimeT(last_check);
if (time_since_last_check.InDays() > kSilenceDurationDays) {
prefs->SetInteger(prefs::kNetworkProfileWarningsLeft, kMaxWarnings);
return !notification_shown_;
}
return false;
}
void NetworkProfileBubble::CheckNetworkProfile(
const base::FilePath& profile_folder) {
DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kNoNetworkProfileWarning)) {
RecordUmaEvent(METRIC_CHECK_SUPPRESSED);
return;
}
LPWSTR buffer = NULL;
DWORD buffer_length = 0;
if (!::WTSQuerySessionInformation(WTS_CURRENT_SERVER, WTS_CURRENT_SESSION,
WTSClientProtocolType,
&buffer, &buffer_length)) {
RecordUmaEvent(METRIC_CHECK_FAILED);
return;
}
unsigned short* type = reinterpret_cast<unsigned short*>(buffer);
if (*type == WTS_PROTOCOL_TYPE_CONSOLE) {
bool profile_on_network = false;
if (!profile_folder.empty()) {
base::FilePath temp_file;
if (base::CreateTemporaryFileInDir(profile_folder, &temp_file) &&
(base::WriteFile(temp_file, ".", 1) == 1)) {
base::FilePath normalized_temp_file;
if (!base::NormalizeFilePath(temp_file, &normalized_temp_file))
profile_on_network = true;
} else {
RecordUmaEvent(METRIC_CHECK_IO_FAILED);
}
base::DeleteFile(temp_file, false);
}
if (profile_on_network) {
RecordUmaEvent(METRIC_PROFILE_ON_NETWORK);
content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
base::Bind(&NotifyNetworkProfileDetected));
} else {
RecordUmaEvent(METRIC_PROFILE_NOT_ON_NETWORK);
}
} else {
RecordUmaEvent(METRIC_REMOTE_SESSION);
}
::WTSFreeMemory(buffer);
}
void NetworkProfileBubble::SetNotificationShown(bool shown) {
notification_shown_ = shown;
}
void NetworkProfileBubble::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterIntegerPref(
prefs::kNetworkProfileWarningsLeft,
kMaxWarnings,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
registry->RegisterInt64Pref(
prefs::kNetworkProfileLastWarningTime,
0,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}
void NetworkProfileBubble::RecordUmaEvent(MetricNetworkedProfileCheck event) {
UMA_HISTOGRAM_ENUMERATION(kMetricNetworkedProfileCheck,
event,
METRIC_NETWORKED_PROFILE_CHECK_SIZE);
}
void NetworkProfileBubble::NotifyNetworkProfileDetected() {
Browser* browser = chrome::FindLastActiveWithHostDesktopType(
chrome::GetActiveDesktop());
if (browser)
ShowNotification(browser);
else
BrowserList::AddObserver(new BrowserListObserver());
}