app_list_controller_delegate.cc revision 1e9bf3e0803691d0a228da41fc608347b6db4340
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
78bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/extensions/extension_service.h"
88bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/extensions/extension_system.h"
91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/extensions/management_policy.h"
101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/app_list/extension_uninstaller.h"
111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/browser_navigator.h"
121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/common/extensions/extension.h"
1368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "chrome/common/extensions/extension_constants.h"
141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/common/extensions/manifest_url_handler.h"
151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "net/base/url_util.h"
161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace {
181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const extensions::Extension* GetExtension(Profile* profile,
201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                              const std::string& extension_id) {
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const ExtensionService* service =
221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      extensions::ExtensionSystem::Get(profile)->extension_service();
231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const extensions::Extension* extension =
241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      service->GetInstalledExtension(extension_id);
251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return extension;
261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}  // namespace
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)AppListControllerDelegate::~AppListControllerDelegate() {}
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void AppListControllerDelegate::ViewClosing() {}
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void AppListControllerDelegate::OnShowExtensionPrompt() {}
351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void AppListControllerDelegate::OnCloseExtensionPrompt() {}
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)std::string AppListControllerDelegate::AppListSourceToString(
381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    AppListSource source) {
391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  switch (source) {
401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case LAUNCH_FROM_APP_LIST:
411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return extension_urls::kLaunchSourceAppList;
421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case LAUNCH_FROM_APP_LIST_SEARCH:
431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return extension_urls::kLaunchSourceAppListSearch;
441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    default: return std::string();
451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool AppListControllerDelegate::UserMayModifySettings(
491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    Profile* profile,
501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const std::string& app_id) {
511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const extensions::Extension* extension = GetExtension(profile, app_id);
521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const extensions::ManagementPolicy* policy =
531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      extensions::ExtensionSystem::Get(profile)->management_policy();
541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return extension &&
551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)         policy->UserMayModifySettings(extension, NULL);
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void AppListControllerDelegate::UninstallApp(Profile* profile,
591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                             const std::string& app_id) {
601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // ExtensionUninstall deletes itself when done or aborted.
611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ExtensionUninstaller* uninstaller =
621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      new ExtensionUninstaller(profile, app_id, this);
631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  uninstaller->Run();
641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool AppListControllerDelegate::IsAppFromWebStore(
671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    Profile* profile,
681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const std::string& app_id) {
691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const extensions::Extension* extension = GetExtension(profile, app_id);
701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return extension && extension->from_webstore();
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void AppListControllerDelegate::ShowAppInWebStore(
741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    Profile* profile,
751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const std::string& app_id,
761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    bool is_search_result) {
771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const extensions::Extension* extension = GetExtension(profile, app_id);
781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!extension)
791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const GURL url = extensions::ManifestURL::GetDetailsURL(extension);
821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK_NE(url, GURL::EmptyGURL());
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const std::string source = AppListSourceToString(
851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      is_search_result ?
861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          AppListControllerDelegate::LAUNCH_FROM_APP_LIST_SEARCH :
871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          AppListControllerDelegate::LAUNCH_FROM_APP_LIST);
881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  chrome::NavigateParams params(
891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      profile,
901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      net::AppendQueryParameter(url,
911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                extension_urls::kWebstoreSourceField,
921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                source),
931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      content::PAGE_TRANSITION_LINK);
941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  chrome::Navigate(&params);
958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool AppListControllerDelegate::HasOptionsPage(
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* profile,
991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const std::string& app_id) {
1001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const ExtensionService* service =
1018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      extensions::ExtensionSystem::Get(profile)->extension_service();
1021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const extensions::Extension* extension = GetExtension(profile, app_id);
1031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return service->IsExtensionEnabledForLauncher(app_id) &&
1041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)         extension &&
1051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)         !extensions::ManifestURL::GetOptionsPage(extension).is_empty();
1061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void AppListControllerDelegate::ShowOptionsPage(
1091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    Profile* profile,
1101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const std::string& app_id) {
1111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const extensions::Extension* extension = GetExtension(profile, app_id);
1121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!extension)
1138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
1141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  chrome::NavigateParams params(
1161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      profile,
1171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      extensions::ManifestURL::GetOptionsPage(extension),
1181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      content::PAGE_TRANSITION_LINK);
1191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  chrome::Navigate(&params);
1208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)extensions::ExtensionPrefs::LaunchType
1231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)AppListControllerDelegate::GetExtensionLaunchType(
1241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    Profile* profile,
1251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const std::string& app_id) {
1261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ExtensionService* service =
1271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      extensions::ExtensionSystem::Get(profile)->extension_service();
1281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return service->extension_prefs()->
1291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      GetLaunchType(GetExtension(profile, app_id),
1301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                    extensions::ExtensionPrefs::LAUNCH_DEFAULT);
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void AppListControllerDelegate::SetExtensionLaunchType(
1341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    Profile* profile,
1351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const std::string& extension_id,
1361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    extensions::ExtensionPrefs::LaunchType launch_type) {
1371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ExtensionService* service =
1381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      extensions::ExtensionSystem::Get(profile)->extension_service();
1391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service->extension_prefs()->SetLaunchType(extension_id, launch_type);
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
14168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
1421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool AppListControllerDelegate::IsExtensionInstalled(
1431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    Profile* profile, const std::string& app_id) {
1441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return !!GetExtension(profile, app_id);
14568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
146