{"action":"created","comment":{"url":"https://api.github.com/repos/atom/electron/pulls/comments/33010612","id":33010612,"diff_hunk":"@@ -0,0 +1,134 @@\n+// Copyright (c) 2015 GitHub, Inc.\n+// Use of this source code is governed by the MIT license that can be\n+// found in the LICENSE file.\n+\n+#include \"atom/browser/atom_download_manager_delegate.h\"\n+\n+#include <string>\n+\n+#include \"atom/browser/ui/file_dialog.h\"\n+#include \"base/bind.h\"\n+#include \"base/files/file_util.h\"\n+#include \"content/public/browser/browser_context.h\"\n+#include \"content/public/browser/browser_thread.h\"\n+#include \"content/public/browser/download_manager.h\"\n+#include \"net/base/filename_util.h\"\n+\n+namespace atom {\n+\n+AtomDownloadManagerDelegate::AtomDownloadManagerDelegate(\n+    content::DownloadManager* manager)\n+    : download_manager_(manager),\n+      weak_ptr_factory_(this) {}\n+\n+AtomDownloadManagerDelegate::~AtomDownloadManagerDelegate() {\n+  if (download_manager_) {\n+    DCHECK_EQ(static_cast<content::DownloadManagerDelegate*>(this),\n+              download_manager_->GetDelegate());\n+    download_manager_->SetDelegate(nullptr);\n+    download_manager_.reset();\n+  }\n+}\n+\n+void AtomDownloadManagerDelegate::CreateDownloadPath(\n+    const GURL& url,\n+    const std::string& content_disposition,\n+    const std::string& suggested_filename,\n+    const std::string& mime_type,\n+    const base::FilePath& default_download_path,\n+    const CreateDownloadPathCallback& callback) {\n+  DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);\n+\n+  auto generated_name = net::GenerateFileName(url,\n+                                              content_disposition,\n+                                              std::string(),\n+                                              suggested_filename,\n+                                              mime_type,\n+                                              std::string());\n+\n+  if (!base::PathExists(default_download_path))\n+    base::CreateDirectory(default_download_path);\n+\n+  base::FilePath path(default_download_path.Append(generated_name));\n+  content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,\n+                                   base::Bind(callback, path));\n+}\n+\n+void AtomDownloadManagerDelegate::OnDownloadPathGenerated(\n+    uint32 download_id,\n+    const content::DownloadTargetCallback& callback,\n+    const base::FilePath& default_path) {\n+  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);\n+\n+  auto item = download_manager_->GetDownload(download_id);\n+  if (!item || (item->GetState() != content::DownloadItem::IN_PROGRESS))\n+    return;\n+\n+  file_dialog::Filters filters;\n+  base::FilePath path;\n+  if (!file_dialog::ShowSaveDialog(\n+          nullptr, item->GetURL().spec(),\n+          default_path, filters, &path)) {\n+    return;\n+  }\n+\n+  callback.Run(path,\n+               content::DownloadItem::TARGET_DISPOSITION_PROMPT,\n+               content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, path);\n+}\n+\n+void AtomDownloadManagerDelegate::Shutdown() {\n+  weak_ptr_factory_.InvalidateWeakPtrs();\n+  download_manager_.reset();\n+}\n+\n+bool AtomDownloadManagerDelegate::DetermineDownloadTarget(\n+    content::DownloadItem* download,\n+    const content::DownloadTargetCallback& callback) {\n+  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);\n+\n+  if (default_download_path_.empty()) {\n+    default_download_path_ = download_manager_->GetBrowserContext()\n+                                ->GetPath().Append(\n+                                    FILE_PATH_LITERAL(\"Downloads\"));\n+  }\n+\n+  if (!download->GetForcedFilePath().empty()) {\n+    callback.Run(download->GetForcedFilePath(),\n+                 content::DownloadItem::TARGET_DISPOSITION_OVERWRITE,\n+                 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,\n+                 download->GetForcedFilePath());\n+    return true;\n+  }\n+\n+  CreateDownloadPathCallback download_path_callback =\n+      base::Bind(&AtomDownloadManagerDelegate::OnDownloadPathGenerated,\n+                 weak_ptr_factory_.GetWeakPtr(),\n+                 download->GetId(), callback);\n+\n+  content::BrowserThread::PostTask(\n+      content::BrowserThread::FILE, FROM_HERE,\n+      base::Bind(&AtomDownloadManagerDelegate::CreateDownloadPath,\n+                 weak_ptr_factory_.GetWeakPtr(),\n+                 download->GetURL(),\n+                 download->GetContentDisposition(),\n+                 download->GetSuggestedFilename(),\n+                 download->GetMimeType(),\n+                 default_download_path_,\n+                 download_path_callback));\n+  return true;\n+}\n+\n+bool AtomDownloadManagerDelegate::ShouldOpenDownload(\n+    content::DownloadItem* download,\n+    const content::DownloadOpenDelayedCallback& callback) {\n+  return true;\n+}\n+\n+void AtomDownloadManagerDelegate::GetNextId(\n+    const content::DownloadIdCallback& callback) {\n+  static uint32 next_id = content::DownloadItem::kInvalidId + 1;","path":"atom/browser/atom_download_manager_delegate.cc","position":130,"original_position":130,"commit_id":"25c4ac94b309ae8186c4a14045e1d2b2495120f2","original_commit_id":"25c4ac94b309ae8186c4a14045e1d2b2495120f2","user":{"login":"zcbenz","id":639601,"avatar_url":"https://avatars.githubusercontent.com/u/639601?v=3","gravatar_id":"","url":"https://api.github.com/users/zcbenz","html_url":"https://github.com/zcbenz","followers_url":"https://api.github.com/users/zcbenz/followers","following_url":"https://api.github.com/users/zcbenz/following{/other_user}","gists_url":"https://api.github.com/users/zcbenz/gists{/gist_id}","starred_url":"https://api.github.com/users/zcbenz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zcbenz/subscriptions","organizations_url":"https://api.github.com/users/zcbenz/orgs","repos_url":"https://api.github.com/users/zcbenz/repos","events_url":"https://api.github.com/users/zcbenz/events{/privacy}","received_events_url":"https://api.github.com/users/zcbenz/received_events","type":"User","site_admin":true},"body":"Using the same ID for all downloads is going to cause troubles.","created_at":"2015-06-23T05:31:25Z","updated_at":"2015-06-23T05:31:25Z","html_url":"https://github.com/atom/electron/pull/2030#discussion_r33010612","pull_request_url":"https://api.github.com/repos/atom/electron/pulls/2030","_links":{"self":{"href":"https://api.github.com/repos/atom/electron/pulls/comments/33010612"},"html":{"href":"https://github.com/atom/electron/pull/2030#discussion_r33010612"},"pull_request":{"href":"https://api.github.com/repos/atom/electron/pulls/2030"}}},"pull_request":{"url":"https://api.github.com/repos/atom/electron/pulls/2030","id":38223716,"html_url":"https://github.com/atom/electron/pull/2030","diff_url":"https://github.com/atom/electron/pull/2030.diff","patch_url":"https://github.com/atom/electron/pull/2030.patch","issue_url":"https://api.github.com/repos/atom/electron/issues/2030","number":2030,"state":"open","locked":false,"title":"browser: creating download manager delegate to handle downloads","user":{"login":"deepak1556","id":964386,"avatar_url":"https://avatars.githubusercontent.com/u/964386?v=3","gravatar_id":"","url":"https://api.github.com/users/deepak1556","html_url":"https://github.com/deepak1556","followers_url":"https://api.github.com/users/deepak1556/followers","following_url":"https://api.github.com/users/deepak1556/following{/other_user}","gists_url":"https://api.github.com/users/deepak1556/gists{/gist_id}","starred_url":"https://api.github.com/users/deepak1556/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/deepak1556/subscriptions","organizations_url":"https://api.github.com/users/deepak1556/orgs","repos_url":"https://api.github.com/users/deepak1556/repos","events_url":"https://api.github.com/users/deepak1556/events{/privacy}","received_events_url":"https://api.github.com/users/deepak1556/received_events","type":"User","site_admin":false},"body":"Fixes #1909 \r\n\r\nthis basic implemetation provides ability to save downloads, the default download directory points to `Downloads` folder in the respective app. We can provide api around this for users track status of each download by adding [observers](https://code.google.com/p/chromium/codesearch#chromium/src/content/public/browser/download_item.h&sq=package:chromium&rcl=1434958068&l=87) to this class, it deserves a separate PR. Thoughts ?","created_at":"2015-06-22T11:50:29Z","updated_at":"2015-06-23T05:31:25Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4d1b57f82ff2b2540e8ce3abf0a59807efd3c9ae","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/atom/electron/pulls/2030/commits","review_comments_url":"https://api.github.com/repos/atom/electron/pulls/2030/comments","review_comment_url":"https://api.github.com/repos/atom/electron/pulls/comments{/number}","comments_url":"https://api.github.com/repos/atom/electron/issues/2030/comments","statuses_url":"https://api.github.com/repos/atom/electron/statuses/25c4ac94b309ae8186c4a14045e1d2b2495120f2","head":{"label":"deepak1556:file_download_patch","ref":"file_download_patch","sha":"25c4ac94b309ae8186c4a14045e1d2b2495120f2","user":{"login":"deepak1556","id":964386,"avatar_url":"https://avatars.githubusercontent.com/u/964386?v=3","gravatar_id":"","url":"https://api.github.com/users/deepak1556","html_url":"https://github.com/deepak1556","followers_url":"https://api.github.com/users/deepak1556/followers","following_url":"https://api.github.com/users/deepak1556/following{/other_user}","gists_url":"https://api.github.com/users/deepak1556/gists{/gist_id}","starred_url":"https://api.github.com/users/deepak1556/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/deepak1556/subscriptions","organizations_url":"https://api.github.com/users/deepak1556/orgs","repos_url":"https://api.github.com/users/deepak1556/repos","events_url":"https://api.github.com/users/deepak1556/events{/privacy}","received_events_url":"https://api.github.com/users/deepak1556/received_events","type":"User","site_admin":false},"repo":{"id":19500833,"name":"atom-shell","full_name":"deepak1556/atom-shell","owner":{"login":"deepak1556","id":964386,"avatar_url":"https://avatars.githubusercontent.com/u/964386?v=3","gravatar_id":"","url":"https://api.github.com/users/deepak1556","html_url":"https://github.com/deepak1556","followers_url":"https://api.github.com/users/deepak1556/followers","following_url":"https://api.github.com/users/deepak1556/following{/other_user}","gists_url":"https://api.github.com/users/deepak1556/gists{/gist_id}","starred_url":"https://api.github.com/users/deepak1556/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/deepak1556/subscriptions","organizations_url":"https://api.github.com/users/deepak1556/orgs","repos_url":"https://api.github.com/users/deepak1556/repos","events_url":"https://api.github.com/users/deepak1556/events{/privacy}","received_events_url":"https://api.github.com/users/deepak1556/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/deepak1556/atom-shell","description":"Cross-platform desktop application shell","fork":true,"url":"https://api.github.com/repos/deepak1556/atom-shell","forks_url":"https://api.github.com/repos/deepak1556/atom-shell/forks","keys_url":"https://api.github.com/repos/deepak1556/atom-shell/keys{/key_id}","collaborators_url":"https://api.github.com/repos/deepak1556/atom-shell/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/deepak1556/atom-shell/teams","hooks_url":"https://api.github.com/repos/deepak1556/atom-shell/hooks","issue_events_url":"https://api.github.com/repos/deepak1556/atom-shell/issues/events{/number}","events_url":"https://api.github.com/repos/deepak1556/atom-shell/events","assignees_url":"https://api.github.com/repos/deepak1556/atom-shell/assignees{/user}","branches_url":"https://api.github.com/repos/deepak1556/atom-shell/branches{/branch}","tags_url":"https://api.github.com/repos/deepak1556/atom-shell/tags","blobs_url":"https://api.github.com/repos/deepak1556/atom-shell/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/deepak1556/atom-shell/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/deepak1556/atom-shell/git/refs{/sha}","trees_url":"https://api.github.com/repos/deepak1556/atom-shell/git/trees{/sha}","statuses_url":"https://api.github.com/repos/deepak1556/atom-shell/statuses/{sha}","languages_url":"https://api.github.com/repos/deepak1556/atom-shell/languages","stargazers_url":"https://api.github.com/repos/deepak1556/atom-shell/stargazers","contributors_url":"https://api.github.com/repos/deepak1556/atom-shell/contributors","subscribers_url":"https://api.github.com/repos/deepak1556/atom-shell/subscribers","subscription_url":"https://api.github.com/repos/deepak1556/atom-shell/subscription","commits_url":"https://api.github.com/repos/deepak1556/atom-shell/commits{/sha}","git_commits_url":"https://api.github.com/repos/deepak1556/atom-shell/git/commits{/sha}","comments_url":"https://api.github.com/repos/deepak1556/atom-shell/comments{/number}","issue_comment_url":"https://api.github.com/repos/deepak1556/atom-shell/issues/comments{/number}","contents_url":"https://api.github.com/repos/deepak1556/atom-shell/contents/{+path}","compare_url":"https://api.github.com/repos/deepak1556/atom-shell/compare/{base}...{head}","merges_url":"https://api.github.com/repos/deepak1556/atom-shell/merges","archive_url":"https://api.github.com/repos/deepak1556/atom-shell/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/deepak1556/atom-shell/downloads","issues_url":"https://api.github.com/repos/deepak1556/atom-shell/issues{/number}","pulls_url":"https://api.github.com/repos/deepak1556/atom-shell/pulls{/number}","milestones_url":"https://api.github.com/repos/deepak1556/atom-shell/milestones{/number}","notifications_url":"https://api.github.com/repos/deepak1556/atom-shell/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/deepak1556/atom-shell/labels{/name}","releases_url":"https://api.github.com/repos/deepak1556/atom-shell/releases{/id}","created_at":"2014-05-06T15:58:25Z","updated_at":"2015-03-10T06:12:17Z","pushed_at":"2015-06-22T11:44:06Z","git_url":"git://github.com/deepak1556/atom-shell.git","ssh_url":"git@github.com:deepak1556/atom-shell.git","clone_url":"https://github.com/deepak1556/atom-shell.git","svn_url":"https://github.com/deepak1556/atom-shell","homepage":"","size":9654,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"atom:master","ref":"master","sha":"a1cbd11b5b60eddfbab2709c21fa95daaefb675f","user":{"login":"atom","id":1089146,"avatar_url":"https://avatars.githubusercontent.com/u/1089146?v=3","gravatar_id":"","url":"https://api.github.com/users/atom","html_url":"https://github.com/atom","followers_url":"https://api.github.com/users/atom/followers","following_url":"https://api.github.com/users/atom/following{/other_user}","gists_url":"https://api.github.com/users/atom/gists{/gist_id}","starred_url":"https://api.github.com/users/atom/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/atom/subscriptions","organizations_url":"https://api.github.com/users/atom/orgs","repos_url":"https://api.github.com/users/atom/repos","events_url":"https://api.github.com/users/atom/events{/privacy}","received_events_url":"https://api.github.com/users/atom/received_events","type":"Organization","site_admin":false},"repo":{"id":9384267,"name":"electron","full_name":"atom/electron","owner":{"login":"atom","id":1089146,"avatar_url":"https://avatars.githubusercontent.com/u/1089146?v=3","gravatar_id":"","url":"https://api.github.com/users/atom","html_url":"https://github.com/atom","followers_url":"https://api.github.com/users/atom/followers","following_url":"https://api.github.com/users/atom/following{/other_user}","gists_url":"https://api.github.com/users/atom/gists{/gist_id}","starred_url":"https://api.github.com/users/atom/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/atom/subscriptions","organizations_url":"https://api.github.com/users/atom/orgs","repos_url":"https://api.github.com/users/atom/repos","events_url":"https://api.github.com/users/atom/events{/privacy}","received_events_url":"https://api.github.com/users/atom/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/atom/electron","description":"Build cross platform desktop apps with web technologies","fork":false,"url":"https://api.github.com/repos/atom/electron","forks_url":"https://api.github.com/repos/atom/electron/forks","keys_url":"https://api.github.com/repos/atom/electron/keys{/key_id}","collaborators_url":"https://api.github.com/repos/atom/electron/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/atom/electron/teams","hooks_url":"https://api.github.com/repos/atom/electron/hooks","issue_events_url":"https://api.github.com/repos/atom/electron/issues/events{/number}","events_url":"https://api.github.com/repos/atom/electron/events","assignees_url":"https://api.github.com/repos/atom/electron/assignees{/user}","branches_url":"https://api.github.com/repos/atom/electron/branches{/branch}","tags_url":"https://api.github.com/repos/atom/electron/tags","blobs_url":"https://api.github.com/repos/atom/electron/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/atom/electron/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/atom/electron/git/refs{/sha}","trees_url":"https://api.github.com/repos/atom/electron/git/trees{/sha}","statuses_url":"https://api.github.com/repos/atom/electron/statuses/{sha}","languages_url":"https://api.github.com/repos/atom/electron/languages","stargazers_url":"https://api.github.com/repos/atom/electron/stargazers","contributors_url":"https://api.github.com/repos/atom/electron/contributors","subscribers_url":"https://api.github.com/repos/atom/electron/subscribers","subscription_url":"https://api.github.com/repos/atom/electron/subscription","commits_url":"https://api.github.com/repos/atom/electron/commits{/sha}","git_commits_url":"https://api.github.com/repos/atom/electron/git/commits{/sha}","comments_url":"https://api.github.com/repos/atom/electron/comments{/number}","issue_comment_url":"https://api.github.com/repos/atom/electron/issues/comments{/number}","contents_url":"https://api.github.com/repos/atom/electron/contents/{+path}","compare_url":"https://api.github.com/repos/atom/electron/compare/{base}...{head}","merges_url":"https://api.github.com/repos/atom/electron/merges","archive_url":"https://api.github.com/repos/atom/electron/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/atom/electron/downloads","issues_url":"https://api.github.com/repos/atom/electron/issues{/number}","pulls_url":"https://api.github.com/repos/atom/electron/pulls{/number}","milestones_url":"https://api.github.com/repos/atom/electron/milestones{/number}","notifications_url":"https://api.github.com/repos/atom/electron/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/atom/electron/labels{/name}","releases_url":"https://api.github.com/repos/atom/electron/releases{/id}","created_at":"2013-04-12T01:47:36Z","updated_at":"2015-06-23T04:11:53Z","pushed_at":"2015-06-23T04:38:03Z","git_url":"git://github.com/atom/electron.git","ssh_url":"git@github.com:atom/electron.git","clone_url":"https://github.com/atom/electron.git","svn_url":"https://github.com/atom/electron","homepage":"http://electron.atom.io","size":20917,"stargazers_count":12657,"watchers_count":12657,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":960,"mirror_url":null,"open_issues_count":194,"forks":960,"open_issues":194,"watchers":12657,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/atom/electron/pulls/2030"},"html":{"href":"https://github.com/atom/electron/pull/2030"},"issue":{"href":"https://api.github.com/repos/atom/electron/issues/2030"},"comments":{"href":"https://api.github.com/repos/atom/electron/issues/2030/comments"},"review_comments":{"href":"https://api.github.com/repos/atom/electron/pulls/2030/comments"},"review_comment":{"href":"https://api.github.com/repos/atom/electron/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/atom/electron/pulls/2030/commits"},"statuses":{"href":"https://api.github.com/repos/atom/electron/statuses/25c4ac94b309ae8186c4a14045e1d2b2495120f2"}}}}