| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  | // Copyright (c) 2019 GitHub, Inc.
 | 
					
						
							|  |  |  | // Use of this source code is governed by the MIT license that can be
 | 
					
						
							|  |  |  | // found in the LICENSE file.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-22 08:34:31 +01:00
										 |  |  | #ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_PROTOCOL_H_
 | 
					
						
							|  |  |  | #define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_PROTOCOL_H_
 | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2019-08-02 16:56:46 -07:00
										 |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-11 16:07:39 -04:00
										 |  |  | #include "base/memory/raw_ptr.h"
 | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  | #include "content/public/browser/content_browser_client.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-26 10:34:32 -07:00
										 |  |  | #include "gin/wrappable.h"
 | 
					
						
							| 
									
										
										
										
											2020-02-04 12:19:40 -08:00
										 |  |  | #include "shell/browser/net/electron_url_loader_factory.h"
 | 
					
						
							| 
									
										
										
										
											2023-03-27 10:00:55 -07:00
										 |  |  | #include "shell/common/gin_helper/constructible.h"
 | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-22 04:31:32 -05:00
										 |  |  | namespace gin { | 
					
						
							|  |  |  | class Arguments; | 
					
						
							| 
									
										
										
										
											2024-07-29 12:42:57 -05:00
										 |  |  | template <typename T> | 
					
						
							|  |  |  | class Handle; | 
					
						
							| 
									
										
										
										
											2024-07-22 04:31:32 -05:00
										 |  |  | }  // namespace gin
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 |  |  | namespace electron { | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 10:34:32 -07:00
										 |  |  | class ProtocolRegistry; | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace api { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:22:41 +09:00
										 |  |  | const std::vector<std::string>& GetStandardSchemes(); | 
					
						
							|  |  |  | const std::vector<std::string>& GetCodeCacheSchemes(); | 
					
						
							| 
									
										
										
										
											2019-08-02 16:56:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-31 18:45:23 -07:00
										 |  |  | void AddServiceWorkerScheme(const std::string& scheme); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-15 10:15:23 +09:00
										 |  |  | void RegisterSchemesAsPrivileged(gin_helper::ErrorThrower thrower, | 
					
						
							|  |  |  |                                  v8::Local<v8::Value> val); | 
					
						
							| 
									
										
										
										
											2019-08-02 16:56:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  | // Protocol implementation based on network services.
 | 
					
						
							| 
									
										
										
										
											2024-09-04 18:40:02 -05:00
										 |  |  | class Protocol final : public gin::Wrappable<Protocol>, | 
					
						
							|  |  |  |                        public gin_helper::Constructible<Protocol> { | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  |  public: | 
					
						
							| 
									
										
										
										
											2019-10-23 17:51:06 -07:00
										 |  |  |   static gin::Handle<Protocol> Create(v8::Isolate* isolate, | 
					
						
							| 
									
										
										
										
											2025-03-21 07:33:25 -05:00
										 |  |  |                                       ProtocolRegistry* protocol_registry); | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-10 11:49:20 +02:00
										 |  |  |   // gin_helper::Constructible
 | 
					
						
							| 
									
										
										
										
											2023-03-27 10:00:55 -07:00
										 |  |  |   static gin::Handle<Protocol> New(gin_helper::ErrorThrower thrower); | 
					
						
							|  |  |  |   static v8::Local<v8::ObjectTemplate> FillObjectTemplate( | 
					
						
							|  |  |  |       v8::Isolate* isolate, | 
					
						
							|  |  |  |       v8::Local<v8::ObjectTemplate> tmpl); | 
					
						
							| 
									
										
										
										
											2023-07-10 11:49:20 +02:00
										 |  |  |   static const char* GetClassName() { return "Protocol"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // gin::Wrappable
 | 
					
						
							|  |  |  |   static gin::WrapperInfo kWrapperInfo; | 
					
						
							| 
									
										
										
										
											2020-03-26 10:34:32 -07:00
										 |  |  |   const char* GetTypeName() override; | 
					
						
							| 
									
										
										
										
											2019-05-24 11:28:00 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  |  private: | 
					
						
							| 
									
										
										
										
											2025-03-21 07:33:25 -05:00
										 |  |  |   // Possible errors.
 | 
					
						
							|  |  |  |   enum class Error { | 
					
						
							|  |  |  |     kOK,  // no error
 | 
					
						
							|  |  |  |     kRegistered, | 
					
						
							|  |  |  |     kNotRegistered, | 
					
						
							|  |  |  |     kIntercepted, | 
					
						
							|  |  |  |     kNotIntercepted, | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Callback types.
 | 
					
						
							| 
									
										
										
										
											2019-05-29 13:02:15 -07:00
										 |  |  |   using CompletionCallback = | 
					
						
							|  |  |  |       base::RepeatingCallback<void(v8::Local<v8::Value>)>; | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 07:33:25 -05:00
										 |  |  |   explicit Protocol(ProtocolRegistry* protocol_registry); | 
					
						
							|  |  |  |   ~Protocol() override = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   [[nodiscard]] static std::string_view ErrorCodeToString(Error error); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  |   // JS APIs.
 | 
					
						
							| 
									
										
										
										
											2025-03-21 07:33:25 -05:00
										 |  |  |   Error RegisterProtocol(ProtocolType type, | 
					
						
							|  |  |  |                          const std::string& scheme, | 
					
						
							|  |  |  |                          const ProtocolHandler& handler); | 
					
						
							| 
									
										
										
										
											2020-06-02 09:46:18 -07:00
										 |  |  |   bool UnregisterProtocol(const std::string& scheme, gin::Arguments* args); | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  |   bool IsProtocolRegistered(const std::string& scheme); | 
					
						
							| 
									
										
										
										
											2019-05-24 11:28:00 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 07:33:25 -05:00
										 |  |  |   Error InterceptProtocol(ProtocolType type, | 
					
						
							|  |  |  |                           const std::string& scheme, | 
					
						
							|  |  |  |                           const ProtocolHandler& handler); | 
					
						
							| 
									
										
										
										
											2020-06-02 09:46:18 -07:00
										 |  |  |   bool UninterceptProtocol(const std::string& scheme, gin::Arguments* args); | 
					
						
							| 
									
										
										
										
											2019-05-24 11:28:00 +09:00
										 |  |  |   bool IsProtocolIntercepted(const std::string& scheme); | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Old async version of IsProtocolRegistered.
 | 
					
						
							| 
									
										
										
										
											2019-06-28 16:25:30 +09:00
										 |  |  |   v8::Local<v8::Promise> IsProtocolHandled(const std::string& scheme, | 
					
						
							| 
									
										
										
										
											2019-10-15 10:15:23 +09:00
										 |  |  |                                            gin::Arguments* args); | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-29 11:37:45 +09:00
										 |  |  |   // Helper for converting old registration APIs to new RegisterProtocol API.
 | 
					
						
							|  |  |  |   template <ProtocolType type> | 
					
						
							| 
									
										
										
										
											2020-06-02 09:46:18 -07:00
										 |  |  |   bool RegisterProtocolFor(const std::string& scheme, | 
					
						
							| 
									
										
										
										
											2019-04-29 11:37:45 +09:00
										 |  |  |                            const ProtocolHandler& handler, | 
					
						
							| 
									
										
										
										
											2019-10-15 10:15:23 +09:00
										 |  |  |                            gin::Arguments* args) { | 
					
						
							| 
									
										
										
										
											2025-03-21 07:33:25 -05:00
										 |  |  |     const auto result = RegisterProtocol(type, scheme, handler); | 
					
						
							| 
									
										
										
										
											2020-06-02 09:46:18 -07:00
										 |  |  |     HandleOptionalCallback(args, result); | 
					
						
							| 
									
										
										
										
											2025-03-21 07:33:25 -05:00
										 |  |  |     return result == Error::kOK; | 
					
						
							| 
									
										
										
										
											2019-04-29 11:37:45 +09:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-05-24 11:28:00 +09:00
										 |  |  |   template <ProtocolType type> | 
					
						
							| 
									
										
										
										
											2020-06-02 09:46:18 -07:00
										 |  |  |   bool InterceptProtocolFor(const std::string& scheme, | 
					
						
							| 
									
										
										
										
											2019-05-24 11:28:00 +09:00
										 |  |  |                             const ProtocolHandler& handler, | 
					
						
							| 
									
										
										
										
											2019-10-15 10:15:23 +09:00
										 |  |  |                             gin::Arguments* args) { | 
					
						
							| 
									
										
										
										
											2025-03-21 07:33:25 -05:00
										 |  |  |     const auto result = InterceptProtocol(type, scheme, handler); | 
					
						
							| 
									
										
										
										
											2020-06-02 09:46:18 -07:00
										 |  |  |     HandleOptionalCallback(args, result); | 
					
						
							| 
									
										
										
										
											2025-03-21 07:33:25 -05:00
										 |  |  |     return result == Error::kOK; | 
					
						
							| 
									
										
										
										
											2019-05-24 11:28:00 +09:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-29 11:37:45 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  |   // Be compatible with old interface, which accepts optional callback.
 | 
					
						
							| 
									
										
										
										
											2025-03-21 07:33:25 -05:00
										 |  |  |   void HandleOptionalCallback(gin::Arguments* args, Error error); | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 10:34:32 -07:00
										 |  |  |   // Weak pointer; the lifetime of the ProtocolRegistry is guaranteed to be
 | 
					
						
							|  |  |  |   // longer than the lifetime of this JS interface.
 | 
					
						
							| 
									
										
										
										
											2023-05-11 16:07:39 -04:00
										 |  |  |   raw_ptr<ProtocolRegistry> protocol_registry_; | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace api
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 |  |  | }  // namespace electron
 | 
					
						
							| 
									
										
										
										
											2019-04-24 06:39:21 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-22 08:34:31 +01:00
										 |  |  | #endif  // ELECTRON_SHELL_BROWSER_API_ELECTRON_API_PROTOCOL_H_
 |