GDP stands for Gross Domestic Product: this is the amount of activity that happens within the borders of the country itself, irrespective of source.
What you are thinking of is GNP: Gross National Product- which ideally would be the amount of economic activity worldwide attributable to citizens or corporations or the government of a particular country.
Historically, countries liked the idea of GNP and it used to be the 'favoured' way of reporting economic activity. However from a practical perspective as the world has become more and more globalized, it turns out that GDP is easier to estimate and is somewhat more realistic when trying to assess the level of economic activity happening in a geographic area.
Ultimately who cares who owns the factory, the fact that it exists where it is and not somewhere else is a huge boost to the local economy. Many industries have profit margins of 5% or less, guess where much of the expenses are being spent?