summaryrefslogtreecommitdiffstats
path: root/content
diff options
context:
space:
mode:
authorprimiano@chromium.org <primiano@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-14 10:28:26 +0000
committerprimiano@chromium.org <primiano@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-14 10:28:26 +0000
commita5274917bb29d595319e39d7303172ef2906a931 (patch)
tree2252f8ba3ae633fd01a56391c60a11276ffe6042 /content
parent42a9cf8ecbcd24ff9cac4d3c7b3fd4220d25acf6 (diff)
downloadchromium_src-a5274917bb29d595319e39d7303172ef2906a931.zip
chromium_src-a5274917bb29d595319e39d7303172ef2906a931.tar.gz
chromium_src-a5274917bb29d595319e39d7303172ef2906a931.tar.bz2
Removed use of AsWeakPtr in speech_recognition_manager_impl.cc when posting to other (FILE) threads.
Now the method posted to the FILE thread is static and does not need any reference to |this|. BUG=132369 TEST=none Review URL: https://chromiumcodereview.appspot.com/10536139 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@142121 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
-rw-r--r--content/browser/speech/speech_recognition_manager_impl.cc49
-rw-r--r--content/browser/speech/speech_recognition_manager_impl.h6
2 files changed, 30 insertions, 25 deletions
diff --git a/content/browser/speech/speech_recognition_manager_impl.cc b/content/browser/speech/speech_recognition_manager_impl.cc
index 6bad70f..47b5420 100644
--- a/content/browser/speech/speech_recognition_manager_impl.cc
+++ b/content/browser/speech/speech_recognition_manager_impl.cc
@@ -39,6 +39,14 @@ SpeechRecognitionManager* SpeechRecognitionManager::GetInstance() {
namespace {
speech::SpeechRecognitionManagerImpl* g_speech_recognition_manager_impl;
+
+void ShowAudioInputSettingsOnFileThread() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+ media::AudioManager* audio_manager = BrowserMainLoop::GetAudioManager();
+ DCHECK(audio_manager->CanShowAudioInputSettings());
+ if (audio_manager->CanShowAudioInputSettings())
+ audio_manager->ShowAudioInputSettings();
+}
} // namespace
namespace speech {
@@ -52,7 +60,8 @@ SpeechRecognitionManagerImpl::SpeechRecognitionManagerImpl()
last_session_id_(kSessionIDInvalid),
is_dispatching_event_(false),
delegate_(content::GetContentClient()->browser()->
- GetSpeechRecognitionManagerDelegate()) {
+ GetSpeechRecognitionManagerDelegate()),
+ ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
DCHECK(!g_speech_recognition_manager_impl);
g_speech_recognition_manager_impl = this;
}
@@ -117,7 +126,7 @@ void SpeechRecognitionManagerImpl::StartSession(int session_id) {
delegate_->CheckRecognitionIsAllowed(
session_id,
base::Bind(&SpeechRecognitionManagerImpl::RecognitionAllowedCallback,
- this->AsWeakPtr()));
+ weak_factory_.GetWeakPtr()));
}
void SpeechRecognitionManagerImpl::RecognitionAllowedCallback(int session_id,
@@ -125,9 +134,9 @@ void SpeechRecognitionManagerImpl::RecognitionAllowedCallback(int session_id,
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(SessionExists(session_id));
if (is_allowed) {
- BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
- this->AsWeakPtr(), session_id, EVENT_START));
+ weak_factory_.GetWeakPtr(), session_id, EVENT_START));
} else {
sessions_.erase(session_id);
}
@@ -138,9 +147,9 @@ void SpeechRecognitionManagerImpl::AbortSession(int session_id) {
if (!SessionExists(session_id))
return;
- BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
- this->AsWeakPtr(), session_id, EVENT_ABORT));
+ weak_factory_.GetWeakPtr(), session_id, EVENT_ABORT));
}
void SpeechRecognitionManagerImpl::StopAudioCaptureForSession(int session_id) {
@@ -148,9 +157,9 @@ void SpeechRecognitionManagerImpl::StopAudioCaptureForSession(int session_id) {
if (!SessionExists(session_id))
return;
- BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
- this->AsWeakPtr(), session_id, EVENT_STOP_CAPTURE));
+ weak_factory_.GetWeakPtr(), session_id, EVENT_STOP_CAPTURE));
}
// Here begins the SpeechRecognitionEventListener interface implementation,
@@ -227,9 +236,9 @@ void SpeechRecognitionManagerImpl::OnAudioEnd(int session_id) {
delegate_listener->OnAudioEnd(session_id);
if (SpeechRecognitionEventListener* listener = GetListener(session_id))
listener->OnAudioEnd(session_id);
- BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
- this->AsWeakPtr(), session_id, EVENT_AUDIO_ENDED));
+ weak_factory_.GetWeakPtr(), session_id, EVENT_AUDIO_ENDED));
}
void SpeechRecognitionManagerImpl::OnRecognitionResult(
@@ -277,9 +286,11 @@ void SpeechRecognitionManagerImpl::OnRecognitionEnd(int session_id) {
delegate_listener->OnRecognitionEnd(session_id);
if (SpeechRecognitionEventListener* listener = GetListener(session_id))
listener->OnRecognitionEnd(session_id);
- BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(&SpeechRecognitionManagerImpl::DispatchEvent,
- this->AsWeakPtr(), session_id, EVENT_RECOGNITION_ENDED));
+ weak_factory_.GetWeakPtr(),
+ session_id,
+ EVENT_RECOGNITION_ENDED));
}
int SpeechRecognitionManagerImpl::GetSession(
@@ -504,18 +515,8 @@ string16 SpeechRecognitionManagerImpl::GetAudioInputDeviceModel() {
void SpeechRecognitionManagerImpl::ShowAudioInputSettings() {
// Since AudioManager::ShowAudioInputSettings can potentially launch external
// processes, do that in the FILE thread to not block the calling threads.
- if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
- BrowserThread::PostTask(
- BrowserThread::FILE, FROM_HERE,
- base::Bind(&SpeechRecognitionManagerImpl::ShowAudioInputSettings,
- this->AsWeakPtr()));
- return;
- }
-
- media::AudioManager* audio_manager = BrowserMainLoop::GetAudioManager();
- DCHECK(audio_manager->CanShowAudioInputSettings());
- if (audio_manager->CanShowAudioInputSettings())
- audio_manager->ShowAudioInputSettings();
+ BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
+ base::Bind(&ShowAudioInputSettingsOnFileThread));
}
SpeechRecognitionManagerImpl::Session::Session()
diff --git a/content/browser/speech/speech_recognition_manager_impl.h b/content/browser/speech/speech_recognition_manager_impl.h
index 5d43dfc..37501a5 100644
--- a/content/browser/speech/speech_recognition_manager_impl.h
+++ b/content/browser/speech/speech_recognition_manager_impl.h
@@ -50,7 +50,6 @@ class SpeechRecognizerImpl;
// the catch-all snoop listener (optionally) provided by the delegate.
class CONTENT_EXPORT SpeechRecognitionManagerImpl :
public NON_EXPORTED_BASE(content::SpeechRecognitionManager),
- public base::SupportsWeakPtr<SpeechRecognitionManagerImpl>,
public content::SpeechRecognitionEventListener {
public:
// Returns the current SpeechRecognitionManagerImpl or NULL if the call is
@@ -163,6 +162,11 @@ class CONTENT_EXPORT SpeechRecognitionManagerImpl :
int last_session_id_;
bool is_dispatching_event_;
scoped_ptr<content::SpeechRecognitionManagerDelegate> delegate_;
+
+ // Used for posting asynchronous tasks (on the IO thread) without worrying
+ // about this class being destroyed in the meanwhile (due to browser shutdown)
+ // since tasks pending on a destroyed WeakPtr are automatically discarded.
+ base::WeakPtrFactory<SpeechRecognitionManagerImpl> weak_factory_;
};
} // namespace speech