The California Highway Patrol detective who aimed his firearm at a crowd of Bay Area protesters on Wednesday night was protecting another plainclothes officer who had been attacked only seconds earlier, and was trying to stop an advancing crowd of at least 30 people, a CHP commander said Thursday afternoon.

The officer told his commander that he was in fear for his life, said Chief Avery Browne of the CHP. The case will be reviewed by the Alameda County district attorney's office and CHP's Internal Affairs division, but Browne said "no one has provided any evidence that the officers were inappropriate in what they did."

"Chief, I didn't know if I was going to make it out of this thing alive," the officer said, according to Browne.

Pictures of the undercover officer, who was aiming at a crowd of Oakland demonstrators marching against recent grand jury decisions in the police-involved killings of Eric Garner and Michael Brown, went viral Thursday, stoking anger and fear as protests in the Bay Area continue to result in looting and arrests.

In the two photos, the plainclothes officer can be seen pointing a handgun at a camera and a crowd while another officer wrestles with a protester who appears to be covering his head.

Speaking to reporters during a phone conference, Browne of the agency's Golden Gate Division, said the officers had been following a group of protesters on Wednesday night when they received information that demonstrators were vandalizing a cellphone store near Ninth and Harrison streets in Oakland.

Two undercover cops pulled guns on #berkeleyprotests and subsequently arrested people w/ masks still on their faces. pic.twitter.com/Os0EQ0p7tt — Anonymous (@OpBerkeley) December 11, 2014

The officers, both detectives with the auto theft unit, had been following the crowd in an unmarked vehicle all night, Browne said. They left the car to walk among the crowd for 18 blocks before some of the protesters recognized them as police officers.

"At that time an individual accused one of our employees or recognized him as being a police officer and started yelling 'Police! Police!'" Browne said.

One of the protesters grabbed at an officer, not the man pictured with the handgun, and the two engaged in a struggle, Browne said. Between 30 and 50 demonstrators circled the officers, and the officer seen holding the weapon displayed his badge and baton and ordered them to stop.

When they ignored his orders, Browne said the officer drew his weapon.

"We’re extremely, extremely cognizant and very sensitive to the display of a gun," Browne said. "It’s very upsetting, it’s very disturbing to individuals who are trying to peacefully protest.”

Oakland police officers arrived seconds later, and the crowd dispersed. The man who attacked the officer has been charged with assault and remains jailed in Alameda County, according to Browne, who declined to identify the officer or the suspect.

Police are searching for a second woman who also kicked the officer who was attacked. Browne said the officer suffered minor injuries.

The CHP and other law enforcement agencies have been using plainclothes officers to observe and gather information about the protests, and Browne said tensions have risen among officers as several protesters have posted pictures of themselves on social media claiming to be armed with handguns, rocks and explosive devices.

Despite Wednesday's incident, Browne said he will continue to deploy plainclothes officers to gather intelligence from protesters. Officers have also been creating Twitter accounts, on which they don't identify themselves as police, in order to monitor planned demonstrations.

"We’re very cognizant of the danger and yes we will continue to use plain clothes officers to observe. Their job is not to get in the middle of it," he said.

Browne said the officers involved in Wednesday night's incident did not see any weapons in the crowd, and of the more than 200 arrests CHP officers have made during this week's protests, very few suspects have been charged with weapons offenses.

The officers were not armed with chemical spray, according to Browne, who said he may consider asking officers responding to protests to carry more non-lethal options in the future.

Oakland has been home to some of the nation's most violent protests after grand juries in New York City and Ferguson, Mo., declined to indict police officers in the killings of unarmed black men this year.

Demonstrators have taken to Northern California streets each night this week, blocking the 24 Freeway and throwing rocks and incendiary devices at police. Nineteen people were arrested Wednesday night. About 200 people have been arrested this week in Oakland and Berkeley.

CHP officers have also used rubber bullets to quell demonstrations this week, an allegation that may have led "hacktivist" group Anonymous to launch a cyber attack against several websites connected to Oakland's city government on Wednesday.

Oakland's City Hall, police and fire department websites were offline for nearly 24 hours.