summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extensions_ui.cc
blob: 6c1c91873dd4caeab6923e3972ad65b3219e0f57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/extensions_ui.h"

#include "base/thread.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/extensions/extension_error_reporter.h"
#include "chrome/browser/profile.h"
#include "chrome/common/jstemplate_builder.h"
#include "chrome/common/l10n_util.h"
#include "chrome/common/resource_bundle.h"
#include "chrome/common/url_constants.h"
#include "net/base/net_util.h"

#include "grit/browser_resources.h"
#include "grit/generated_resources.h"

// ExtensionsUI is accessible from chrome-ui://extensions.
static const char kExtensionsHost[] = "extensions";

////////////////////////////////////////////////////////////////////////////////
//
// ExtensionsHTMLSource
//
////////////////////////////////////////////////////////////////////////////////

ExtensionsUIHTMLSource::ExtensionsUIHTMLSource()
    : DataSource(kExtensionsHost, MessageLoop::current()) {
}

void ExtensionsUIHTMLSource::StartDataRequest(const std::string& path,
                                              int request_id) {
  DictionaryValue localized_strings;
  localized_strings.SetString(L"title",
      l10n_util::GetString(IDS_EXTENSIONS_TITLE));

  static const StringPiece extensions_html(
      ResourceBundle::GetSharedInstance().GetRawDataResource(
          IDR_EXTENSIONS_HTML));
  const std::string full_html = jstemplate_builder::GetTemplateHtml(
      extensions_html, &localized_strings, "root");

  scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
  html_bytes->data.resize(full_html.size());
  std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin());

  SendResponse(request_id, html_bytes);
}

///////////////////////////////////////////////////////////////////////////////
//
// ExtensionsDOMHandler
//
///////////////////////////////////////////////////////////////////////////////

ExtensionsDOMHandler::ExtensionsDOMHandler(DOMUI* dom_ui,
                                           ExtensionsService* extension_service)
    : DOMMessageHandler(dom_ui), extensions_service_(extension_service) {
  dom_ui_->RegisterMessageCallback("requestExtensionsData",
      NewCallback(this, &ExtensionsDOMHandler::HandleRequestExtensionsData));
}

void ExtensionsDOMHandler::HandleRequestExtensionsData(const Value* value) {
  DictionaryValue results;

  // Add the extensions to the results structure.
  ListValue *extensions_list = new ListValue();
  const ExtensionList* extensions = extensions_service_->extensions();
  for (ExtensionList::const_iterator extension = extensions->begin();
       extension != extensions->end(); ++extension) {
     extensions_list->Append(CreateExtensionDetailValue(*extension));
  }
  results.Set(L"extensions", extensions_list);

  // Add any error log lines to the result structure.
  ListValue *errors_list = new ListValue();
  const std::vector<std::string>* errors =
      ExtensionErrorReporter::GetInstance()->GetErrors();
  for (std::vector<std::string>::const_iterator error = errors->begin();
       error != errors->end(); ++error) {
    errors_list->Append(Value::CreateStringValue(*error));
  }
  results.Set(L"errors", errors_list);

  dom_ui_->CallJavascriptFunction(L"returnExtensionsData", results);
}

// Static
DictionaryValue* ExtensionsDOMHandler::CreateContentScriptDetailValue(
  const UserScript& script, const FilePath& extension_path) {
  DictionaryValue* script_data = new DictionaryValue();

  // TODO(rafaelw): When UserScript supports multiple js, this will have to
  // put them all in this list;
  ListValue *js_list = new ListValue();
  // We are passing through GURLs to canonicalize the output to a valid
  // URL path fragment.
  GURL script_url = net::FilePathToFileURL(script.path());
  GURL extension_url = net::FilePathToFileURL(extension_path);
  std::string relative_js_path =
      script_url.spec().substr(extension_url.spec().length() + 1);

  js_list->Append(new StringValue(relative_js_path));
  script_data->Set(L"js", js_list);

  // Get list of glob "matches" strings
  ListValue *url_pattern_list = new ListValue();
  const std::vector<URLPattern>& url_patterns = script.url_patterns();
  for (std::vector<URLPattern>::const_iterator url_pattern =
      url_patterns.begin();
    url_pattern != url_patterns.end(); ++url_pattern) {
    url_pattern_list->Append(new StringValue(url_pattern->GetAsString()));
  }

  script_data->Set(L"matches", url_pattern_list);

  return script_data;
}

// Static
DictionaryValue* ExtensionsDOMHandler::CreateExtensionDetailValue(
    const Extension *extension) {
  DictionaryValue* extension_data = new DictionaryValue();

  extension_data->SetString(L"name", extension->name());
  extension_data->SetString(L"description", extension->description());
  extension_data->SetString(L"version", extension->version()->GetString());

  // Add list of content_script detail DictionaryValues
  ListValue *content_script_list = new ListValue();
  UserScriptList content_scripts = extension->content_scripts();
  for (UserScriptList::const_iterator script = content_scripts.begin();
    script != content_scripts.end(); ++script) {
      content_script_list->Append(CreateContentScriptDetailValue(*script,
          extension->path()));
  }
  extension_data->Set(L"content_scripts", content_script_list);

  return extension_data;
}

ExtensionsDOMHandler::~ExtensionsDOMHandler() {
}

// ExtensionsDOMHandler, public: -----------------------------------------------

void ExtensionsDOMHandler::Init() {
}

ExtensionsUI::ExtensionsUI(DOMUIContents* contents) : DOMUI(contents) {
}

void ExtensionsUI::Init() {
  ExtensionsService *exstension_service = get_profile()->GetExtensionsService();

  ExtensionsDOMHandler* handler = new ExtensionsDOMHandler(this,
      exstension_service);
  AddMessageHandler(handler);
  handler->Init();

  ExtensionsUIHTMLSource* html_source = new ExtensionsUIHTMLSource();

  // Set up the chrome-ui://extensions/ source.
  g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
      NewRunnableMethod(&chrome_url_data_manager,
          &ChromeURLDataManager::AddDataSource, html_source));
}

// static
GURL ExtensionsUI::GetBaseURL() {
  std::string url = DOMUIContents::GetScheme();
  url += chrome::kStandardSchemeSeparator;
  url += kExtensionsHost;
  return GURL(url);
}