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

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

Issue 696543002: Move the chrome.management API to extensions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month 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
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 "extensions/browser/api/management/management_api.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/json/json_writer.h" 12 #include "base/json/json_writer.h"
13 #include "base/lazy_instance.h" 13 #include "base/lazy_instance.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/memory/linked_ptr.h" 15 #include "base/memory/linked_ptr.h"
16 #include "base/memory/scoped_ptr.h" 16 #include "base/memory/scoped_ptr.h"
17 #include "base/metrics/histogram.h" 17 #include "base/metrics/histogram.h"
18 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
20 #include "base/strings/utf_string_conversions.h" 20 #include "base/strings/utf_string_conversions.h"
21 #include "chrome/browser/extensions/api/management/management_api_constants.h" 21 #include "content/public/browser/browser_context.h"
22 #include "chrome/browser/extensions/extension_service.h" 22 #include "extensions/browser/api/extensions_api_client.h"
23 #include "chrome/browser/extensions/extension_ui_util.h" 23 #include "extensions/browser/api/management/management_api_constants.h"
24 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
25 #include "chrome/browser/extensions/extension_util.h"
26 #include "chrome/browser/extensions/launch_util.h"
27 #include "chrome/browser/favicon/favicon_service_factory.h"
28 #include "chrome/browser/profiles/profile.h"
29 #include "chrome/browser/ui/browser_dialogs.h"
30 #include "chrome/browser/ui/browser_finder.h"
31 #include "chrome/browser/ui/browser_window.h"
32 #include "chrome/browser/ui/extensions/application_launch.h"
33 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
34 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h"
35 #include "chrome/common/extensions/api/management.h"
36 #include "chrome/common/extensions/chrome_utility_extensions_messages.h"
37 #include "chrome/common/extensions/extension_constants.h"
38 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
39 #include "content/public/browser/utility_process_host.h"
40 #include "content/public/browser/utility_process_host_client.h"
41 #include "extensions/browser/event_router.h" 24 #include "extensions/browser/event_router.h"
42 #include "extensions/browser/extension_prefs.h" 25 #include "extensions/browser/extension_prefs.h"
43 #include "extensions/browser/extension_registry.h" 26 #include "extensions/browser/extension_registry.h"
44 #include "extensions/browser/extension_system.h" 27 #include "extensions/browser/extension_system.h"
45 #include "extensions/browser/management_policy.h" 28 #include "extensions/browser/management_policy.h"
46 #include "extensions/browser/uninstall_reason.h" 29 #include "extensions/browser/uninstall_reason.h"
30 #include "extensions/common/api/management.h"
47 #include "extensions/common/constants.h" 31 #include "extensions/common/constants.h"
48 #include "extensions/common/error_utils.h" 32 #include "extensions/common/error_utils.h"
49 #include "extensions/common/extension.h" 33 #include "extensions/common/extension.h"
50 #include "extensions/common/extension_icon_set.h" 34 #include "extensions/common/extension_icon_set.h"
51 #include "extensions/common/manifest_handlers/icons_handler.h" 35 #include "extensions/common/manifest_handlers/icons_handler.h"
52 #include "extensions/common/manifest_handlers/offline_enabled_info.h" 36 #include "extensions/common/manifest_handlers/offline_enabled_info.h"
53 #include "extensions/common/manifest_handlers/options_page_info.h" 37 #include "extensions/common/manifest_handlers/options_page_info.h"
54 #include "extensions/common/manifest_url_handlers.h" 38 #include "extensions/common/manifest_url_handlers.h"
55 #include "extensions/common/permissions/permission_set.h" 39 #include "extensions/common/permissions/permission_set.h"
56 #include "extensions/common/permissions/permissions_data.h" 40 #include "extensions/common/permissions/permissions_data.h"
57 #include "extensions/common/url_pattern.h" 41 #include "extensions/common/url_pattern.h"
58 42
59 using base::IntToString; 43 using base::IntToString;
60 using content::BrowserThread; 44 using content::BrowserThread;
61 using content::UtilityProcessHost;
62 using content::UtilityProcessHostClient;
63 45
64 namespace keys = extension_management_api_constants; 46 namespace keys = extension_management_api_constants;
65 47
66 namespace extensions { 48 namespace extensions {
67 49
68 namespace management = api::management; 50 namespace management = core_api::management;
69 51
70 namespace { 52 namespace {
71 53
72 typedef std::vector<linked_ptr<management::ExtensionInfo> > ExtensionInfoList; 54 typedef std::vector<linked_ptr<management::ExtensionInfo>> ExtensionInfoList;
73 typedef std::vector<linked_ptr<management::IconInfo> > IconInfoList; 55 typedef std::vector<linked_ptr<management::IconInfo>> IconInfoList;
74 56
75 enum AutoConfirmForTest { 57 enum AutoConfirmForTest { DO_NOT_SKIP = 0, PROCEED, ABORT };
76 DO_NOT_SKIP = 0,
77 PROCEED,
78 ABORT
79 };
80 58
81 AutoConfirmForTest auto_confirm_for_test = DO_NOT_SKIP; 59 AutoConfirmForTest auto_confirm_for_test = DO_NOT_SKIP;
82 60
83 std::vector<std::string> CreateWarningsList(const Extension* extension) { 61 std::vector<std::string> CreateWarningsList(const Extension* extension) {
84 std::vector<std::string> warnings_list; 62 std::vector<std::string> warnings_list;
85 PermissionMessages warnings = 63 PermissionMessages warnings =
86 extension->permissions_data()->GetPermissionMessages(); 64 extension->permissions_data()->GetPermissionMessages();
87 for (PermissionMessages::const_iterator iter = warnings.begin(); 65 for (PermissionMessages::const_iterator iter = warnings.begin();
88 iter != warnings.end(); ++iter) { 66 iter != warnings.end(); ++iter) {
89 warnings_list.push_back(base::UTF16ToUTF8(iter->message())); 67 warnings_list.push_back(base::UTF16ToUTF8(iter->message()));
90 } 68 }
91 69
92 return warnings_list; 70 return warnings_list;
93 } 71 }
94 72
95 std::vector<management::LaunchType> GetAvailableLaunchTypes( 73 std::vector<management::LaunchType> GetAvailableLaunchTypes(
96 const Extension& extension) { 74 const Extension& extension,
75 const ManagementAPIDelegate* delegate) {
97 std::vector<management::LaunchType> launch_type_list; 76 std::vector<management::LaunchType> launch_type_list;
98 if (extension.is_platform_app()) { 77 if (extension.is_platform_app()) {
99 launch_type_list.push_back(management::LAUNCH_TYPE_OPEN_AS_WINDOW); 78 launch_type_list.push_back(management::LAUNCH_TYPE_OPEN_AS_WINDOW);
100 return launch_type_list; 79 return launch_type_list;
101 } 80 }
102 81
103 launch_type_list.push_back(management::LAUNCH_TYPE_OPEN_AS_REGULAR_TAB); 82 launch_type_list.push_back(management::LAUNCH_TYPE_OPEN_AS_REGULAR_TAB);
104 83
105 #if !defined(OS_MACOSX) 84 #if !defined(OS_MACOSX)
106 launch_type_list.push_back(management::LAUNCH_TYPE_OPEN_AS_WINDOW); 85 launch_type_list.push_back(management::LAUNCH_TYPE_OPEN_AS_WINDOW);
107 #endif 86 #endif
108 87
109 if (!util::IsStreamlinedHostedAppsEnabled()) { 88 if (!delegate->IsStreamlinedHostedAppsEnabled()) {
110 launch_type_list.push_back(management::LAUNCH_TYPE_OPEN_AS_PINNED_TAB); 89 launch_type_list.push_back(management::LAUNCH_TYPE_OPEN_AS_PINNED_TAB);
111 launch_type_list.push_back(management::LAUNCH_TYPE_OPEN_FULL_SCREEN); 90 launch_type_list.push_back(management::LAUNCH_TYPE_OPEN_FULL_SCREEN);
112 } 91 }
113 return launch_type_list; 92 return launch_type_list;
114 } 93 }
115 94
116 scoped_ptr<management::ExtensionInfo> CreateExtensionInfo( 95 scoped_ptr<management::ExtensionInfo> CreateExtensionInfo(
117 const Extension& extension, 96 const Extension& extension,
118 ExtensionSystem* system) { 97 content::BrowserContext* context) {
98 ExtensionSystem* system = ExtensionSystem::Get(context);
99 ExtensionRegistry* registry = ExtensionRegistry::Get(context);
100 const ManagementAPIDelegate* delegate =
101 ManagementAPI::GetFactoryInstance()->Get(context)->GetDelegate();
119 scoped_ptr<management::ExtensionInfo> info(new management::ExtensionInfo()); 102 scoped_ptr<management::ExtensionInfo> info(new management::ExtensionInfo());
120 ExtensionService* service = system->extension_service();
121 103
122 info->id = extension.id(); 104 info->id = extension.id();
123 info->name = extension.name(); 105 info->name = extension.name();
124 info->short_name = extension.short_name(); 106 info->short_name = extension.short_name();
125 info->enabled = service->IsExtensionEnabled(info->id); 107 info->enabled = registry->enabled_extensions().Contains(info->id);
126 info->offline_enabled = OfflineEnabledInfo::IsOfflineEnabled(&extension); 108 info->offline_enabled = OfflineEnabledInfo::IsOfflineEnabled(&extension);
127 info->version = extension.VersionString(); 109 info->version = extension.VersionString();
128 info->description = extension.description(); 110 info->description = extension.description();
129 info->options_url = OptionsPageInfo::GetOptionsPage(&extension).spec(); 111 info->options_url = OptionsPageInfo::GetOptionsPage(&extension).spec();
130 info->homepage_url.reset(new std::string( 112 info->homepage_url.reset(
131 ManifestURL::GetHomepageURL(&extension).spec())); 113 new std::string(ManifestURL::GetHomepageURL(&extension).spec()));
132 info->may_disable = system->management_policy()-> 114 info->may_disable =
133 UserMayModifySettings(&extension, NULL); 115 system->management_policy()->UserMayModifySettings(&extension, NULL);
134 info->is_app = extension.is_app(); 116 info->is_app = extension.is_app();
135 if (info->is_app) { 117 if (info->is_app) {
136 if (extension.is_legacy_packaged_app()) 118 if (extension.is_legacy_packaged_app())
137 info->type = management::ExtensionInfo::TYPE_LEGACY_PACKAGED_APP; 119 info->type = management::ExtensionInfo::TYPE_LEGACY_PACKAGED_APP;
138 else if (extension.is_hosted_app()) 120 else if (extension.is_hosted_app())
139 info->type = management::ExtensionInfo::TYPE_HOSTED_APP; 121 info->type = management::ExtensionInfo::TYPE_HOSTED_APP;
140 else 122 else
141 info->type = management::ExtensionInfo::TYPE_PACKAGED_APP; 123 info->type = management::ExtensionInfo::TYPE_PACKAGED_APP;
142 } else if (extension.is_theme()) { 124 } else if (extension.is_theme()) {
143 info->type = management::ExtensionInfo::TYPE_THEME; 125 info->type = management::ExtensionInfo::TYPE_THEME;
144 } else { 126 } else {
145 info->type = management::ExtensionInfo::TYPE_EXTENSION; 127 info->type = management::ExtensionInfo::TYPE_EXTENSION;
146 } 128 }
147 129
148 if (info->enabled) { 130 if (info->enabled) {
149 info->disabled_reason = management::ExtensionInfo::DISABLED_REASON_NONE; 131 info->disabled_reason = management::ExtensionInfo::DISABLED_REASON_NONE;
150 } else { 132 } else {
151 ExtensionPrefs* prefs = ExtensionPrefs::Get(service->profile()); 133 ExtensionPrefs* prefs = ExtensionPrefs::Get(context);
152 if (prefs->DidExtensionEscalatePermissions(extension.id())) { 134 if (prefs->DidExtensionEscalatePermissions(extension.id())) {
153 info->disabled_reason = 135 info->disabled_reason =
154 management::ExtensionInfo::DISABLED_REASON_PERMISSIONS_INCREASE; 136 management::ExtensionInfo::DISABLED_REASON_PERMISSIONS_INCREASE;
155 } else { 137 } else {
156 info->disabled_reason = 138 info->disabled_reason =
157 management::ExtensionInfo::DISABLED_REASON_UNKNOWN; 139 management::ExtensionInfo::DISABLED_REASON_UNKNOWN;
158 } 140 }
159 } 141 }
160 142
161 if (!ManifestURL::GetUpdateURL(&extension).is_empty()) { 143 if (!ManifestURL::GetUpdateURL(&extension).is_empty()) {
162 info->update_url.reset(new std::string( 144 info->update_url.reset(
163 ManifestURL::GetUpdateURL(&extension).spec())); 145 new std::string(ManifestURL::GetUpdateURL(&extension).spec()));
164 } 146 }
165 147
166 if (extension.is_app()) { 148 if (extension.is_app()) {
167 info->app_launch_url.reset(new std::string( 149 info->app_launch_url.reset(
168 AppLaunchInfo::GetFullLaunchURL(&extension).spec())); 150 new std::string(delegate->GetFullLaunchURL(&extension).spec()));
169 } 151 }
170 152
171 const ExtensionIconSet::IconMap& icons = 153 const ExtensionIconSet::IconMap& icons =
172 IconsInfo::GetIcons(&extension).map(); 154 IconsInfo::GetIcons(&extension).map();
173 if (!icons.empty()) { 155 if (!icons.empty()) {
174 info->icons.reset(new IconInfoList()); 156 info->icons.reset(new IconInfoList());
175 ExtensionIconSet::IconMap::const_iterator icon_iter; 157 ExtensionIconSet::IconMap::const_iterator icon_iter;
176 for (icon_iter = icons.begin(); icon_iter != icons.end(); ++icon_iter) { 158 for (icon_iter = icons.begin(); icon_iter != icons.end(); ++icon_iter) {
177 management::IconInfo* icon_info = new management::IconInfo(); 159 management::IconInfo* icon_info = new management::IconInfo();
178 icon_info->size = icon_iter->first; 160 icon_info->size = icon_iter->first;
179 GURL url = ExtensionIconSource::GetIconURL( 161 GURL url =
180 &extension, icon_info->size, ExtensionIconSet::MATCH_EXACTLY, false, 162 delegate->GetIconURL(&extension, icon_info->size,
181 NULL); 163 ExtensionIconSet::MATCH_EXACTLY, false, nullptr);
182 icon_info->url = url.spec(); 164 icon_info->url = url.spec();
183 info->icons->push_back(make_linked_ptr<management::IconInfo>(icon_info)); 165 info->icons->push_back(make_linked_ptr<management::IconInfo>(icon_info));
184 } 166 }
185 } 167 }
186 168
187 const std::set<std::string> perms = 169 const std::set<std::string> perms =
188 extension.permissions_data()->active_permissions()->GetAPIsAsStrings(); 170 extension.permissions_data()->active_permissions()->GetAPIsAsStrings();
189 if (!perms.empty()) { 171 if (!perms.empty()) {
190 std::set<std::string>::const_iterator perms_iter; 172 std::set<std::string>::const_iterator perms_iter;
191 for (perms_iter = perms.begin(); perms_iter != perms.end(); ++perms_iter) 173 for (perms_iter = perms.begin(); perms_iter != perms.end(); ++perms_iter)
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 break; 212 break;
231 } 213 }
232 214
233 info->launch_type = management::LAUNCH_TYPE_NONE; 215 info->launch_type = management::LAUNCH_TYPE_NONE;
234 if (extension.is_app()) { 216 if (extension.is_app()) {
235 LaunchType launch_type; 217 LaunchType launch_type;
236 if (extension.is_platform_app()) { 218 if (extension.is_platform_app()) {
237 launch_type = LAUNCH_TYPE_WINDOW; 219 launch_type = LAUNCH_TYPE_WINDOW;
238 } else { 220 } else {
239 launch_type = 221 launch_type =
240 GetLaunchType(ExtensionPrefs::Get(service->profile()), &extension); 222 delegate->GetLaunchType(ExtensionPrefs::Get(context), &extension);
241 } 223 }
242 224
243 switch (launch_type) { 225 switch (launch_type) {
244 case LAUNCH_TYPE_PINNED: 226 case LAUNCH_TYPE_PINNED:
245 info->launch_type = management::LAUNCH_TYPE_OPEN_AS_PINNED_TAB; 227 info->launch_type = management::LAUNCH_TYPE_OPEN_AS_PINNED_TAB;
246 break; 228 break;
247 case LAUNCH_TYPE_REGULAR: 229 case LAUNCH_TYPE_REGULAR:
248 info->launch_type = management::LAUNCH_TYPE_OPEN_AS_REGULAR_TAB; 230 info->launch_type = management::LAUNCH_TYPE_OPEN_AS_REGULAR_TAB;
249 break; 231 break;
250 case LAUNCH_TYPE_FULLSCREEN: 232 case LAUNCH_TYPE_FULLSCREEN:
251 info->launch_type = management::LAUNCH_TYPE_OPEN_FULL_SCREEN; 233 info->launch_type = management::LAUNCH_TYPE_OPEN_FULL_SCREEN;
252 break; 234 break;
253 case LAUNCH_TYPE_WINDOW: 235 case LAUNCH_TYPE_WINDOW:
254 info->launch_type = management::LAUNCH_TYPE_OPEN_AS_WINDOW; 236 info->launch_type = management::LAUNCH_TYPE_OPEN_AS_WINDOW;
255 break; 237 break;
256 case LAUNCH_TYPE_INVALID: 238 case LAUNCH_TYPE_INVALID:
257 case NUM_LAUNCH_TYPES: 239 case NUM_LAUNCH_TYPES:
258 NOTREACHED(); 240 NOTREACHED();
259 } 241 }
260 242
261 info->available_launch_types.reset(new std::vector<management::LaunchType>( 243 info->available_launch_types.reset(new std::vector<management::LaunchType>(
262 GetAvailableLaunchTypes(extension))); 244 GetAvailableLaunchTypes(extension, delegate)));
263 } 245 }
264 246
265 return info.Pass(); 247 return info.Pass();
266 } 248 }
267 249
250 bool ShouldNotBeVisible(const Extension* extension,
251 content::BrowserContext* context) {
252 return (extension->ShouldNotBeVisible() ||
253 ExtensionPrefs::Get(context)->IsEphemeralApp(extension->id()));
254 }
255
268 void AddExtensionInfo(const ExtensionSet& extensions, 256 void AddExtensionInfo(const ExtensionSet& extensions,
269 ExtensionSystem* system, 257 ExtensionInfoList* extension_list,
270 ExtensionInfoList* extension_list, 258 content::BrowserContext* context) {
271 content::BrowserContext* context) {
272 for (ExtensionSet::const_iterator iter = extensions.begin(); 259 for (ExtensionSet::const_iterator iter = extensions.begin();
273 iter != extensions.end(); ++iter) { 260 iter != extensions.end(); ++iter) {
274 const Extension& extension = *iter->get(); 261 const Extension& extension = *iter->get();
275 262
276 if (ui_util::ShouldNotBeVisible(&extension, context)) 263 if (ShouldNotBeVisible(&extension, context))
277 continue; // Skip built-in extensions/apps. 264 continue; // Skip built-in extensions/apps.
278 265
279 extension_list->push_back(make_linked_ptr<management::ExtensionInfo>( 266 extension_list->push_back(make_linked_ptr<management::ExtensionInfo>(
280 CreateExtensionInfo(extension, system).release())); 267 CreateExtensionInfo(extension, context).release()));
281 } 268 }
282 } 269 }
283 270
284 } // namespace 271 } // namespace
285 272
286 ExtensionService* ManagementFunction::service() {
287 return ExtensionSystem::Get(GetProfile())->extension_service();
288 }
289
290 ExtensionService* AsyncManagementFunction::service() {
291 return ExtensionSystem::Get(GetProfile())->extension_service();
292 }
293
294 bool ManagementGetAllFunction::RunSync() { 273 bool ManagementGetAllFunction::RunSync() {
295 ExtensionInfoList extensions; 274 ExtensionInfoList extensions;
296 ExtensionRegistry* registry = ExtensionRegistry::Get(GetProfile()); 275 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context());
297 ExtensionSystem* system = ExtensionSystem::Get(GetProfile());
298 276
299 AddExtensionInfo(registry->enabled_extensions(), 277 AddExtensionInfo(registry->enabled_extensions(), &extensions,
300 system, &extensions, browser_context()); 278 browser_context());
301 AddExtensionInfo(registry->disabled_extensions(), 279 AddExtensionInfo(registry->disabled_extensions(), &extensions,
302 system, &extensions, browser_context()); 280 browser_context());
303 AddExtensionInfo(registry->terminated_extensions(), 281 AddExtensionInfo(registry->terminated_extensions(), &extensions,
304 system, &extensions, browser_context()); 282 browser_context());
305 283
306 results_ = management::GetAll::Results::Create(extensions); 284 results_ = management::GetAll::Results::Create(extensions);
307 return true; 285 return true;
308 } 286 }
309 287
310 bool ManagementGetFunction::RunSync() { 288 bool ManagementGetFunction::RunSync() {
311 scoped_ptr<management::Get::Params> params( 289 scoped_ptr<management::Get::Params> params(
312 management::Get::Params::Create(*args_)); 290 management::Get::Params::Create(*args_));
313 EXTENSION_FUNCTION_VALIDATE(params.get()); 291 EXTENSION_FUNCTION_VALIDATE(params.get());
292 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context());
314 293
315 const Extension* extension = service()->GetExtensionById(params->id, true); 294 const Extension* extension =
295 registry->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING);
316 if (!extension) { 296 if (!extension) {
317 error_ = ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, 297 error_ =
318 params->id); 298 ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, params->id);
319 return false; 299 return false;
320 } 300 }
321 301
322 scoped_ptr<management::ExtensionInfo> info = 302 scoped_ptr<management::ExtensionInfo> info =
323 CreateExtensionInfo(*extension, ExtensionSystem::Get(GetProfile())); 303 CreateExtensionInfo(*extension, browser_context());
324 results_ = management::Get::Results::Create(*info); 304 results_ = management::Get::Results::Create(*info);
325 305
326 return true; 306 return true;
327 } 307 }
328 308
329 bool ManagementGetSelfFunction::RunSync() { 309 bool ManagementGetSelfFunction::RunSync() {
330 scoped_ptr<management::ExtensionInfo> info = 310 scoped_ptr<management::ExtensionInfo> info =
331 CreateExtensionInfo(*extension_, ExtensionSystem::Get(GetProfile())); 311 CreateExtensionInfo(*extension_, browser_context());
332 results_ = management::Get::Results::Create(*info); 312 results_ = management::Get::Results::Create(*info);
333 313
334 return true; 314 return true;
335 } 315 }
336 316
337 bool ManagementGetPermissionWarningsByIdFunction::RunSync() { 317 bool ManagementGetPermissionWarningsByIdFunction::RunSync() {
338 scoped_ptr<management::GetPermissionWarningsById::Params> params( 318 scoped_ptr<management::GetPermissionWarningsById::Params> params(
339 management::GetPermissionWarningsById::Params::Create(*args_)); 319 management::GetPermissionWarningsById::Params::Create(*args_));
340 EXTENSION_FUNCTION_VALIDATE(params.get()); 320 EXTENSION_FUNCTION_VALIDATE(params.get());
341 321
342 const Extension* extension = service()->GetExtensionById(params->id, true); 322 const Extension* extension =
323 ExtensionRegistry::Get(browser_context())
324 ->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING);
343 if (!extension) { 325 if (!extension) {
344 error_ = ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, 326 error_ =
345 params->id); 327 ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, params->id);
346 return false; 328 return false;
347 } 329 }
348 330
349 std::vector<std::string> warnings = CreateWarningsList(extension); 331 std::vector<std::string> warnings = CreateWarningsList(extension);
350 results_ = management::GetPermissionWarningsById::Results::Create(warnings); 332 results_ = management::GetPermissionWarningsById::Results::Create(warnings);
351 return true; 333 return true;
352 } 334 }
353 335
354 namespace {
355
356 // This class helps ManagementGetPermissionWarningsByManifestFunction manage
357 // sending manifest JSON strings to the utility process for parsing.
358 class SafeManifestJSONParser : public UtilityProcessHostClient {
359 public:
360 SafeManifestJSONParser(
361 ManagementGetPermissionWarningsByManifestFunction* client,
362 const std::string& manifest)
363 : client_(client),
364 manifest_(manifest) {}
365
366 void Start() {
367 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
368 BrowserThread::PostTask(
369 BrowserThread::IO,
370 FROM_HERE,
371 base::Bind(&SafeManifestJSONParser::StartWorkOnIOThread, this));
372 }
373
374 void StartWorkOnIOThread() {
375 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
376 UtilityProcessHost* host = UtilityProcessHost::Create(
377 this, base::MessageLoopProxy::current().get());
378 host->Send(new ChromeUtilityMsg_ParseJSON(manifest_));
379 }
380
381 bool OnMessageReceived(const IPC::Message& message) override {
382 bool handled = true;
383 IPC_BEGIN_MESSAGE_MAP(SafeManifestJSONParser, message)
384 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Succeeded,
385 OnJSONParseSucceeded)
386 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Failed,
387 OnJSONParseFailed)
388 IPC_MESSAGE_UNHANDLED(handled = false)
389 IPC_END_MESSAGE_MAP()
390 return handled;
391 }
392
393 void OnJSONParseSucceeded(const base::ListValue& wrapper) {
394 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
395 const base::Value* value = NULL;
396 CHECK(wrapper.Get(0, &value));
397 if (value->IsType(base::Value::TYPE_DICTIONARY))
398 parsed_manifest_.reset(
399 static_cast<const base::DictionaryValue*>(value)->DeepCopy());
400 else
401 error_ = keys::kManifestParseError;
402
403 BrowserThread::PostTask(
404 BrowserThread::UI,
405 FROM_HERE,
406 base::Bind(&SafeManifestJSONParser::ReportResultFromUIThread, this));
407 }
408
409 void OnJSONParseFailed(const std::string& error) {
410 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
411 error_ = error;
412 BrowserThread::PostTask(
413 BrowserThread::UI,
414 FROM_HERE,
415 base::Bind(&SafeManifestJSONParser::ReportResultFromUIThread, this));
416 }
417
418 void ReportResultFromUIThread() {
419 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
420 if (error_.empty() && parsed_manifest_.get())
421 client_->OnParseSuccess(parsed_manifest_.Pass());
422 else
423 client_->OnParseFailure(error_);
424 }
425
426 private:
427 ~SafeManifestJSONParser() override {}
428
429 // The client who we'll report results back to.
430 ManagementGetPermissionWarningsByManifestFunction* client_;
431
432 // Data to parse.
433 std::string manifest_;
434
435 // Results of parsing.
436 scoped_ptr<base::DictionaryValue> parsed_manifest_;
437
438 std::string error_;
439 };
440
441 } // namespace
442
443 bool ManagementGetPermissionWarningsByManifestFunction::RunAsync() { 336 bool ManagementGetPermissionWarningsByManifestFunction::RunAsync() {
444 scoped_ptr<management::GetPermissionWarningsByManifest::Params> params( 337 scoped_ptr<management::GetPermissionWarningsByManifest::Params> params(
445 management::GetPermissionWarningsByManifest::Params::Create(*args_)); 338 management::GetPermissionWarningsByManifest::Params::Create(*args_));
446 EXTENSION_FUNCTION_VALIDATE(params.get()); 339 EXTENSION_FUNCTION_VALIDATE(params.get());
447 340
448 scoped_refptr<SafeManifestJSONParser> parser = 341 const ManagementAPIDelegate* delegate = ManagementAPI::GetFactoryInstance()
449 new SafeManifestJSONParser(this, params->manifest_str); 342 ->Get(browser_context())
450 parser->Start(); 343 ->GetDelegate();
451 344
452 // Matched with a Release() in OnParseSuccess/Failure(). 345 if (delegate) {
453 AddRef(); 346 delegate->GetPermissionWarningsByManifestFunctionDelegate(
347 this, params->manifest_str);
454 348
455 // Response is sent async in OnParseSuccess/Failure(). 349 // Matched with a Release() in OnParseSuccess/Failure().
456 return true; 350 AddRef();
351
352 // Response is sent async in OnParseSuccess/Failure().
353 return true;
354 } else {
355 // TODO(lfg) add error string
356 OnParseFailure("");
357 return false;
358 }
457 } 359 }
458 360
459 void ManagementGetPermissionWarningsByManifestFunction::OnParseSuccess( 361 void ManagementGetPermissionWarningsByManifestFunction::OnParseSuccess(
460 scoped_ptr<base::DictionaryValue> parsed_manifest) { 362 scoped_ptr<base::DictionaryValue> parsed_manifest) {
461 CHECK(parsed_manifest.get()); 363 CHECK(parsed_manifest.get());
462 364
463 scoped_refptr<Extension> extension = Extension::Create( 365 scoped_refptr<Extension> extension =
464 base::FilePath(), Manifest::INVALID_LOCATION, *parsed_manifest, 366 Extension::Create(base::FilePath(), Manifest::INVALID_LOCATION,
465 Extension::NO_FLAGS, &error_); 367 *parsed_manifest, Extension::NO_FLAGS, &error_);
466 if (!extension.get()) { 368 if (!extension.get()) {
467 OnParseFailure(keys::kExtensionCreateError); 369 OnParseFailure(keys::kExtensionCreateError);
468 return; 370 return;
469 } 371 }
470 372
471 std::vector<std::string> warnings = CreateWarningsList(extension.get()); 373 std::vector<std::string> warnings = CreateWarningsList(extension.get());
472 results_ = 374 results_ =
473 management::GetPermissionWarningsByManifest::Results::Create(warnings); 375 management::GetPermissionWarningsByManifest::Results::Create(warnings);
474 SendResponse(true); 376 SendResponse(true);
475 377
476 // Matched with AddRef() in RunAsync(). 378 // Matched with AddRef() in RunAsync().
477 Release(); 379 Release();
478 } 380 }
479 381
480 void ManagementGetPermissionWarningsByManifestFunction::OnParseFailure( 382 void ManagementGetPermissionWarningsByManifestFunction::OnParseFailure(
481 const std::string& error) { 383 const std::string& error) {
482 error_ = error; 384 error_ = error;
483 SendResponse(false); 385 SendResponse(false);
484 386
485 // Matched with AddRef() in RunAsync(). 387 // Matched with AddRef() in RunAsync().
486 Release(); 388 Release();
487 } 389 }
488 390
489 bool ManagementLaunchAppFunction::RunSync() { 391 bool ManagementLaunchAppFunction::RunSync() {
490 scoped_ptr<management::LaunchApp::Params> params( 392 scoped_ptr<management::LaunchApp::Params> params(
491 management::LaunchApp::Params::Create(*args_)); 393 management::LaunchApp::Params::Create(*args_));
492 EXTENSION_FUNCTION_VALIDATE(params.get()); 394 EXTENSION_FUNCTION_VALIDATE(params.get());
493 const Extension* extension = service()->GetExtensionById(params->id, true); 395 const Extension* extension =
396 ExtensionRegistry::Get(browser_context())
397 ->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING);
494 if (!extension) { 398 if (!extension) {
495 error_ = ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, 399 error_ =
496 params->id); 400 ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, params->id);
497 return false; 401 return false;
498 } 402 }
499 if (!extension->is_app()) { 403 if (!extension->is_app()) {
500 error_ = ErrorUtils::FormatErrorMessage(keys::kNotAnAppError, 404 error_ = ErrorUtils::FormatErrorMessage(keys::kNotAnAppError, params->id);
501 params->id);
502 return false; 405 return false;
503 } 406 }
504 407
505 // Look at prefs to find the right launch container. 408 const ManagementAPIDelegate* delegate = ManagementAPI::GetFactoryInstance()
506 // If the user has not set a preference, the default launch value will be 409 ->Get(browser_context())
507 // returned. 410 ->GetDelegate();
508 LaunchContainer launch_container = 411 return delegate->LaunchAppFunctionDelegate(extension, browser_context());
509 GetLaunchContainer(ExtensionPrefs::Get(GetProfile()), extension);
510 OpenApplication(AppLaunchParams(
511 GetProfile(), extension, launch_container, NEW_FOREGROUND_TAB));
512 CoreAppLauncherHandler::RecordAppLaunchType(
513 extension_misc::APP_LAUNCH_EXTENSION_API,
514 extension->GetType());
515
516 return true;
517 } 412 }
518 413
519 ManagementSetEnabledFunction::ManagementSetEnabledFunction() { 414 ManagementSetEnabledFunction::ManagementSetEnabledFunction() {
520 } 415 }
521 416
522 ManagementSetEnabledFunction::~ManagementSetEnabledFunction() { 417 ManagementSetEnabledFunction::~ManagementSetEnabledFunction() {
523 } 418 }
524 419
525 bool ManagementSetEnabledFunction::RunAsync() { 420 bool ManagementSetEnabledFunction::RunAsync() {
526 scoped_ptr<management::SetEnabled::Params> params( 421 scoped_ptr<management::SetEnabled::Params> params(
527 management::SetEnabled::Params::Create(*args_)); 422 management::SetEnabled::Params::Create(*args_));
528 EXTENSION_FUNCTION_VALIDATE(params.get()); 423 EXTENSION_FUNCTION_VALIDATE(params.get());
424 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context());
425 const ManagementAPIDelegate* delegate = ManagementAPI::GetFactoryInstance()
426 ->Get(browser_context())
427 ->GetDelegate();
529 428
530 extension_id_ = params->id; 429 extension_id_ = params->id;
531 430
532 const Extension* extension = 431 const Extension* extension =
533 ExtensionRegistry::Get(GetProfile()) 432 registry->GetExtensionById(extension_id_, ExtensionRegistry::EVERYTHING);
534 ->GetExtensionById(extension_id_, ExtensionRegistry::EVERYTHING); 433 if (!extension || ShouldNotBeVisible(extension, browser_context())) {
535 if (!extension || ui_util::ShouldNotBeVisible(extension, browser_context())) { 434 error_ =
536 error_ = ErrorUtils::FormatErrorMessage( 435 ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, extension_id_);
537 keys::kNoExtensionError, extension_id_);
538 return false; 436 return false;
539 } 437 }
540 438
541 const ManagementPolicy* policy = 439 const ManagementPolicy* policy =
542 ExtensionSystem::Get(GetProfile())->management_policy(); 440 ExtensionSystem::Get(browser_context())->management_policy();
543 if (!policy->UserMayModifySettings(extension, NULL) || 441 if (!policy->UserMayModifySettings(extension, NULL) ||
544 (!params->enabled && policy->MustRemainEnabled(extension, NULL)) || 442 (!params->enabled && policy->MustRemainEnabled(extension, NULL)) ||
545 (params->enabled && policy->MustRemainDisabled(extension, NULL, NULL))) { 443 (params->enabled && policy->MustRemainDisabled(extension, NULL, NULL))) {
546 error_ = ErrorUtils::FormatErrorMessage( 444 error_ = ErrorUtils::FormatErrorMessage(keys::kUserCantModifyError,
547 keys::kUserCantModifyError, extension_id_); 445 extension_id_);
548 return false; 446 return false;
549 } 447 }
550 448
551 bool currently_enabled = service()->IsExtensionEnabled(extension_id_); 449 bool currently_enabled =
450 registry->enabled_extensions().Contains(extension_id_) ||
451 registry->terminated_extensions().Contains(extension_id_);
552 452
553 if (!currently_enabled && params->enabled) { 453 if (!currently_enabled && params->enabled) {
554 ExtensionPrefs* prefs = ExtensionPrefs::Get(GetProfile()); 454 ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context());
555 if (prefs->DidExtensionEscalatePermissions(extension_id_)) { 455 if (prefs->DidExtensionEscalatePermissions(extension_id_)) {
556 if (!user_gesture()) { 456 if (!user_gesture()) {
557 error_ = keys::kGestureNeededForEscalationError; 457 SetError(keys::kGestureNeededForEscalationError);
558 return false; 458 return false;
559 } 459 }
560 AddRef(); // Matched in InstallUIProceed/InstallUIAbort 460 AddRef(); // Matched in InstallUIProceed/InstallUIAbort
561 install_prompt_.reset( 461 install_prompt_ = delegate->SetEnabledFunctionDelegate(this, extension);
562 new ExtensionInstallPrompt(GetAssociatedWebContents()));
563 install_prompt_->ConfirmReEnable(this, extension);
564 return true; 462 return true;
565 } 463 }
566 service()->EnableExtension(extension_id_); 464 delegate->EnableExtension(browser_context(), extension_id_);
567 } else if (currently_enabled && !params->enabled) { 465 } else if (currently_enabled && !params->enabled) {
568 service()->DisableExtension(extension_id_, Extension::DISABLE_USER_ACTION); 466 delegate->DisableExtension(browser_context(), extension_id_,
467 Extension::DISABLE_USER_ACTION);
569 } 468 }
570 469
571 BrowserThread::PostTask( 470 BrowserThread::PostTask(
572 BrowserThread::UI, 471 BrowserThread::UI, FROM_HERE,
573 FROM_HERE,
574 base::Bind(&ManagementSetEnabledFunction::SendResponse, this, true)); 472 base::Bind(&ManagementSetEnabledFunction::SendResponse, this, true));
575 473
576 return true; 474 return true;
577 } 475 }
578 476
579 void ManagementSetEnabledFunction::InstallUIProceed() { 477 void ManagementSetEnabledFunction::InstallUIProceed() {
580 service()->EnableExtension(extension_id_); 478 ManagementAPI::GetFactoryInstance()
479 ->Get(browser_context())
480 ->GetDelegate()
481 ->EnableExtension(browser_context(), extension_id_);
581 SendResponse(true); 482 SendResponse(true);
582 Release(); 483 Release();
583 } 484 }
584 485
585 void ManagementSetEnabledFunction::InstallUIAbort(bool user_initiated) { 486 void ManagementSetEnabledFunction::InstallUIAbort(bool user_initiated) {
586 error_ = keys::kUserDidNotReEnableError; 487 error_ = keys::kUserDidNotReEnableError;
587 SendResponse(false); 488 SendResponse(false);
588 Release(); 489 Release();
589 } 490 }
590 491
591 ManagementUninstallFunctionBase::ManagementUninstallFunctionBase() { 492 ManagementUninstallFunctionBase::ManagementUninstallFunctionBase() {
592 } 493 }
593 494
594 ManagementUninstallFunctionBase::~ManagementUninstallFunctionBase() { 495 ManagementUninstallFunctionBase::~ManagementUninstallFunctionBase() {
595 } 496 }
596 497
597 bool ManagementUninstallFunctionBase::Uninstall( 498 bool ManagementUninstallFunctionBase::Uninstall(
598 const std::string& target_extension_id, 499 const std::string& target_extension_id,
599 bool show_confirm_dialog) { 500 bool show_confirm_dialog) {
501 const ManagementAPIDelegate* delegate = ManagementAPI::GetFactoryInstance()
502 ->Get(browser_context())
503 ->GetDelegate();
600 extension_id_ = target_extension_id; 504 extension_id_ = target_extension_id;
601 const Extension* target_extension = 505 const Extension* target_extension =
602 extensions::ExtensionRegistry::Get(browser_context())-> 506 extensions::ExtensionRegistry::Get(browser_context())
603 GetExtensionById(extension_id_, ExtensionRegistry::EVERYTHING); 507 ->GetExtensionById(extension_id_, ExtensionRegistry::EVERYTHING);
604 if (!target_extension || 508 if (!target_extension ||
605 ui_util::ShouldNotBeVisible(target_extension, browser_context())) { 509 ShouldNotBeVisible(target_extension, browser_context())) {
606 error_ = ErrorUtils::FormatErrorMessage( 510 error_ =
607 keys::kNoExtensionError, extension_id_); 511 ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, extension_id_);
608 return false; 512 return false;
609 } 513 }
610 514
611 if (!ExtensionSystem::Get(GetProfile()) 515 if (!ExtensionSystem::Get(browser_context())
612 ->management_policy() 516 ->management_policy()
613 ->UserMayModifySettings(target_extension, NULL)) { 517 ->UserMayModifySettings(target_extension, NULL)) {
614 error_ = ErrorUtils::FormatErrorMessage( 518 error_ = ErrorUtils::FormatErrorMessage(keys::kUserCantModifyError,
615 keys::kUserCantModifyError, extension_id_); 519 extension_id_);
616 return false; 520 return false;
617 } 521 }
618 522
619 if (auto_confirm_for_test == DO_NOT_SKIP) { 523 if (auto_confirm_for_test == DO_NOT_SKIP) {
620 if (show_confirm_dialog) { 524 if (show_confirm_dialog) {
621 AddRef(); // Balanced in ExtensionUninstallAccepted/Canceled 525 AddRef(); // Balanced in ExtensionUninstallAccepted/Canceled
622 content::WebContents* web_contents = GetAssociatedWebContents(); 526 delegate->UninstallFunctionDelegate(this, target_extension_id);
623 extension_uninstall_dialog_.reset(ExtensionUninstallDialog::Create(
624 GetProfile(),
625 web_contents ? web_contents->GetTopLevelNativeWindow() : NULL,
626 this));
627 if (extension_id() != target_extension_id) {
628 extension_uninstall_dialog_->ConfirmProgrammaticUninstall(
629 target_extension, extension());
630 } else {
631 // If this is a self uninstall, show the generic uninstall dialog.
632 extension_uninstall_dialog_->ConfirmUninstall(target_extension);
633 }
634 } else { 527 } else {
635 Finish(true); 528 Finish(true);
636 } 529 }
637 } else { 530 } else {
638 Finish(auto_confirm_for_test == PROCEED); 531 Finish(auto_confirm_for_test == PROCEED);
639 } 532 }
640 533
641 return true; 534 return true;
642 } 535 }
643 536
644 // static 537 // static
645 void ManagementUninstallFunctionBase::SetAutoConfirmForTest( 538 void ManagementUninstallFunctionBase::SetAutoConfirmForTest(
646 bool should_proceed) { 539 bool should_proceed) {
647 auto_confirm_for_test = should_proceed ? PROCEED : ABORT; 540 auto_confirm_for_test = should_proceed ? PROCEED : ABORT;
648 } 541 }
649 542
650 void ManagementUninstallFunctionBase::Finish(bool should_uninstall) { 543 void ManagementUninstallFunctionBase::Finish(bool should_uninstall) {
651 if (should_uninstall) { 544 if (should_uninstall) {
652 // The extension can be uninstalled in another window while the UI was 545 // The extension can be uninstalled in another window while the UI was
653 // showing. Do nothing in that case. 546 // showing. Do nothing in that case.
654 ExtensionRegistry* registry = ExtensionRegistry::Get(GetProfile()); 547 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context());
655 const Extension* extension = registry->GetExtensionById( 548 const Extension* extension = registry->GetExtensionById(
656 extension_id_, ExtensionRegistry::EVERYTHING); 549 extension_id_, ExtensionRegistry::EVERYTHING);
657 if (!extension) { 550 if (!extension) {
658 error_ = ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, 551 error_ = ErrorUtils::FormatErrorMessage(keys::kNoExtensionError,
659 extension_id_); 552 extension_id_);
660 SendResponse(false); 553 SendResponse(false);
661 } else { 554 } else {
662 bool success = service()->UninstallExtension( 555 const ManagementAPIDelegate* delegate =
663 extension_id_, 556 ManagementAPI::GetFactoryInstance()
557 ->Get(browser_context())
558 ->GetDelegate();
559 bool success = delegate->UninstallExtension(
560 browser_context(), extension_id_,
664 extensions::UNINSTALL_REASON_MANAGEMENT_API, 561 extensions::UNINSTALL_REASON_MANAGEMENT_API,
665 base::Bind(&base::DoNothing), 562 base::Bind(&base::DoNothing), NULL);
666 NULL);
667 563
668 // TODO set error_ if !success 564 // TODO set error_ if !success
669 SendResponse(success); 565 SendResponse(success);
670 } 566 }
671 } else { 567 } else {
672 error_ = ErrorUtils::FormatErrorMessage( 568 error_ = ErrorUtils::FormatErrorMessage(keys::kUninstallCanceledError,
673 keys::kUninstallCanceledError, extension_id_); 569 extension_id_);
674 SendResponse(false); 570 SendResponse(false);
675 } 571 }
676 } 572 }
677 573
678 void ManagementUninstallFunctionBase::ExtensionUninstallAccepted() { 574 void ManagementUninstallFunctionBase::ExtensionUninstallAccepted() {
679 Finish(true); 575 Finish(true);
680 Release(); 576 Release();
681 } 577 }
682 578
683 void ManagementUninstallFunctionBase::ExtensionUninstallCanceled() { 579 void ManagementUninstallFunctionBase::ExtensionUninstallCanceled() {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
750 646
751 bool ManagementCreateAppShortcutFunction::RunAsync() { 647 bool ManagementCreateAppShortcutFunction::RunAsync() {
752 if (!user_gesture()) { 648 if (!user_gesture()) {
753 error_ = keys::kGestureNeededForCreateAppShortcutError; 649 error_ = keys::kGestureNeededForCreateAppShortcutError;
754 return false; 650 return false;
755 } 651 }
756 652
757 scoped_ptr<management::CreateAppShortcut::Params> params( 653 scoped_ptr<management::CreateAppShortcut::Params> params(
758 management::CreateAppShortcut::Params::Create(*args_)); 654 management::CreateAppShortcut::Params::Create(*args_));
759 EXTENSION_FUNCTION_VALIDATE(params.get()); 655 EXTENSION_FUNCTION_VALIDATE(params.get());
760 const Extension* extension = service()->GetExtensionById(params->id, true); 656 const Extension* extension =
657 ExtensionRegistry::Get(browser_context())
658 ->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING);
761 if (!extension) { 659 if (!extension) {
762 error_ = ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, 660 error_ =
763 params->id); 661 ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, params->id);
764 return false; 662 return false;
765 } 663 }
766 664
767 if (!extension->is_app()) { 665 if (!extension->is_app()) {
768 error_ = ErrorUtils::FormatErrorMessage(keys::kNotAnAppError, params->id); 666 error_ = ErrorUtils::FormatErrorMessage(keys::kNotAnAppError, params->id);
769 return false; 667 return false;
770 } 668 }
771 669
772 #if defined(OS_MACOSX) 670 #if defined(OS_MACOSX)
773 if (!extension->is_platform_app()) { 671 if (!extension->is_platform_app()) {
774 error_ = keys::kCreateOnlyPackagedAppShortcutMac; 672 error_ = keys::kCreateOnlyPackagedAppShortcutMac;
775 return false; 673 return false;
776 } 674 }
777 #endif 675 #endif
778 676
779 Browser* browser = chrome::FindBrowserWithProfile( 677 if (auto_confirm_for_test != DO_NOT_SKIP) {
780 GetProfile(), chrome::HOST_DESKTOP_TYPE_NATIVE); 678 // Matched with a Release() in OnCloseShortcutPrompt().
781 if (!browser) { 679 AddRef();
782 // Shouldn't happen if we have user gesture. 680
783 error_ = keys::kNoBrowserToCreateShortcut; 681 OnCloseShortcutPrompt(auto_confirm_for_test == PROCEED);
784 return false; 682
683 return true;
785 } 684 }
786 685
787 // Matched with a Release() in OnCloseShortcutPrompt(). 686 if (ManagementAPI::GetFactoryInstance()
788 AddRef(); 687 ->Get(browser_context())
789 688 ->GetDelegate()
790 if (auto_confirm_for_test == DO_NOT_SKIP) { 689 ->CreateAppShortcutFunctionDelegate(this, extension)) {
791 chrome::ShowCreateChromeAppShortcutsDialog( 690 // Matched with a Release() in OnCloseShortcutPrompt().
792 browser->window()->GetNativeWindow(), browser->profile(), extension, 691 AddRef();
793 base::Bind(&ManagementCreateAppShortcutFunction::OnCloseShortcutPrompt,
794 this));
795 } else {
796 OnCloseShortcutPrompt(auto_confirm_for_test == PROCEED);
797 } 692 }
798 693
799 // Response is sent async in OnCloseShortcutPrompt(). 694 // Response is sent async in OnCloseShortcutPrompt().
800 return true; 695 return true;
801 } 696 }
802 697
803 bool ManagementSetLaunchTypeFunction::RunSync() { 698 bool ManagementSetLaunchTypeFunction::RunSync() {
804 if (!user_gesture()) { 699 if (!user_gesture()) {
805 error_ = keys::kGestureNeededForSetLaunchTypeError; 700 error_ = keys::kGestureNeededForSetLaunchTypeError;
806 return false; 701 return false;
807 } 702 }
808 703
809 scoped_ptr<management::SetLaunchType::Params> params( 704 scoped_ptr<management::SetLaunchType::Params> params(
810 management::SetLaunchType::Params::Create(*args_)); 705 management::SetLaunchType::Params::Create(*args_));
811 EXTENSION_FUNCTION_VALIDATE(params.get()); 706 EXTENSION_FUNCTION_VALIDATE(params.get());
812 const Extension* extension = service()->GetExtensionById(params->id, true); 707 const Extension* extension =
708 ExtensionRegistry::Get(browser_context())
709 ->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING);
710 const ManagementAPIDelegate* delegate = ManagementAPI::GetFactoryInstance()
711 ->Get(browser_context())
712 ->GetDelegate();
813 if (!extension) { 713 if (!extension) {
814 error_ = 714 error_ =
815 ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, params->id); 715 ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, params->id);
816 return false; 716 return false;
817 } 717 }
818 718
819 if (!extension->is_app()) { 719 if (!extension->is_app()) {
820 error_ = ErrorUtils::FormatErrorMessage(keys::kNotAnAppError, params->id); 720 error_ = ErrorUtils::FormatErrorMessage(keys::kNotAnAppError, params->id);
821 return false; 721 return false;
822 } 722 }
823 723
824 std::vector<management::LaunchType> available_launch_types = 724 std::vector<management::LaunchType> available_launch_types =
825 GetAvailableLaunchTypes(*extension); 725 GetAvailableLaunchTypes(*extension, delegate);
826 726
827 management::LaunchType app_launch_type = params->launch_type; 727 management::LaunchType app_launch_type = params->launch_type;
828 if (std::find(available_launch_types.begin(), 728 if (std::find(available_launch_types.begin(), available_launch_types.end(),
829 available_launch_types.end(),
830 app_launch_type) == available_launch_types.end()) { 729 app_launch_type) == available_launch_types.end()) {
831 error_ = keys::kLaunchTypeNotAvailableError; 730 error_ = keys::kLaunchTypeNotAvailableError;
832 return false; 731 return false;
833 } 732 }
834 733
835 LaunchType launch_type = LAUNCH_TYPE_DEFAULT; 734 LaunchType launch_type = LAUNCH_TYPE_DEFAULT;
836 switch (app_launch_type) { 735 switch (app_launch_type) {
837 case management::LAUNCH_TYPE_OPEN_AS_PINNED_TAB: 736 case management::LAUNCH_TYPE_OPEN_AS_PINNED_TAB:
838 launch_type = LAUNCH_TYPE_PINNED; 737 launch_type = LAUNCH_TYPE_PINNED;
839 break; 738 break;
840 case management::LAUNCH_TYPE_OPEN_AS_REGULAR_TAB: 739 case management::LAUNCH_TYPE_OPEN_AS_REGULAR_TAB:
841 launch_type = LAUNCH_TYPE_REGULAR; 740 launch_type = LAUNCH_TYPE_REGULAR;
842 break; 741 break;
843 case management::LAUNCH_TYPE_OPEN_FULL_SCREEN: 742 case management::LAUNCH_TYPE_OPEN_FULL_SCREEN:
844 launch_type = LAUNCH_TYPE_FULLSCREEN; 743 launch_type = LAUNCH_TYPE_FULLSCREEN;
845 break; 744 break;
846 case management::LAUNCH_TYPE_OPEN_AS_WINDOW: 745 case management::LAUNCH_TYPE_OPEN_AS_WINDOW:
847 launch_type = LAUNCH_TYPE_WINDOW; 746 launch_type = LAUNCH_TYPE_WINDOW;
848 break; 747 break;
849 case management::LAUNCH_TYPE_NONE: 748 case management::LAUNCH_TYPE_NONE:
850 NOTREACHED(); 749 NOTREACHED();
851 } 750 }
852 751
853 SetLaunchType(service(), params->id, launch_type); 752 delegate->SetLaunchType(browser_context(), params->id, launch_type);
854 753
855 return true; 754 return true;
856 } 755 }
857 756
858 ManagementGenerateAppForLinkFunction::ManagementGenerateAppForLinkFunction() { 757 ManagementGenerateAppForLinkFunction::ManagementGenerateAppForLinkFunction() {
859 } 758 }
860 759
861 ManagementGenerateAppForLinkFunction::~ManagementGenerateAppForLinkFunction() { 760 ManagementGenerateAppForLinkFunction::~ManagementGenerateAppForLinkFunction() {
862 } 761 }
863 762
864 void ManagementGenerateAppForLinkFunction::FinishCreateBookmarkApp( 763 void ManagementGenerateAppForLinkFunction::FinishCreateBookmarkApp(
865 const Extension* extension, 764 const Extension* extension,
866 const WebApplicationInfo& web_app_info) { 765 const WebApplicationInfo& web_app_info) {
867 if (extension) { 766 if (extension) {
868 scoped_ptr<management::ExtensionInfo> info = 767 scoped_ptr<management::ExtensionInfo> info =
869 CreateExtensionInfo(*extension, ExtensionSystem::Get(GetProfile())); 768 CreateExtensionInfo(*extension, browser_context());
870 results_ = management::GenerateAppForLink::Results::Create(*info); 769 results_ = management::GenerateAppForLink::Results::Create(*info);
871 770
872 SendResponse(true); 771 SendResponse(true);
873 Release(); 772 Release();
874 } else { 773 } else {
875 error_ = keys::kGenerateAppForLinkInstallError; 774 error_ = keys::kGenerateAppForLinkInstallError;
876 SendResponse(false); 775 SendResponse(false);
877 Release(); 776 Release();
878 } 777 }
879 } 778 }
880 779
881 void ManagementGenerateAppForLinkFunction::OnFaviconForApp(
882 const favicon_base::FaviconImageResult& image_result) {
883 WebApplicationInfo web_app;
884 web_app.title = base::UTF8ToUTF16(title_);
885 web_app.app_url = launch_url_;
886
887 if (!image_result.image.IsEmpty()) {
888 WebApplicationInfo::IconInfo icon;
889 icon.data = image_result.image.AsBitmap();
890 icon.width = icon.data.width();
891 icon.height = icon.data.height();
892 web_app.icons.push_back(icon);
893 }
894
895 bookmark_app_helper_.reset(new BookmarkAppHelper(service(), web_app, NULL));
896 bookmark_app_helper_->Create(base::Bind(
897 &ManagementGenerateAppForLinkFunction::FinishCreateBookmarkApp, this));
898 }
899
900 bool ManagementGenerateAppForLinkFunction::RunAsync() { 780 bool ManagementGenerateAppForLinkFunction::RunAsync() {
901 if (!user_gesture()) { 781 if (!user_gesture()) {
902 error_ = keys::kGestureNeededForGenerateAppForLinkError; 782 error_ = keys::kGestureNeededForGenerateAppForLinkError;
903 return false; 783 return false;
904 } 784 }
905 785
906 scoped_ptr<management::GenerateAppForLink::Params> params( 786 scoped_ptr<management::GenerateAppForLink::Params> params(
907 management::GenerateAppForLink::Params::Create(*args_)); 787 management::GenerateAppForLink::Params::Create(*args_));
908 EXTENSION_FUNCTION_VALIDATE(params.get()); 788 EXTENSION_FUNCTION_VALIDATE(params.get());
909 789
910 GURL launch_url(params->url); 790 GURL launch_url(params->url);
911 if (!launch_url.is_valid() || !launch_url.SchemeIsHTTPOrHTTPS()) { 791 if (!launch_url.is_valid() || !launch_url.SchemeIsHTTPOrHTTPS()) {
912 error_ = ErrorUtils::FormatErrorMessage(keys::kInvalidURLError, 792 error_ =
913 params->url); 793 ErrorUtils::FormatErrorMessage(keys::kInvalidURLError, params->url);
914 return false; 794 return false;
915 } 795 }
916 796
917 if (params->title.empty()) { 797 if (params->title.empty()) {
918 error_ = keys::kEmptyTitleError; 798 error_ = keys::kEmptyTitleError;
919 return false; 799 return false;
920 } 800 }
921 801
922 FaviconService* favicon_service = 802 app_for_link_delegate_ =
923 FaviconServiceFactory::GetForProfile(GetProfile(), 803 ManagementAPI::GetFactoryInstance()
924 Profile::EXPLICIT_ACCESS); 804 ->Get(browser_context())
925 DCHECK(favicon_service); 805 ->GetDelegate()
806 ->GenerateAppForLinkFunctionDelegate(this, browser_context(),
807 params->title, launch_url);
926 808
927 title_ = params->title; 809 // Matched with a Release() in FinishCreateBookmarkApp().
928 launch_url_ = launch_url;
929
930 favicon_service->GetFaviconImageForPageURL(
931 launch_url,
932 base::Bind(&ManagementGenerateAppForLinkFunction::OnFaviconForApp, this),
933 &cancelable_task_tracker_);
934
935 // Matched with a Release() in OnExtensionLoaded().
936 AddRef(); 810 AddRef();
937 811
938 // Response is sent async in OnExtensionLoaded(). 812 // Response is sent async in FinishCreateBookmarkApp().
939 return true; 813 return true;
940 } 814 }
941 815
942 ManagementEventRouter::ManagementEventRouter(content::BrowserContext* context) 816 ManagementEventRouter::ManagementEventRouter(content::BrowserContext* context)
943 : browser_context_(context), extension_registry_observer_(this) { 817 : browser_context_(context), extension_registry_observer_(this) {
944 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); 818 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
945 } 819 }
946 820
947 ManagementEventRouter::~ManagementEventRouter() {} 821 ManagementEventRouter::~ManagementEventRouter() {
822 }
948 823
949 void ManagementEventRouter::OnExtensionLoaded( 824 void ManagementEventRouter::OnExtensionLoaded(
950 content::BrowserContext* browser_context, 825 content::BrowserContext* browser_context,
951 const Extension* extension) { 826 const Extension* extension) {
952 BroadcastEvent(extension, management::OnEnabled::kEventName); 827 BroadcastEvent(extension, management::OnEnabled::kEventName);
953 } 828 }
954 829
955 void ManagementEventRouter::OnExtensionUnloaded( 830 void ManagementEventRouter::OnExtensionUnloaded(
956 content::BrowserContext* browser_context, 831 content::BrowserContext* browser_context,
957 const Extension* extension, 832 const Extension* extension,
(...skipping 10 matching lines...) Expand all
968 843
969 void ManagementEventRouter::OnExtensionUninstalled( 844 void ManagementEventRouter::OnExtensionUninstalled(
970 content::BrowserContext* browser_context, 845 content::BrowserContext* browser_context,
971 const Extension* extension, 846 const Extension* extension,
972 extensions::UninstallReason reason) { 847 extensions::UninstallReason reason) {
973 BroadcastEvent(extension, management::OnUninstalled::kEventName); 848 BroadcastEvent(extension, management::OnUninstalled::kEventName);
974 } 849 }
975 850
976 void ManagementEventRouter::BroadcastEvent(const Extension* extension, 851 void ManagementEventRouter::BroadcastEvent(const Extension* extension,
977 const char* event_name) { 852 const char* event_name) {
978 if (ui_util::ShouldNotBeVisible(extension, browser_context_)) 853 if (ShouldNotBeVisible(extension, browser_context_))
979 return; // Don't dispatch events for built-in extenions. 854 return; // Don't dispatch events for built-in extenions.
980 scoped_ptr<base::ListValue> args(new base::ListValue()); 855 scoped_ptr<base::ListValue> args(new base::ListValue());
981 if (event_name == management::OnUninstalled::kEventName) { 856 if (event_name == management::OnUninstalled::kEventName) {
982 args->Append(new base::StringValue(extension->id())); 857 args->Append(new base::StringValue(extension->id()));
983 } else { 858 } else {
984 scoped_ptr<management::ExtensionInfo> info = 859 scoped_ptr<management::ExtensionInfo> info =
985 CreateExtensionInfo(*extension, ExtensionSystem::Get(browser_context_)); 860 CreateExtensionInfo(*extension, browser_context_);
986 args->Append(info->ToValue().release()); 861 args->Append(info->ToValue().release());
987 } 862 }
988 863
989 EventRouter::Get(browser_context_) 864 EventRouter::Get(browser_context_)
990 ->BroadcastEvent(scoped_ptr<Event>(new Event(event_name, args.Pass()))); 865 ->BroadcastEvent(scoped_ptr<Event>(new Event(event_name, args.Pass())));
991 } 866 }
992 867
993 ManagementAPI::ManagementAPI(content::BrowserContext* context) 868 ManagementAPI::ManagementAPI(content::BrowserContext* context)
994 : browser_context_(context) { 869 : browser_context_(context),
870 delegate_(ExtensionsAPIClient::Get()->CreateManagementAPIDelegate()) {
995 EventRouter* event_router = EventRouter::Get(browser_context_); 871 EventRouter* event_router = EventRouter::Get(browser_context_);
996 event_router->RegisterObserver(this, management::OnInstalled::kEventName); 872 event_router->RegisterObserver(this, management::OnInstalled::kEventName);
997 event_router->RegisterObserver(this, management::OnUninstalled::kEventName); 873 event_router->RegisterObserver(this, management::OnUninstalled::kEventName);
998 event_router->RegisterObserver(this, management::OnEnabled::kEventName); 874 event_router->RegisterObserver(this, management::OnEnabled::kEventName);
999 event_router->RegisterObserver(this, management::OnDisabled::kEventName); 875 event_router->RegisterObserver(this, management::OnDisabled::kEventName);
1000 } 876 }
1001 877
1002 ManagementAPI::~ManagementAPI() { 878 ManagementAPI::~ManagementAPI() {
1003 } 879 }
1004 880
1005 void ManagementAPI::Shutdown() { 881 void ManagementAPI::Shutdown() {
1006 EventRouter::Get(browser_context_)->UnregisterObserver(this); 882 EventRouter::Get(browser_context_)->UnregisterObserver(this);
1007 } 883 }
1008 884
1009 static base::LazyInstance<BrowserContextKeyedAPIFactory<ManagementAPI> > 885 static base::LazyInstance<BrowserContextKeyedAPIFactory<ManagementAPI>>
1010 g_factory = LAZY_INSTANCE_INITIALIZER; 886 g_factory = LAZY_INSTANCE_INITIALIZER;
1011 887
1012 // static 888 // static
1013 BrowserContextKeyedAPIFactory<ManagementAPI>* 889 BrowserContextKeyedAPIFactory<ManagementAPI>*
1014 ManagementAPI::GetFactoryInstance() { 890 ManagementAPI::GetFactoryInstance() {
1015 return g_factory.Pointer(); 891 return g_factory.Pointer();
1016 } 892 }
1017 893
1018 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { 894 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) {
1019 management_event_router_.reset(new ManagementEventRouter(browser_context_)); 895 management_event_router_.reset(new ManagementEventRouter(browser_context_));
1020 EventRouter::Get(browser_context_)->UnregisterObserver(this); 896 EventRouter::Get(browser_context_)->UnregisterObserver(this);
1021 } 897 }
1022 898
1023 } // namespace extensions 899 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/management/management_api.h ('k') | extensions/browser/api/management/management_api_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698