Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / chromeos / app_mode / kiosk_diagnosis_runner.cc
blob59631ea0c6535585e446ea1e26c835e8f1f201fc
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.
5 #include "chrome/browser/chromeos/app_mode/kiosk_diagnosis_runner.h"
7 #include "base/bind.h"
8 #include "base/memory/singleton.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/time/time.h"
12 #include "chrome/browser/extensions/api/feedback_private/feedback_private_api.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "components/keyed_service/content/browser_context_dependency_manager.h"
15 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "extensions/browser/extension_system_provider.h"
18 #include "extensions/browser/extensions_browser_client.h"
20 using feedback::FeedbackData;
22 namespace chromeos {
24 class KioskDiagnosisRunner::Factory : public BrowserContextKeyedServiceFactory {
25 public:
26 static KioskDiagnosisRunner* GetForProfile(Profile* profile) {
27 return static_cast<KioskDiagnosisRunner*>(
28 GetInstance()->GetServiceForBrowserContext(profile, true));
31 static Factory* GetInstance() { return base::Singleton<Factory>::get(); }
33 private:
34 friend struct base::DefaultSingletonTraits<Factory>;
36 Factory()
37 : BrowserContextKeyedServiceFactory(
38 "KioskDiagnosisRunner",
39 BrowserContextDependencyManager::GetInstance()) {
40 DependsOn(extensions::ExtensionsBrowserClient::Get()
41 ->GetExtensionSystemFactory());
42 DependsOn(extensions::FeedbackPrivateAPI::GetFactoryInstance());
45 ~Factory() override {}
47 // BrowserContextKeyedServiceFactory overrides:
48 KeyedService* BuildServiceInstanceFor(
49 content::BrowserContext* context) const override {
50 Profile* profile = static_cast<Profile*>(context);
51 return new KioskDiagnosisRunner(profile);
54 private:
55 DISALLOW_COPY_AND_ASSIGN(Factory);
58 // static
59 void KioskDiagnosisRunner::Run(Profile* profile,
60 const std::string& app_id) {
61 KioskDiagnosisRunner::Factory::GetForProfile(profile)->Start(app_id);
64 KioskDiagnosisRunner::KioskDiagnosisRunner(Profile* profile)
65 : profile_(profile),
66 weak_factory_(this) {}
68 KioskDiagnosisRunner::~KioskDiagnosisRunner() {}
70 void KioskDiagnosisRunner::Start(const std::string& app_id) {
71 app_id_ = app_id;
73 // Schedules system logs to be collected after 1 minute.
74 content::BrowserThread::PostDelayedTask(
75 content::BrowserThread::UI,
76 FROM_HERE,
77 base::Bind(&KioskDiagnosisRunner::StartSystemLogCollection,
78 weak_factory_.GetWeakPtr()),
79 base::TimeDelta::FromMinutes(1));
82 void KioskDiagnosisRunner::StartSystemLogCollection() {
83 extensions::FeedbackService* service =
84 extensions::FeedbackPrivateAPI::GetFactoryInstance()
85 ->Get(profile_)
86 ->GetService();
87 DCHECK(service);
89 service->GetSystemInformation(
90 base::Bind(&KioskDiagnosisRunner::SendSysLogFeedback,
91 weak_factory_.GetWeakPtr()));
94 void KioskDiagnosisRunner::SendSysLogFeedback(
95 const extensions::SystemInformationList& sys_info) {
96 scoped_refptr<FeedbackData> feedback_data(new FeedbackData());
98 feedback_data->set_context(profile_);
99 feedback_data->set_description(base::StringPrintf(
100 "Autogenerated feedback:\nAppId: %s\n(uniquifier:%s)",
101 app_id_.c_str(),
102 base::Int64ToString(base::Time::Now().ToInternalValue()).c_str()));
104 scoped_ptr<FeedbackData::SystemLogsMap> sys_logs(
105 new FeedbackData::SystemLogsMap);
106 for (extensions::SystemInformationList::const_iterator it = sys_info.begin();
107 it != sys_info.end(); ++it) {
108 (*sys_logs.get())[it->get()->key] = it->get()->value;
110 feedback_data->SetAndCompressSystemInfo(sys_logs.Pass());
112 extensions::FeedbackService* service =
113 extensions::FeedbackPrivateAPI::GetFactoryInstance()
114 ->Get(profile_)
115 ->GetService();
116 DCHECK(service);
117 service->SendFeedback(profile_,
118 feedback_data,
119 base::Bind(&KioskDiagnosisRunner::OnFeedbackSent,
120 weak_factory_.GetWeakPtr()));
123 void KioskDiagnosisRunner::OnFeedbackSent(bool) {
124 // Do nothing.
127 } // namespace chromeos