1 // Copyright 2014 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.
5 #include "chrome/browser/ui/webui/extensions/extension_error_ui_util.h"
10 #include "base/file_util.h"
11 #include "base/files/file_path.h"
12 #include "base/location.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/threading/sequenced_worker_pool.h"
16 #include "base/values.h"
17 #include "chrome/browser/devtools/devtools_window.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_finder.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/render_view_host.h"
24 #include "content/public/browser/web_contents.h"
25 #include "extensions/browser/extension_error.h"
26 #include "extensions/browser/extension_registry.h"
27 #include "extensions/browser/file_highlighter.h"
28 #include "extensions/common/constants.h"
29 #include "extensions/common/extension.h"
31 namespace extensions
{
32 namespace error_ui_util
{
36 // Keys for objects passed to and from extension error UI.
37 const char kPathSuffixKey
[] = "pathSuffix";
38 const char kTitleKey
[] = "title";
40 std::string
ReadFileToString(const base::FilePath
& path
) {
42 base::ReadFileToString(path
, &data
);
46 void GetManifestFileCallback(base::DictionaryValue
* results
,
47 const std::string
& key
,
48 const std::string
& specific
,
49 const RequestFileSourceCallback
& response
,
50 const std::string
& contents
) {
51 ManifestHighlighter
highlighter(contents
, key
, specific
);
52 highlighter
.SetHighlightedRegions(results
);
53 response
.Run(*results
);
56 void GetSourceFileCallback(base::DictionaryValue
* results
,
58 const RequestFileSourceCallback
& response
,
59 const std::string
& contents
) {
60 SourceHighlighter
highlighter(contents
, line_number
);
61 highlighter
.SetHighlightedRegions(results
);
62 response
.Run(*results
);
67 void HandleRequestFileSource(const base::DictionaryValue
* args
,
69 const RequestFileSourceCallback
& response
) {
70 // Three required arguments: extension_id, path_suffix, and error_message.
71 std::string extension_id
;
72 base::FilePath::StringType path_suffix_string
;
73 base::string16 error_message
;
75 if (!args
->GetString(kPathSuffixKey
, &path_suffix_string
) ||
76 !args
->GetString(ExtensionError::kExtensionIdKey
, &extension_id
) ||
77 !args
->GetString(ExtensionError::kMessageKey
, &error_message
)) {
82 const Extension
* extension
=
83 ExtensionRegistry::Get(profile
)->GetExtensionById(
84 extension_id
, ExtensionRegistry::EVERYTHING
);
91 // Under no circumstances should we ever need to reference a file outside of
92 // the extension's directory. If it tries to, abort.
93 base::FilePath
path_suffix(path_suffix_string
);
94 if (path_suffix
.ReferencesParent())
97 base::FilePath path
= extension
->path().Append(path_suffix
);
99 // Setting the title and the error message is the same for all file types.
100 scoped_ptr
<base::DictionaryValue
> results(new base::DictionaryValue
);
101 results
->SetString(kTitleKey
,
102 base::UTF8ToUTF16(extension
->name()) +
103 base::ASCIIToUTF16(": ") +
104 path
.BaseName().LossyDisplayName());
105 results
->SetString(ExtensionError::kMessageKey
, error_message
);
107 base::Callback
<void(const std::string
&)> reply
;
108 if (path_suffix_string
== kManifestFilename
) {
109 std::string manifest_key
;
110 if (!args
->GetString(ManifestError::kManifestKeyKey
, &manifest_key
)) {
115 // A "specific" location is optional.
116 std::string specific
;
117 args
->GetString(ManifestError::kManifestSpecificKey
, &specific
);
119 reply
= base::Bind(&GetManifestFileCallback
,
120 base::Owned(results
.release()),
126 args
->GetInteger(RuntimeError::kLineNumberKey
, &line_number
);
128 reply
= base::Bind(&GetSourceFileCallback
,
129 base::Owned(results
.release()),
134 base::PostTaskAndReplyWithResult(content::BrowserThread::GetBlockingPool(),
136 base::Bind(&ReadFileToString
, path
),
140 void HandleOpenDevTools(const base::DictionaryValue
* args
) {
141 int render_process_id
= 0;
142 int render_view_id
= 0;
144 // The render view and render process ids are required.
145 if (!args
->GetInteger(RuntimeError::kRenderProcessIdKey
,
146 &render_process_id
) ||
147 !args
->GetInteger(RuntimeError::kRenderViewIdKey
, &render_view_id
)) {
152 content::RenderViewHost
* rvh
=
153 content::RenderViewHost::FromID(render_process_id
, render_view_id
);
155 // It's possible that the render view was closed since we last updated the
156 // links. Handle this gracefully.
160 // If we include a url, we should inspect it specifically (and not just the
163 if (args
->GetString(RuntimeError::kUrlKey
, &url
)) {
164 // Line and column numbers are optional; default to the first line.
166 int column_number
= 1;
167 args
->GetInteger(RuntimeError::kLineNumberKey
, &line_number
);
168 args
->GetInteger(RuntimeError::kColumnNumberKey
, &column_number
);
170 // Line/column numbers are reported in display-friendly 1-based numbers,
171 // but are inspected in zero-based numbers.
172 DevToolsWindow::OpenDevToolsWindow(
174 DevToolsToggleAction::Reveal(url
, line_number
- 1, column_number
- 1));
176 DevToolsWindow::OpenDevToolsWindow(rvh
);
179 // Once we open the inspector, we focus on the appropriate tab...
180 content::WebContents
* web_contents
=
181 content::WebContents::FromRenderViewHost(rvh
);
182 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents
);
184 // ... but some pages (popups and apps) don't have tabs, and some (background
185 // pages) don't have an associated browser. For these, the inspector opens in
186 // a new window, and our work is done.
187 if (!browser
|| !browser
->is_type_tabbed())
190 TabStripModel
* tab_strip
= browser
->tab_strip_model();
191 tab_strip
->ActivateTabAt(tab_strip
->GetIndexOfWebContents(web_contents
),
192 false); // Not through direct user gesture.
195 } // namespace error_ui_util
196 } // namespace extensions