Commit 9396b97a authored by malaykeshav's avatar malaykeshav Committed by Commit bot

Loads device data to be passed as site context for HaTS survey.

- Loads device info including OS, platform, browser version info, locale
  and firmware.

Design Doc=go/cros-hats-eng
BUG=611781
COMPONENT=Chrome OS, HaTS

Review-Url: https://codereview.chromium.org/2153553002
Cr-Commit-Position: refs/heads/master@{#405590}
parent f380ce14
...@@ -4,9 +4,15 @@ ...@@ -4,9 +4,15 @@
#include "chrome/browser/chromeos/hats/hats_dialog.h" #include "chrome/browser/chromeos/hats/hats_dialog.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser_dialogs.h" #include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/common/pref_names.h"
#include "chromeos/system/version_loader.h"
#include "components/prefs/pref_service.h"
#include "components/version_info/version_info.h"
#include "content/public/browser/browser_thread.h"
#include "grit/browser_resources.h" #include "grit/browser_resources.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/size.h"
...@@ -28,31 +34,108 @@ constexpr char kSiteID[] = "ckvqucibdlzn2"; ...@@ -28,31 +34,108 @@ constexpr char kSiteID[] = "ckvqucibdlzn2";
// Base URL to fetch the google consumer survey script. // Base URL to fetch the google consumer survey script.
constexpr char kBaseFormatUrl[] = constexpr char kBaseFormatUrl[] =
"https://www.google.com/insights/consumersurveys/" "https://www.google.com/insights/consumersurveys/"
"async_survey?site=%s&force_https=1"; "async_survey?site=%s&force_https=1&sc=%s";
// Keyword used to join the separate device info elements into a single string
std::string LoadLocalHtmlAsString() { // to be used as site context.
const char kDeviceInfoStopKeyword[] = "STOP";
const char kDefaultProfileLocale[] = "en-US";
enum class DeviceInfoKey : unsigned int {
BROWSER = 0,
PLATFORM,
FIRMWARE,
LOCALE,
};
// Returns the local HaTS HTML file as a string with the correct Hats script
// URL.
std::string LoadLocalHtmlAsString(std::string site_id,
std::string site_context) {
std::string html_data; std::string html_data;
ResourceBundle::GetSharedInstance() ResourceBundle::GetSharedInstance()
.GetRawDataResource(IDR_HATS_HTML) .GetRawDataResource(IDR_HATS_HTML)
.CopyToString(&html_data); .CopyToString(&html_data);
size_t pos = html_data.find(kReplacementToken); size_t pos = html_data.find(kReplacementToken);
html_data.replace(pos, strlen(kReplacementToken), html_data.replace(pos, strlen(kReplacementToken),
base::StringPrintf(kBaseFormatUrl, kSiteID)); base::StringPrintf(kBaseFormatUrl, site_id.c_str(),
site_context.c_str()));
return html_data; return html_data;
} }
// Maps the given DeviceInfoKey |key| enum to the corresponding string value
// that can be used as a key when creating a URL parameter.
const std::string KeyEnumToString(DeviceInfoKey key) {
switch (key) {
case DeviceInfoKey::BROWSER:
return "browser";
case DeviceInfoKey::PLATFORM:
return "platform";
case DeviceInfoKey::FIRMWARE:
return "firmware";
case DeviceInfoKey::LOCALE:
return "locale";
default:
NOTREACHED();
return std::string();
}
}
// Must be run on a blocking thread pool.
// Gathers the browser version info, firmware info and platform info and returns
// them in a single encoded string, the format of which is defined below.
// Currently the format is "<key><value>STOP<key><value>STOP<key><value>" where
// 'STOP' is used as a token to identify the end of a key value pair. This is
// done since GCS only allows the use of alphanumeric characters to be passed as
// a site context.
std::string GetFormattedSiteContext(std::string user_locale,
base::StringPiece join_keyword) {
std::vector<std::string> pairs;
pairs.push_back(KeyEnumToString(DeviceInfoKey::BROWSER) +
version_info::GetVersionNumber());
pairs.push_back(KeyEnumToString(DeviceInfoKey::PLATFORM) +
version_loader::GetVersion(version_loader::VERSION_FULL));
pairs.push_back(KeyEnumToString(DeviceInfoKey::FIRMWARE) +
version_loader::GetFirmware());
pairs.push_back(KeyEnumToString(DeviceInfoKey::LOCALE) + user_locale);
return base::JoinString(pairs, join_keyword);
}
} // namespace } // namespace
HatsDialog::HatsDialog() : html_data_(LoadLocalHtmlAsString()) {} // static
void HatsDialog::CreateAndShow() {
Profile* profile = ProfileManager::GetActiveUserProfile();
std::string user_locale =
profile->GetPrefs()->GetString(prefs::kApplicationLocale);
if (!user_locale.length())
user_locale = kDefaultProfileLocale;
HatsDialog::~HatsDialog() {} std::unique_ptr<HatsDialog> hats_dialog(new HatsDialog);
base::PostTaskAndReplyWithResult(
content::BrowserThread::GetBlockingPool(), FROM_HERE,
base::Bind(&GetFormattedSiteContext, user_locale, kDeviceInfoStopKeyword),
base::Bind(&HatsDialog::Show, base::Passed(&hats_dialog)));
}
// static
void HatsDialog::Show(std::unique_ptr<HatsDialog> hats_dialog,
std::string site_context) {
// Load and set the html data that needs to be displayed in the dialog.
hats_dialog->html_data_ = LoadLocalHtmlAsString(kSiteID, site_context);
void HatsDialog::Show() {
chrome::ShowWebDialog( chrome::ShowWebDialog(
nullptr, ProfileManager::GetActiveUserProfile()->GetOffTheRecordProfile(), nullptr, ProfileManager::GetActiveUserProfile()->GetOffTheRecordProfile(),
this); hats_dialog.release());
} }
HatsDialog::HatsDialog() {}
HatsDialog::~HatsDialog() {}
ui::ModalType HatsDialog::GetDialogModalType() const { ui::ModalType HatsDialog::GetDialogModalType() const {
return ui::MODAL_TYPE_SYSTEM; return ui::MODAL_TYPE_SYSTEM;
} }
......
...@@ -18,12 +18,15 @@ namespace chromeos { ...@@ -18,12 +18,15 @@ namespace chromeos {
class HatsDialog : public ui::WebDialogDelegate { class HatsDialog : public ui::WebDialogDelegate {
public: public:
HatsDialog(); HatsDialog();
~HatsDialog() override;
virtual void Show(); // Creates an instance of HatsDialog and posts a task to load all the relevant
// device info before displaying the dialog.
static void CreateAndShow();
private: private:
// Object is self deleting on close. static void Show(std::unique_ptr<HatsDialog> hats_dialog,
~HatsDialog() override; std::string site_context);
// ui::WebDialogDelegate implementation. // ui::WebDialogDelegate implementation.
ui::ModalType GetDialogModalType() const override; ui::ModalType GetDialogModalType() const override;
...@@ -41,7 +44,7 @@ class HatsDialog : public ui::WebDialogDelegate { ...@@ -41,7 +44,7 @@ class HatsDialog : public ui::WebDialogDelegate {
bool ShouldShowDialogTitle() const override; bool ShouldShowDialogTitle() const override;
bool HandleContextMenu(const content::ContextMenuParams& params) override; bool HandleContextMenu(const content::ContextMenuParams& params) override;
const std::string html_data_; std::string html_data_;
DISALLOW_COPY_AND_ASSIGN(HatsDialog); DISALLOW_COPY_AND_ASSIGN(HatsDialog);
}; };
......
...@@ -135,8 +135,7 @@ void HatsNotificationController::ButtonClick(int button_index) { ...@@ -135,8 +135,7 @@ void HatsNotificationController::ButtonClick(int button_index) {
UpdateLastInteractionTime(); UpdateLastInteractionTime();
// The dialog deletes itslef on close. // The dialog deletes itslef on close.
HatsDialog* hats_dialog = new HatsDialog(); HatsDialog::CreateAndShow();
hats_dialog->Show();
// Remove the notification. // Remove the notification.
g_browser_process->notification_ui_manager()->CancelById( g_browser_process->notification_ui_manager()->CancelById(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment