BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / chromeos / extensions / dictionary_event_router.cc
blob8ca8b9ca08df28fb74455445b613a1a3d6fd4400
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.
5 #include "chrome/browser/chromeos/extensions/dictionary_event_router.h"
7 #include <string>
9 #include "base/json/json_writer.h"
10 #include "base/values.h"
11 #include "chrome/browser/chromeos/extensions/input_method_api.h"
12 #include "chrome/browser/spellchecker/spellcheck_factory.h"
13 #include "content/public/browser/browser_context.h"
14 #include "extensions/browser/event_router.h"
15 #include "extensions/browser/extension_system.h"
17 namespace chromeos {
19 ExtensionDictionaryEventRouter::ExtensionDictionaryEventRouter(
20 content::BrowserContext* context)
21 : context_(context), loaded_() {
22 SpellcheckService* spellcheck = SpellcheckServiceFactory::GetForContext(
23 context_);
24 if (spellcheck) {
25 service_ = spellcheck->GetWeakPtr();
26 service_->GetCustomDictionary()->AddObserver(this);
27 loaded_ = service_->GetCustomDictionary()->IsLoaded();
31 ExtensionDictionaryEventRouter::~ExtensionDictionaryEventRouter() {
32 if (service_)
33 service_->GetCustomDictionary()->RemoveObserver(this);
36 void ExtensionDictionaryEventRouter::DispatchLoadedEventIfLoaded() {
37 if (!loaded_)
38 return;
40 extensions::EventRouter* router = extensions::EventRouter::Get(context_);
41 if (!router->HasEventListener(
42 extensions::InputMethodAPI::kOnDictionaryLoaded)) {
43 return;
46 scoped_ptr<base::ListValue> args(new base::ListValue());
47 // The router will only send the event to extensions that are listening.
48 scoped_ptr<extensions::Event> event(new extensions::Event(
49 extensions::events::INPUT_METHOD_PRIVATE_ON_DICTIONARY_LOADED,
50 extensions::InputMethodAPI::kOnDictionaryLoaded, args.Pass()));
51 event->restrict_to_browser_context = context_;
52 router->BroadcastEvent(event.Pass());
55 void ExtensionDictionaryEventRouter::OnCustomDictionaryLoaded() {
56 loaded_ = true;
57 DispatchLoadedEventIfLoaded();
60 void ExtensionDictionaryEventRouter::OnCustomDictionaryChanged(
61 const SpellcheckCustomDictionary::Change& dictionary_change) {
62 extensions::EventRouter* router = extensions::EventRouter::Get(context_);
64 if (!router->HasEventListener(
65 extensions::InputMethodAPI::kOnDictionaryChanged)) {
66 return;
69 scoped_ptr<base::ListValue> added_words(new base::ListValue());
70 for (const std::string& word : dictionary_change.to_add())
71 added_words->AppendString(word);
73 scoped_ptr<base::ListValue> removed_words(new base::ListValue());
74 for (const std::string& word : dictionary_change.to_remove())
75 removed_words->AppendString(word);
77 scoped_ptr<base::ListValue> args(new base::ListValue());
78 args->Append(added_words.release());
79 args->Append(removed_words.release());
81 // The router will only send the event to extensions that are listening.
82 scoped_ptr<extensions::Event> event(new extensions::Event(
83 extensions::events::INPUT_METHOD_PRIVATE_ON_DICTIONARY_CHANGED,
84 extensions::InputMethodAPI::kOnDictionaryChanged, args.Pass()));
85 event->restrict_to_browser_context = context_;
86 router->BroadcastEvent(event.Pass());
89 } // namespace chromeos