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/profiles/profile.h" 10 #include "components/feedback/feedback_report.h" 11 #include "components/feedback/feedback_uploader.h" 12 #include "components/feedback/feedback_uploader_factory.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::QueueSingleReport( 51 feedback::FeedbackUploader* uploader, 52 const std::string& data) { 53 BrowserThread::PostTask( 54 BrowserThread::UI, FROM_HERE, base::Bind(&FeedbackUploader::QueueReport, 55 uploader->AsWeakPtr(), data)); 56 } 57 58 void FeedbackProfileObserver::QueueUnsentReports( 59 content::BrowserContext* context) { 60 feedback::FeedbackUploader* uploader = 61 feedback::FeedbackUploaderFactory::GetForBrowserContext(context); 62 BrowserThread::PostBlockingPoolTask(FROM_HERE, 63 base::Bind( 64 &FeedbackReport::LoadReportsAndQueue, 65 uploader->GetFeedbackReportsPath(), 66 base::Bind(&FeedbackProfileObserver::QueueSingleReport, uploader))); 67 } 68 69 } // namespace feedback 70