OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h" | 5 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h" |
6 | 6 |
7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
8 #include "base/string16.h" | 8 #include "base/string16.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" | 12 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" |
| 13 #include "chrome/browser/extensions/event_names.h" |
13 #include "chrome/browser/extensions/event_router.h" | 14 #include "chrome/browser/extensions/event_router.h" |
14 #include "chrome/browser/extensions/extension_system.h" | 15 #include "chrome/browser/extensions/extension_system.h" |
15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/common/extensions/extension.h" | 17 #include "chrome/common/extensions/extension.h" |
17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
18 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
19 | 20 |
20 namespace extensions { | 21 namespace extensions { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 const char kOnLaunchedEvent[] = "app.runtime.onLaunched"; | 25 using event_names::kOnLaunched; |
25 const char kOnRestartedEvent[] = "app.runtime.onRestarted"; | 26 using event_names::kOnRestarted; |
26 | 27 |
27 void DispatchOnLaunchedEventImpl(const std::string& extension_id, | 28 void DispatchOnLaunchedEventImpl(const std::string& extension_id, |
28 scoped_ptr<base::ListValue> args, | 29 scoped_ptr<base::ListValue> args, |
29 Profile* profile) { | 30 Profile* profile) { |
30 extensions::ExtensionSystem* system = | 31 extensions::ExtensionSystem* system = |
31 extensions::ExtensionSystem::Get(profile); | 32 extensions::ExtensionSystem::Get(profile); |
32 // Special case: normally, extensions add their own lazy event listeners. | 33 // Special case: normally, extensions add their own lazy event listeners. |
33 // However, since the extension might have just been enabled, it hasn't had a | 34 // However, since the extension might have just been enabled, it hasn't had a |
34 // chance to register for events. So we register on its behalf. If the | 35 // chance to register for events. So we register on its behalf. If the |
35 // extension does not actually have a listener, the event will just be | 36 // extension does not actually have a listener, the event will just be |
36 // ignored (but an app that doesn't listen for the onLaunched event doesn't | 37 // ignored (but an app that doesn't listen for the onLaunched event doesn't |
37 // make sense anyway). | 38 // make sense anyway). |
38 system->event_router()->AddLazyEventListener(kOnLaunchedEvent, extension_id); | 39 system->event_router()->AddLazyEventListener(kOnLaunched, extension_id); |
39 scoped_ptr<Event> event(new Event(kOnLaunchedEvent, args.Pass())); | 40 scoped_ptr<Event> event(new Event(kOnLaunched, args.Pass())); |
40 event->restrict_to_profile = profile; | 41 event->restrict_to_profile = profile; |
41 system->event_router()->DispatchEventToExtension(extension_id, event.Pass()); | 42 system->event_router()->DispatchEventToExtension(extension_id, event.Pass()); |
42 system->event_router()->RemoveLazyEventListener(kOnLaunchedEvent, | 43 system->event_router()->RemoveLazyEventListener(kOnLaunched, extension_id); |
43 extension_id); | |
44 } | 44 } |
45 | 45 |
46 } // anonymous namespace | 46 } // anonymous namespace |
47 | 47 |
48 // static. | 48 // static. |
49 void AppEventRouter::DispatchOnLaunchedEvent( | 49 void AppEventRouter::DispatchOnLaunchedEvent( |
50 Profile* profile, const Extension* extension) { | 50 Profile* profile, const Extension* extension) { |
51 scoped_ptr<ListValue> arguments(new ListValue()); | 51 scoped_ptr<ListValue> arguments(new ListValue()); |
52 DispatchOnLaunchedEventImpl(extension->id(), arguments.Pass(), profile); | 52 DispatchOnLaunchedEventImpl(extension->id(), arguments.Pass(), profile); |
53 } | 53 } |
(...skipping 13 matching lines...) Expand all Loading... |
67 const Extension* extension, | 67 const Extension* extension, |
68 const std::vector<app_file_handler_util::GrantedFileEntry>& file_entries) { | 68 const std::vector<app_file_handler_util::GrantedFileEntry>& file_entries) { |
69 ListValue* file_entries_list = new ListValue(); | 69 ListValue* file_entries_list = new ListValue(); |
70 for (std::vector<extensions::app_file_handler_util::GrantedFileEntry> | 70 for (std::vector<extensions::app_file_handler_util::GrantedFileEntry> |
71 ::const_iterator it = file_entries.begin(); it != file_entries.end(); | 71 ::const_iterator it = file_entries.begin(); it != file_entries.end(); |
72 ++it) { | 72 ++it) { |
73 file_entries_list->Append(DictionaryFromSavedFileEntry(*it)); | 73 file_entries_list->Append(DictionaryFromSavedFileEntry(*it)); |
74 } | 74 } |
75 scoped_ptr<ListValue> arguments(new ListValue()); | 75 scoped_ptr<ListValue> arguments(new ListValue()); |
76 arguments->Append(file_entries_list); | 76 arguments->Append(file_entries_list); |
77 scoped_ptr<Event> event(new Event(kOnRestartedEvent, arguments.Pass())); | 77 scoped_ptr<Event> event(new Event(kOnRestarted, arguments.Pass())); |
78 event->restrict_to_profile = profile; | 78 event->restrict_to_profile = profile; |
79 extensions::ExtensionSystem::Get(profile)->event_router()-> | 79 extensions::ExtensionSystem::Get(profile)->event_router()-> |
80 DispatchEventToExtension(extension->id(), event.Pass()); | 80 DispatchEventToExtension(extension->id(), event.Pass()); |
81 } | 81 } |
82 | 82 |
83 // static. | 83 // static. |
84 void AppEventRouter::DispatchOnLaunchedEventWithFileEntry( | 84 void AppEventRouter::DispatchOnLaunchedEventWithFileEntry( |
85 Profile* profile, const Extension* extension, | 85 Profile* profile, const Extension* extension, |
86 const std::string& handler_id, const std::string& mime_type, | 86 const std::string& handler_id, const std::string& mime_type, |
87 const std::string& file_system_id, const std::string& base_name) { | 87 const std::string& file_system_id, const std::string& base_name) { |
88 scoped_ptr<ListValue> args(new ListValue()); | 88 scoped_ptr<ListValue> args(new ListValue()); |
89 DictionaryValue* launch_data = new DictionaryValue(); | 89 DictionaryValue* launch_data = new DictionaryValue(); |
90 launch_data->SetString("id", handler_id); | 90 launch_data->SetString("id", handler_id); |
91 DictionaryValue* launch_item = new DictionaryValue; | 91 DictionaryValue* launch_item = new DictionaryValue; |
92 launch_item->SetString("fileSystemId", file_system_id); | 92 launch_item->SetString("fileSystemId", file_system_id); |
93 launch_item->SetString("baseName", base_name); | 93 launch_item->SetString("baseName", base_name); |
94 launch_item->SetString("mimeType", mime_type); | 94 launch_item->SetString("mimeType", mime_type); |
95 ListValue* items = new ListValue; | 95 ListValue* items = new ListValue; |
96 items->Append(launch_item); | 96 items->Append(launch_item); |
97 launch_data->Set("items", items); | 97 launch_data->Set("items", items); |
98 args->Append(launch_data); | 98 args->Append(launch_data); |
99 DispatchOnLaunchedEventImpl(extension->id(), args.Pass(), profile); | 99 DispatchOnLaunchedEventImpl(extension->id(), args.Pass(), profile); |
100 } | 100 } |
101 | 101 |
102 } // namespace extensions | 102 } // namespace extensions |
OLD | NEW |