install_extension_handler.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/webui/extensions/install_extension_handler.h"
6
7#include "base/bind.h"
8#include "base/string_util.h"
9#include "base/utf_string_conversions.h"
10#include "chrome/browser/extensions/crx_installer.h"
11#include "chrome/browser/extensions/extension_install_prompt.h"
12#include "chrome/browser/extensions/extension_service.h"
13#include "chrome/browser/extensions/extension_system.h"
14#include "chrome/browser/profiles/profile.h"
15#include "chrome/common/extensions/feature_switch.h"
16#include "content/public/browser/web_contents.h"
17#include "content/public/browser/web_contents_view.h"
18#include "content/public/browser/web_ui.h"
19#include "grit/generated_resources.h"
20#include "net/base/net_util.h"
21#include "ui/base/l10n/l10n_util.h"
22#include "webkit/glue/webdropdata.h"
23
24InstallExtensionHandler::InstallExtensionHandler() {
25}
26
27InstallExtensionHandler::~InstallExtensionHandler() {
28}
29
30void InstallExtensionHandler::GetLocalizedValues(
31    DictionaryValue* localized_strings) {
32  DCHECK(localized_strings);
33  localized_strings->SetString(
34      "extensionSettingsInstallDropTarget",
35      l10n_util::GetStringUTF16(IDS_EXTENSIONS_INSTALL_DROP_TARGET));
36  localized_strings->SetBoolean(
37      "offStoreInstallEnabled",
38      extensions::FeatureSwitch::easy_off_store_install()->IsEnabled());
39}
40
41void InstallExtensionHandler::RegisterMessages() {
42  web_ui()->RegisterMessageCallback(
43      "startDrag",
44      base::Bind(&InstallExtensionHandler::HandleStartDragMessage,
45                 base::Unretained(this)));
46  web_ui()->RegisterMessageCallback(
47      "stopDrag",
48      base::Bind(&InstallExtensionHandler::HandleStopDragMessage,
49                 base::Unretained(this)));
50  web_ui()->RegisterMessageCallback(
51      "installDroppedFile",
52      base::Bind(&InstallExtensionHandler::HandleInstallMessage,
53                 base::Unretained(this)));
54}
55
56void InstallExtensionHandler::HandleStartDragMessage(const ListValue* args) {
57  WebDropData* drop_data = web_ui()->GetWebContents()->GetView()->GetDropData();
58  if (!drop_data) {
59    DLOG(ERROR) << "No current drop data.";
60    return;
61  }
62
63  if (drop_data->filenames.empty()) {
64    DLOG(ERROR) << "Current drop data contains no files.";
65    return;
66  }
67
68  file_to_install_ = FilePath::FromWStringHack(
69      UTF16ToWide(drop_data->filenames.front().path));
70}
71
72void InstallExtensionHandler::HandleStopDragMessage(const ListValue* args) {
73  file_to_install_.clear();
74}
75
76void InstallExtensionHandler::HandleInstallMessage(const ListValue* args) {
77  if (file_to_install_.empty()) {
78    LOG(ERROR) << "No file captured to install.";
79    return;
80  }
81
82  Profile* profile = Profile::FromBrowserContext(
83      web_ui()->GetWebContents()->GetBrowserContext());
84  scoped_refptr<extensions::CrxInstaller> crx_installer(
85      extensions::CrxInstaller::Create(
86          extensions::ExtensionSystem::Get(profile)->extension_service(),
87          new ExtensionInstallPrompt(web_ui()->GetWebContents())));
88  crx_installer->set_error_on_unsupported_requirements(true);
89  crx_installer->set_off_store_install_allow_reason(
90      extensions::CrxInstaller::OffStoreInstallAllowedFromSettingsPage);
91  crx_installer->set_install_wait_for_idle(false);
92
93  const bool kCaseSensitive = false;
94
95  // Have to use EndsWith() because FilePath::Extension() would return ".js" for
96  // "foo.user.js".
97  if (EndsWith(file_to_install_.BaseName().value(),
98               FILE_PATH_LITERAL(".user.js"),
99               kCaseSensitive)) {
100    crx_installer->InstallUserScript(
101        file_to_install_,
102        net::FilePathToFileURL(file_to_install_));
103  } else if (EndsWith(file_to_install_.BaseName().value(),
104                      FILE_PATH_LITERAL(".crx"),
105                      kCaseSensitive)) {
106    crx_installer->InstallCrx(file_to_install_);
107  } else {
108    CHECK(false);
109  }
110
111  file_to_install_.clear();
112}
113