OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 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/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 "content/public/browser/browser_context.h" |
| 12 #include "extensions/browser/event_router.h" |
| 13 #include "extensions/browser/extension_system.h" |
| 14 |
| 15 namespace chromeos { |
| 16 |
| 17 ExtensionDictionaryEventRouter::ExtensionDictionaryEventRouter( |
| 18 content::BrowserContext* context) |
| 19 : context_(context), loaded_() { |
| 20 SpellcheckService* spellcheck = SpellcheckServiceFactory::GetForContext( |
| 21 context_); |
| 22 if (spellcheck) { |
| 23 service_ = spellcheck->GetWeakPtr(); |
| 24 service_->GetCustomDictionary()->AddObserver(this); |
| 25 loaded_ = service_->GetCustomDictionary()->IsLoaded(); |
| 26 } |
| 27 } |
| 28 |
| 29 ExtensionDictionaryEventRouter::~ExtensionDictionaryEventRouter() { |
| 30 if (service_) |
| 31 service_->GetCustomDictionary()->RemoveObserver(this); |
| 32 } |
| 33 |
| 34 void ExtensionDictionaryEventRouter::DispatchLoadedEventIfLoaded() { |
| 35 if (!loaded_) |
| 36 return; |
| 37 |
| 38 extensions::EventRouter* router = extensions::EventRouter::Get(context_); |
| 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::OnCustomDictionaryLoaded() { |
| 53 loaded_ = true; |
| 54 DispatchLoadedEventIfLoaded(); |
| 55 } |
| 56 |
| 57 void ExtensionDictionaryEventRouter::OnCustomDictionaryChanged( |
| 58 const SpellcheckCustomDictionary::Change& dictionary_change) { |
| 59 extensions::EventRouter* router = extensions::EventRouter::Get(context_); |
| 60 |
| 61 if (!router->HasEventListener( |
| 62 extensions::InputMethodAPI::kOnDictionaryChanged)) { |
| 63 return; |
| 64 } |
| 65 |
| 66 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 67 scoped_ptr<base::ListValue> added_words(new base::ListValue()); |
| 68 scoped_ptr<base::ListValue> removed_words(new base::ListValue()); |
| 69 added_words->AppendStrings(dictionary_change.to_add()); |
| 70 removed_words->AppendStrings(dictionary_change.to_remove()); |
| 71 args->Append(added_words.release()); |
| 72 args->Append(removed_words.release()); |
| 73 |
| 74 // The router will only send the event to extensions that are listening. |
| 75 scoped_ptr<extensions::Event> event(new extensions::Event( |
| 76 extensions::InputMethodAPI::kOnDictionaryChanged, args.Pass())); |
| 77 event->restrict_to_browser_context = context_; |
| 78 router->BroadcastEvent(event.Pass()); |
| 79 } |
| 80 |
| 81 } // namespace chromeos |
OLD | NEW |