atom_api_session.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifndef ATOM_BROWSER_API_ATOM_API_SESSION_H_
  5. #define ATOM_BROWSER_API_ATOM_API_SESSION_H_
  6. #include <string>
  7. #include <vector>
  8. #include "atom/browser/api/trackable_object.h"
  9. #include "atom/browser/atom_blob_reader.h"
  10. #include "atom/browser/net/resolve_proxy_helper.h"
  11. #include "base/values.h"
  12. #include "content/public/browser/download_manager.h"
  13. #include "native_mate/handle.h"
  14. #include "net/base/completion_callback.h"
  15. class GURL;
  16. namespace base {
  17. class FilePath;
  18. }
  19. namespace mate {
  20. class Arguments;
  21. class Dictionary;
  22. } // namespace mate
  23. namespace net {
  24. class ProxyConfig;
  25. }
  26. namespace atom {
  27. class AtomBrowserContext;
  28. namespace api {
  29. class Session : public mate::TrackableObject<Session>,
  30. public content::DownloadManager::Observer {
  31. public:
  32. enum class CacheAction {
  33. CLEAR,
  34. STATS,
  35. };
  36. // Gets or creates Session from the |browser_context|.
  37. static mate::Handle<Session> CreateFrom(v8::Isolate* isolate,
  38. AtomBrowserContext* browser_context);
  39. // Gets the Session of |partition|.
  40. static mate::Handle<Session> FromPartition(
  41. v8::Isolate* isolate,
  42. const std::string& partition,
  43. const base::DictionaryValue& options = base::DictionaryValue());
  44. AtomBrowserContext* browser_context() const { return browser_context_.get(); }
  45. // mate::TrackableObject:
  46. static void BuildPrototype(v8::Isolate* isolate,
  47. v8::Local<v8::FunctionTemplate> prototype);
  48. // Methods.
  49. void ResolveProxy(const GURL& url,
  50. const ResolveProxyHelper::ResolveProxyCallback& callback);
  51. template <CacheAction action>
  52. void DoCacheAction(const net::CompletionCallback& callback);
  53. void GetCacheSize(const net::CompletionCallback& callback);
  54. void ClearStorageData(mate::Arguments* args);
  55. void FlushStorageData();
  56. void SetProxy(const mate::Dictionary& options, const base::Closure& callback);
  57. void SetDownloadPath(const base::FilePath& path);
  58. void EnableNetworkEmulation(const mate::Dictionary& options);
  59. void DisableNetworkEmulation();
  60. void SetCertVerifyProc(v8::Local<v8::Value> proc, mate::Arguments* args);
  61. void SetPermissionRequestHandler(v8::Local<v8::Value> val,
  62. mate::Arguments* args);
  63. void SetPermissionCheckHandler(v8::Local<v8::Value> val,
  64. mate::Arguments* args);
  65. void ClearHostResolverCache(mate::Arguments* args);
  66. void ClearAuthCache(mate::Arguments* args);
  67. void AllowNTLMCredentialsForDomains(const std::string& domains);
  68. void SetUserAgent(const std::string& user_agent, mate::Arguments* args);
  69. std::string GetUserAgent();
  70. void GetBlobData(const std::string& uuid,
  71. const AtomBlobReader::CompletionCallback& callback);
  72. void CreateInterruptedDownload(const mate::Dictionary& options);
  73. void SetPreloads(const std::vector<base::FilePath::StringType>& preloads);
  74. std::vector<base::FilePath::StringType> GetPreloads() const;
  75. v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
  76. v8::Local<v8::Value> Protocol(v8::Isolate* isolate);
  77. v8::Local<v8::Value> WebRequest(v8::Isolate* isolate);
  78. v8::Local<v8::Value> NetLog(v8::Isolate* isolate);
  79. protected:
  80. Session(v8::Isolate* isolate, AtomBrowserContext* browser_context);
  81. ~Session() override;
  82. // content::DownloadManager::Observer:
  83. void OnDownloadCreated(content::DownloadManager* manager,
  84. download::DownloadItem* item) override;
  85. private:
  86. // Cached object.
  87. v8::Global<v8::Value> cookies_;
  88. v8::Global<v8::Value> protocol_;
  89. v8::Global<v8::Value> web_request_;
  90. v8::Global<v8::Value> net_log_;
  91. // The client id to enable the network throttler.
  92. base::UnguessableToken network_emulation_token_;
  93. scoped_refptr<AtomBrowserContext> browser_context_;
  94. DISALLOW_COPY_AND_ASSIGN(Session);
  95. };
  96. } // namespace api
  97. } // namespace atom
  98. #endif // ATOM_BROWSER_API_ATOM_API_SESSION_H_