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/app/app_api.h" | |
6 | |
7 #include "base/time.h" | |
8 #include "base/values.h" | |
9 #include "chrome/browser/extensions/app_notification_manager.h" | |
10 #include "chrome/browser/extensions/extension_service.h" | |
11 #include "chrome/browser/extensions/extension_system.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/common/extensions/extension_constants.h" | |
14 #include "googleurl/src/gurl.h" | |
15 | |
16 namespace { | |
17 | |
18 const char kBodyTextKey[] = "bodyText"; | |
19 const char kExtensionIdKey[] = "extensionId"; | |
20 const char kLinkTextKey[] = "linkText"; | |
21 const char kLinkUrlKey[] = "linkUrl"; | |
22 const char kTitleKey[] = "title"; | |
23 | |
24 const char kInvalidExtensionIdError[] = | |
25 "Invalid extension id"; | |
26 const char kMissingLinkTextError[] = | |
27 "You must specify linkText if you use linkUrl"; | |
28 | |
29 } // anonymous namespace | |
30 | |
31 namespace extensions { | |
32 | |
33 bool AppNotifyFunction::RunImpl() { | |
34 if (!include_incognito() && profile_->IsOffTheRecord()) { | |
35 error_ = extension_misc::kAppNotificationsIncognitoError; | |
36 return false; | |
37 } | |
38 | |
39 DictionaryValue* details; | |
40 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | |
41 EXTENSION_FUNCTION_VALIDATE(details != NULL); | |
42 | |
43 // TODO(asargent) remove this before the API leaves experimental. | |
44 std::string id = extension_id(); | |
45 if (details->HasKey(kExtensionIdKey)) { | |
46 EXTENSION_FUNCTION_VALIDATE(details->GetString(kExtensionIdKey, &id)); | |
47 if (!extensions::ExtensionSystem::Get(profile())->extension_service()-> | |
48 GetExtensionById(id, true)) { | |
49 error_ = kInvalidExtensionIdError; | |
50 return false; | |
51 } | |
52 } | |
53 | |
54 std::string title; | |
55 if (details->HasKey(kTitleKey)) | |
56 EXTENSION_FUNCTION_VALIDATE(details->GetString(kTitleKey, &title)); | |
57 | |
58 std::string body; | |
59 if (details->HasKey(kBodyTextKey)) | |
60 EXTENSION_FUNCTION_VALIDATE(details->GetString(kBodyTextKey, &body)); | |
61 | |
62 scoped_ptr<AppNotification> item(new AppNotification( | |
63 true, base::Time::Now(), "", id, title, body)); | |
64 | |
65 if (details->HasKey(kLinkUrlKey)) { | |
66 std::string link_url; | |
67 EXTENSION_FUNCTION_VALIDATE(details->GetString(kLinkUrlKey, &link_url)); | |
68 item->set_link_url(GURL(link_url)); | |
69 if (!item->link_url().is_valid()) { | |
70 error_ = "Invalid url: " + link_url; | |
71 return false; | |
72 } | |
73 if (!details->HasKey(kLinkTextKey)) { | |
74 error_ = kMissingLinkTextError; | |
75 return false; | |
76 } | |
77 std::string link_text; | |
78 EXTENSION_FUNCTION_VALIDATE(details->GetString(kLinkTextKey, | |
79 &link_text)); | |
80 item->set_link_text(link_text); | |
81 } | |
82 | |
83 AppNotificationManager* manager = extensions::ExtensionSystem::Get( | |
84 profile())->extension_service()->app_notification_manager(); | |
85 | |
86 // TODO(beaudoin) We should probably report an error if Add returns false. | |
87 manager->Add(item.release()); | |
88 | |
89 return true; | |
90 } | |
91 | |
92 bool AppClearAllNotificationsFunction::RunImpl() { | |
93 if (!include_incognito() && profile_->IsOffTheRecord()) { | |
94 error_ = extension_misc::kAppNotificationsIncognitoError; | |
95 return false; | |
96 } | |
97 | |
98 std::string id = extension_id(); | |
99 DictionaryValue* details = NULL; | |
100 if (args_->GetDictionary(0, &details) && details->HasKey(kExtensionIdKey)) { | |
101 EXTENSION_FUNCTION_VALIDATE(details->GetString(kExtensionIdKey, &id)); | |
102 if (!extensions::ExtensionSystem::Get(profile())->extension_service()-> | |
103 GetExtensionById(id, true)) { | |
104 error_ = kInvalidExtensionIdError; | |
105 return false; | |
106 } | |
107 } | |
108 | |
109 AppNotificationManager* manager = extensions::ExtensionSystem::Get( | |
110 profile())->extension_service()->app_notification_manager(); | |
111 manager->ClearAll(id); | |
112 return true; | |
113 } | |
114 | |
115 } // namespace extensions | |
OLD | NEW |