@@ -50,7 +50,7 @@ func (st *chromiumConfigureState) cleanup() {
5050}
5151
5252// ChromiumConfigure batched Chromium/session configuration plus optional navigation.
53- func (s * ApiService ) ChromiumConfigure (ctx context.Context , request oapi.ChromiumConfigureRequestObject ) (resp oapi.ChromiumConfigureResponseObject , err error ) {
53+ func (s * ApiService ) ChromiumConfigure (ctx context.Context , request oapi.ChromiumConfigureRequestObject ) (oapi.ChromiumConfigureResponseObject , error ) {
5454 start := time .Now ()
5555
5656 if request .Body == nil {
@@ -80,7 +80,60 @@ func (s *ApiService) ChromiumConfigure(ctx context.Context, request oapi.Chromiu
8080 s .chromiumConfigMu .Lock ()
8181 defer s .chromiumConfigMu .Unlock ()
8282
83- needsStop := chromiumNeedsStopCycle (st )
83+ var configureResp oapi.ChromiumConfigureResponseObject
84+ switch chromiumConfigureModeFor (st ) {
85+ case chromiumConfigureModeLive :
86+ configureResp = s .chromiumConfigureLive (ctx , st )
87+ case chromiumConfigureModeRestart :
88+ configureResp = s .chromiumConfigureRestart (ctx , st , spec )
89+ }
90+ if configureResp != nil {
91+ return configureResp , nil
92+ }
93+
94+ if spec .needsNav {
95+ if err := chromiumDoNavigate (ctx , s , spec ); err != nil {
96+ logger .FromContext (ctx ).Warn ("start_url dispatch failed" , "error" , err )
97+ }
98+ }
99+
100+ logger .FromContext (ctx ).Info ("chromium configure finished" , "elapsed" , time .Since (start ).String ())
101+ return oapi.ChromiumConfigure200JSONResponse {Ok : true }, nil
102+ }
103+
104+ type chromiumConfigureMode uint8
105+
106+ const (
107+ chromiumConfigureModeLive chromiumConfigureMode = iota
108+ chromiumConfigureModeRestart
109+ )
110+
111+ func chromiumConfigureModeFor (st * chromiumConfigureState ) chromiumConfigureMode {
112+ if st .hasProfile ||
113+ len (st .extItems ) > 0 ||
114+ policiesContentNonEmpty (st .chromePoliciesJSON ) ||
115+ flagsContentNonEmpty (st .chromiumFlagsJSON ) {
116+ return chromiumConfigureModeRestart
117+ }
118+ return chromiumConfigureModeLive
119+ }
120+
121+ func (s * ApiService ) chromiumConfigureLive (ctx context.Context , st * chromiumConfigureState ) oapi.ChromiumConfigureResponseObject {
122+ if st .displayJSON == nil || strings .TrimSpace (* st .displayJSON ) == "" {
123+ return nil
124+ }
125+
126+ displayPlan , displayResp := chromiumPrepareDisplay (ctx , s , st .displayJSON )
127+ if displayResp != nil {
128+ return displayResp
129+ }
130+ if displayPlan == nil {
131+ return nil
132+ }
133+ return chromiumRunPatchDisplay (ctx , s , displayPlan .body )
134+ }
135+
136+ func (s * ApiService ) chromiumConfigureRestart (ctx context.Context , st * chromiumConfigureState , spec startURLParsed ) (resp oapi.ChromiumConfigureResponseObject ) {
84137 chromiumStopped := false
85138 restartAfterStop := func () error {
86139 if ! chromiumStopped {
@@ -99,103 +152,80 @@ func (s *ApiService) ChromiumConfigure(ctx context.Context, request oapi.Chromiu
99152 return
100153 }
101154 resp = cfg500ConfigureStep (chromiumConfigureStepStart , restartErr .Error ())
102- err = nil
103155 }
104156 }()
105157
106- if needsStop {
107- logger .FromContext (ctx ).Info ("chromium configure (stop/start path)" )
108- if err := s .stopChromium (ctx ); err != nil {
109- return cfg500ConfigureStep (chromiumConfigureStepStop , err .Error ()), nil
110- }
111- chromiumStopped = true
112-
113- policyOverrides , err := chromiumValidatePolicies (st .chromePoliciesJSON )
114- if err != nil {
115- return cfgResponseFromStepError (chromiumConfigureStepPolicies , err ), nil
116- }
117- if err := chromiumApplyPolicies (ctx , s , policyOverrides ); err != nil {
118- return cfgResponseFromStepError (chromiumConfigureStepPolicies , err ), nil
119- }
158+ logger .FromContext (ctx ).Info ("chromium configure (stop/start path)" )
159+ if err := s .stopChromium (ctx ); err != nil {
160+ return cfg500ConfigureStep (chromiumConfigureStepStop , err .Error ())
161+ }
162+ chromiumStopped = true
120163
121- if reqMsgs , ierr := chromiumApplyExtensions (ctx , s , st .extItems ); reqMsgs != "" {
122- return cfg400 (fmt .Sprintf ("%s: %s" , chromiumConfigureStepExtensions , reqMsgs )), nil
123- } else if ierr != nil {
124- return cfg500ConfigureStep (chromiumConfigureStepExtensions , ierr .Error ()), nil
125- }
164+ policyOverrides , err := chromiumValidatePolicies (st .chromePoliciesJSON )
165+ if err != nil {
166+ return cfgResponseFromStepError (chromiumConfigureStepPolicies , err )
167+ }
168+ if err := chromiumApplyPolicies (ctx , s , policyOverrides ); err != nil {
169+ return cfgResponseFromStepError (chromiumConfigureStepPolicies , err )
170+ }
126171
127- if st .displayJSON != nil && strings .TrimSpace (* st .displayJSON ) != "" {
128- displayPlan , displayResp := chromiumPrepareDisplay (ctx , s , st .displayJSON )
129- if displayResp != nil {
130- return displayResp , nil
131- }
132- if displayPlan != nil {
133- stopped , stopErr := s .stopActiveRecordings (ctx )
134- if stopErr != nil {
135- return cfg500ConfigureStep (chromiumConfigureStepDisplay , fmt .Sprintf ("failed to stop recordings: %v" , stopErr )), nil
136- }
137- if len (stopped ) > 0 {
138- defer func () {
139- go s .startNewRecordingSegments (context .WithoutCancel (ctx ), stopped )
140- }()
141- }
142- if rr := chromiumDisplayApplyWhileStopped (ctx , s , displayPlan ); rr != nil {
143- return rr , nil
144- }
145- }
146- }
172+ if reqMsgs , ierr := chromiumApplyExtensions (ctx , s , st .extItems ); reqMsgs != "" {
173+ return cfg400 (fmt .Sprintf ("%s: %s" , chromiumConfigureStepExtensions , reqMsgs ))
174+ } else if ierr != nil {
175+ return cfg500ConfigureStep (chromiumConfigureStepExtensions , ierr .Error ())
176+ }
147177
148- flagsPlan , err := chromiumValidateFlags (st .chromiumFlagsJSON )
149- if err != nil {
150- return cfgResponseFromStepError (chromiumConfigureStepFlags , err ), nil
151- }
152- if err := chromiumMergeFlags (ctx , s , flagsPlan ); err != nil {
153- return cfgResponseFromStepError (chromiumConfigureStepFlags , err ), nil
178+ if st .displayJSON != nil && strings .TrimSpace (* st .displayJSON ) != "" {
179+ displayPlan , displayResp := chromiumPrepareDisplay (ctx , s , st .displayJSON )
180+ if displayResp != nil {
181+ return displayResp
154182 }
155-
156- if st .hasProfile {
157- preparedProfile , cleanupProfile , err := chromiumPrepareProfileArchive (st .profileTemp , st .stripComponents )
158- if cleanupProfile != nil {
159- defer cleanupProfile ()
160- }
161- if err != nil {
162- return cfg500ConfigureStep (chromiumConfigureStepProfile , err .Error ()), nil
183+ if displayPlan != nil {
184+ stopped , stopErr := s .stopActiveRecordings (ctx )
185+ if stopErr != nil {
186+ return cfg500ConfigureStep (chromiumConfigureStepDisplay , fmt .Sprintf ("failed to stop recordings: %v" , stopErr ))
163187 }
164- if spec . needsNav {
165- if err := stripProfileSessionRestore ( preparedProfile ); err != nil {
166- return cfg500ConfigureStep ( chromiumConfigureStepProfile , err . Error ()), nil
167- }
188+ if len ( stopped ) > 0 {
189+ defer func () {
190+ go s . startNewRecordingSegments ( context . WithoutCancel ( ctx ), stopped )
191+ }()
168192 }
169- if err := chromiumInstallPreparedProfile ( preparedProfile ); err != nil {
170- return cfg500ConfigureStep ( chromiumConfigureStepProfile , err . Error ()), nil
193+ if rr := chromiumDisplayApplyWhileStopped ( ctx , s , displayPlan ); rr != nil {
194+ return rr
171195 }
172196 }
197+ }
198+
199+ flagsPlan , err := chromiumValidateFlags (st .chromiumFlagsJSON )
200+ if err != nil {
201+ return cfgResponseFromStepError (chromiumConfigureStepFlags , err )
202+ }
203+ if err := chromiumMergeFlags (ctx , s , flagsPlan ); err != nil {
204+ return cfgResponseFromStepError (chromiumConfigureStepFlags , err )
205+ }
173206
174- if err := restartAfterStop (); err != nil {
175- return cfg500ConfigureStep (chromiumConfigureStepStart , err .Error ()), nil
207+ if st .hasProfile {
208+ preparedProfile , cleanupProfile , err := chromiumPrepareProfileArchive (st .profileTemp , st .stripComponents )
209+ if cleanupProfile != nil {
210+ defer cleanupProfile ()
176211 }
177- } else {
178- if st .displayJSON != nil && strings .TrimSpace (* st .displayJSON ) != "" {
179- displayPlan , displayResp := chromiumPrepareDisplay (ctx , s , st .displayJSON )
180- if displayResp != nil {
181- return displayResp , nil
182- }
183- if displayPlan != nil {
184- if rr := chromiumRunPatchDisplay (ctx , s , displayPlan .body ); rr != nil {
185- return rr , nil
186- }
212+ if err != nil {
213+ return cfg500ConfigureStep (chromiumConfigureStepProfile , err .Error ())
214+ }
215+ if spec .needsNav {
216+ if err := stripProfileSessionRestore (preparedProfile ); err != nil {
217+ return cfg500ConfigureStep (chromiumConfigureStepProfile , err .Error ())
187218 }
188219 }
189- }
190-
191- if spec .needsNav {
192- if err := chromiumDoNavigate (ctx , s , spec ); err != nil {
193- logger .FromContext (ctx ).Warn ("start_url dispatch failed" , "error" , err )
220+ if err := chromiumInstallPreparedProfile (preparedProfile ); err != nil {
221+ return cfg500ConfigureStep (chromiumConfigureStepProfile , err .Error ())
194222 }
195223 }
196224
197- logger .FromContext (ctx ).Info ("chromium configure finished" , "elapsed" , time .Since (start ).String ())
198- return oapi.ChromiumConfigure200JSONResponse {Ok : true }, nil
225+ if err := restartAfterStop (); err != nil {
226+ return cfg500ConfigureStep (chromiumConfigureStepStart , err .Error ())
227+ }
228+ return nil
199229}
200230
201231type startURLParsed struct {
@@ -300,13 +330,6 @@ type chromiumDisplayPlan struct {
300330 refreshRate int
301331}
302332
303- func chromiumNeedsStopCycle (st * chromiumConfigureState ) bool {
304- return st .hasProfile ||
305- len (st .extItems ) > 0 ||
306- policiesContentNonEmpty (st .chromePoliciesJSON ) ||
307- flagsContentNonEmpty (st .chromiumFlagsJSON )
308- }
309-
310333func policiesContentNonEmpty (s * string ) bool {
311334 if ! policiesNonEmpty (s ) {
312335 return false
0 commit comments