OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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 base::ReadFileToString(path, &data); | |
not at google - send to devlin
2015/03/04 00:11:27
Wrap this in ignore_result().
Devlin
2015/03/04 18:05:25
Done.
| |
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 1116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1299 DeveloperPrivateIsProfileManagedFunction:: | 1312 DeveloperPrivateIsProfileManagedFunction:: |
1300 ~DeveloperPrivateIsProfileManagedFunction() { | 1313 ~DeveloperPrivateIsProfileManagedFunction() { |
1301 } | 1314 } |
1302 | 1315 |
1303 DeveloperPrivateRequestFileSourceFunction:: | 1316 DeveloperPrivateRequestFileSourceFunction:: |
1304 DeveloperPrivateRequestFileSourceFunction() {} | 1317 DeveloperPrivateRequestFileSourceFunction() {} |
1305 | 1318 |
1306 DeveloperPrivateRequestFileSourceFunction:: | 1319 DeveloperPrivateRequestFileSourceFunction:: |
1307 ~DeveloperPrivateRequestFileSourceFunction() {} | 1320 ~DeveloperPrivateRequestFileSourceFunction() {} |
1308 | 1321 |
1309 bool DeveloperPrivateRequestFileSourceFunction::RunAsync() { | 1322 ExtensionFunction::ResponseAction |
1310 scoped_ptr<developer::RequestFileSource::Params> params( | 1323 DeveloperPrivateRequestFileSourceFunction::Run() { |
1311 developer::RequestFileSource::Params::Create(*args_)); | 1324 params_ = developer::RequestFileSource::Params::Create(*args_); |
1312 EXTENSION_FUNCTION_VALIDATE(params); | 1325 EXTENSION_FUNCTION_VALIDATE(params_); |
1313 | 1326 |
1314 base::DictionaryValue* dict = nullptr; | 1327 const developer::RequestFileSourceProperties& properties = |
1315 // TODO(devlin): Use generated |params|. | 1328 params_->properties; |
1316 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0u, &dict)); | 1329 const Extension* extension = GetExtensionById(properties.extension_id); |
1330 if (!extension) | |
1331 return RespondNow(Error(kNoSuchExtensionError)); | |
1317 | 1332 |
1318 AddRef(); // Balanced in LaunchCallback(). | 1333 // Under no circumstances should we ever need to reference a file outside of |
1319 error_ui_util::HandleRequestFileSource( | 1334 // the extension's directory. If it tries to, abort. |
1320 dict, | 1335 base::FilePath path_suffix = |
1321 GetProfile(), | 1336 base::FilePath::FromUTF8Unsafe(properties.path_suffix); |
1322 base::Bind(&DeveloperPrivateRequestFileSourceFunction::LaunchCallback, | 1337 if (path_suffix.empty() || path_suffix.ReferencesParent()) |
1323 base::Unretained(this))); | 1338 return RespondNow(Error(kInvalidPathError)); |
1324 return true; | 1339 |
1340 if (properties.path_suffix == kManifestFilename && !properties.manifest_key) | |
1341 return RespondNow(Error(kManifestKeyIsRequiredError)); | |
1342 | |
1343 base::PostTaskAndReplyWithResult( | |
1344 content::BrowserThread::GetBlockingPool(), | |
1345 FROM_HERE, | |
1346 base::Bind(&ReadFileToString, extension->path().Append(path_suffix)), | |
1347 base::Bind(&DeveloperPrivateRequestFileSourceFunction::Finish, this)); | |
1348 | |
1349 return RespondLater(); | |
1325 } | 1350 } |
1326 | 1351 |
1327 void DeveloperPrivateRequestFileSourceFunction::LaunchCallback( | 1352 void DeveloperPrivateRequestFileSourceFunction::Finish( |
1328 const base::DictionaryValue& results) { | 1353 const std::string& file_contents) { |
1329 SetResult(results.DeepCopy()); | 1354 const developer::RequestFileSourceProperties& properties = |
1330 SendResponse(true); | 1355 params_->properties; |
1331 Release(); // Balanced in RunAsync(). | 1356 const Extension* extension = GetExtensionById(properties.extension_id); |
1357 if (!extension) { | |
1358 Respond(Error(kNoSuchExtensionError)); | |
1359 return; | |
1360 } | |
1361 | |
1362 developer::RequestFileSourceResponse response; | |
1363 base::FilePath path_suffix = | |
1364 base::FilePath::FromUTF8Unsafe(properties.path_suffix); | |
1365 base::FilePath path = extension->path().Append(path_suffix); | |
1366 response.title = base::StringPrintf("%s: %s", | |
1367 extension->name().c_str(), | |
1368 path.BaseName().AsUTF8Unsafe().c_str()); | |
1369 response.message = properties.message; | |
1370 | |
1371 scoped_ptr<FileHighlighter> highlighter; | |
1372 if (properties.path_suffix == kManifestFilename) { | |
1373 highlighter.reset(new ManifestHighlighter( | |
1374 file_contents, | |
1375 *properties.manifest_key, | |
1376 properties.manifest_specific ? | |
1377 *properties.manifest_specific : std::string())); | |
1378 } else { | |
1379 highlighter.reset(new SourceHighlighter( | |
1380 file_contents, | |
1381 properties.line_number ? *properties.line_number : 0)); | |
1382 } | |
1383 | |
1384 response.before_highlight = highlighter->GetBeforeFeature(); | |
1385 response.highlight = highlighter->GetFeature(); | |
1386 response.after_highlight = highlighter->GetAfterFeature(); | |
1387 | |
1388 Respond(OneArgument(response.ToValue().release())); | |
1332 } | 1389 } |
1333 | 1390 |
1334 DeveloperPrivateOpenDevToolsFunction::DeveloperPrivateOpenDevToolsFunction() {} | 1391 DeveloperPrivateOpenDevToolsFunction::DeveloperPrivateOpenDevToolsFunction() {} |
1335 DeveloperPrivateOpenDevToolsFunction::~DeveloperPrivateOpenDevToolsFunction() {} | 1392 DeveloperPrivateOpenDevToolsFunction::~DeveloperPrivateOpenDevToolsFunction() {} |
1336 | 1393 |
1337 bool DeveloperPrivateOpenDevToolsFunction::RunAsync() { | 1394 ExtensionFunction::ResponseAction |
1395 DeveloperPrivateOpenDevToolsFunction::Run() { | |
1338 scoped_ptr<developer::OpenDevTools::Params> params( | 1396 scoped_ptr<developer::OpenDevTools::Params> params( |
1339 developer::OpenDevTools::Params::Create(*args_)); | 1397 developer::OpenDevTools::Params::Create(*args_)); |
1340 EXTENSION_FUNCTION_VALIDATE(params); | 1398 EXTENSION_FUNCTION_VALIDATE(params); |
1399 const developer::OpenDevToolsProperties& properties = params->properties; | |
1341 | 1400 |
1342 base::DictionaryValue* dict = nullptr; | 1401 content::RenderViewHost* rvh = |
1343 // TODO(devlin): Use generated |params|. | 1402 content::RenderViewHost::FromID(properties.render_process_id, |
1344 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0u, &dict)); | 1403 properties.render_view_id); |
1404 content::WebContents* web_contents = | |
1405 rvh ? content::WebContents::FromRenderViewHost(rvh) : nullptr; | |
1406 // It's possible that the render view was closed since we last updated the | |
1407 // links. Handle this gracefully. | |
1408 if (!web_contents) | |
1409 return RespondNow(Error(kNoSuchRendererError)); | |
1345 | 1410 |
1346 error_ui_util::HandleOpenDevTools(dict); | 1411 // If we include a url, we should inspect it specifically (and not just the |
1412 // render view). | |
1413 if (properties.url) { | |
1414 // Line/column numbers are reported in display-friendly 1-based numbers, | |
1415 // but are inspected in zero-based numbers. | |
1416 // Default to the first line/column. | |
1417 DevToolsWindow::OpenDevToolsWindow( | |
1418 web_contents, | |
1419 DevToolsToggleAction::Reveal( | |
1420 base::UTF8ToUTF16(*properties.url), | |
1421 properties.line_number ? *properties.line_number - 1 : 0, | |
1422 properties.column_number ? *properties.column_number - 1 : 0)); | |
1423 } else { | |
1424 DevToolsWindow::OpenDevToolsWindow(web_contents); | |
1425 } | |
1347 | 1426 |
1348 return true; | 1427 // Once we open the inspector, we focus on the appropriate tab... |
1428 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | |
1429 | |
1430 // ... but some pages (popups and apps) don't have tabs, and some (background | |
1431 // pages) don't have an associated browser. For these, the inspector opens in | |
1432 // a new window, and our work is done. | |
1433 if (!browser || !browser->is_type_tabbed()) | |
1434 return RespondNow(NoArguments()); | |
1435 | |
1436 TabStripModel* tab_strip = browser->tab_strip_model(); | |
1437 tab_strip->ActivateTabAt(tab_strip->GetIndexOfWebContents(web_contents), | |
1438 false); // Not through direct user gesture. | |
1439 return RespondNow(NoArguments()); | |
1349 } | 1440 } |
1350 | 1441 |
1351 } // namespace api | 1442 } // namespace api |
1352 | 1443 |
1353 } // namespace extensions | 1444 } // namespace extensions |
OLD | NEW |