diff --git a/atom/browser/common_web_contents_delegate.cc b/atom/browser/common_web_contents_delegate.cc index 4bebf7480b89..e17bcb9b2198 100644 --- a/atom/browser/common_web_contents_delegate.cc +++ b/atom/browser/common_web_contents_delegate.cc @@ -145,6 +145,13 @@ std::set GetAddedFileSystemPaths( return result; } +bool IsDevToolsFileSystemAdded( + content::WebContents* web_contents, + const std::string& file_system_path) { + auto file_system_paths = GetAddedFileSystemPaths(web_contents); + return file_system_paths.find(file_system_path) != file_system_paths.end(); +} + content::SecurityStyle SecurityLevelToSecurityStyle( SecurityStateModel::SecurityLevel security_level) { switch (security_level) { @@ -167,7 +174,8 @@ content::SecurityStyle SecurityLevelToSecurityStyle( CommonWebContentsDelegate::CommonWebContentsDelegate() : html_fullscreen_(false), - native_fullscreen_(false) { + native_fullscreen_(false), + devtools_file_system_indexer_(new DevToolsFileSystemIndexer) { } CommonWebContentsDelegate::~CommonWebContentsDelegate() { @@ -456,8 +464,7 @@ void CommonWebContentsDelegate::DevToolsAddFileSystem( std::string file_system_id = RegisterFileSystem(GetDevToolsWebContents(), path); - auto file_system_paths = GetAddedFileSystemPaths(GetDevToolsWebContents()); - if (file_system_paths.find(path.AsUTF8Unsafe()) != file_system_paths.end()) + if (IsDevToolsFileSystemAdded(GetDevToolsWebContents(), path.AsUTF8Unsafe())) return; FileSystem file_system = CreateFileSystemStruct(GetDevToolsWebContents(), @@ -495,6 +502,61 @@ void CommonWebContentsDelegate::DevToolsRemoveFileSystem( nullptr, nullptr); } +void CommonWebContentsDelegate::DevToolsIndexPath( + int request_id, + const std::string& file_system_path) { + if (!IsDevToolsFileSystemAdded(GetDevToolsWebContents(), file_system_path)) { + OnDevToolsIndexingDone(request_id, file_system_path); + return; + } + if (devtools_indexing_jobs_.count(request_id) != 0) + return; + devtools_indexing_jobs_[request_id] = + scoped_refptr( + devtools_file_system_indexer_->IndexPath( + file_system_path, + base::Bind( + &CommonWebContentsDelegate::OnDevToolsIndexingWorkCalculated, + base::Unretained(this), + request_id, + file_system_path), + base::Bind(&CommonWebContentsDelegate::OnDevToolsIndexingWorked, + base::Unretained(this), + request_id, + file_system_path), + base::Bind(&CommonWebContentsDelegate::OnDevToolsIndexingDone, + base::Unretained(this), + request_id, + file_system_path))); +} + +void CommonWebContentsDelegate::DevToolsStopIndexing(int request_id) { + auto it = devtools_indexing_jobs_.find(request_id); + if (it == devtools_indexing_jobs_.end()) + return; + it->second->Stop(); + devtools_indexing_jobs_.erase(it); +} + +void CommonWebContentsDelegate::DevToolsSearchInPath( + int request_id, + const std::string& file_system_path, + const std::string& query) { + if (!IsDevToolsFileSystemAdded(GetDevToolsWebContents(), file_system_path)) { + OnDevToolsSearchCompleted(request_id, + file_system_path, + std::vector()); + return; + } + devtools_file_system_indexer_->SearchInPath( + file_system_path, + query, + base::Bind(&CommonWebContentsDelegate::OnDevToolsSearchCompleted, + base::Unretained(this), + request_id, + file_system_path)); +} + void CommonWebContentsDelegate::OnDevToolsSaveToFile( const std::string& url) { // Notify DevTools. @@ -511,6 +573,61 @@ void CommonWebContentsDelegate::OnDevToolsAppendToFile( "DevToolsAPI.appendedToURL", &url_value, nullptr, nullptr); } +void CommonWebContentsDelegate::OnDevToolsIndexingWorkCalculated( + int request_id, + const std::string& file_system_path, + int total_work) { + base::FundamentalValue request_id_value(request_id); + base::StringValue file_system_path_value(file_system_path); + base::FundamentalValue total_work_value(total_work); + web_contents_->CallClientFunction("DevToolsAPI.indexingTotalWorkCalculated", + &request_id_value, + &file_system_path_value, + &total_work_value); +} + +void CommonWebContentsDelegate::OnDevToolsIndexingWorked( + int request_id, + const std::string& file_system_path, + int worked) { + base::FundamentalValue request_id_value(request_id); + base::StringValue file_system_path_value(file_system_path); + base::FundamentalValue worked_value(worked); + web_contents_->CallClientFunction("DevToolsAPI.indexingWorked", + &request_id_value, + &file_system_path_value, + &worked_value); +} + +void CommonWebContentsDelegate::OnDevToolsIndexingDone( + int request_id, + const std::string& file_system_path) { + devtools_indexing_jobs_.erase(request_id); + base::FundamentalValue request_id_value(request_id); + base::StringValue file_system_path_value(file_system_path); + web_contents_->CallClientFunction("DevToolsAPI.indexingDone", + &request_id_value, + &file_system_path_value, + nullptr); +} + +void CommonWebContentsDelegate::OnDevToolsSearchCompleted( + int request_id, + const std::string& file_system_path, + const std::vector& file_paths) { + base::ListValue file_paths_value; + for (std::vector::const_iterator it(file_paths.begin()); + it != file_paths.end(); ++it) { + file_paths_value.AppendString(*it); + } + base::FundamentalValue request_id_value(request_id); + base::StringValue file_system_path_value(file_system_path); + web_contents_->CallClientFunction("DevToolsAPI.searchCompleted", + &request_id_value, + &file_system_path_value, + &file_paths_value); +} + #if defined(TOOLKIT_VIEWS) gfx::ImageSkia CommonWebContentsDelegate::GetDevToolsWindowIcon() { if (!owner_window()) diff --git a/atom/browser/common_web_contents_delegate.h b/atom/browser/common_web_contents_delegate.h index 699d00e18bce..dbd5ddeebd9c 100644 --- a/atom/browser/common_web_contents_delegate.h +++ b/atom/browser/common_web_contents_delegate.h @@ -12,8 +12,11 @@ #include "brightray/browser/inspectable_web_contents_impl.h" #include "brightray/browser/inspectable_web_contents_delegate.h" #include "brightray/browser/inspectable_web_contents_view_delegate.h" +#include "brightray/browser/devtools_file_system_indexer.h" #include "content/public/browser/web_contents_delegate.h" +using brightray::DevToolsFileSystemIndexer; + namespace atom { class AtomJavaScriptDialogManager; @@ -90,6 +93,12 @@ class CommonWebContentsDelegate void DevToolsAddFileSystem(const base::FilePath& path) override; void DevToolsRemoveFileSystem( const base::FilePath& file_system_path) override; + void DevToolsIndexPath(int request_id, + const std::string& file_system_path) override; + void DevToolsStopIndexing(int request_id) override; + void DevToolsSearchInPath(int request_id, + const std::string& file_system_path, + const std::string& query) override; // brightray::InspectableWebContentsViewDelegate: #if defined(TOOLKIT_VIEWS) @@ -107,6 +116,19 @@ class CommonWebContentsDelegate // Callback for when DevToolsAppendToFile has completed. void OnDevToolsAppendToFile(const std::string& url); + // + void OnDevToolsIndexingWorkCalculated(int request_id, + const std::string& file_system_path, + int total_work); + void OnDevToolsIndexingWorked(int request_id, + const std::string& file_system_path, + int worked); + void OnDevToolsIndexingDone(int request_id, + const std::string& file_system_path); + void OnDevToolsSearchCompleted(int request_id, + const std::string& file_system_path, + const std::vector& file_paths); + // Set fullscreen mode triggered by html api. void SetHtmlApiFullscreen(bool enter_fullscreen); @@ -121,6 +143,7 @@ class CommonWebContentsDelegate scoped_ptr web_dialog_helper_; scoped_ptr dialog_manager_; + scoped_refptr devtools_file_system_indexer_; // The stored InspectableWebContents object. // Notice that web_contents_ must be placed after dialog_manager_, so we can @@ -132,6 +155,13 @@ class CommonWebContentsDelegate typedef std::map PathsMap; PathsMap saved_files_; + // Map id to index job, used for file system indexing requests from devtools. + typedef std::map< + int, + scoped_refptr> + DevToolsIndexingJobsMap; + DevToolsIndexingJobsMap devtools_indexing_jobs_; + DISALLOW_COPY_AND_ASSIGN(CommonWebContentsDelegate); };