Which of "她睡觉的时候...", "她在睡觉的时候...", "她睡觉时候...", and "她在睡觉时候..." are grammatically correct and do they all have the same meaning?
她在睡觉的时候，她的朋友正在上网。
  她睡觉时候，她的朋友正在上网。
  
  
  3.她在睡觉时候，她的朋友正在上网。


The first one is the most natural way to say it, grammatically and formally. 

The second one is ok in colloquial contexts with omitting both 在 and 的. 

The third one is a bit odd because it adds 在, I may expect the interlocutor to say the full version. Otherwise, I expect they say the shorten version like the second one. 

But, in practice, especially when you speak fast, it really doesn't matter. These sentences are all ok, though the first one is preferable. There are no problems for understanding. They all could sound natural if you have a good command of Chinese speaking.