summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_speech_input_manager.cc
blob: fb4ae7914a14e41810b385373894655f3d3e57d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/extension_speech_input_manager.h"

#include "chrome/browser/profiles/profile_dependency_manager.h"
#include "chrome/browser/profiles/profile_keyed_service.h"
#include "chrome/browser/profiles/profile_keyed_service_factory.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/extensions/extension.h"
#include "content/common/notification_service.h"

namespace {

// Wrap an ExtensionSpeechInputManager using scoped_refptr to avoid
// assertion failures on destruction because of not using release().
class ExtensionSpeechInputManagerWrapper : public ProfileKeyedService {
 public:
  explicit ExtensionSpeechInputManagerWrapper(
      ExtensionSpeechInputManager* manager)
      : manager_(manager) {}

  virtual ~ExtensionSpeechInputManagerWrapper() {}

  ExtensionSpeechInputManager* manager() const { return manager_.get(); }

 private:
  // Methods from ProfileKeyedService.
  virtual void Shutdown() OVERRIDE {
    manager()->ShutdownOnUIThread();
  }

  scoped_refptr<ExtensionSpeechInputManager> manager_;
};

}

// Factory for ExtensionSpeechInputManagers as profile keyed services.
class ExtensionSpeechInputManager::Factory : public ProfileKeyedServiceFactory {
 public:
  static void Initialize();
  static Factory* GetInstance();

  ExtensionSpeechInputManagerWrapper* GetForProfile(Profile* profile);

 private:
  friend struct DefaultSingletonTraits<Factory>;

  Factory();
  virtual ~Factory();

  // ProfileKeyedServiceFactory methods:
  virtual ProfileKeyedService* BuildServiceInstanceFor(
      Profile* profile) const;
  virtual bool ServiceRedirectedInIncognito() { return false; }
  virtual bool ServiceIsNULLWhileTesting() { return true; }
  virtual bool ServiceIsCreatedWithProfile() { return true; }

  DISALLOW_COPY_AND_ASSIGN(Factory);
};

void ExtensionSpeechInputManager::Factory::Initialize() {
  GetInstance();
}

ExtensionSpeechInputManager::Factory*
    ExtensionSpeechInputManager::Factory::GetInstance() {
  return Singleton<ExtensionSpeechInputManager::Factory>::get();
}

ExtensionSpeechInputManagerWrapper*
    ExtensionSpeechInputManager::Factory::GetForProfile(
    Profile* profile) {
  return static_cast<ExtensionSpeechInputManagerWrapper*>(
      GetServiceForProfile(profile, true));
}

ExtensionSpeechInputManager::Factory::Factory()
    : ProfileKeyedServiceFactory(ProfileDependencyManager::GetInstance()) {
}

ExtensionSpeechInputManager::Factory::~Factory() {
}

ProfileKeyedService*
    ExtensionSpeechInputManager::Factory::BuildServiceInstanceFor(
    Profile* profile) const {
  scoped_refptr<ExtensionSpeechInputManager> manager(
      new ExtensionSpeechInputManager(profile));
  return new ExtensionSpeechInputManagerWrapper(manager);
}

ExtensionSpeechInputManager::ExtensionSpeechInputManager(Profile* profile)
    : profile_(profile),
      extension_(NULL) {
  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
                 Source<Profile>(profile_));
}

ExtensionSpeechInputManager::~ExtensionSpeechInputManager() {
}

ExtensionSpeechInputManager* ExtensionSpeechInputManager::GetForProfile(
    Profile* profile) {
  ExtensionSpeechInputManagerWrapper *wrapper =
      Factory::GetInstance()->GetForProfile(profile);
  if (!wrapper)
    return NULL;
  return wrapper->manager();
}

void ExtensionSpeechInputManager::InitializeFactory() {
  Factory::Initialize();
}

void ExtensionSpeechInputManager::Observe(int type,
    const NotificationSource& source,
    const NotificationDetails& details) {
  if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED)
    ExtensionUnloaded(Details<UnloadedExtensionInfo>(details)->extension->id());
  else
    NOTREACHED();
}

void ExtensionSpeechInputManager::ShutdownOnUIThread() {
  // TODO(leandrogracia): Force stop to speech recognition if active.
  registrar_.RemoveAll();
  profile_ = NULL;
}

void ExtensionSpeechInputManager::ExtensionUnloaded(const std::string& id) {
  // TODO(leandrogracia): Force stop to speech recognition if the extension
  // is currently using the API.
}

void ExtensionSpeechInputManager::Start(const Extension *extension) {
  // TODO(leandrogracia): Start speech recognition.
}

void ExtensionSpeechInputManager::Stop(const Extension *extension) {
  // TODO(leandrogracia): Stop speech recognition.
}