Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // 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.
| |
| 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/chromeos/extensions/dictionary_event_router.h" | |
| 6 | |
| 7 #include "base/json/json_writer.h" | |
| 8 #include "base/values.h" | |
| 9 #include "chrome/browser/chromeos/extensions/input_method_api.h" | |
| 10 #include "chrome/browser/spellchecker/spellcheck_factory.h" | |
| 11 #include "chrome/browser/spellchecker/spellcheck_service.h" | |
| 12 #include "content/public/browser/browser_context.h" | |
| 13 #include "extensions/browser/event_router.h" | |
| 14 #include "extensions/browser/extension_system.h" | |
| 15 | |
| 16 namespace chromeos { | |
| 17 | |
| 18 ExtensionDictionaryEventRouter::ExtensionDictionaryEventRouter( | |
| 19 content::BrowserContext* context) | |
| 20 : context_(context), | |
| 21 dictionary_(nullptr) { | |
| 22 SpellcheckService* spellcheck = SpellcheckServiceFactory::GetForContext( | |
| 23 context); | |
| 24 if (spellcheck) { | |
| 25 dictionary_ = spellcheck->GetCustomDictionary(); | |
| 26 dictionary_->AddObserver(this); | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 ExtensionDictionaryEventRouter::~ExtensionDictionaryEventRouter() { | |
| 31 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
| |
| 32 dictionary_->RemoveObserver(this); | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 void ExtensionDictionaryEventRouter::OnCustomDictionaryLoaded() { | |
| 37 extensions::EventRouter* router = extensions::EventRouter::Get(context_); | |
| 38 | |
| 39 if (!router->HasEventListener( | |
| 40 extensions::InputMethodAPI::kOnDictionaryLoaded)) { | |
| 41 return; | |
| 42 } | |
| 43 | |
| 44 scoped_ptr<base::ListValue> args(new base::ListValue()); | |
| 45 // The router will only send the event to extensions that are listening. | |
| 46 scoped_ptr<extensions::Event> event(new extensions::Event( | |
| 47 extensions::InputMethodAPI::kOnDictionaryLoaded, args.Pass())); | |
| 48 event->restrict_to_browser_context = context_; | |
| 49 router->BroadcastEvent(event.Pass()); | |
| 50 } | |
| 51 | |
| 52 void ExtensionDictionaryEventRouter::OnCustomDictionaryChanged( | |
| 53 const SpellcheckCustomDictionary::Change& dictionary_change) { | |
| 54 extensions::EventRouter* router = extensions::EventRouter::Get(context_); | |
| 55 | |
| 56 if (!router->HasEventListener( | |
| 57 extensions::InputMethodAPI::kOnDictionaryChanged)) { | |
| 58 return; | |
| 59 } | |
| 60 | |
| 61 scoped_ptr<base::ListValue> args(new base::ListValue()); | |
| 62 scoped_ptr<base::ListValue> added_words(new base::ListValue()); | |
| 63 scoped_ptr<base::ListValue> removed_words(new base::ListValue()); | |
| 64 added_words->AppendStrings(dictionary_change.to_add()); | |
| 65 removed_words->AppendStrings(dictionary_change.to_remove()); | |
| 66 args->Append(added_words.release()); | |
| 67 args->Append(removed_words.release()); | |
| 68 | |
| 69 // The router will only send the event to extensions that are listening. | |
| 70 scoped_ptr<extensions::Event> event(new extensions::Event( | |
| 71 extensions::InputMethodAPI::kOnDictionaryChanged, args.Pass())); | |
| 72 event->restrict_to_browser_context = context_; | |
| 73 router->BroadcastEvent(event.Pass()); | |
| 74 } | |
| 75 | |
| 76 } // namespace chromeos | |
| OLD | NEW |