af74890ab7c129271d520a6a7801c055f6cf4c31
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / app_mode / kiosk_diagnosis_runner.cc
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.
4
5 #include "chrome/browser/chromeos/app_mode/kiosk_diagnosis_runner.h"
6
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"
19
20 using feedback::FeedbackData;
21
22 namespace chromeos {
23
24 class KioskDiagnosisRunner::Factory : public BrowserContextKeyedServiceFactory {
25  public:
26   static KioskDiagnosisRunner* GetForProfile(Profile* profile) {
27     return static_cast<KioskDiagnosisRunner*>(
28         GetInstance()->GetServiceForBrowserContext(profile, true));
29   }
30
31   static Factory* GetInstance() {
32     return Singleton<Factory>::get();
33   }
34
35  private:
36   friend struct DefaultSingletonTraits<Factory>;
37
38   Factory()
39       : BrowserContextKeyedServiceFactory(
40             "KioskDiagnosisRunner",
41             BrowserContextDependencyManager::GetInstance()) {
42     DependsOn(extensions::ExtensionsBrowserClient::Get()
43                   ->GetExtensionSystemFactory());
44     DependsOn(extensions::FeedbackPrivateAPI::GetFactoryInstance());
45   }
46
47   virtual ~Factory() {}
48
49   // BrowserContextKeyedServiceFactory overrides:
50   virtual KeyedService* BuildServiceInstanceFor(
51       content::BrowserContext* context) const OVERRIDE {
52     Profile* profile = static_cast<Profile*>(context);
53     return new KioskDiagnosisRunner(profile);
54   }
55
56  private:
57   DISALLOW_COPY_AND_ASSIGN(Factory);
58 };
59
60 // static
61 void KioskDiagnosisRunner::Run(Profile* profile,
62                                const std::string& app_id) {
63   KioskDiagnosisRunner::Factory::GetForProfile(profile)->Start(app_id);
64 }
65
66 KioskDiagnosisRunner::KioskDiagnosisRunner(Profile* profile)
67     : profile_(profile),
68       weak_factory_(this) {}
69
70 KioskDiagnosisRunner::~KioskDiagnosisRunner() {}
71
72 void KioskDiagnosisRunner::Start(const std::string& app_id) {
73   app_id_ = app_id;
74
75   // Schedules system logs to be collected after 1 minute.
76   content::BrowserThread::PostDelayedTask(
77       content::BrowserThread::UI,
78       FROM_HERE,
79       base::Bind(&KioskDiagnosisRunner::StartSystemLogCollection,
80                  weak_factory_.GetWeakPtr()),
81       base::TimeDelta::FromMinutes(1));
82 }
83
84 void KioskDiagnosisRunner::StartSystemLogCollection() {
85   extensions::FeedbackService* service =
86       extensions::FeedbackPrivateAPI::GetFactoryInstance()
87           ->Get(profile_)
88           ->GetService();
89   DCHECK(service);
90
91   service->GetSystemInformation(
92       base::Bind(&KioskDiagnosisRunner::SendSysLogFeedback,
93                  weak_factory_.GetWeakPtr()));
94 }
95
96 void KioskDiagnosisRunner::SendSysLogFeedback(
97     const extensions::SystemInformationList& sys_info) {
98   scoped_refptr<FeedbackData> feedback_data(new FeedbackData());
99
100   feedback_data->set_context(profile_);
101   feedback_data->set_description(base::StringPrintf(
102       "Autogenerated feedback:\nAppId: %s\n(uniquifier:%s)",
103       app_id_.c_str(),
104       base::Int64ToString(base::Time::Now().ToInternalValue()).c_str()));
105
106   scoped_ptr<FeedbackData::SystemLogsMap> sys_logs(
107       new FeedbackData::SystemLogsMap);
108   for (extensions::SystemInformationList::const_iterator it = sys_info.begin();
109        it != sys_info.end(); ++it) {
110     (*sys_logs.get())[it->get()->key] = it->get()->value;
111   }
112   feedback_data->SetAndCompressSystemInfo(sys_logs.Pass());
113
114   extensions::FeedbackService* service =
115       extensions::FeedbackPrivateAPI::GetFactoryInstance()
116           ->Get(profile_)
117           ->GetService();
118   DCHECK(service);
119   service->SendFeedback(profile_,
120                         feedback_data,
121                         base::Bind(&KioskDiagnosisRunner::OnFeedbackSent,
122                                    weak_factory_.GetWeakPtr()));
123 }
124
125 void KioskDiagnosisRunner::OnFeedbackSent(bool) {
126   // Do nothing.
127 }
128
129 }  // namespace chromeos