Skip to content

Commit fd9eadd

Browse files
committed
Merge pull request electron#5 from atom/master
Update from original
2 parents d3a7901 + 6c3a104 commit fd9eadd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+779
-192
lines changed

atom.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
'product_name%': 'Electron',
55
'company_name%': 'GitHub, Inc',
66
'company_abbr%': 'github',
7-
'version%': '0.33.1',
7+
'version%': '0.33.3',
88
},
99
'includes': [
1010
'filenames.gypi',
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
// Copyright (c) 2015 GitHub, Inc.
2+
// Use of this source code is governed by the MIT license that can be
3+
// found in the LICENSE file.
4+
5+
#include "atom/browser/api/atom_api_download_item.h"
6+
7+
#include <map>
8+
9+
#include "atom/common/native_mate_converters/callback.h"
10+
#include "atom/common/native_mate_converters/file_path_converter.h"
11+
#include "atom/common/native_mate_converters/gurl_converter.h"
12+
#include "atom/common/node_includes.h"
13+
#include "base/memory/linked_ptr.h"
14+
#include "base/strings/utf_string_conversions.h"
15+
#include "native_mate/dictionary.h"
16+
#include "net/base/filename_util.h"
17+
18+
namespace mate {
19+
20+
template<>
21+
struct Converter<content::DownloadItem::DownloadState> {
22+
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
23+
content::DownloadItem::DownloadState state) {
24+
std::string download_state;
25+
switch (state) {
26+
case content::DownloadItem::COMPLETE:
27+
download_state = "completed";
28+
break;
29+
case content::DownloadItem::CANCELLED:
30+
download_state = "cancelled";
31+
break;
32+
case content::DownloadItem::INTERRUPTED:
33+
download_state = "interrupted";
34+
break;
35+
default:
36+
break;
37+
}
38+
return ConvertToV8(isolate, download_state);
39+
}
40+
};
41+
42+
} // namespace mate
43+
44+
namespace atom {
45+
46+
namespace api {
47+
48+
namespace {
49+
// The wrapDownloadItem funtion which is implemented in JavaScript
50+
using WrapDownloadItemCallback = base::Callback<void(v8::Local<v8::Value>)>;
51+
WrapDownloadItemCallback g_wrap_download_item;
52+
53+
char kDownloadItemSavePathKey[] = "DownloadItemSavePathKey";
54+
55+
std::map<uint32, linked_ptr<v8::Global<v8::Value>>> g_download_item_objects;
56+
} // namespace
57+
58+
DownloadItem::SavePathData::SavePathData(const base::FilePath& path) :
59+
path_(path) {
60+
}
61+
62+
const base::FilePath& DownloadItem::SavePathData::path() {
63+
return path_;
64+
}
65+
66+
DownloadItem::DownloadItem(content::DownloadItem* download_item) :
67+
download_item_(download_item) {
68+
download_item_->AddObserver(this);
69+
}
70+
71+
DownloadItem::~DownloadItem() {
72+
Destroy();
73+
}
74+
75+
void DownloadItem::Destroy() {
76+
if (download_item_) {
77+
download_item_->RemoveObserver(this);
78+
auto iter = g_download_item_objects.find(download_item_->GetId());
79+
if (iter != g_download_item_objects.end())
80+
g_download_item_objects.erase(iter);
81+
download_item_ = nullptr;
82+
}
83+
}
84+
85+
bool DownloadItem::IsDestroyed() const {
86+
return download_item_ == nullptr;
87+
}
88+
89+
void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) {
90+
download_item_->IsDone() ? Emit("done", item->GetState()) : Emit("updated");
91+
}
92+
93+
void DownloadItem::OnDownloadDestroyed(content::DownloadItem* download) {
94+
Destroy();
95+
}
96+
97+
int64 DownloadItem::GetReceivedBytes() {
98+
return download_item_->GetReceivedBytes();
99+
}
100+
101+
int64 DownloadItem::GetTotalBytes() {
102+
return download_item_->GetTotalBytes();
103+
}
104+
105+
const GURL& DownloadItem::GetUrl() {
106+
return download_item_->GetURL();
107+
}
108+
109+
std::string DownloadItem::GetMimeType() {
110+
return download_item_->GetMimeType();
111+
}
112+
113+
bool DownloadItem::HasUserGesture() {
114+
return download_item_->HasUserGesture();
115+
}
116+
117+
std::string DownloadItem::GetFilename() {
118+
return base::UTF16ToUTF8(net::GenerateFileName(GetUrl(),
119+
GetContentDisposition(),
120+
std::string(),
121+
download_item_->GetSuggestedFilename(),
122+
GetMimeType(),
123+
std::string()).LossyDisplayName());
124+
}
125+
126+
std::string DownloadItem::GetContentDisposition() {
127+
return download_item_->GetContentDisposition();
128+
}
129+
130+
void DownloadItem::SetSavePath(const base::FilePath& path) {
131+
download_item_->SetUserData(UserDataKey(), new SavePathData(path));
132+
}
133+
134+
void DownloadItem::Pause() {
135+
download_item_->Pause();
136+
}
137+
138+
void DownloadItem::Resume() {
139+
download_item_->Resume();
140+
}
141+
142+
void DownloadItem::Cancel() {
143+
download_item_->Cancel(true);
144+
}
145+
146+
mate::ObjectTemplateBuilder DownloadItem::GetObjectTemplateBuilder(
147+
v8::Isolate* isolate) {
148+
return mate::ObjectTemplateBuilder(isolate)
149+
.SetMethod("pause", &DownloadItem::Pause)
150+
.SetMethod("resume", &DownloadItem::Resume)
151+
.SetMethod("cancel", &DownloadItem::Cancel)
152+
.SetMethod("getReceivedBytes", &DownloadItem::GetReceivedBytes)
153+
.SetMethod("getTotalBytes", &DownloadItem::GetTotalBytes)
154+
.SetMethod("getUrl", &DownloadItem::GetUrl)
155+
.SetMethod("getMimeType", &DownloadItem::GetMimeType)
156+
.SetMethod("hasUserGesture", &DownloadItem::HasUserGesture)
157+
.SetMethod("getFilename", &DownloadItem::GetFilename)
158+
.SetMethod("getContentDisposition", &DownloadItem::GetContentDisposition)
159+
.SetMethod("setSavePath", &DownloadItem::SetSavePath);
160+
}
161+
162+
void SetWrapDownloadItem(const WrapDownloadItemCallback& callback) {
163+
g_wrap_download_item = callback;
164+
}
165+
166+
void ClearWrapDownloadItem() {
167+
g_wrap_download_item.Reset();
168+
}
169+
170+
// static
171+
mate::Handle<DownloadItem> DownloadItem::Create(
172+
v8::Isolate* isolate, content::DownloadItem* item) {
173+
auto handle = mate::CreateHandle(isolate, new DownloadItem(item));
174+
g_wrap_download_item.Run(handle.ToV8());
175+
g_download_item_objects[item->GetId()] = make_linked_ptr(
176+
new v8::Global<v8::Value>(isolate, handle.ToV8()));
177+
return handle;
178+
}
179+
180+
// static
181+
void* DownloadItem::UserDataKey() {
182+
return &kDownloadItemSavePathKey;
183+
}
184+
185+
} // namespace api
186+
187+
} // namespace atom
188+
189+
namespace {
190+
191+
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
192+
v8::Local<v8::Context> context, void* priv) {
193+
v8::Isolate* isolate = context->GetIsolate();
194+
mate::Dictionary dict(isolate, exports);
195+
dict.SetMethod("_setWrapDownloadItem", &atom::api::SetWrapDownloadItem);
196+
dict.SetMethod("_clearWrapDownloadItem", &atom::api::ClearWrapDownloadItem);
197+
}
198+
199+
} // namespace
200+
201+
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_download_item, Initialize);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) 2015 GitHub, Inc.
2+
// Use of this source code is governed by the MIT license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef ATOM_BROWSER_API_ATOM_API_DOWNLOAD_ITEM_H_
6+
#define ATOM_BROWSER_API_ATOM_API_DOWNLOAD_ITEM_H_
7+
8+
#include <string>
9+
10+
#include "atom/browser/api/trackable_object.h"
11+
#include "base/files/file_path.h"
12+
#include "content/public/browser/download_item.h"
13+
#include "native_mate/handle.h"
14+
#include "url/gurl.h"
15+
16+
namespace atom {
17+
18+
namespace api {
19+
20+
class DownloadItem : public mate::EventEmitter,
21+
public content::DownloadItem::Observer {
22+
public:
23+
class SavePathData : public base::SupportsUserData::Data {
24+
public:
25+
explicit SavePathData(const base::FilePath& path);
26+
const base::FilePath& path();
27+
private:
28+
base::FilePath path_;
29+
};
30+
31+
static mate::Handle<DownloadItem> Create(v8::Isolate* isolate,
32+
content::DownloadItem* item);
33+
static void* UserDataKey();
34+
35+
protected:
36+
explicit DownloadItem(content::DownloadItem* download_item);
37+
~DownloadItem();
38+
39+
// Override content::DownloadItem::Observer methods
40+
void OnDownloadUpdated(content::DownloadItem* download) override;
41+
void OnDownloadDestroyed(content::DownloadItem* download) override;
42+
43+
void Pause();
44+
void Resume();
45+
void Cancel();
46+
int64 GetReceivedBytes();
47+
int64 GetTotalBytes();
48+
std::string GetMimeType();
49+
bool HasUserGesture();
50+
std::string GetFilename();
51+
std::string GetContentDisposition();
52+
const GURL& GetUrl();
53+
void SetSavePath(const base::FilePath& path);
54+
55+
private:
56+
// mate::Wrappable:
57+
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
58+
v8::Isolate* isolate) override;
59+
bool IsDestroyed() const override;
60+
61+
void Destroy();
62+
63+
content::DownloadItem* download_item_;
64+
65+
DISALLOW_COPY_AND_ASSIGN(DownloadItem);
66+
};
67+
68+
} // namespace api
69+
70+
} // namespace atom
71+
72+
#endif // ATOM_BROWSER_API_ATOM_API_DOWNLOAD_ITEM_H_

atom/browser/api/atom_api_session.cc

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <vector>
99

1010
#include "atom/browser/api/atom_api_cookies.h"
11+
#include "atom/browser/api/atom_api_download_item.h"
1112
#include "atom/browser/atom_browser_context.h"
1213
#include "atom/browser/api/atom_api_web_contents.h"
1314
#include "atom/common/native_mate_converters/callback.h"
@@ -101,19 +102,6 @@ struct Converter<ClearStorageDataOptions> {
101102
}
102103
};
103104

104-
template<>
105-
struct Converter<content::DownloadItem*> {
106-
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
107-
content::DownloadItem* val) {
108-
mate::Dictionary dict(isolate, v8::Object::New(isolate));
109-
dict.Set("url", val->GetURL());
110-
dict.Set("filename", val->GetSuggestedFilename());
111-
dict.Set("mimeType", val->GetMimeType());
112-
dict.Set("hasUserGesture", val->HasUserGesture());
113-
return dict.GetHandle();
114-
}
115-
};
116-
117105
} // namespace mate
118106

119107
namespace atom {
@@ -245,11 +233,12 @@ Session::~Session() {
245233
}
246234

247235
void Session::OnDownloadCreated(content::DownloadManager* manager,
248-
content::DownloadItem* item) {
236+
content::DownloadItem* item) {
249237
auto web_contents = item->GetWebContents();
250-
bool prevent_default = Emit("will-download", item,
251-
api::WebContents::CreateFrom(isolate(),
252-
web_contents));
238+
bool prevent_default = Emit(
239+
"will-download",
240+
DownloadItem::Create(isolate(), item),
241+
api::WebContents::CreateFrom(isolate(), web_contents));
253242
if (prevent_default) {
254243
item->Cancel(true);
255244
item->Remove();

atom/browser/api/lib/app.coffee

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ EventEmitter = require('events').EventEmitter
22

33
bindings = process.atomBinding 'app'
44
sessionBindings = process.atomBinding 'session'
5+
downloadItemBindings = process.atomBinding 'download_item'
56

67
app = bindings.app
78
app.__proto__ = EventEmitter.prototype
@@ -10,6 +11,15 @@ wrapSession = (session) ->
1011
# session is an Event Emitter.
1112
session.__proto__ = EventEmitter.prototype
1213

14+
wrapDownloadItem = (download_item) ->
15+
# download_item is an Event Emitter.
16+
download_item.__proto__ = EventEmitter.prototype
17+
# Be compatible with old APIs.
18+
download_item.url = download_item.getUrl()
19+
download_item.filename = download_item.getFilename()
20+
download_item.mimeType = download_item.getMimeType()
21+
download_item.hasUserGesture = download_item.hasUserGesture()
22+
1323
app.setApplicationMenu = (menu) ->
1424
require('menu').setApplicationMenu menu
1525

@@ -51,5 +61,8 @@ app.on 'activate', (event, hasVisibleWindows) -> @emit 'activate-with-no-open-wi
5161
sessionBindings._setWrapSession wrapSession
5262
process.once 'exit', sessionBindings._clearWrapSession
5363

64+
downloadItemBindings._setWrapDownloadItem wrapDownloadItem
65+
process.once 'exit', downloadItemBindings._clearWrapDownloadItem
66+
5467
# Only one App object pemitted.
5568
module.exports = app

atom/browser/atom_browser_context.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "atom/browser/atom_browser_main_parts.h"
88
#include "atom/browser/atom_download_manager_delegate.h"
9+
#include "atom/browser/atom_ssl_config_service.h"
910
#include "atom/browser/browser.h"
1011
#include "atom/browser/net/atom_url_request_job_factory.h"
1112
#include "atom/browser/net/asar/asar_protocol_handler.h"
@@ -156,6 +157,10 @@ content::BrowserPluginGuestManager* AtomBrowserContext::GetGuestManager() {
156157
return guest_manager_.get();
157158
}
158159

160+
net::SSLConfigService* AtomBrowserContext::CreateSSLConfigService() {
161+
return new AtomSSLConfigService;
162+
}
163+
159164
void AtomBrowserContext::RegisterPrefs(PrefRegistrySimple* pref_registry) {
160165
pref_registry->RegisterFilePathPref(prefs::kSelectFileLastDirectory,
161166
base::FilePath());

atom/browser/atom_browser_context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class AtomBrowserContext : public brightray::BrowserContext {
2727
content::URLRequestInterceptorScopedVector* interceptors) override;
2828
net::HttpCache::BackendFactory* CreateHttpCacheBackendFactory(
2929
const base::FilePath& base_path) override;
30+
net::SSLConfigService* CreateSSLConfigService() override;
3031

3132
// content::BrowserContext:
3233
content::DownloadManagerDelegate* GetDownloadManagerDelegate() override;

0 commit comments

Comments
 (0)