Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: chrome/browser/chromeos/extensions/dictionary_event_router.cc

Issue 973213003: Adds extension events for custom dictionary load/change. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Use weakptr. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_);
22 if (spellcheck) {
23 service_ = spellcheck->GetWeakPtr();
24 service_->GetCustomDictionary()->AddObserver(this);
25 }
26 }
27
28 ExtensionDictionaryEventRouter::~ExtensionDictionaryEventRouter() {
29 if (service_) {
please use gerrit instead 2015/03/06 17:54:34 nit: no need for { and }.
Peter Wen 2015/03/06 18:46:04 Done.
30 service_->GetCustomDictionary()->RemoveObserver(this);
31 }
32 }
33
34 void ExtensionDictionaryEventRouter::OnCustomDictionaryLoaded() {
35 extensions::EventRouter* router = extensions::EventRouter::Get(context_);
36
37 if (!router->HasEventListener(
38 extensions::InputMethodAPI::kOnDictionaryLoaded)) {
39 return;
40 }
41
42 scoped_ptr<base::ListValue> args(new base::ListValue());
43 // The router will only send the event to extensions that are listening.
44 scoped_ptr<extensions::Event> event(new extensions::Event(
45 extensions::InputMethodAPI::kOnDictionaryLoaded, args.Pass()));
46 event->restrict_to_browser_context = context_;
47 router->BroadcastEvent(event.Pass());
48 }
49
50 void ExtensionDictionaryEventRouter::OnCustomDictionaryChanged(
51 const SpellcheckCustomDictionary::Change& dictionary_change) {
52 extensions::EventRouter* router = extensions::EventRouter::Get(context_);
53
54 if (!router->HasEventListener(
55 extensions::InputMethodAPI::kOnDictionaryChanged)) {
56 return;
57 }
58
59 scoped_ptr<base::ListValue> args(new base::ListValue());
60 scoped_ptr<base::ListValue> added_words(new base::ListValue());
61 scoped_ptr<base::ListValue> removed_words(new base::ListValue());
62 added_words->AppendStrings(dictionary_change.to_add());
63 removed_words->AppendStrings(dictionary_change.to_remove());
64 args->Append(added_words.release());
65 args->Append(removed_words.release());
66
67 // The router will only send the event to extensions that are listening.
68 scoped_ptr<extensions::Event> event(new extensions::Event(
69 extensions::InputMethodAPI::kOnDictionaryChanged, args.Pass()));
70 event->restrict_to_browser_context = context_;
71 router->BroadcastEvent(event.Pass());
72 }
73
74 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/extensions/dictionary_event_router.h ('k') | chrome/browser/chromeos/extensions/input_method_api.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698