OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/extensions/api/api_resource_event_notifier.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/json/json_writer.h" | |
9 #include "base/values.h" | |
10 #include "chrome/browser/extensions/event_router.h" | |
11 #include "chrome/browser/profiles/profile.h" | |
12 #include "content/public/browser/browser_thread.h" | |
13 | |
14 using content::BrowserThread; | |
15 | |
16 namespace extensions { | |
17 | |
18 const char kEventTypeKey[] = "type"; | |
19 | |
20 const char kSrcIdKey[] = "srcId"; | |
21 const char kIsFinalEventKey[] = "isFinalEvent"; | |
22 const char kResultCodeKey[] = "resultCode"; | |
23 | |
24 ApiResourceEventNotifier::ApiResourceEventNotifier( | |
25 EventRouter* router, | |
26 Profile* profile, | |
27 const std::string& src_extension_id, | |
28 int src_id, | |
29 const GURL& src_url) | |
30 : router_(router), | |
31 profile_(profile), | |
32 src_extension_id_(src_extension_id), | |
33 src_id_(src_id), | |
34 src_url_(src_url) { | |
35 } | |
36 | |
37 // static | |
38 std::string ApiResourceEventNotifier::ApiResourceEventTypeToString( | |
39 ApiResourceEventType event_type) { | |
40 NOTREACHED(); | |
41 return std::string(); | |
42 } | |
43 | |
44 ApiResourceEventNotifier::~ApiResourceEventNotifier() {} | |
45 | |
46 void ApiResourceEventNotifier::DispatchEvent( | |
47 const std::string& event_name, DictionaryValue* args) { | |
48 BrowserThread::PostTask( | |
49 BrowserThread::UI, FROM_HERE, | |
50 base::Bind( | |
51 &ApiResourceEventNotifier::DispatchEventOnUIThread, this, | |
52 event_name, args)); | |
53 } | |
54 | |
55 void ApiResourceEventNotifier::DispatchEventOnUIThread( | |
56 const std::string& event_name, DictionaryValue* args) { | |
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
58 | |
59 scoped_ptr<ListValue> arguments(new ListValue()); | |
60 arguments->Set(0, args); | |
61 scoped_ptr<Event> event(new Event(event_name, arguments.Pass())); | |
62 event->restrict_to_profile = profile_; | |
63 event->event_url = src_url_; | |
64 router_->DispatchEventToExtension(src_extension_id_, event.Pass()); | |
65 } | |
66 | |
67 DictionaryValue* ApiResourceEventNotifier::CreateApiResourceEvent( | |
68 ApiResourceEventType event_type) { | |
69 DictionaryValue* event = new DictionaryValue(); | |
70 event->SetString(kEventTypeKey, ApiResourceEventTypeToString(event_type)); | |
71 event->SetInteger(kSrcIdKey, src_id_); | |
72 | |
73 // TODO(miket): Signal that it's OK to clean up onEvent listeners. This is | |
74 // the framework we'll use, but we need to start using it. | |
75 event->SetBoolean(kIsFinalEventKey, false); | |
76 | |
77 // The caller owns the created event, which typically is then given to a | |
78 // ListValue to dispose of. | |
79 return event; | |
80 } | |
81 | |
82 } // namespace extensions | |
OLD | NEW |