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/app_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/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
9 #include "base/time.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/app_notification_manager.h" | |
13 #include "chrome/browser/extensions/event_router.h" | 12 #include "chrome/browser/extensions/event_router.h" |
14 #include "chrome/browser/extensions/extension_service.h" | |
15 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/common/chrome_notification_types.h" | |
17 #include "chrome/common/extensions/extension.h" | 14 #include "chrome/common/extensions/extension.h" |
18 #include "chrome/common/extensions/extension_constants.h" | |
19 #include "content/public/browser/notification_service.h" | |
20 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
21 #include "webkit/glue/web_intent_data.h" | 16 #include "webkit/glue/web_intent_data.h" |
22 | 17 |
23 namespace { | 18 namespace { |
24 | 19 |
25 const char kBodyTextKey[] = "bodyText"; | 20 const char kOnLaunchedEvent[] = "app.runtime.onLaunched"; |
26 const char kExtensionIdKey[] = "extensionId"; | |
27 const char kLinkTextKey[] = "linkText"; | |
28 const char kLinkUrlKey[] = "linkUrl"; | |
29 const char kTitleKey[] = "title"; | |
30 | |
31 const char kInvalidExtensionIdError[] = | |
32 "Invalid extension id"; | |
33 const char kMissingLinkTextError[] = | |
34 "You must specify linkText if you use linkUrl"; | |
35 const char kOnLaunchedEvent[] = "experimental.app.onLaunched"; | |
36 | 21 |
37 } // anonymous namespace | 22 } // anonymous namespace |
38 | 23 |
39 namespace extensions { | 24 namespace extensions { |
40 | 25 |
41 bool AppNotifyFunction::RunImpl() { | |
42 if (!include_incognito() && profile_->IsOffTheRecord()) { | |
43 error_ = extension_misc::kAppNotificationsIncognitoError; | |
44 return false; | |
45 } | |
46 | |
47 DictionaryValue* details; | |
48 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | |
49 EXTENSION_FUNCTION_VALIDATE(details != NULL); | |
50 | |
51 // TODO(asargent) remove this before the API leaves experimental. | |
52 std::string id = extension_id(); | |
53 if (details->HasKey(kExtensionIdKey)) { | |
54 EXTENSION_FUNCTION_VALIDATE(details->GetString(kExtensionIdKey, &id)); | |
55 if (!profile()->GetExtensionService()->GetExtensionById(id, true)) { | |
56 error_ = kInvalidExtensionIdError; | |
57 return false; | |
58 } | |
59 } | |
60 | |
61 std::string title; | |
62 if (details->HasKey(kTitleKey)) | |
63 EXTENSION_FUNCTION_VALIDATE(details->GetString(kTitleKey, &title)); | |
64 | |
65 std::string body; | |
66 if (details->HasKey(kBodyTextKey)) | |
67 EXTENSION_FUNCTION_VALIDATE(details->GetString(kBodyTextKey, &body)); | |
68 | |
69 scoped_ptr<AppNotification> item(new AppNotification( | |
70 true, base::Time::Now(), "", id, title, body)); | |
71 | |
72 if (details->HasKey(kLinkUrlKey)) { | |
73 std::string link_url; | |
74 EXTENSION_FUNCTION_VALIDATE(details->GetString(kLinkUrlKey, &link_url)); | |
75 item->set_link_url(GURL(link_url)); | |
76 if (!item->link_url().is_valid()) { | |
77 error_ = "Invalid url: " + link_url; | |
78 return false; | |
79 } | |
80 if (!details->HasKey(kLinkTextKey)) { | |
81 error_ = kMissingLinkTextError; | |
82 return false; | |
83 } | |
84 std::string link_text; | |
85 EXTENSION_FUNCTION_VALIDATE(details->GetString(kLinkTextKey, | |
86 &link_text)); | |
87 item->set_link_text(link_text); | |
88 } | |
89 | |
90 AppNotificationManager* manager = | |
91 profile()->GetExtensionService()->app_notification_manager(); | |
92 | |
93 // TODO(beaudoin) We should probably report an error if Add returns false. | |
94 manager->Add(item.release()); | |
95 | |
96 return true; | |
97 } | |
98 | |
99 bool AppClearAllNotificationsFunction::RunImpl() { | |
100 if (!include_incognito() && profile_->IsOffTheRecord()) { | |
101 error_ = extension_misc::kAppNotificationsIncognitoError; | |
102 return false; | |
103 } | |
104 | |
105 std::string id = extension_id(); | |
106 DictionaryValue* details = NULL; | |
107 if (args_->GetDictionary(0, &details) && details->HasKey(kExtensionIdKey)) { | |
108 EXTENSION_FUNCTION_VALIDATE(details->GetString(kExtensionIdKey, &id)); | |
109 if (!profile()->GetExtensionService()->GetExtensionById(id, true)) { | |
110 error_ = kInvalidExtensionIdError; | |
111 return false; | |
112 } | |
113 } | |
114 | |
115 AppNotificationManager* manager = | |
116 profile()->GetExtensionService()->app_notification_manager(); | |
117 manager->ClearAll(id); | |
118 return true; | |
119 } | |
120 | |
121 // static. | 26 // static. |
122 void AppEventRouter::DispatchOnLaunchedEvent( | 27 void AppEventRouter::DispatchOnLaunchedEvent( |
123 Profile* profile, const Extension* extension) { | 28 Profile* profile, const Extension* extension) { |
124 scoped_ptr<ListValue> arguments(new ListValue()); | 29 scoped_ptr<ListValue> arguments(new ListValue()); |
125 profile->GetExtensionEventRouter()->DispatchEventToExtension( | 30 profile->GetExtensionEventRouter()->DispatchEventToExtension( |
126 extension->id(), kOnLaunchedEvent, arguments.Pass(), NULL, GURL()); | 31 extension->id(), kOnLaunchedEvent, arguments.Pass(), NULL, GURL()); |
127 } | 32 } |
128 | 33 |
129 // static. | 34 // static. |
130 void AppEventRouter::DispatchOnLaunchedEventWithFileEntry( | 35 void AppEventRouter::DispatchOnLaunchedEventWithFileEntry( |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 break; | 83 break; |
179 default: | 84 default: |
180 NOTREACHED(); | 85 NOTREACHED(); |
181 break; | 86 break; |
182 } | 87 } |
183 profile->GetExtensionEventRouter()->DispatchEventToExtension( | 88 profile->GetExtensionEventRouter()->DispatchEventToExtension( |
184 extension->id(), kOnLaunchedEvent, args.Pass(), NULL, GURL()); | 89 extension->id(), kOnLaunchedEvent, args.Pass(), NULL, GURL()); |
185 } | 90 } |
186 | 91 |
187 } // namespace extensions | 92 } // namespace extensions |
OLD | NEW |