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

Side by Side Diff: chrome/browser/extensions/api/management/management_api.cc

Issue 10834160: Use JSON schema compiler in chrome.management code (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 4 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
« no previous file with comments | « no previous file | chrome/common/extensions/api/api.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/management/management_api.h" 5 #include "chrome/browser/extensions/api/management/management_api.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 #include <vector>
9 10
10 #include "base/basictypes.h" 11 #include "base/basictypes.h"
11 #include "base/bind.h" 12 #include "base/bind.h"
12 #include "base/json/json_writer.h" 13 #include "base/json/json_writer.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/scoped_ptr.h"
13 #include "base/metrics/histogram.h" 16 #include "base/metrics/histogram.h"
14 #include "base/string_number_conversions.h" 17 #include "base/string_number_conversions.h"
15 #include "base/string_util.h" 18 #include "base/string_util.h"
19 #include "base/utf_string_conversions.h"
16 #include "chrome/browser/extensions/api/management/management_api_constants.h" 20 #include "chrome/browser/extensions/api/management/management_api_constants.h"
17 #include "chrome/browser/extensions/event_names.h" 21 #include "chrome/browser/extensions/event_names.h"
18 #include "chrome/browser/extensions/event_router.h" 22 #include "chrome/browser/extensions/event_router.h"
19 #include "chrome/browser/extensions/extension_service.h" 23 #include "chrome/browser/extensions/extension_service.h"
20 #include "chrome/browser/extensions/extension_system.h" 24 #include "chrome/browser/extensions/extension_system.h"
21 #include "chrome/browser/extensions/extension_uninstall_dialog.h" 25 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
22 #include "chrome/browser/extensions/management_policy.h" 26 #include "chrome/browser/extensions/management_policy.h"
23 #include "chrome/browser/profiles/profile.h" 27 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/ui/extensions/application_launch.h" 28 #include "chrome/browser/ui/extensions/application_launch.h"
25 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" 29 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
26 #include "chrome/common/chrome_notification_types.h" 30 #include "chrome/common/chrome_notification_types.h"
27 #include "chrome/common/chrome_utility_messages.h" 31 #include "chrome/common/chrome_utility_messages.h"
32 #include "chrome/common/extensions/api/management.h"
28 #include "chrome/common/extensions/extension.h" 33 #include "chrome/common/extensions/extension.h"
29 #include "chrome/common/extensions/extension_constants.h" 34 #include "chrome/common/extensions/extension_constants.h"
30 #include "chrome/common/extensions/extension_error_utils.h" 35 #include "chrome/common/extensions/extension_error_utils.h"
31 #include "chrome/common/extensions/extension_icon_set.h" 36 #include "chrome/common/extensions/extension_icon_set.h"
32 #include "chrome/common/extensions/permissions/permission_set.h" 37 #include "chrome/common/extensions/permissions/permission_set.h"
33 #include "chrome/common/extensions/url_pattern.h" 38 #include "chrome/common/extensions/url_pattern.h"
34 #include "content/public/browser/notification_details.h" 39 #include "content/public/browser/notification_details.h"
35 #include "content/public/browser/notification_source.h" 40 #include "content/public/browser/notification_source.h"
36 #include "content/public/browser/utility_process_host.h" 41 #include "content/public/browser/utility_process_host.h"
37 #include "content/public/browser/utility_process_host_client.h" 42 #include "content/public/browser/utility_process_host_client.h"
38 43
39 #if !defined(OS_ANDROID) 44 #if !defined(OS_ANDROID)
40 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" 45 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
41 #endif 46 #endif
42 47
43 using base::IntToString; 48 using base::IntToString;
44 using content::BrowserThread; 49 using content::BrowserThread;
45 using content::UtilityProcessHost; 50 using content::UtilityProcessHost;
46 using content::UtilityProcessHostClient; 51 using content::UtilityProcessHostClient;
52 using extensions::api::management::ExtensionInfo;
53 using extensions::api::management::IconInfo;
47 using extensions::Extension; 54 using extensions::Extension;
55 using extensions::ExtensionSystem;
48 using extensions::PermissionMessages; 56 using extensions::PermissionMessages;
49 57
58 namespace Get = extensions::api::management::Get;
Aaron Boodman 2012/08/09 13:17:19 I think it would be more clear to just do: using
mitchellwrosen 2012/08/09 18:15:41 Do you mean using namespace extensions::api::manag
Aaron Boodman 2012/08/10 06:12:36 I meant: using extensions::management. So you'd
mitchellwrosen 2012/08/10 18:10:13 Oh, I gotcha. namespace management = extensions::a
59 namespace GetAll = extensions::api::management::GetAll;
60 namespace GetPermissionWarningsById =
61 extensions::api::management::GetPermissionWarningsById;
62 namespace GetPermissionWarningsByManifest =
63 extensions::api::management::GetPermissionWarningsByManifest;
64 namespace LaunchApp = extensions::api::management::LaunchApp;
65 namespace OnDisabled = extensions::api::management::OnDisabled;
66 namespace OnEnabled = extensions::api::management::OnEnabled;
67 namespace OnInstalled = extensions::api::management::OnInstalled;
68 namespace OnUninstalled = extensions::api::management::OnUninstalled;
69 namespace SetEnabled = extensions::api::management::SetEnabled;
70 namespace Uninstall = extensions::api::management::Uninstall;
71
50 namespace events = extensions::event_names; 72 namespace events = extensions::event_names;
51 namespace keys = extension_management_api_constants; 73 namespace keys = extension_management_api_constants;
52 74
53 namespace { 75 namespace {
54 76
77 typedef std::vector<linked_ptr<ExtensionInfo> > ExtensionInfoList;
Aaron Boodman 2012/08/09 13:17:19 ScopedVector<T> is now preferred over std::vector<
mitchellwrosen 2012/08/09 18:15:41 Hm. For now, the JSC uses std::vector<std::linked_
Aaron Boodman 2012/08/10 06:12:36 Ah, makes sense. Nevermind.
78 typedef std::vector<linked_ptr<IconInfo> > IconInfoList;
79
55 enum AutoConfirmForTest { 80 enum AutoConfirmForTest {
56 DO_NOT_SKIP = 0, 81 DO_NOT_SKIP = 0,
57 PROCEED, 82 PROCEED,
58 ABORT 83 ABORT
59 }; 84 };
60 85
61 AutoConfirmForTest auto_confirm_for_test = DO_NOT_SKIP; 86 AutoConfirmForTest auto_confirm_for_test = DO_NOT_SKIP;
62 87
88 void CreateWarningsList(const Extension* extension,
Aaron Boodman 2012/08/09 13:17:19 You can just return std::vector<std::string>. It's
mitchellwrosen 2012/08/09 18:15:41 Done.
89 std::vector<std::string>* warnings_list) {
90 PermissionMessages warnings = extension->GetPermissionMessages();
91 for (PermissionMessages::const_iterator iter = warnings.begin();
92 iter != warnings.end(); ++iter) {
93 warnings_list->push_back(UTF16ToUTF8(iter->message()));
94 }
95 }
96
63 } // namespace 97 } // namespace
64 98
65 ExtensionService* ExtensionManagementFunction::service() { 99 ExtensionService* ExtensionManagementFunction::service() {
66 return profile()->GetExtensionService(); 100 return profile()->GetExtensionService();
67 } 101 }
68 102
69 ExtensionService* AsyncExtensionManagementFunction::service() { 103 ExtensionService* AsyncExtensionManagementFunction::service() {
70 return profile()->GetExtensionService(); 104 return profile()->GetExtensionService();
71 } 105 }
72 106
73 static DictionaryValue* CreateExtensionInfo(const Extension& extension, 107 static scoped_ptr<ExtensionInfo> CreateExtensionInfo(
Aaron Boodman 2012/08/09 13:17:19 Can you move this into the anonymous namespace? We
mitchellwrosen 2012/08/09 18:15:41 Done.
74 ExtensionService* service) { 108 const Extension& extension,
75 DictionaryValue* info = new DictionaryValue(); 109 ExtensionService* service) {
76 bool enabled = service->IsExtensionEnabled(extension.id()); 110 scoped_ptr<ExtensionInfo> info(new ExtensionInfo());
77 extension.GetBasicInfo(enabled, info);
78 111
79 const extensions::ManagementPolicy* policy = extensions::ExtensionSystem::Get( 112 info->id = extension.id();
80 service->profile())->management_policy(); 113 info->name = extension.name();
81 info->SetBoolean(keys::kMayDisableKey, 114 info->enabled = service->IsExtensionEnabled(info->id);
82 policy->UserMayModifySettings(&extension, NULL)); 115 info->offline_enabled = extension.offline_enabled();
116 info->version = extension.VersionString();
117 info->description = extension.description();
118 info->options_url = extension.options_url().possibly_invalid_spec();
Aaron Boodman 2012/08/09 13:17:19 I think just spec() will do? Same thing below.
mitchellwrosen 2012/08/09 18:15:41 Done.
119 info->homepage_url.reset(new std::string(
120 extension.GetHomepageURL().possibly_invalid_spec()));
121 info->may_disable = ExtensionSystem::Get(service->profile())->
Aaron Boodman 2012/08/09 13:17:19 Pass ExtensionSystem* instead into this function.
mitchellwrosen 2012/08/09 18:15:41 Done.
122 management_policy()->UserMayModifySettings(&extension, NULL);
123 info->is_app = extension.is_app();
83 124
84 info->SetBoolean(keys::kIsAppKey, extension.is_app()); 125 if (!info->enabled) {
Aaron Boodman 2012/08/09 13:17:19 Flip this check to put the positive branch first.
mitchellwrosen 2012/08/09 18:15:41 Done.
85
86 if (!enabled) {
87 extensions::ExtensionPrefs* prefs = service->extension_prefs(); 126 extensions::ExtensionPrefs* prefs = service->extension_prefs();
88 bool permissions_escalated = 127 if (prefs->DidExtensionEscalatePermissions(extension.id())) {
89 prefs->DidExtensionEscalatePermissions(extension.id()); 128 info->disabled_reason =
90 const char* reason = permissions_escalated ? 129 ExtensionInfo::DISABLED_REASON_PERMISSIONS_INCREASE;
91 keys::kDisabledReasonPermissionsIncrease : keys::kDisabledReasonUnknown; 130 } else {
92 info->SetString(keys::kDisabledReasonKey, reason); 131 info->disabled_reason = ExtensionInfo::DISABLED_REASON_UNKNOWN;
132 }
133 } else {
134 info->disabled_reason = ExtensionInfo::DISABLED_REASON_NONE;
93 } 135 }
94 136
95 if (!extension.update_url().is_empty()) 137 if (!extension.update_url().is_empty()) {
96 info->SetString(keys::kUpdateUrlKey, 138 info->update_url.reset(new std::string(
97 extension.update_url().possibly_invalid_spec()); 139 extension.update_url().possibly_invalid_spec()));
98 if (extension.is_app()) 140 }
99 info->SetString(keys::kAppLaunchUrlKey, 141
100 extension.GetFullLaunchURL().possibly_invalid_spec()); 142 if (extension.is_app()) {
143 info->app_launch_url.reset(new std::string(
144 extension.GetFullLaunchURL().possibly_invalid_spec()));
145 }
101 146
102 const ExtensionIconSet::IconMap& icons = extension.icons().map(); 147 const ExtensionIconSet::IconMap& icons = extension.icons().map();
103 if (!icons.empty()) { 148 if (!icons.empty()) {
104 ListValue* icon_list = new ListValue(); 149 info->icons.reset(new IconInfoList());
105 std::map<ExtensionIconSet::Icons, std::string>::const_iterator icon_iter; 150 std::map<ExtensionIconSet::Icons, std::string>::const_iterator icon_iter;
106 for (icon_iter = icons.begin(); icon_iter != icons.end(); ++icon_iter) { 151 for (icon_iter = icons.begin(); icon_iter != icons.end(); ++icon_iter) {
107 DictionaryValue* icon_info = new DictionaryValue(); 152 IconInfo* icon_info = new IconInfo();
108 ExtensionIconSet::Icons size = icon_iter->first; 153 icon_info->size = icon_iter->first;
109 GURL url = ExtensionIconSource::GetIconURL( 154 GURL url = ExtensionIconSource::GetIconURL(
110 &extension, size, ExtensionIconSet::MATCH_EXACTLY, false, NULL); 155 &extension,
Aaron Boodman 2012/08/09 13:17:19 Put multiple arguments on one line, so long as the
mitchellwrosen 2012/08/09 18:15:41 Agh, that's how I did it, but was told by another
Aaron Boodman 2012/08/10 06:12:36 Don't think so: http://google-styleguide.googlecod
111 icon_info->SetInteger(keys::kSizeKey, icon_iter->first); 156 icon_info->size,
112 icon_info->SetString(keys::kUrlKey, url.spec()); 157 ExtensionIconSet::MATCH_EXACTLY,
113 icon_list->Append(icon_info); 158 false,
159 NULL);
160 icon_info->url = url.spec();
161 info->icons->push_back(make_linked_ptr<IconInfo>(icon_info));
114 } 162 }
115 info->Set(keys::kIconsKey, icon_list);
116 } 163 }
117 164
118 const std::set<std::string> perms = 165 const std::set<std::string> perms =
119 extension.GetActivePermissions()->GetAPIsAsStrings(); 166 extension.GetActivePermissions()->GetAPIsAsStrings();
120 ListValue* permission_list = new ListValue();
121 if (!perms.empty()) { 167 if (!perms.empty()) {
122 std::set<std::string>::const_iterator perms_iter; 168 std::set<std::string>::const_iterator perms_iter;
123 for (perms_iter = perms.begin(); perms_iter != perms.end(); ++perms_iter) { 169 for (perms_iter = perms.begin(); perms_iter != perms.end(); ++perms_iter)
124 StringValue* permission_name = new StringValue(*perms_iter); 170 info->permissions.push_back(*perms_iter);
125 permission_list->Append(permission_name);
126 }
127 } 171 }
128 info->Set(keys::kPermissionsKey, permission_list);
129 172
130 ListValue* host_permission_list = new ListValue();
131 if (!extension.is_hosted_app()) { 173 if (!extension.is_hosted_app()) {
132 // Skip host permissions for hosted apps. 174 // Skip host permissions for hosted apps.
133 const URLPatternSet host_perms = 175 const URLPatternSet host_perms =
134 extension.GetActivePermissions()->explicit_hosts(); 176 extension.GetActivePermissions()->explicit_hosts();
135 if (!host_perms.is_empty()) { 177 if (!host_perms.is_empty()) {
136 URLPatternSet::const_iterator host_perms_iter; 178 for (URLPatternSet::const_iterator iter = host_perms.begin();
137 for (host_perms_iter = host_perms.begin(); 179 iter != host_perms.end(); ++iter) {
138 host_perms_iter != host_perms.end(); 180 info->host_permissions.push_back(iter->GetAsString());
139 ++host_perms_iter) {
140 StringValue* name = new StringValue(host_perms_iter->GetAsString());
141 host_permission_list->Append(name);
142 } 181 }
143 } 182 }
144 } 183 }
145 info->Set(keys::kHostPermissionsKey, host_permission_list);
146 184
147 std::string install_type = keys::kInstallTypeOther;
148 switch (extension.location()) { 185 switch (extension.location()) {
149 case Extension::INTERNAL: 186 case Extension::INTERNAL:
150 install_type = keys::kInstallTypeNormal; 187 info->install_type = ExtensionInfo::INSTALL_TYPE_NORMAL;
151 break; 188 break;
152 case Extension::LOAD: 189 case Extension::LOAD:
153 install_type = keys::kInstallTypeDevelopment; 190 info->install_type = ExtensionInfo::INSTALL_TYPE_DEVELOPMENT;
154 break; 191 break;
155 case Extension::EXTERNAL_PREF: 192 case Extension::EXTERNAL_PREF:
156 case Extension::EXTERNAL_REGISTRY: 193 case Extension::EXTERNAL_REGISTRY:
157 case Extension::EXTERNAL_PREF_DOWNLOAD: 194 case Extension::EXTERNAL_PREF_DOWNLOAD:
158 install_type = keys::kInstallTypeSideload; 195 info->install_type = ExtensionInfo::INSTALL_TYPE_SIDELOAD;
159 break; 196 break;
160 case Extension::EXTERNAL_POLICY_DOWNLOAD: 197 case Extension::EXTERNAL_POLICY_DOWNLOAD:
161 install_type = keys::kInstallTypeAdmin; 198 info->install_type = ExtensionInfo::INSTALL_TYPE_ADMIN;
162 break; 199 break;
163 default: 200 default:
164 install_type = keys::kInstallTypeOther; 201 info->install_type = ExtensionInfo::INSTALL_TYPE_OTHER;
202 break;
165 } 203 }
166 info->SetString(keys::kInstallTypeKey, install_type);
167 204
168 return info; 205 return info.Pass();
169 } 206 }
170 207
171 static void AddExtensionInfo(ListValue* list, 208 static void AddExtensionInfo(const ExtensionSet& extensions,
Aaron Boodman 2012/08/09 13:17:19 Same comment as above about anon namespace.
mitchellwrosen 2012/08/09 18:15:41 Done.
172 const ExtensionSet& extensions, 209 ExtensionService* service,
173 ExtensionService* service) { 210 ExtensionInfoList* extension_list) {
174 for (ExtensionSet::const_iterator i = extensions.begin(); 211 for (ExtensionSet::const_iterator iter = extensions.begin();
175 i != extensions.end(); ++i) { 212 iter != extensions.end(); ++iter) {
176 const Extension& extension = **i; 213 const Extension& extension = **iter;
177 214
178 if (extension.location() == Extension::COMPONENT) 215 if (extension.location() == Extension::COMPONENT)
179 continue; // Skip built-in extensions. 216 continue; // Skip built-in extensions.
180 217
181 list->Append(CreateExtensionInfo(extension, service)); 218 extension_list->push_back(make_linked_ptr<ExtensionInfo>(
219 CreateExtensionInfo(extension, service).release()));
182 } 220 }
183 } 221 }
184 222
185 bool GetAllExtensionsFunction::RunImpl() { 223 bool GetAllExtensionsFunction::RunImpl() {
186 ListValue* result = new ListValue(); 224 ExtensionInfoList extensions;
187 SetResult(result);
188 225
189 AddExtensionInfo(result, *service()->extensions(), service()); 226 AddExtensionInfo(*service()->extensions(), service(), &extensions);
190 AddExtensionInfo(result, *service()->disabled_extensions(), service()); 227 AddExtensionInfo(*service()->disabled_extensions(), service(), &extensions);
191 228
229 results_ = GetAll::Results::Create(extensions);
192 return true; 230 return true;
193 } 231 }
194 232
195 bool GetExtensionByIdFunction::RunImpl() { 233 bool GetExtensionByIdFunction::RunImpl() {
196 std::string extension_id; 234 scoped_ptr<Get::Params> params(Get::Params::Create(*args_));
197 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_id)); 235 EXTENSION_FUNCTION_VALIDATE(params.get());
198 const Extension* extension = service()->GetExtensionById(extension_id, true); 236
237 const Extension* extension = service()->GetExtensionById(params->id, true);
199 if (!extension) { 238 if (!extension) {
200 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kNoExtensionError, 239 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kNoExtensionError,
201 extension_id); 240 params->id);
202 return false; 241 return false;
203 } 242 }
204 DictionaryValue* result = CreateExtensionInfo(*extension, service()); 243
205 SetResult(result); 244 scoped_ptr<ExtensionInfo> info = CreateExtensionInfo(*extension, service());
245 results_ = Get::Results::Create(*info);
206 246
207 return true; 247 return true;
208 } 248 }
209 249
210 bool GetPermissionWarningsByIdFunction::RunImpl() { 250 bool GetPermissionWarningsByIdFunction::RunImpl() {
211 std::string ext_id; 251 scoped_ptr<GetPermissionWarningsById::Params> params(
212 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &ext_id)); 252 GetPermissionWarningsById::Params::Create(*args_));
253 EXTENSION_FUNCTION_VALIDATE(params.get());
213 254
214 const Extension* extension = service()->GetExtensionById(ext_id, true); 255 const Extension* extension = service()->GetExtensionById(params->id, true);
215 if (!extension) { 256 if (!extension) {
216 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kNoExtensionError, 257 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kNoExtensionError,
217 ext_id); 258 params->id);
218 return false; 259 return false;
219 } 260 }
220 261
221 PermissionMessages warnings = extension->GetPermissionMessages(); 262 std::vector<std::string> warnings;
222 ListValue* result = new ListValue(); 263 CreateWarningsList(extension, &warnings);
223 for (PermissionMessages::const_iterator i = warnings.begin(); 264 results_ = GetPermissionWarningsById::Results::Create(warnings);
224 i < warnings.end(); ++i)
225 result->Append(Value::CreateStringValue(i->message()));
226 SetResult(result);
227 return true; 265 return true;
228 } 266 }
229 267
230 namespace { 268 namespace {
231 269
232 // This class helps GetPermissionWarningsByManifestFunction manage 270 // This class helps GetPermissionWarningsByManifestFunction manage
233 // sending manifest JSON strings to the utility process for parsing. 271 // sending manifest JSON strings to the utility process for parsing.
234 class SafeManifestJSONParser : public UtilityProcessHostClient { 272 class SafeManifestJSONParser : public UtilityProcessHostClient {
235 public: 273 public:
236 SafeManifestJSONParser(GetPermissionWarningsByManifestFunction* client, 274 SafeManifestJSONParser(GetPermissionWarningsByManifestFunction* client,
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 348
311 // Results of parsing. 349 // Results of parsing.
312 scoped_ptr<DictionaryValue> parsed_manifest_; 350 scoped_ptr<DictionaryValue> parsed_manifest_;
313 351
314 std::string error_; 352 std::string error_;
315 }; 353 };
316 354
317 } // namespace 355 } // namespace
318 356
319 bool GetPermissionWarningsByManifestFunction::RunImpl() { 357 bool GetPermissionWarningsByManifestFunction::RunImpl() {
320 std::string manifest_str; 358 scoped_ptr<GetPermissionWarningsByManifest::Params> params(
321 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &manifest_str)); 359 GetPermissionWarningsByManifest::Params::Create(*args_));
360 EXTENSION_FUNCTION_VALIDATE(params.get());
322 361
323 scoped_refptr<SafeManifestJSONParser> parser = 362 scoped_refptr<SafeManifestJSONParser> parser =
324 new SafeManifestJSONParser(this, manifest_str); 363 new SafeManifestJSONParser(this, params->manifest_str);
325 parser->Start(); 364 parser->Start();
326 365
327 // Matched with a Release() in OnParseSuccess/Failure(). 366 // Matched with a Release() in OnParseSuccess/Failure().
328 AddRef(); 367 AddRef();
329 368
330 // Response is sent async in OnParseSuccess/Failure(). 369 // Response is sent async in OnParseSuccess/Failure().
331 return true; 370 return true;
332 } 371 }
333 372
334 void GetPermissionWarningsByManifestFunction::OnParseSuccess( 373 void GetPermissionWarningsByManifestFunction::OnParseSuccess(
335 DictionaryValue* parsed_manifest) { 374 DictionaryValue* parsed_manifest) {
336 CHECK(parsed_manifest); 375 CHECK(parsed_manifest);
337 376
338 scoped_refptr<Extension> extension = Extension::Create( 377 scoped_refptr<Extension> extension = Extension::Create(
339 FilePath(), Extension::INVALID, *parsed_manifest, Extension::NO_FLAGS, 378 FilePath(), Extension::INVALID, *parsed_manifest, Extension::NO_FLAGS,
340 &error_); 379 &error_);
341 if (!extension.get()) { 380 if (!extension.get()) {
342 OnParseFailure(keys::kExtensionCreateError); 381 OnParseFailure(keys::kExtensionCreateError);
343 return; 382 return;
344 } 383 }
345 384
346 PermissionMessages warnings = extension->GetPermissionMessages(); 385 std::vector<std::string> warnings;
347 ListValue* result = new ListValue(); 386 CreateWarningsList(extension, &warnings);
348 for (PermissionMessages::const_iterator i = warnings.begin(); 387 results_ = GetPermissionWarningsByManifest::Results::Create(warnings);
349 i < warnings.end(); ++i)
350 result->Append(Value::CreateStringValue(i->message()));
351 SetResult(result);
352 SendResponse(true); 388 SendResponse(true);
353 389
354 // Matched with AddRef() in RunImpl(). 390 // Matched with AddRef() in RunImpl().
355 Release(); 391 Release();
356 } 392 }
357 393
358 void GetPermissionWarningsByManifestFunction::OnParseFailure( 394 void GetPermissionWarningsByManifestFunction::OnParseFailure(
359 const std::string& error) { 395 const std::string& error) {
360 error_ = error; 396 error_ = error;
361 SendResponse(false); 397 SendResponse(false);
362 398
363 // Matched with AddRef() in RunImpl(). 399 // Matched with AddRef() in RunImpl().
364 Release(); 400 Release();
365 } 401 }
366 402
367 bool LaunchAppFunction::RunImpl() { 403 bool LaunchAppFunction::RunImpl() {
368 std::string extension_id; 404 scoped_ptr<LaunchApp::Params> params(LaunchApp::Params::Create(*args_));
369 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_id)); 405 EXTENSION_FUNCTION_VALIDATE(params.get());
370 const Extension* extension = service()->GetExtensionById(extension_id, true); 406 const Extension* extension = service()->GetExtensionById(params->id, true);
371 if (!extension) { 407 if (!extension) {
372 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kNoExtensionError, 408 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kNoExtensionError,
373 extension_id); 409 params->id);
374 return false; 410 return false;
375 } 411 }
376 if (!extension->is_app()) { 412 if (!extension->is_app()) {
377 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kNotAnAppError, 413 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kNotAnAppError,
378 extension_id); 414 params->id);
379 return false; 415 return false;
380 } 416 }
381 417
382 // Look at prefs to find the right launch container. 418 // Look at prefs to find the right launch container.
383 // |default_pref_value| is set to LAUNCH_REGULAR so that if 419 // |default_pref_value| is set to LAUNCH_REGULAR so that if
384 // the user has not set a preference, we open the app in a tab. 420 // the user has not set a preference, we open the app in a tab.
385 extension_misc::LaunchContainer launch_container = 421 extension_misc::LaunchContainer launch_container =
386 service()->extension_prefs()->GetLaunchContainer( 422 service()->extension_prefs()->GetLaunchContainer(
387 extension, extensions::ExtensionPrefs::LAUNCH_DEFAULT); 423 extension, extensions::ExtensionPrefs::LAUNCH_DEFAULT);
388 application_launch::OpenApplication(application_launch::LaunchParams( 424 application_launch::OpenApplication(application_launch::LaunchParams(
389 profile(), extension, launch_container, NEW_FOREGROUND_TAB)); 425 profile(), extension, launch_container, NEW_FOREGROUND_TAB));
390 #if !defined(OS_ANDROID) 426 #if !defined(OS_ANDROID)
391 AppLauncherHandler::RecordAppLaunchType( 427 AppLauncherHandler::RecordAppLaunchType(
392 extension_misc::APP_LAUNCH_EXTENSION_API); 428 extension_misc::APP_LAUNCH_EXTENSION_API);
393 #endif 429 #endif
394 430
395 return true; 431 return true;
396 } 432 }
397 433
398 SetEnabledFunction::SetEnabledFunction() { 434 SetEnabledFunction::SetEnabledFunction() {
399 } 435 }
400 436
401 SetEnabledFunction::~SetEnabledFunction() { 437 SetEnabledFunction::~SetEnabledFunction() {
402 } 438 }
403 439
404 bool SetEnabledFunction::RunImpl() { 440 bool SetEnabledFunction::RunImpl() {
405 bool enable; 441 scoped_ptr<SetEnabled::Params> params(SetEnabled::Params::Create(*args_));
406 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_id_)); 442 EXTENSION_FUNCTION_VALIDATE(params.get());
407 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(1, &enable)); 443
444 extension_id_ = params->id;
408 445
409 const Extension* extension = service()->GetExtensionById(extension_id_, true); 446 const Extension* extension = service()->GetExtensionById(extension_id_, true);
410 if (!extension) { 447 if (!extension) {
411 error_ = ExtensionErrorUtils::FormatErrorMessage( 448 error_ = ExtensionErrorUtils::FormatErrorMessage(
412 keys::kNoExtensionError, extension_id_); 449 keys::kNoExtensionError, extension_id_);
413 return false; 450 return false;
414 } 451 }
415 452
416 const extensions::ManagementPolicy* policy = extensions::ExtensionSystem::Get( 453 const extensions::ManagementPolicy* policy = extensions::ExtensionSystem::Get(
417 profile())->management_policy(); 454 profile())->management_policy();
418 if (!policy->UserMayModifySettings(extension, NULL)) { 455 if (!policy->UserMayModifySettings(extension, NULL)) {
419 error_ = ExtensionErrorUtils::FormatErrorMessage( 456 error_ = ExtensionErrorUtils::FormatErrorMessage(
420 keys::kUserCantModifyError, extension_id_); 457 keys::kUserCantModifyError, extension_id_);
421 return false; 458 return false;
422 } 459 }
423 460
424 bool currently_enabled = service()->IsExtensionEnabled(extension_id_); 461 bool currently_enabled = service()->IsExtensionEnabled(extension_id_);
425 462
426 if (!currently_enabled && enable) { 463 if (!currently_enabled && params->enabled) {
427 extensions::ExtensionPrefs* prefs = service()->extension_prefs(); 464 extensions::ExtensionPrefs* prefs = service()->extension_prefs();
428 if (prefs->DidExtensionEscalatePermissions(extension_id_)) { 465 if (prefs->DidExtensionEscalatePermissions(extension_id_)) {
429 if (!user_gesture()) { 466 if (!user_gesture()) {
430 error_ = keys::kGestureNeededForEscalationError; 467 error_ = keys::kGestureNeededForEscalationError;
431 return false; 468 return false;
432 } 469 }
433 AddRef(); // Matched in InstallUIProceed/InstallUIAbort 470 AddRef(); // Matched in InstallUIProceed/InstallUIAbort
434 install_prompt_.reset( 471 install_prompt_.reset(
435 chrome::CreateExtensionInstallPromptWithBrowser(GetCurrentBrowser())); 472 chrome::CreateExtensionInstallPromptWithBrowser(GetCurrentBrowser()));
436 install_prompt_->ConfirmReEnable(this, extension); 473 install_prompt_->ConfirmReEnable(this, extension);
437 return true; 474 return true;
438 } 475 }
439 service()->EnableExtension(extension_id_); 476 service()->EnableExtension(extension_id_);
440 } else if (currently_enabled && !enable) { 477 } else if (currently_enabled && !params->enabled) {
441 service()->DisableExtension(extension_id_, Extension::DISABLE_USER_ACTION); 478 service()->DisableExtension(extension_id_, Extension::DISABLE_USER_ACTION);
442 } 479 }
443 480
444 BrowserThread::PostTask( 481 BrowserThread::PostTask(
445 BrowserThread::UI, 482 BrowserThread::UI,
446 FROM_HERE, 483 FROM_HERE,
447 base::Bind(&SetEnabledFunction::SendResponse, this, true)); 484 base::Bind(&SetEnabledFunction::SendResponse, this, true));
448 485
449 return true; 486 return true;
450 } 487 }
(...skipping 10 matching lines...) Expand all
461 Release(); 498 Release();
462 } 499 }
463 500
464 UninstallFunction::UninstallFunction() { 501 UninstallFunction::UninstallFunction() {
465 } 502 }
466 503
467 UninstallFunction::~UninstallFunction() { 504 UninstallFunction::~UninstallFunction() {
468 } 505 }
469 506
470 bool UninstallFunction::RunImpl() { 507 bool UninstallFunction::RunImpl() {
508 scoped_ptr<Uninstall::Params> params(Uninstall::Params::Create(*args_));
509 EXTENSION_FUNCTION_VALIDATE(params.get());
510
511 extension_id_ = params->id;
512
471 bool show_confirm_dialog = false; 513 bool show_confirm_dialog = false;
472 514 if (params->options.get() && params->options->show_confirm_dialog.get())
473 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_id_)); 515 show_confirm_dialog = *params->options->show_confirm_dialog;
474
475 if (HasOptionalArgument(1)) {
476 DictionaryValue* options = NULL;
477 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options));
478
479 if (options->HasKey(keys::kShowConfirmDialogKey)) {
480 EXTENSION_FUNCTION_VALIDATE(options->GetBoolean(
481 keys::kShowConfirmDialogKey, &show_confirm_dialog));
482 }
483 }
484 516
485 const Extension* extension = service()->GetExtensionById(extension_id_, true); 517 const Extension* extension = service()->GetExtensionById(extension_id_, true);
486 if (!extension) { 518 if (!extension) {
487 error_ = ExtensionErrorUtils::FormatErrorMessage( 519 error_ = ExtensionErrorUtils::FormatErrorMessage(
488 keys::kNoExtensionError, extension_id_); 520 keys::kNoExtensionError, extension_id_);
489 return false; 521 return false;
490 } 522 }
491 523
492 if (!extensions::ExtensionSystem::Get( 524 if (!extensions::ExtensionSystem::Get(
493 profile())->management_policy()->UserMayModifySettings(extension, NULL)) { 525 profile())->management_policy()->UserMayModifySettings(extension, NULL)) {
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 event_name = events::kOnExtensionEnabled; 617 event_name = events::kOnExtensionEnabled;
586 break; 618 break;
587 case chrome::NOTIFICATION_EXTENSION_UNLOADED: 619 case chrome::NOTIFICATION_EXTENSION_UNLOADED:
588 event_name = events::kOnExtensionDisabled; 620 event_name = events::kOnExtensionDisabled;
589 break; 621 break;
590 default: 622 default:
591 NOTREACHED(); 623 NOTREACHED();
592 return; 624 return;
593 } 625 }
594 626
595 ListValue args; 627 std::string json = "";
Aaron Boodman 2012/08/09 13:17:19 No need to initialize non-primitives.
mitchellwrosen 2012/08/09 18:15:41 Done.
596 if (event_name == events::kOnExtensionUninstalled) { 628 if (event_name == events::kOnExtensionUninstalled) {
597 args.Append(Value::CreateStringValue( 629 json = OnUninstalled::ToJson(
598 content::Details<const extensions::Extension>(details).ptr()->id())); 630 content::Details<const Extension>(details).ptr()->id());
599 } else { 631 } else {
600 const Extension* extension = NULL; 632 const Extension* extension = NULL;
601 if (event_name == events::kOnExtensionDisabled) { 633 if (event_name == events::kOnExtensionDisabled) {
602 extension = content::Details<extensions::UnloadedExtensionInfo>( 634 extension = content::Details<extensions::UnloadedExtensionInfo>(
603 details)->extension; 635 details)->extension;
604 } else { 636 } else {
605 extension = content::Details<const Extension>(details).ptr(); 637 extension = content::Details<const Extension>(details).ptr();
606 } 638 }
607 CHECK(extension); 639 CHECK(extension);
608 ExtensionService* service = profile->GetExtensionService(); 640 ExtensionService* service = profile->GetExtensionService();
609 args.Append(CreateExtensionInfo(*extension, service)); 641 scoped_ptr<ExtensionInfo> info = CreateExtensionInfo(*extension, service);
642 // OnInstalled::ToJson, OnEnabled::ToJson, and OnDisabled::ToJson are all
Aaron Boodman 2012/08/09 13:17:19 This is brittle - if one of these changed to be no
mitchellwrosen 2012/08/09 18:15:41 Good point. Done.
643 // extensionally and intensionally equal, so pick one.
644 json = OnInstalled::ToJson(*info);
610 } 645 }
611 646
612 std::string args_json;
613 base::JSONWriter::Write(&args, &args_json);
614
615 profile->GetExtensionEventRouter()->DispatchEventToRenderers( 647 profile->GetExtensionEventRouter()->DispatchEventToRenderers(
616 event_name, args_json, NULL, GURL(), extensions::EventFilteringInfo()); 648 event_name, json, NULL, GURL(), extensions::EventFilteringInfo());
617 } 649 }
OLDNEW
« no previous file with comments | « no previous file | chrome/common/extensions/api/api.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698