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

Side by Side Diff: chrome/browser/extensions/api/developer_private/developer_private_api.cc

Issue 973303002: [Extensions] Make chrome://extensions use developerPrivate for error calls (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Kalman's Created 5 years, 9 months 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/developer_private/developer_private_api. h" 5 #include "chrome/browser/extensions/api/developer_private/developer_private_api. h"
6 6
7 #include "apps/app_load_service.h" 7 #include "apps/app_load_service.h"
8 #include "apps/saved_files_service.h" 8 #include "apps/saved_files_service.h"
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/files/file_enumerator.h" 12 #include "base/files/file_enumerator.h"
13 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
14 #include "base/i18n/file_util_icu.h" 14 #include "base/i18n/file_util_icu.h"
15 #include "base/lazy_instance.h" 15 #include "base/lazy_instance.h"
16 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
18 #include "base/values.h" 19 #include "base/values.h"
19 #include "chrome/browser/devtools/devtools_window.h" 20 #include "chrome/browser/devtools/devtools_window.h"
20 #include "chrome/browser/extensions/api/developer_private/entry_picker.h" 21 #include "chrome/browser/extensions/api/developer_private/entry_picker.h"
21 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" 22 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
22 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" 23 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h"
23 #include "chrome/browser/extensions/chrome_requirements_checker.h" 24 #include "chrome/browser/extensions/chrome_requirements_checker.h"
24 #include "chrome/browser/extensions/devtools_util.h" 25 #include "chrome/browser/extensions/devtools_util.h"
25 #include "chrome/browser/extensions/extension_disabled_ui.h" 26 #include "chrome/browser/extensions/extension_disabled_ui.h"
26 #include "chrome/browser/extensions/extension_error_reporter.h" 27 #include "chrome/browser/extensions/extension_error_reporter.h"
27 #include "chrome/browser/extensions/extension_service.h" 28 #include "chrome/browser/extensions/extension_service.h"
28 #include "chrome/browser/extensions/extension_ui_util.h" 29 #include "chrome/browser/extensions/extension_ui_util.h"
29 #include "chrome/browser/extensions/extension_util.h" 30 #include "chrome/browser/extensions/extension_util.h"
30 #include "chrome/browser/extensions/unpacked_installer.h" 31 #include "chrome/browser/extensions/unpacked_installer.h"
31 #include "chrome/browser/extensions/updater/extension_updater.h" 32 #include "chrome/browser/extensions/updater/extension_updater.h"
32 #include "chrome/browser/platform_util.h" 33 #include "chrome/browser/platform_util.h"
33 #include "chrome/browser/profiles/profile.h" 34 #include "chrome/browser/profiles/profile.h"
34 #include "chrome/browser/sync_file_system/syncable_file_system_util.h" 35 #include "chrome/browser/sync_file_system/syncable_file_system_util.h"
36 #include "chrome/browser/ui/browser_finder.h"
35 #include "chrome/browser/ui/chrome_select_file_policy.h" 37 #include "chrome/browser/ui/chrome_select_file_policy.h"
36 #include "chrome/browser/ui/webui/extensions/extension_error_ui_util.h" 38 #include "chrome/browser/ui/tabs/tab_strip_model.h"
37 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" 39 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
38 #include "chrome/common/extensions/api/developer_private.h" 40 #include "chrome/common/extensions/api/developer_private.h"
39 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" 41 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
40 #include "chrome/common/url_constants.h" 42 #include "chrome/common/url_constants.h"
41 #include "chrome/grit/generated_resources.h" 43 #include "chrome/grit/generated_resources.h"
42 #include "content/public/browser/browser_thread.h" 44 #include "content/public/browser/browser_thread.h"
43 #include "content/public/browser/notification_service.h" 45 #include "content/public/browser/notification_service.h"
44 #include "content/public/browser/render_process_host.h" 46 #include "content/public/browser/render_process_host.h"
45 #include "content/public/browser/render_view_host.h" 47 #include "content/public/browser/render_view_host.h"
46 #include "content/public/browser/site_instance.h" 48 #include "content/public/browser/site_instance.h"
47 #include "content/public/browser/storage_partition.h" 49 #include "content/public/browser/storage_partition.h"
48 #include "content/public/browser/web_contents.h" 50 #include "content/public/browser/web_contents.h"
49 #include "extensions/browser/api/device_permissions_manager.h" 51 #include "extensions/browser/api/device_permissions_manager.h"
50 #include "extensions/browser/app_window/app_window.h" 52 #include "extensions/browser/app_window/app_window.h"
51 #include "extensions/browser/app_window/app_window_registry.h" 53 #include "extensions/browser/app_window/app_window_registry.h"
52 #include "extensions/browser/extension_error.h" 54 #include "extensions/browser/extension_error.h"
53 #include "extensions/browser/extension_prefs.h" 55 #include "extensions/browser/extension_prefs.h"
54 #include "extensions/browser/extension_registry.h" 56 #include "extensions/browser/extension_registry.h"
55 #include "extensions/browser/extension_system.h" 57 #include "extensions/browser/extension_system.h"
58 #include "extensions/browser/file_highlighter.h"
56 #include "extensions/browser/management_policy.h" 59 #include "extensions/browser/management_policy.h"
57 #include "extensions/browser/notification_types.h" 60 #include "extensions/browser/notification_types.h"
58 #include "extensions/browser/view_type_utils.h" 61 #include "extensions/browser/view_type_utils.h"
59 #include "extensions/common/constants.h" 62 #include "extensions/common/constants.h"
60 #include "extensions/common/extension_resource.h" 63 #include "extensions/common/extension_resource.h"
61 #include "extensions/common/extension_set.h" 64 #include "extensions/common/extension_set.h"
62 #include "extensions/common/install_warning.h" 65 #include "extensions/common/install_warning.h"
63 #include "extensions/common/manifest.h" 66 #include "extensions/common/manifest.h"
64 #include "extensions/common/manifest_handlers/background_info.h" 67 #include "extensions/common/manifest_handlers/background_info.h"
65 #include "extensions/common/manifest_handlers/icons_handler.h" 68 #include "extensions/common/manifest_handlers/icons_handler.h"
(...skipping 27 matching lines...) Expand all
93 const char kSupervisedUserError[] = 96 const char kSupervisedUserError[] =
94 "Supervised users cannot modify extension settings."; 97 "Supervised users cannot modify extension settings.";
95 const char kCannotModifyPolicyExtensionError[] = 98 const char kCannotModifyPolicyExtensionError[] =
96 "Cannot modify the extension by policy."; 99 "Cannot modify the extension by policy.";
97 const char kRequiresUserGestureError[] = 100 const char kRequiresUserGestureError[] =
98 "This action requires a user gesture."; 101 "This action requires a user gesture.";
99 const char kCouldNotShowSelectFileDialogError[] = 102 const char kCouldNotShowSelectFileDialogError[] =
100 "Could not show a file chooser."; 103 "Could not show a file chooser.";
101 const char kFileSelectionCanceled[] = 104 const char kFileSelectionCanceled[] =
102 "File selection was canceled."; 105 "File selection was canceled.";
106 const char kInvalidPathError[] = "Invalid path.";
107 const char kManifestKeyIsRequiredError[] =
108 "The 'manifestKey' argument is required for manifest files.";
109 const char kNoSuchRendererError[] = "Could not find the renderer.";
103 110
104 const char kUnpackedAppsFolder[] = "apps_target"; 111 const char kUnpackedAppsFolder[] = "apps_target";
105 112
106 ExtensionService* GetExtensionService(content::BrowserContext* context) { 113 ExtensionService* GetExtensionService(content::BrowserContext* context) {
107 return ExtensionSystem::Get(context)->extension_service(); 114 return ExtensionSystem::Get(context)->extension_service();
108 } 115 }
109 116
110 ExtensionUpdater* GetExtensionUpdater(Profile* profile) { 117 ExtensionUpdater* GetExtensionUpdater(Profile* profile) {
111 return GetExtensionService(profile)->updater(); 118 return GetExtensionService(profile)->updater();
112 } 119 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 event_data.event_type = event_type; 170 event_data.event_type = event_type;
164 event_data.item_id = item_id; 171 event_data.item_id = item_id;
165 172
166 scoped_ptr<base::ListValue> args(new base::ListValue()); 173 scoped_ptr<base::ListValue> args(new base::ListValue());
167 args->Append(event_data.ToValue().release()); 174 args->Append(event_data.ToValue().release());
168 scoped_ptr<Event> event(new Event( 175 scoped_ptr<Event> event(new Event(
169 developer_private::OnItemStateChanged::kEventName, args.Pass())); 176 developer_private::OnItemStateChanged::kEventName, args.Pass()));
170 EventRouter::Get(browser_context)->BroadcastEvent(event.Pass()); 177 EventRouter::Get(browser_context)->BroadcastEvent(event.Pass());
171 } 178 }
172 179
180 std::string ReadFileToString(const base::FilePath& path) {
181 std::string data;
182 ignore_result(base::ReadFileToString(path, &data));
183 return data;
184 }
185
173 } // namespace 186 } // namespace
174 187
175 namespace AllowFileAccess = api::developer_private::AllowFileAccess; 188 namespace AllowFileAccess = api::developer_private::AllowFileAccess;
176 namespace AllowIncognito = api::developer_private::AllowIncognito; 189 namespace AllowIncognito = api::developer_private::AllowIncognito;
177 namespace ChoosePath = api::developer_private::ChoosePath; 190 namespace ChoosePath = api::developer_private::ChoosePath;
178 namespace GetItemsInfo = api::developer_private::GetItemsInfo; 191 namespace GetItemsInfo = api::developer_private::GetItemsInfo;
179 namespace Inspect = api::developer_private::Inspect; 192 namespace Inspect = api::developer_private::Inspect;
180 namespace PackDirectory = api::developer_private::PackDirectory; 193 namespace PackDirectory = api::developer_private::PackDirectory;
181 namespace Reload = api::developer_private::Reload; 194 namespace Reload = api::developer_private::Reload;
182 195
(...skipping 1142 matching lines...) Expand 10 before | Expand all | Expand 10 after
1325 DeveloperPrivateIsProfileManagedFunction:: 1338 DeveloperPrivateIsProfileManagedFunction::
1326 ~DeveloperPrivateIsProfileManagedFunction() { 1339 ~DeveloperPrivateIsProfileManagedFunction() {
1327 } 1340 }
1328 1341
1329 DeveloperPrivateRequestFileSourceFunction:: 1342 DeveloperPrivateRequestFileSourceFunction::
1330 DeveloperPrivateRequestFileSourceFunction() {} 1343 DeveloperPrivateRequestFileSourceFunction() {}
1331 1344
1332 DeveloperPrivateRequestFileSourceFunction:: 1345 DeveloperPrivateRequestFileSourceFunction::
1333 ~DeveloperPrivateRequestFileSourceFunction() {} 1346 ~DeveloperPrivateRequestFileSourceFunction() {}
1334 1347
1335 bool DeveloperPrivateRequestFileSourceFunction::RunAsync() { 1348 ExtensionFunction::ResponseAction
1336 scoped_ptr<developer::RequestFileSource::Params> params( 1349 DeveloperPrivateRequestFileSourceFunction::Run() {
1337 developer::RequestFileSource::Params::Create(*args_)); 1350 params_ = developer::RequestFileSource::Params::Create(*args_);
1338 EXTENSION_FUNCTION_VALIDATE(params); 1351 EXTENSION_FUNCTION_VALIDATE(params_);
1339 1352
1340 base::DictionaryValue* dict = nullptr; 1353 const developer::RequestFileSourceProperties& properties =
1341 // TODO(devlin): Use generated |params|. 1354 params_->properties;
1342 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0u, &dict)); 1355 const Extension* extension = GetExtensionById(properties.extension_id);
1356 if (!extension)
1357 return RespondNow(Error(kNoSuchExtensionError));
1343 1358
1344 AddRef(); // Balanced in LaunchCallback(). 1359 // Under no circumstances should we ever need to reference a file outside of
1345 error_ui_util::HandleRequestFileSource( 1360 // the extension's directory. If it tries to, abort.
1346 dict, 1361 base::FilePath path_suffix =
1347 GetProfile(), 1362 base::FilePath::FromUTF8Unsafe(properties.path_suffix);
1348 base::Bind(&DeveloperPrivateRequestFileSourceFunction::LaunchCallback, 1363 if (path_suffix.empty() || path_suffix.ReferencesParent())
1349 base::Unretained(this))); 1364 return RespondNow(Error(kInvalidPathError));
1350 return true; 1365
1366 if (properties.path_suffix == kManifestFilename && !properties.manifest_key)
1367 return RespondNow(Error(kManifestKeyIsRequiredError));
1368
1369 base::PostTaskAndReplyWithResult(
1370 content::BrowserThread::GetBlockingPool(),
1371 FROM_HERE,
1372 base::Bind(&ReadFileToString, extension->path().Append(path_suffix)),
1373 base::Bind(&DeveloperPrivateRequestFileSourceFunction::Finish, this));
1374
1375 return RespondLater();
1351 } 1376 }
1352 1377
1353 void DeveloperPrivateRequestFileSourceFunction::LaunchCallback( 1378 void DeveloperPrivateRequestFileSourceFunction::Finish(
1354 const base::DictionaryValue& results) { 1379 const std::string& file_contents) {
1355 SetResult(results.DeepCopy()); 1380 const developer::RequestFileSourceProperties& properties =
1356 SendResponse(true); 1381 params_->properties;
1357 Release(); // Balanced in RunAsync(). 1382 const Extension* extension = GetExtensionById(properties.extension_id);
1383 if (!extension) {
1384 Respond(Error(kNoSuchExtensionError));
1385 return;
1386 }
1387
1388 developer::RequestFileSourceResponse response;
1389 base::FilePath path_suffix =
1390 base::FilePath::FromUTF8Unsafe(properties.path_suffix);
1391 base::FilePath path = extension->path().Append(path_suffix);
1392 response.title = base::StringPrintf("%s: %s",
1393 extension->name().c_str(),
1394 path.BaseName().AsUTF8Unsafe().c_str());
1395 response.message = properties.message;
1396
1397 scoped_ptr<FileHighlighter> highlighter;
1398 if (properties.path_suffix == kManifestFilename) {
1399 highlighter.reset(new ManifestHighlighter(
1400 file_contents,
1401 *properties.manifest_key,
1402 properties.manifest_specific ?
1403 *properties.manifest_specific : std::string()));
1404 } else {
1405 highlighter.reset(new SourceHighlighter(
1406 file_contents,
1407 properties.line_number ? *properties.line_number : 0));
1408 }
1409
1410 response.before_highlight = highlighter->GetBeforeFeature();
1411 response.highlight = highlighter->GetFeature();
1412 response.after_highlight = highlighter->GetAfterFeature();
1413
1414 Respond(OneArgument(response.ToValue().release()));
1358 } 1415 }
1359 1416
1360 DeveloperPrivateOpenDevToolsFunction::DeveloperPrivateOpenDevToolsFunction() {} 1417 DeveloperPrivateOpenDevToolsFunction::DeveloperPrivateOpenDevToolsFunction() {}
1361 DeveloperPrivateOpenDevToolsFunction::~DeveloperPrivateOpenDevToolsFunction() {} 1418 DeveloperPrivateOpenDevToolsFunction::~DeveloperPrivateOpenDevToolsFunction() {}
1362 1419
1363 bool DeveloperPrivateOpenDevToolsFunction::RunAsync() { 1420 ExtensionFunction::ResponseAction
1421 DeveloperPrivateOpenDevToolsFunction::Run() {
1364 scoped_ptr<developer::OpenDevTools::Params> params( 1422 scoped_ptr<developer::OpenDevTools::Params> params(
1365 developer::OpenDevTools::Params::Create(*args_)); 1423 developer::OpenDevTools::Params::Create(*args_));
1366 EXTENSION_FUNCTION_VALIDATE(params); 1424 EXTENSION_FUNCTION_VALIDATE(params);
1425 const developer::OpenDevToolsProperties& properties = params->properties;
1367 1426
1368 base::DictionaryValue* dict = nullptr; 1427 content::RenderViewHost* rvh =
1369 // TODO(devlin): Use generated |params|. 1428 content::RenderViewHost::FromID(properties.render_process_id,
1370 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0u, &dict)); 1429 properties.render_view_id);
1430 content::WebContents* web_contents =
1431 rvh ? content::WebContents::FromRenderViewHost(rvh) : nullptr;
1432 // It's possible that the render view was closed since we last updated the
1433 // links. Handle this gracefully.
1434 if (!web_contents)
1435 return RespondNow(Error(kNoSuchRendererError));
1371 1436
1372 error_ui_util::HandleOpenDevTools(dict); 1437 // If we include a url, we should inspect it specifically (and not just the
1438 // render view).
1439 if (properties.url) {
1440 // Line/column numbers are reported in display-friendly 1-based numbers,
1441 // but are inspected in zero-based numbers.
1442 // Default to the first line/column.
1443 DevToolsWindow::OpenDevToolsWindow(
1444 web_contents,
1445 DevToolsToggleAction::Reveal(
1446 base::UTF8ToUTF16(*properties.url),
1447 properties.line_number ? *properties.line_number - 1 : 0,
1448 properties.column_number ? *properties.column_number - 1 : 0));
1449 } else {
1450 DevToolsWindow::OpenDevToolsWindow(web_contents);
1451 }
1373 1452
1374 return true; 1453 // Once we open the inspector, we focus on the appropriate tab...
1454 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
1455
1456 // ... but some pages (popups and apps) don't have tabs, and some (background
1457 // pages) don't have an associated browser. For these, the inspector opens in
1458 // a new window, and our work is done.
1459 if (!browser || !browser->is_type_tabbed())
1460 return RespondNow(NoArguments());
1461
1462 TabStripModel* tab_strip = browser->tab_strip_model();
1463 tab_strip->ActivateTabAt(tab_strip->GetIndexOfWebContents(web_contents),
1464 false); // Not through direct user gesture.
1465 return RespondNow(NoArguments());
1375 } 1466 }
1376 1467
1377 } // namespace api 1468 } // namespace api
1378 1469
1379 } // namespace extensions 1470 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698