Here's what I know for Ontario Police.
You cannot be arbitrarily searched.
An officer can search you for his safety and yours. He cannot go into your pockets or any personal effects unless he has reasonable and probable grounds that you are carrying something that may endanger their live or yours - This is commonly known as a patdown
You can be searched if during the patdown if the officer feels any type of weapon or reasonably suspicious object.
You will be searched if you have been arrested.
You can also consent to a search and at any time revoke that consent.