Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / feedback / feedback_profile_observer.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/feedback/feedback_profile_observer.h"
6
7 #include "base/callback.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/feedback/feedback_report.h"
10 #include "chrome/browser/feedback/feedback_uploader.h"
11 #include "chrome/browser/feedback/feedback_uploader_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/notification_service.h"
16
17 using content::BrowserThread;
18
19 static base::LazyInstance<feedback::FeedbackProfileObserver>::Leaky
20     g_feedback_profile_observer = LAZY_INSTANCE_INITIALIZER;
21
22 namespace feedback {
23
24 // static
25 void FeedbackProfileObserver::Initialize() {
26   g_feedback_profile_observer.Get();
27 }
28
29 FeedbackProfileObserver::FeedbackProfileObserver() {
30   prefs_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED,
31                        content::NotificationService::AllSources());
32 }
33
34 FeedbackProfileObserver::~FeedbackProfileObserver() {
35   prefs_registrar_.RemoveAll();
36 }
37
38 void FeedbackProfileObserver::Observe(
39     int type,
40     const content::NotificationSource& source,
41     const content::NotificationDetails& details) {
42   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
43   DCHECK_EQ(chrome::NOTIFICATION_PROFILE_CREATED, type);
44
45   Profile* profile = content::Source<Profile>(source).ptr();
46   if (profile && !profile->IsOffTheRecord())
47     QueueUnsentReports(profile);
48 }
49
50 void FeedbackProfileObserver::QueueUnsentReports(
51     content::BrowserContext* context) {
52   feedback::FeedbackUploader* uploader =
53       feedback::FeedbackUploaderFactory::GetForBrowserContext(context);
54   BrowserThread::PostBlockingPoolTask(FROM_HERE,
55       base::Bind(&FeedbackReport::LoadReportsAndQueue,
56                  context->GetPath(),
57                  base::Bind(&FeedbackUploader::QueueReport,
58                             uploader->AsWeakPtr())));
59 }
60
61 }  // namespace feedback