Chromium Code Reviews| Index: chrome/browser/chromeos/extensions/dictionary_event_router.cc |
| diff --git a/chrome/browser/chromeos/extensions/dictionary_event_router.cc b/chrome/browser/chromeos/extensions/dictionary_event_router.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f2c6a9d08f401dee45da12499c84d99920949aac |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/extensions/dictionary_event_router.cc |
| @@ -0,0 +1,76 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
|
please use gerrit instead
2015/03/05 21:43:33
2015
Peter Wen
2015/03/06 16:09:09
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/extensions/dictionary_event_router.h" |
| + |
| +#include "base/json/json_writer.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/chromeos/extensions/input_method_api.h" |
| +#include "chrome/browser/spellchecker/spellcheck_factory.h" |
| +#include "chrome/browser/spellchecker/spellcheck_service.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "extensions/browser/event_router.h" |
| +#include "extensions/browser/extension_system.h" |
| + |
| +namespace chromeos { |
| + |
| +ExtensionDictionaryEventRouter::ExtensionDictionaryEventRouter( |
| + content::BrowserContext* context) |
| + : context_(context), |
| + dictionary_(nullptr) { |
| + SpellcheckService* spellcheck = SpellcheckServiceFactory::GetForContext( |
| + context); |
| + if (spellcheck) { |
| + dictionary_ = spellcheck->GetCustomDictionary(); |
| + dictionary_->AddObserver(this); |
| + } |
| +} |
| + |
| +ExtensionDictionaryEventRouter::~ExtensionDictionaryEventRouter() { |
| + if (dictionary_) { |
|
please use gerrit instead
2015/03/05 21:42:51
No need to save "dictionary_". SpellcheckService c
Peter Wen
2015/03/06 16:09:09
I changed it to use base::WeakPtr for SpellcheckSe
please use gerrit instead
2015/03/06 17:54:34
Sorry I meant SupportsWeakPtr, like this for examp
|
| + dictionary_->RemoveObserver(this); |
| + } |
| +} |
| + |
| +void ExtensionDictionaryEventRouter::OnCustomDictionaryLoaded() { |
| + extensions::EventRouter* router = extensions::EventRouter::Get(context_); |
| + |
| + if (!router->HasEventListener( |
| + extensions::InputMethodAPI::kOnDictionaryLoaded)) { |
| + return; |
| + } |
| + |
| + scoped_ptr<base::ListValue> args(new base::ListValue()); |
| + // The router will only send the event to extensions that are listening. |
| + scoped_ptr<extensions::Event> event(new extensions::Event( |
| + extensions::InputMethodAPI::kOnDictionaryLoaded, args.Pass())); |
| + event->restrict_to_browser_context = context_; |
| + router->BroadcastEvent(event.Pass()); |
| +} |
| + |
| +void ExtensionDictionaryEventRouter::OnCustomDictionaryChanged( |
| + const SpellcheckCustomDictionary::Change& dictionary_change) { |
| + extensions::EventRouter* router = extensions::EventRouter::Get(context_); |
| + |
| + if (!router->HasEventListener( |
| + extensions::InputMethodAPI::kOnDictionaryChanged)) { |
| + return; |
| + } |
| + |
| + scoped_ptr<base::ListValue> args(new base::ListValue()); |
| + scoped_ptr<base::ListValue> added_words(new base::ListValue()); |
| + scoped_ptr<base::ListValue> removed_words(new base::ListValue()); |
| + added_words->AppendStrings(dictionary_change.to_add()); |
| + removed_words->AppendStrings(dictionary_change.to_remove()); |
| + args->Append(added_words.release()); |
| + args->Append(removed_words.release()); |
| + |
| + // The router will only send the event to extensions that are listening. |
| + scoped_ptr<extensions::Event> event(new extensions::Event( |
| + extensions::InputMethodAPI::kOnDictionaryChanged, args.Pass())); |
| + event->restrict_to_browser_context = context_; |
| + router->BroadcastEvent(event.Pass()); |
| +} |
| + |
| +} // namespace chromeos |