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) { | |
20 SpellcheckService* spellcheck = SpellcheckServiceFactory::GetForContext( | |
21 context_); | |
bshe
2015/03/09 13:16:39
Not sure when spellcheck will be null. But is it p
Peter Wen
2015/03/09 14:56:07
If I understood correctly, spellcheck is null'ed o
| |
22 if (spellcheck) { | |
23 service_ = spellcheck->GetWeakPtr(); | |
24 service_->GetCustomDictionary()->AddObserver(this); | |
25 } | |
26 } | |
27 | |
28 ExtensionDictionaryEventRouter::~ExtensionDictionaryEventRouter() { | |
29 if (service_) | |
30 service_->GetCustomDictionary()->RemoveObserver(this); | |
31 } | |
32 | |
33 void ExtensionDictionaryEventRouter::OnCustomDictionaryLoaded() { | |
34 extensions::EventRouter* router = extensions::EventRouter::Get(context_); | |
35 | |
36 if (!router->HasEventListener( | |
37 extensions::InputMethodAPI::kOnDictionaryLoaded)) { | |
38 return; | |
39 } | |
40 | |
41 scoped_ptr<base::ListValue> args(new base::ListValue()); | |
42 // The router will only send the event to extensions that are listening. | |
43 scoped_ptr<extensions::Event> event(new extensions::Event( | |
44 extensions::InputMethodAPI::kOnDictionaryLoaded, args.Pass())); | |
45 event->restrict_to_browser_context = context_; | |
46 router->BroadcastEvent(event.Pass()); | |
47 } | |
48 | |
49 void ExtensionDictionaryEventRouter::OnCustomDictionaryChanged( | |
50 const SpellcheckCustomDictionary::Change& dictionary_change) { | |
51 extensions::EventRouter* router = extensions::EventRouter::Get(context_); | |
52 | |
53 if (!router->HasEventListener( | |
54 extensions::InputMethodAPI::kOnDictionaryChanged)) { | |
55 return; | |
56 } | |
57 | |
58 scoped_ptr<base::ListValue> args(new base::ListValue()); | |
59 scoped_ptr<base::ListValue> added_words(new base::ListValue()); | |
60 scoped_ptr<base::ListValue> removed_words(new base::ListValue()); | |
61 added_words->AppendStrings(dictionary_change.to_add()); | |
62 removed_words->AppendStrings(dictionary_change.to_remove()); | |
63 args->Append(added_words.release()); | |
64 args->Append(removed_words.release()); | |
65 | |
66 // The router will only send the event to extensions that are listening. | |
67 scoped_ptr<extensions::Event> event(new extensions::Event( | |
68 extensions::InputMethodAPI::kOnDictionaryChanged, args.Pass())); | |
69 event->restrict_to_browser_context = context_; | |
70 router->BroadcastEvent(event.Pass()); | |
71 } | |
72 | |
73 } // namespace chromeos | |
OLD | NEW |