feat: add support for webContents option in BrowserView (#26802)
* feat: add support for webContents option in BrowserView * tests: add tests
This commit is contained in:
		
					parent
					
						
							
								6932e17088
							
						
					
				
			
			
				commit
				
					
						1e2a2004e9
					
				
			
		
					 5 changed files with 54 additions and 21 deletions
				
			
		|  | @ -78,8 +78,17 @@ BrowserView::BrowserView(gin::Arguments* args, | ||||||
|       gin::Dictionary::CreateEmpty(isolate); |       gin::Dictionary::CreateEmpty(isolate); | ||||||
|   options.Get(options::kWebPreferences, &web_preferences); |   options.Get(options::kWebPreferences, &web_preferences); | ||||||
|   web_preferences.Set("type", "browserView"); |   web_preferences.Set("type", "browserView"); | ||||||
|   gin::Handle<class WebContents> web_contents = | 
 | ||||||
|       WebContents::New(isolate, web_preferences); |   v8::Local<v8::Value> value; | ||||||
|  | 
 | ||||||
|  |   // Copy the webContents option to webPreferences. This is only used internally
 | ||||||
|  |   // to implement nativeWindowOpen option.
 | ||||||
|  |   if (options.Get("webContents", &value)) { | ||||||
|  |     web_preferences.SetHidden("webContents", value); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   auto web_contents = | ||||||
|  |       WebContents::CreateFromWebPreferences(args->isolate(), web_preferences); | ||||||
| 
 | 
 | ||||||
|   web_contents_.Reset(isolate, web_contents.ToV8()); |   web_contents_.Reset(isolate, web_contents.ToV8()); | ||||||
|   api_web_contents_ = web_contents.get(); |   api_web_contents_ = web_contents.get(); | ||||||
|  |  | ||||||
|  | @ -3738,6 +3738,33 @@ gin::Handle<WebContents> WebContents::FromOrCreate( | ||||||
|   return gin::CreateHandle(isolate, api_web_contents); |   return gin::CreateHandle(isolate, api_web_contents); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | // static
 | ||||||
|  | gin::Handle<WebContents> WebContents::CreateFromWebPreferences( | ||||||
|  |     v8::Isolate* isolate, | ||||||
|  |     const gin_helper::Dictionary& web_preferences) { | ||||||
|  |   // Check if webPreferences has |webContents| option.
 | ||||||
|  |   gin::Handle<WebContents> web_contents; | ||||||
|  |   if (web_preferences.GetHidden("webContents", &web_contents) && | ||||||
|  |       !web_contents.IsEmpty()) { | ||||||
|  |     // Set webPreferences from options if using an existing webContents.
 | ||||||
|  |     // These preferences will be used when the webContent launches new
 | ||||||
|  |     // render processes.
 | ||||||
|  |     auto* existing_preferences = | ||||||
|  |         WebContentsPreferences::From(web_contents->web_contents()); | ||||||
|  |     base::DictionaryValue web_preferences_dict; | ||||||
|  |     if (gin::ConvertFromV8(isolate, web_preferences.GetHandle(), | ||||||
|  |                            &web_preferences_dict)) { | ||||||
|  |       existing_preferences->Clear(); | ||||||
|  |       existing_preferences->Merge(web_preferences_dict); | ||||||
|  |     } | ||||||
|  |   } else { | ||||||
|  |     // Create one if not.
 | ||||||
|  |     web_contents = WebContents::New(isolate, web_preferences); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   return web_contents; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // static
 | // static
 | ||||||
| WebContents* WebContents::FromID(int32_t id) { | WebContents* WebContents::FromID(int32_t id) { | ||||||
|   return GetAllWebContents().Lookup(id); |   return GetAllWebContents().Lookup(id); | ||||||
|  |  | ||||||
|  | @ -134,6 +134,10 @@ class WebContents : public gin::Wrappable<WebContents>, | ||||||
|       v8::Isolate* isolate, |       v8::Isolate* isolate, | ||||||
|       content::WebContents* web_contents); |       content::WebContents* web_contents); | ||||||
| 
 | 
 | ||||||
|  |   static gin::Handle<WebContents> CreateFromWebPreferences( | ||||||
|  |       v8::Isolate* isolate, | ||||||
|  |       const gin_helper::Dictionary& web_preferences); | ||||||
|  | 
 | ||||||
|   // gin::Wrappable
 |   // gin::Wrappable
 | ||||||
|   static gin::WrapperInfo kWrapperInfo; |   static gin::WrapperInfo kWrapperInfo; | ||||||
|   static v8::Local<v8::ObjectTemplate> FillObjectTemplate( |   static v8::Local<v8::ObjectTemplate> FillObjectTemplate( | ||||||
|  |  | ||||||
|  | @ -97,25 +97,9 @@ v8::Local<v8::Function> WebContentsView::GetConstructor(v8::Isolate* isolate) { | ||||||
| gin_helper::WrappableBase* WebContentsView::New( | gin_helper::WrappableBase* WebContentsView::New( | ||||||
|     gin_helper::Arguments* args, |     gin_helper::Arguments* args, | ||||||
|     const gin_helper::Dictionary& web_preferences) { |     const gin_helper::Dictionary& web_preferences) { | ||||||
|   // Check if BrowserWindow has passend |webContents| option to us.
 |   auto web_contents = | ||||||
|   gin::Handle<WebContents> web_contents; |       WebContents::CreateFromWebPreferences(args->isolate(), web_preferences); | ||||||
|   if (web_preferences.GetHidden("webContents", &web_contents) && | 
 | ||||||
|       !web_contents.IsEmpty()) { |  | ||||||
|     // Set webPreferences from options if using an existing webContents.
 |  | ||||||
|     // These preferences will be used when the webContent launches new
 |  | ||||||
|     // render processes.
 |  | ||||||
|     auto* existing_preferences = |  | ||||||
|         WebContentsPreferences::From(web_contents->web_contents()); |  | ||||||
|     base::DictionaryValue web_preferences_dict; |  | ||||||
|     if (gin::ConvertFromV8(args->isolate(), web_preferences.GetHandle(), |  | ||||||
|                            &web_preferences_dict)) { |  | ||||||
|       existing_preferences->Clear(); |  | ||||||
|       existing_preferences->Merge(web_preferences_dict); |  | ||||||
|     } |  | ||||||
|   } else { |  | ||||||
|     // Create one if not.
 |  | ||||||
|     web_contents = WebContents::New(args->isolate(), web_preferences); |  | ||||||
|   } |  | ||||||
|   // Constructor call.
 |   // Constructor call.
 | ||||||
|   auto* view = new WebContentsView(args->isolate(), web_contents); |   auto* view = new WebContentsView(args->isolate(), web_contents); | ||||||
|   view->InitWithArgs(args); |   view->InitWithArgs(args); | ||||||
|  |  | ||||||
|  | @ -39,6 +39,15 @@ describe('BrowserView module', () => { | ||||||
|     expect(webContents.getAllWebContents()).to.have.length(0); |     expect(webContents.getAllWebContents()).to.have.length(0); | ||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
|  |   it('can be created with an existing webContents', async () => { | ||||||
|  |     const wc = (webContents as any).create({ sandbox: true }); | ||||||
|  |     await wc.loadURL('about:blank'); | ||||||
|  | 
 | ||||||
|  |     view = new BrowserView({ webContents: wc } as any); | ||||||
|  | 
 | ||||||
|  |     expect(view.webContents.getURL()).to.equal('about:blank'); | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|   describe('BrowserView.setBackgroundColor()', () => { |   describe('BrowserView.setBackgroundColor()', () => { | ||||||
|     it('does not throw for valid args', () => { |     it('does not throw for valid args', () => { | ||||||
|       view = new BrowserView(); |       view = new BrowserView(); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Eryk Rakowski
				Eryk Rakowski